Attachment 'man.py'
Download 1 import xml.sax
2
3 import gobject
4 import re
5 import os
6 import os.path
7 from deskbar.Handler import Handler
8 from deskbar.Match import Match
9 import gtk, gtk.gdk
10 from bisect import bisect
11
12 HANDLERS = {
13 "ManHandler" : {
14 "name": "Man",
15 "description": "Open man pages",
16 }
17 }
18
19 class ManMatch(Match):
20 def __init__ (self, backend, **args):
21 Match.__init__(self, backend, **args)
22
23 def action(self, text=None):
24 argv = [ 'yelp', 'man:' + self.name.encode() ] # spawn_async is unfriendly to unicode strings
25 #os.system('zenity --info --text="%s"' % str(argv))
26 gobject.spawn_async(argv, flags=gobject.SPAWN_SEARCH_PATH)
27
28 def get_verb(self):
29 return 'View man page for <b>%(name)s</b>'
30
31 def get_category(self):
32 return 'actions'
33
34 class ManXMLContentHandler(xml.sax.ContentHandler):
35 def __init__(self):
36 self.fullstring = ''
37 self.indices = [ ]
38 self.section_indices = [ ]
39 self.section_numbers = [ ]
40
41 def startElement(self, tag, attrs):
42 self.page = (tag == 'page')
43
44 def endElement(self, tag):
45 self.page = False
46
47 def characters(self, data):
48 if self.page:
49 try:
50 split = data.rsplit('.', 2)
51 secnum = int(split[1])
52 if not self.section_numbers or self.section_numbers[-1] != secnum:
53 self.section_indices.append(len(self.fullstring))
54 self.section_numbers.append(secnum)
55 self.indices.append(len(self.fullstring))
56 self.fullstring += split[0]
57 except:
58 pass # FIXME: not all follow the name.secnumber.gz format
59
60 class ManHandler(Handler):
61 def __init__(self):
62 Handler.__init__(self, 'gnome-help.png')
63
64 def initialize(self):
65 f = open(os.path.expanduser('~/.gnome2/yelp.d/manindex.xml'))
66 ch = ManXMLContentHandler()
67 xml.sax.parse(f, ch)
68 f.close()
69 self.fullstring = ch.fullstring
70 self.indices = ch.indices
71 self.section_indices = ch.section_indices
72 self.section_numbers = ch.section_numbers
73
74 def query(self, query, max=15):
75 results = [ ]
76 idx = self.fullstring.find(query)
77 while idx > -1 and len(results) < max:
78 strstart = bisect(self.indices, idx) - 1
79 secstart = bisect(self.section_indices, idx) - 1
80 str = '%s(%d)' % (self.fullstring[self.indices[strstart]:self.indices[strstart+1]], self.section_numbers[secstart])
81 results.append(ManMatch(self, name=str))
82 idx = self.fullstring.find(query, idx+1)
83 return results
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.