-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebui.py
executable file
·485 lines (460 loc) · 14.5 KB
/
webui.py
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#{{{ imports
import os
import bottle
import time
import sys
import datetime
import glob
import hashlib
import csv
import io
import string
import shlex
from urllib.parse import urlencode, quote as urlquote
from recoll import recoll, rclextract, rclconfig
def msg(s):
print("%s" % s, file=sys.stderr)
# use ujson if avalible (faster than built in json)
try:
import ujson as json
except ImportError:
import json
msg("ujson module not found, using (slower) built-in json module instead")
g_fscharset=sys.getfilesystemencoding()
#}}}
#{{{ settings
# settings defaults
DEFAULTS = {
'context': 30,
'stem': 1,
'timefmt': '%c',
'dirdepth': 2,
'maxchars': 500,
'maxresults': 0,
'perpage': 25,
'csvfields': 'filename title author size time mtype url',
'title_link': 'download',
}
# sort fields/labels
SORTS = [
("relevancyrating", "Relevancy"),
("mtime", "Date",),
("url", "Path"),
("filename", "Filename"),
("fbytes", "Size"),
("author", "Author"),
]
# doc fields
FIELDS = [
# exposed by python api
'ipath',
'filename',
'title',
'author',
'fbytes',
'dbytes',
'size',
'fmtime',
'dmtime',
'mtime',
'mtype',
'origcharset',
'sig',
'relevancyrating',
'url',
'abstract',
'keywords',
# calculated
'time',
'snippet',
'label',
]
#}}}
#{{{ functions
#{{{ helpers
def select(ls, invalid=[None]):
for value in ls:
if value not in invalid:
return value
def timestr(secs, fmt):
if secs == '' or secs is None:
secs = '0'
t = time.gmtime(int(secs))
return time.strftime(fmt, t)
def normalise_filename(fn):
valid_chars = "_-%s%s" % (string.ascii_letters, string.digits)
out = ""
for i in range(0,len(fn)):
if fn[i] in valid_chars:
out += fn[i]
else:
out += "_"
return out
def get_topdirs(confdir):
rclconf = rclconfig.RclConfig(confdir)
return rclconf.getConfParam('topdirs')
# Environment fetch for the cases where we don't care if unset or null
def safe_envget(varnm):
try:
return os.environ[varnm]
except Exception as ex:
return None
# get database directory from recoll.conf, defaults to confdir/xapiandb. Note
# that this is available as getDbDir() in newer rclconfig versions
def get_dbdir(confdir):
confdir = os.path.expanduser(confdir)
rclconf = rclconfig.RclConfig(confdir)
dbdir = rclconf.getConfParam('dbdir')
if not dbdir:
dbdir = 'xapiandb'
if not os.path.isabs(dbdir):
cachedir = rclconf.getConfParam('cachedir')
if not cachedir:
cachedir = confdir
dbdir = os.path.join(cachedir, dbdir)
# recoll API expects bytes, not strings
return os.path.normpath(dbdir).encode(g_fscharset)
#}}}
#{{{ get_config
def get_config():
config = {}
envdir = safe_envget('RECOLL_CONFDIR')
# get useful things from recoll.conf
rclconf = rclconfig.RclConfig(envdir)
config['confdir'] = rclconf.getConfDir()
config['dirs'] = dict.fromkeys([os.path.expanduser(d) for d in
shlex.split(rclconf.getConfParam('topdirs'))],
config['confdir'])
# add topdirs from extra config dirs
extraconfdirs = safe_envget('RECOLL_EXTRACONFDIRS')
if extraconfdirs:
config['extraconfdirs'] = shlex.split(extraconfdirs)
for e in config['extraconfdirs']:
config['dirs'].update(dict.fromkeys([os.path.expanduser(d) for d in
shlex.split(get_topdirs(e))],e))
config['extradbs'] = list(map(get_dbdir, config['extraconfdirs']))
else:
config['extraconfdirs'] = None
config['extradbs'] = None
config['stemlang'] = rclconf.getConfParam('indexstemminglanguages')
# get config from cookies or defaults
for k, v in DEFAULTS.items():
value = select([bottle.request.get_cookie(k), v])
config[k] = type(v)(value)
# Fix csvfields: get rid of invalid ones to avoid needing tests in
# the dump function
cf = config['csvfields'].split()
ncf = [f for f in cf if f in FIELDS]
config['csvfields'] = ' '.join(ncf)
config['fields'] = ' '.join(FIELDS)
# get mountpoints
config['mounts'] = {}
for d in config['dirs']:
name = 'mount_%s' % urlquote(d,'')
config['mounts'][d] = select([bottle.request.get_cookie(name),
'file://%s' % d], [None, ''])
# Parameters set by the admin in the recoll configuration
# file. These override anything else, so read them last
val = rclconf.getConfParam('webui_nojsoncsv')
val = 0 if val is None else int(val)
config['rclc_nojsoncsv'] = val
val = rclconf.getConfParam('webui_maxperpage')
val = 0 if val is None else int(val)
if val:
if config['perpage'] == 0 or config['perpage'] > val:
config['perpage'] = val
val = rclconf.getConfParam('webui_nosettings')
val = 0 if val is None else int(val)
config['rclc_nosettings'] = val
return config
#}}}
#{{{ get_dirs
def get_dirs(tops, depth):
v = []
for top in tops:
dirs = [top]
for d in range(1, depth+1):
dirs = dirs + glob.glob(top + '/*' * d)
dirs = filter(lambda f: os.path.isdir(f), dirs)
top_path = top.rsplit('/', 1)[0]
dirs = [w.replace(top_path+'/', '', 1) for w in dirs]
v = v + dirs
return ['<all>'] + v
#}}}
#{{{ get_query
def get_query():
query = {
'query': select([bottle.request.query.query, '']),
'before': select([bottle.request.query.before, '']),
'after': select([bottle.request.query.after, '']),
'dir': select([bottle.request.query.dir, '', '<all>'], [None, '']),
'sort': select([bottle.request.query.sort, SORTS[0][0]]),
'ascending': int(select([bottle.request.query.ascending, 0], [None, ''])),
'page': int(select([bottle.request.query.page, 0], [None, ''])),
'highlight': int(select([bottle.request.query.highlight, 1], [None, ''])),
'snippets': int(select([bottle.request.query.snippets, 1], [None, ''])),
}
#msg("query['query'] : %s" % query['query'])
return query
#}}}
#{{{ query_to_recoll_string
def query_to_recoll_string(q):
qs = q['query']
if len(q['after']) > 0 or len(q['before']) > 0:
qs += " date:%s/%s" % (q['after'], q['before'])
qdir = q['dir']
if qdir != '<all>':
qs += " dir:\"%s\" " % qdir
return qs
#}}}
#{{{ recoll_initsearch
def recoll_initsearch(q):
config = get_config()
confdir = config['confdir']
dbs = []
""" The reason for this somewhat elaborate scheme is to keep the
set size as small as possible by searching only those databases
with matching topdirs """
if q['dir'] == '<all>':
if config['extraconfdirs']:
dbs.extend(map(get_dbdir,config['extraconfdirs']))
else:
confdirs = []
for d,conf in config['dirs'].items():
tdbasename = os.path.basename(d)
if os.path.commonprefix([tdbasename, q['dir']]) == tdbasename:
confdirs.append(conf)
if len(confdirs) == 0:
# should not happen, using non-existing q['dir']?
bottle.abort(400, 'no matching database for search directory ' + q['dir'])
elif len(confdirs) == 1:
# only one config (most common situation)
confdir = confdirs[0]
else:
# more than one config with matching topdir, use 'm all
confdir = confdirs[0]
dbs.extend(map(get_dbdir, confdirs[1:]))
if config['extradbs']:
dbs.extend(config['extradbs'])
if dbs:
db = recoll.connect(confdir,dbs)
else:
db = recoll.connect(confdir)
db.setAbstractParams(config['maxchars'], config['context'])
query = db.query()
query.sortby(q['sort'], q['ascending'])
try:
qs = query_to_recoll_string(q)
query.execute(qs, config['stem'], config['stemlang'])
except Exception as ex:
msg("Query execute failed: %s" % ex)
pass
return query
#}}}
#{{{ HlMeths
class HlMeths:
def startMatch(self, idx):
return '<span class="search-result-highlight">'
def endMatch(self):
return '</span>'
#}}}
#{{{ recoll_search
def recoll_search(q):
config = get_config()
tstart = datetime.datetime.now()
results = []
query = recoll_initsearch(q)
nres = query.rowcount
if config['maxresults'] == 0:
config['maxresults'] = nres
if nres > config['maxresults']:
nres = config['maxresults']
if config['perpage'] == 0 or q['page'] == 0:
config['perpage'] = nres
q['page'] = 1
offset = (q['page'] - 1) * config['perpage']
if query.rowcount > 0:
if type(query.next) == int:
query.next = offset
else:
query.scroll(offset, mode='absolute')
highlighter = HlMeths()
for i in range(config['perpage']):
try:
doc = query.fetchone()
# Later Recoll versions return None at EOL instead of
# exception This change restores conformance to PEP 249
# Python Database API Specification
if not doc:
break
except:
break
d = {}
for f in FIELDS:
v = getattr(doc, f)
if v is not None:
d[f] = v
else:
d[f] = ''
d['label'] = select([d['title'], d['filename'], '?'], [None, ''])
d['sha'] = hashlib.sha1((d['url']+d['ipath']).encode('utf-8')).hexdigest()
d['time'] = timestr(d['mtime'], config['timefmt'])
if 'snippets' in q and q['snippets']:
if 'highlight' in q and q['highlight']:
d['snippet'] = query.makedocabstract(
doc, highlighter)
else:
d['snippet'] = query.makedocabstract(doc)
#for n,v in d.items():
# print("type(%s) is %s" % (n,type(v)))
results.append(d)
tend = datetime.datetime.now()
return results, nres, tend - tstart
#}}}
#}}}
#{{{ routes
#{{{ static
@bottle.route('/static/:path#.+#')
def server_static(path):
return bottle.static_file(path, root='./static')
#}}}
#{{{ main
@bottle.route('/')
@bottle.view('main')
def main():
config = get_config()
return { 'dirs': get_dirs(config['dirs'], config['dirdepth']),
'query': get_query(), 'sorts': SORTS, 'config': config}
#}}}
#{{{ results
@bottle.route('/results')
@bottle.view('results')
def results():
config = get_config()
query = get_query()
qs = query_to_recoll_string(query)
res, nres, timer = recoll_search(query)
if config['maxresults'] == 0:
config['maxresults'] = nres
if config['perpage'] == 0:
config['perpage'] = nres
return { 'res': res, 'time': timer, 'query': query, 'dirs':
get_dirs(config['dirs'], config['dirdepth']),
'qs': qs, 'sorts': SORTS, 'config': config,
'query_string': bottle.request.query_string, 'nres': nres,
'config': config}
#}}}
#{{{ preview
@bottle.route('/preview/<resnum:int>')
def preview(resnum):
query = get_query()
qs = query_to_recoll_string(query)
rclq = recoll_initsearch(query)
if resnum > rclq.rowcount - 1:
return 'Bad result index %d' % resnum
rclq.scroll(resnum)
doc = rclq.fetchone()
xt = rclextract.Extractor(doc)
tdoc = xt.textextract(doc.ipath)
if tdoc.mimetype == 'text/html':
bottle.response.content_type = 'text/html; charset=utf-8'
else:
bottle.response.content_type = 'text/plain; charset=utf-8'
return tdoc.text
#}}}
#{{{ download
@bottle.route('/download/<resnum:int>')
def edit(resnum):
query = get_query()
qs = query_to_recoll_string(query)
rclq = recoll_initsearch(query)
if resnum > rclq.rowcount - 1:
return 'Bad result index %d' % resnum
rclq.scroll(resnum)
doc = rclq.fetchone()
bottle.response.content_type = doc.mimetype
pathismine = False
xt = rclextract.Extractor(doc)
path = xt.idoctofile(doc.ipath, doc.mimetype)
pathismine = True
if (not doc.ipath) and "filename" in doc.keys():
filename = doc.filename
else:
filename = os.path.basename(path)
bottle.response.headers['Content-Disposition'] = \
'attachment; filename="%s"' % filename
bottle.response.headers['Content-Length'] = os.stat(path).st_size
f = open(path, 'rb')
if pathismine:
os.unlink(path)
return f
#}}}
#{{{ json
@bottle.route('/json')
def get_json():
query = get_query()
qs = query_to_recoll_string(query)
bottle.response.headers['Content-Type'] = 'application/json'
bottle.response.headers['Content-Disposition'] = \
'attachment; filename=recoll-%s.json' % normalise_filename(qs)
res, nres, timer = recoll_search(query)
ures = []
for d in res:
ud={}
for f,v in d.items():
ud[f] = v
ures.append(ud)
res = ures
return json.dumps({ 'query': query, 'results': res })
#}}}
#{{{ csv
@bottle.route('/csv')
def get_csv():
config = get_config()
query = get_query()
query['page'] = 0
query['snippets'] = 0
qs = query_to_recoll_string(query)
bottle.response.headers['Content-Type'] = 'text/csv'
bottle.response.headers['Content-Disposition'] = \
'attachment; filename=recoll-%s.csv' % normalise_filename(qs)
res, nres, timer = recoll_search(query)
si = io.StringIO()
cw = csv.writer(si)
fields = config['csvfields'].split()
cw.writerow(fields)
for doc in res:
row = []
for f in fields:
if f in doc:
row.append(doc[f])
else:
row.append('')
cw.writerow(row)
return si.getvalue().strip("\r\n")
#}}}
#{{{ settings/set
@bottle.route('/settings')
@bottle.view('settings')
def settings():
return get_config()
@bottle.route('/set')
def set():
config = get_config()
for k, v in DEFAULTS.items():
bottle.response.set_cookie(k, str(bottle.request.query.get(k)), max_age=3153600000, expires=315360000)
for d in config['dirs']:
cookie_name = 'mount_%s' % urlquote(d, '')
bottle.response.set_cookie(cookie_name, str(bottle.request.query.get('mount_%s' % d)), max_age=3153600000, expires=315360000)
bottle.redirect('./')
#}}}
#{{{ osd
@bottle.route('/osd.xml')
@bottle.view('osd')
def main():
#config = get_config()
url = bottle.request.urlparts
url = '%s://%s' % (url.scheme, url.netloc)
return {'url': url}
#}}}
# vim: fdm=marker:tw=80:ts=4:sw=4:sts=4:et