Parent: [f9a241] (diff)

Child: [a95cdb] (diff)

Download this file

qrecoll.py    281 lines (239 with data), 8.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
import sys
import datetime
try:
from recoll import recoll
from recoll import rclextract
hasextract = True
except:
import recoll
hasextract = False
import rclmain
from getopt import getopt
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import pyqtSlot
####################
# Highlighting methods. Just for showing the groups usage, we add the
# original string for the match to the highlighted text. I don't think
# you'd want to do this in a real app, but maybe some kind of tooltip?
class HlMeths:
def __init__(self, groups):
self.groups = groups
def startMatch(self, idx):
ugroup = " ".join(self.groups[idx][0])
return '<font size="tiny">'+ugroup+'</font><font color="blue">'
def endMatch(self):
return '</font>'
############
# Data extraction. The 2 following methods use the extractor module
# and get the data from the original document
#
# Extract and return document text (in text or html format, indicated
# by newdoc.mimetype)
def textextract(doc):
extractor = rclextract.Extractor(doc)
newdoc = extractor.textextract(doc.ipath)
return newdoc
# Extract document in original format (ie: application/msword) and
# save it to a file. This only works if ipath is not null (else just
# use the url !)
def extractofile(doc, outfilename=""):
extractor = rclextract.Extractor(doc)
outfilename = extractor.idoctofile(doc.ipath, doc.mimetype, \
ofilename=outfilename)
return outfilename
#########
# RecollQuery wraps a recoll.query object in a Qt model
class RecollQuery(QtCore.QAbstractTableModel):
def __init__(self):
QtCore.QAbstractTableModel.__init__(self)
self.totres = -1
self.db = None
self.query = None
self.qtext = ""
self.docs = []
self.pagelen = 10
self.attrs = ("filename", "title", "mtime", "url", "ipath")
def rowCount(self, parent):
ret = len(self.docs)
#print "RecollQuery.rowCount(): ", ret
return ret
def columnCount(self, parent):
#print "RecollQuery.columnCount()"
if parent.isValid():
return 0
else:
return len(self.attrs)
def setquery(self, db, q, sortfield="", ascending=True):
"""Parse and execute query on open db"""
#print "RecollQuery.setquery():"
# Get query object
self.query = db.query()
if sortfield:
self.query.sortby(sortfield, ascending)
# Parse/run input query string
self.totres = self.query.execute(q)
self.qtext = q
self.db = db
self.docs = []
self.fetchMore(None)
def getdoc(self, index):
if index.row() < len(self.docs):
return self.docs[index.row()]
else:
return None
def sort(self, col, order):
#print "sort", col, order
self.setquery(self.db, self.qtext, sortfield=self.attrs[col],
ascending = order)
def headerData(self, idx, orient, role):
if orient == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return self.attrs[idx]
return None
def data(self, index, role):
#print "RecollQuery.data: row %d, role: " % (index.row(),), role
if not index.isValid():
return QtCore.QVariant()
if index.row() >= len(self.docs):
return QtCore.QVariant()
if role == QtCore.Qt.DisplayRole:
#print "RecollQuery.data: row %d, col %d role: " % \
# (index.row(), index.column()), role
attr = self.attrs[index.column()]
value = getattr(self.docs[index.row()], attr)
if attr == "mtime":
dte = datetime.datetime.fromtimestamp(int(value))
value = str(dte)
return value
else:
return QtCore.QVariant()
def canFetchMore(self, parent):
#print "RecollQuery.canFetchMore:"
if len(self.docs) < self.totres:
return True
else:
return False
def fetchMore(self, parent):
#print "RecollQuery.fetchMore:"
self.beginInsertRows(QtCore.QModelIndex(), len(self.docs), \
len(self.docs) + self.pagelen)
count = 0
while self.query.next >= 0 and self.query.next < self.totres \
and count < self.pagelen:
#print "Got: ", title.encode("utf-8")
self.docs.append(self.query.fetchone())
count += 1
self.endInsertRows()
###
# UI interaction code
class RclGui_Main(QtGui.QMainWindow):
def __init__(self, db, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = rclmain.Ui_MainWindow()
self.ui.setupUi(self)
self.db = db
self.qmodel = RecollQuery()
scq = QtGui.QShortcut(QtGui.QKeySequence("Ctrl+Q"), self);
self.connect(scq, QtCore.SIGNAL("activated()"), self.onexit)
header = self.ui.resTable.horizontalHeader();
header.setSortIndicatorShown(True);
header.setSortIndicator(-1, QtCore.Qt.AscendingOrder);
self.ui.resTable.setSortingEnabled(True)
self.currentindex = -1
self.currentdoc = None
def on_searchEntry_returnPressed(self):
self.startQuery()
def on_resTable_clicked(self, index):
doc = self.qmodel.getdoc(index)
self.currentindex = index
self.currentdoc = doc
if doc is None:
print "NO DoC"
return
query = self.qmodel.query
groups = query.getgroups()
meths = HlMeths(groups)
abs = query.makedocabstract(doc, methods=meths)
self.ui.resDetail.setText(abs)
if hasextract:
ipath = doc.get('ipath')
#print "ipath[%s]" % (ipath,)
self.ui.previewPB.setEnabled(True)
if ipath:
self.ui.savePB.setEnabled(True)
else:
self.ui.savePB.setEnabled(False)
@pyqtSlot()
def on_previewPB_clicked(self):
print "on_previewPB_clicked(self)"
newdoc = textextract(self.currentdoc)
query = self.qmodel.query;
groups = query.getgroups()
meths = HlMeths(groups)
#print "newdoc.mimetype:", newdoc.mimetype
if newdoc.mimetype == 'text/html':
ishtml = True
else:
ishtml = False
text = '<qt><head></head><body>' + \
query.highlight(newdoc.text,
methods=meths,
ishtml=ishtml,
eolbr=True)
text += '</body></qt>'
self.ui.resDetail.setText(text)
@pyqtSlot()
def on_savePB_clicked(self):
print "on_savePB_clicked(self)"
doc = self.currentdoc
ipath = doc.ipath
if not ipath:
return
fn = QtGui.QFileDialog.getSaveFileName(self)
if fn:
docitems = doc.items()
fn = extractofile(doc, str(fn.toLocal8Bit()))
print "Saved as", fn
else:
print >> sys.stderr, "Canceled"
def startQuery(self):
self.qmodel.setquery(self.db, self.ui.searchEntry.text())
self.ui.resTable.setModel(self.qmodel)
def onexit(self):
sys.exit(0)
def Usage():
print >> sys.stderr, '''Usage: qt.py [<qword1> [<qword2> ...]]'''
sys.exit(1)
def main(args):
app = QtGui.QApplication(args)
confdir=""
extra_dbs = []
# Snippet params
maxchars = 300
contextwords = 6
# Process options: [-c confdir] [-i extra_db [-i extra_db] ...]
options, args = getopt(args[1:], "c:i:")
for opt,val in options:
if opt == "-c":
confdir = val
elif opt == "-i":
extra_dbs.append(val)
else:
print >> sys.stderr, "Bad opt: ", opt
Usage()
# The query should be in the remaining arg(s)
q = None
if len(args) > 0:
q = ""
for word in args:
q += word + " "
db = recoll.connect(confdir=confdir, extra_dbs=extra_dbs)
db.setAbstractParams(maxchars=maxchars, contextwords=contextwords)
topwindow = RclGui_Main(db)
topwindow.show()
if q is not None:
topwindow.ui.searchEntry.setText(q)
topwindow.startQuery()
sys.exit(app.exec_())
if __name__=="__main__":
main(sys.argv)