annotate python/html2flux.py @ 343:71345f4de3ce

programs to list make targets
author Jeff Hammel <jhammel@mozilla.com>
date Sun, 23 Jun 2013 12:54:11 -0700
parents ee3c1b65d6d1
children a43d0205f80b
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
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
27 def readmenu(dl, output, top=True):
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
28 """read menu from an <dl> tag"""
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
29 # TODO: probably don't really need lxml
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
30
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
31 menu_items = []
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
32 name = None # menu name
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
33 firstchild = True
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
34 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
35 for child in dl.iterchildren():
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
36
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
37 if not top and child.tag == 'a' and firstchild:
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
38 # TODO: better way of labeling this!
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
39 name = child.text.strip()
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
40
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 if child.tag == 'dt':
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
42 # item label
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
43 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
44 if child.tag == 'dd':
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
45 # command
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
46 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
47 # 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
48 executable = command.split()[0]
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
49 if executable in executables or os.path.isabs(executable):
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
50 menu_items.append((label, command))
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
51
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
52 # submenu
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
53 if child.tag == 'dl':
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
54 menu_items.append(readmenu(child, output, top=False))
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
55
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
56 return (name, menu_items)
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
57
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
58 def printflux(name, menu, output, top=True):
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
59 """
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
60 - output: file-like object for writing
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
61 """
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
62
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
63 # print [submenu] tag for this menu
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
64 name = name or ''
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
65 if not top:
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
66 print >> output, '[submenu] (%s)' % name
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
67
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
68 # print menu items
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
69 for name, item in menu:
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
70 if isinstance(item, basestring):
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
71 # command
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
72 print >> output, '[exec] (%s) {%s}' % (name, item)
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
73 else:
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
74 # submenu
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
75 printflux(name, item, output, top=False)
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 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
78 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
79 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
80
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
81 def printmenu(dl, output):
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
82 name, menu = readmenu(dl, output)
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
83 printflux(name, menu, output)
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
84
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
85 def main(args=sys.argv[1:]):
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
86 """command line interface"""
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
87
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
88 # parse command line option
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
89 usage = '%prog [options] [menu.html]'
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
90 parser = optparse.OptionParser(usage=usage,
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
91 description=__doc__)
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
92 parser.add_option('--collapse', dest='collapse',
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
93 action='store_true', default=False,
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
94 help="collapse menus with a single item to that item")
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
95 parser.add_option('-o', '--output', dest='output',
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
96 help="output file [Default: <stdout>]")
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
97 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
98
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
99 # 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
100 if args:
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
101 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
102 else:
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
103 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
104 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
105 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
106
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
107 # 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
108 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
109 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
110
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
111 # 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
112 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
113
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
114 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
115 main()