annotate python/html2flux.py @ 644:b4dc72b7c5c3

string types
author Jeff Hammel <k0scist@gmail.com>
date Tue, 11 Mar 2014 14:38:35 -0700
parents a43d0205f80b
children fbfc02ea7d8e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
3 """
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
4 transform an HTML <dl> file into a fluxbox menu
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
5 if no file give, read from stdin
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
6
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
7 <dl><a>submenu name</a>
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
8 <dt>program label</dt><dd>command</dd>
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
9 <dt>another program label</dt><dd>command2</dd>
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
10 </dl>
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
11
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
12 x-form -> internal format:
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
13
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
14 ('submenu name': [('program label', 'command'),
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
15 ('another program label', 'command2')])
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
16 """
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
17
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
18 import optparse
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
19 import os
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 import sys
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 from lxml import etree
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
22 from lsex import lsex # local import
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
24 # available executables
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 executables = set([i.rsplit('/', 1)[-1] for i in lsex() ])
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26
620
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
27 # TODO: next generation
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
28 # class HtmlMenu
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
29 # def _init__(self, html):
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
30 # if isinstance (html, string):
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
31 # html =
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
32 # for item in html
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
33
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
34 # class Command
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
35 # classname = ''
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
36
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
37 # class Sudo(Command)
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
38 # classname = 'sudo'
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
39 # programs = (('gksudo',),)
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
40
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
41 def readmenu(dl, output, top=True):
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
42 """read menu from an <dl> tag"""
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
43 # TODO: probably don't really need lxml
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
44
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
45 menu_items = []
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
46 name = None # menu name
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
47 firstchild = True
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
48 label = None
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 for child in dl.iterchildren():
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
50
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
51 if not top and child.tag == 'a' and firstchild:
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
52 # TODO: better way of labeling this!
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
53 name = child.text.strip()
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
54
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 if child.tag == 'dt':
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
56 # item label
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
57 label = ' '.join([i.strip() for i in child.itertext() if i.strip()])
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 if child.tag == 'dd':
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
59 # command
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
60 command = ' '.join([i.strip() for i in child.itertext() if i.strip()])
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
61 # TODO: classes
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 executable = command.split()[0]
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
63 if executable in executables or os.path.isabs(executable):
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
64 menu_items.append((label, command))
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
65
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
66 # submenu
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
67 if child.tag == 'dl':
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
68 menu_items.append(readmenu(child, output, top=False))
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
69
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
70 return (name, menu_items)
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
71
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
72 def printflux(name, menu, output, top=True):
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
73 """
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
74 - output: file-like object for writing
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
75 """
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
76
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
77 # print [submenu] tag for this menu
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
78 name = name or ''
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
79 if not top:
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
80 print >> output, '[submenu] (%s)' % name
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
81
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
82 # print menu items
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
83 for name, item in menu:
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
84 if isinstance(item, basestring):
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
85 # command
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
86 print >> output, '[exec] (%s) {%s}' % (name, item)
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
87 else:
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
88 # submenu
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
89 printflux(name, item, output, top=False)
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
90
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
91 # print end of this submenu
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
92 if not top:
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
93 print >> output, '[end]'
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
94
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
95 def printmenu(dl, output):
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
96 name, menu = readmenu(dl, output)
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
97 printflux(name, menu, output)
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
98
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
99 def main(args=sys.argv[1:]):
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
100 """command line interface"""
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
101
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
102 # parse command line option
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
103 usage = '%prog [options] [menu.html]'
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
104 parser = optparse.OptionParser(usage=usage,
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
105 description=__doc__)
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
106 parser.add_option('--collapse', dest='collapse',
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
107 action='store_true', default=False,
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
108 help="collapse menus with a single item to that item")
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
109 parser.add_option('-o', '--output', dest='output',
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
110 help="output file [Default: <stdout>]")
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
111 options, args = parser.parse_args(args)
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
112
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
113 # setup input, output
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
114 if args:
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
115 htmlfile = file(args[0])
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
116 else:
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
117 htmlfile = sys.stdin
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
118 html = htmlfile.read()
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
119 fluxout = sys.stdout
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
120
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
121 # get first element
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
122 dom = etree.fromstring(html)
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
123 dl = dom.find('.//dl')
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
124
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
125 # print to stdout
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
126 printmenu(dl, fluxout)
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
127
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
128 if __name__ == '__main__':
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
129 main()