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