Commit 6e0cc3b8 authored by Jean-Francois Dockes's avatar Jean-Francois Dockes
Browse files

simplify code by restricting to python3 and modern recoll versions

parent 0e67c1ce
#!/usr/bin/env python3
from __future__ import print_function
import locale
import re
import os
import sys
import base64
import platform
import conftree
class RclDynConf:
def __init__(self, fname): = ConfSimple(fname)
def getStringList(self, sk):
nms =
out = []
if nms is not None:
for nm in nms:
out.append(base64.b64decode(, sk)))
return out
class RclConfig:
def __init__(self, argcnf = None):
self.config = None
platsys = platform.system()
# Find configuration directory
if argcnf is not None:
self.confdir = os.path.abspath(argcnf)
elif "RECOLL_CONFDIR" in os.environ:
self.confdir = os.environ["RECOLL_CONFDIR"]
if platsys == "Windows":
if "LOCALAPPDATA" in os.environ:
dir = os.environ["LOCALAPPDATA"]
dir = os.path.expanduser("~")
self.confdir = os.path.join(dir, "Recoll")
self.confdir = os.path.expanduser("~/.recoll")
#print("Confdir: [%s]" % self.confdir, file=sys.stderr)
# Also find datadir. This is trickier because this is set by
# "configure" in the C code. We can only do our best. Have to
# choose a preference order. Use RECOLL_DATADIR if the order is wrong
self.datadir = None
if "RECOLL_DATADIR" in os.environ:
self.datadir = os.environ["RECOLL_DATADIR"]
if platsys == "Windows":
self.datadir = os.path.join(os.path.dirname(sys.argv[0]), "..")
dirs = ("/opt/local", "/usr", "/usr/local")
for dir in dirs:
dd = os.path.join(dir, "share/recoll")
if os.path.exists(dd):
self.datadir = dd
if self.datadir is None:
self.datadir = "/usr/share/recoll"
#print("Datadir: [%s]" % self.datadir, file=sys.stderr)
self.cdirs = []
# Additional config directory, values override user ones
if "RECOLL_CONFTOP" in os.environ:
# Additional config directory, overrides system's, overridden by user's
if "RECOLL_CONFMID" in os.environ:
self.cdirs.append(os.path.join(self.datadir, "examples"))
#print("Config dirs: %s" % self.cdirs, file=sys.stderr)
self.keydir = ''
def getConfDir(self):
return self.confdir
def setKeyDir(self, dir):
self.keydir = dir
def getConfParam(self, nm):
if not self.config:
self.config = conftree.ConfStack("recoll.conf", self.cdirs, "tree")
return self.config.get(nm, self.keydir)
class RclExtraDbs:
def __init__(self, config):
self.config = config
def getActDbs(self):
dyncfile = os.path.join(self.config.getConfDir(), "history")
dync = RclDynConf(dyncfile)
return dync.getStringList("actExtDbs")
if __name__ == '__main__':
config = RclConfig()
extradbs = RclExtraDbs(config)
......@@ -10,6 +10,8 @@ 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)
......@@ -21,19 +23,6 @@ except ImportError:
import json
msg("ujson module not found, using (slower) built-in json module instead")
py3k = sys.version_info >= (3, 0, 0)
if py3k:
from urllib.parse import urlencode, quote as urlquote
else: # 2.x
from urllib import urlencode, quote as urlquote
import urllib
# import recoll and rclextract
from recoll import recoll,rclextract
from recoll import rclconfig
import rclconfig
......@@ -122,22 +111,21 @@ def safe_envget(varnm):
except Exception as ex:
return None
# get database directory from recoll.conf, defaults to
# confdir/xapiandb
# 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')
cachedir = rclconf.getConfParam('cachedir')
basename = 'xapiandb'
dirname = confdir
if cachedir:
dirname = cachedir
if dbdir:
basename = dbdir
if os.path.isabs(dbdir):
dirname = ''
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(os.path.join(dirname, basename)).encode(g_fscharset)
return os.path.normpath(dbdir).encode(g_fscharset)
#{{{ get_config
def get_config():
......@@ -146,23 +134,20 @@ def get_config():
# get useful things from recoll.conf
rclconf = rclconfig.RclConfig(envdir)
config['confdir'] = rclconf.getConfDir()
extradbs = safe_envget('RECOLL_EXTRADBS')
extraconfdirs = safe_envget('RECOLL_EXTRACONFDIRS')
if extradbs:
config['extradbs']=[s.encode(g_fscharset) for s in shlex.split(extradbs)]
config['extradbs'] = None
config['dirs'] = dict.fromkeys([os.path.expanduser(d) for d in
# 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
config['extradbs'] = list(map(get_dbdir, config['extraconfdirs']))
config['extraconfdirs'] = None
config['extradbs'] = None
config['stemlang'] = rclconf.getConfParam('indexstemminglanguages')
# get config from cookies or defaults
for k, v in DEFAULTS.items():
......@@ -433,17 +418,16 @@ 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)
bottle.response.headers['Content-Disposition'] = \
'attachment; filename=recoll-%s.json' % normalise_filename(qs)
res, nres, timer = recoll_search(query)
if py3k:
ures = []
for d in res:
for f,v in d.items():
ud[f] = v
res = ures
ures = []
for d in res:
for f,v in d.items():
ud[f] = v
res = ures
return json.dumps({ 'query': query, 'results': res })
#{{{ csv
......@@ -455,12 +439,10 @@ def get_csv():
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)
bottle.response.headers['Content-Disposition'] = \
'attachment; filename=recoll-%s.csv' % normalise_filename(qs)
res, nres, timer = recoll_search(query)
if py3k:
si = io.StringIO()
si = io.BytesIO()
si = io.StringIO()
cw = csv.writer(si)
fields = config['csvfields'].split()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment