Parent: [afd57f] (diff)

Download this file

gssp-recoll.py    142 lines (121 with data), 5.1 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
#!/usr/bin/python3
# Copyright (C) 2017-2019 J.F.Dockes
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import sys
import subprocess
import os
from gi.repository import GLib
from gi.repository import Gio
from pydbus.generic import signal
from pydbus import SessionBus
from recoll import recoll
_defxmlfile = '/usr/share/dbus-1/interfaces/org.gnome.ShellSearchProvider2.xml'
_myname = 'org.recoll.gnomeShellSearchProvider'
def debug(s):
print("%s"%s, file=sys.stderr)
# debug("GSSPRecoll:recoll-search.py LOADED")
class RecollSearchProvider(object):
def __init__(self):
self.results = []
self.proc = None
self.db = recoll.connect()
def GetInitialResultSet(self, terms):
debug("GSSPRecoll:GetInitialResultSet: terms: %s" % terms)
qs = ""
for term in terms:
if qs:
qs += " "
qs += term
if len(qs) < 2:
return []
q = self.db.query()
q.execute(qs)
self.results = {}
outlist = []
if q.rowcount == 0:
# There is a bug in the current module (1.25.4), and the 'for'
# line will throw if the result set is empty. For now:
return outlist
for doc in q:
doc.abstract = q.makedocabstract(doc)
if doc.abstract is None:
doc.abstract = ''
key = str(doc.xdocid)
self.results[key] = doc
outlist.append(key)
if len(outlist) >= 10:
break
debug("GSSPRecoll:GetInitialResultSet: %d results: %s" %
(len(outlist),outlist))
return outlist
def GetSubsearchResultSet(self, prevres, terms):
#debug("GSSPRecoll:GetSubsearchResultSet: prevres: %s terms: %s" %
# (prevres,terms))
return self.GetInitialResultSet(terms)
def GetResultMetas(self, idents):
#debug("GSSPRecoll:GetResultMetas: idents %s " % idents)
out = []
for key in idents:
if key in self.results:
doc = self.results[key]
entry = {}
entry['id'] = GLib.Variant('s', key)
# "name": the display name for the result
name = doc.title if doc.title else doc.filename
if not name:
name = "No title or file name?"
entry['name'] = GLib.Variant('s', name)
# "icon": a serialized GIcon (see g_icon_serialize()),
# or alternatively, "gicon": a textual representation
# of a GIcon (see g_icon_to_string()), or
# alternativly, "icon-data": a tuple of type
# (iiibiiay) describing a pixbuf with width, height,
# rowstride, has-alpha, bits-per-sample, and image
# data
ct = Gio.content_type_from_mime_type(doc.mimetype)
icon = Gio.content_type_get_icon(ct)
entry['icon'] = icon.serialize()
# "description": an optional short description (1-2 lines)
entry['description'] = GLib.Variant('s', doc.abstract)
out.append(entry)
else:
debug("GSSPRecoll: GetResultMetas: %s not found" % key)
out.append({'name':'empty', 'icon':None, 'description':'empty'})
#debug("GSSPRecoll:GetResultMetas: return %s" % out)
return out
def ActivateResult(self, ident, terms, ts):
debug("GSSPRecoll:ActivateResult: id %s terms %s"%(ident, terms))
if ident in self.results:
doc = self.results[ident]
if doc.ipath:
debug("GSSPRecoll:ActivateResult: ipath[%s]"%doc.ipath)
url = doc.url + "#" + doc.ipath
proc = subprocess.Popen(["recoll", url])
else:
debug("GSSPRecoll:ActivateResult: Exec xdg-open on %s"%doc.url)
os.system("xdg-open '%s'" % str(doc.url))
def LaunchSearch(self, terms, ts):
debug("GSSPRecoll:LaunchSearch: terms [%s]" % terms)
qs = ""
for term in terms:
qs += " " + term
proc = subprocess.Popen(["recoll", "-q", qs])
_defxml = open(_defxmlfile, 'r').read()
RecollSearchProvider.dbus = _defxml
mainloop = GLib.MainLoop()
session_bus = SessionBus()
session_bus.publish(_myname, RecollSearchProvider())
mainloop.run()