This repository has been archived by the owner on Aug 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnepomuksearch.py
executable file
·116 lines (92 loc) · 3.61 KB
/
nepomuksearch.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
#!/usr/bin/python
import signal, os
import optparse
import sys
from PyQt4 import QtCore,QtGui
from PyKDE4 import kdecore
from PyKDE4 import kdeui
from PyKDE4.nepomuk import Nepomuk
from PyKDE4.kio import KIO
from PyKDE4.soprano import Soprano
app = None
job = None
q = None
class NepomukQuery(QtCore.QObject):
def __init__(self, parent=None, options={}):
super(NepomukQuery, self).__init__(parent)
self.options = options
self.count = 0
def query_string(self, url):
search_job = KIO.listDir(kdecore.KUrl('nepomuksearch:/?query=' + url), KIO.HideProgressInfo)
search_job.entries.connect(self.search_slot)
search_job.result.connect(self.result)
search_job.start()
return search_job
def query_tag(self, tagNames):
soprano_term_uri = Soprano.Vocabulary.NAO.hasTag()
nepomuk_property = Nepomuk.Types.Property(soprano_term_uri)
tag = Nepomuk.Tag(tagNames[0])
if tag.uri() == "":
print "Tag \"%s\" does not exist" % tagNames[0]
return False
comparison_term = Nepomuk.Query.ComparisonTerm(nepomuk_property, Nepomuk.Query.ResourceTerm(tag))
if self.options.filesOnly:
query = Nepomuk.Query.FileQuery(comparison_term)
else:
query = Nepomuk.Query.Query(comparison_term)
search_url = query.toSearchUrl()
search_job = KIO.listDir(kdecore.KUrl(search_url), KIO.HideProgressInfo)
search_job.entries.connect(self.search_slot)
search_job.result.connect(self.result)
search_job.start()
return search_job
def search_slot(self, job, data):
global app
for item in data:
if self.options.showUrls:
print item.stringValue(KIO.UDSEntry.UDS_URL),
print "\t",
print (item.stringValue(KIO.UDSEntry.UDS_LOCAL_PATH).__str__() or item.stringValue(KIO.UDSEntry.UDS_NAME).__str__())
self.count = self.count + 1
def result(self, job):
print >> sys.stderr, "Retrieved %i result(s)" % self.count
job.entries.disconnect()
app.exit()
def term_handler(signum, frame):
job.kill()
app.exit()
print >> sys.stderr, "Interrupted"
def main():
global app
global job
parser = optparse.OptionParser(description='Nepomuk Search')
parser.add_option('-t', '--tags', action="store",
dest='useTags',
default=False, help='comma separated list of tags to search for')
parser.add_option('-q', '--query', action="store",
dest='useQuery',
default=False, help='specify Nepomuk query')
parser.add_option('-f', '--files', action="store_true",
dest='filesOnly',
default=False, help='limit results to files')
parser.add_option('-u', '--urls', action="store_true",
dest='showUrls',
default=False, help='show resource URLs')
(args, rest) = parser.parse_args()
result = Nepomuk.ResourceManager.instance().init()
if result != 0:
print("Error initializing nepomuk: result code %i" % result)
sys.exit(2)
app = QtGui.QApplication(sys.argv)
signal.signal(signal.SIGTERM, term_handler)
signal.signal(signal.SIGINT, term_handler)
if args.useQuery:
q = NepomukQuery(None, args)
job = q.query_string(args.useQuery)
elif args.useTags:
q = NepomukQuery(None, args)
job = q.query_tag(args.useTags.split(','))
if job:
app.exec_()
if __name__ == '__main__':
main()