comparison python/html2flux.py @ 45:069a739d88ad

get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
author Jeff Hammel <k0scist@gmail.com>
date Sat, 27 Mar 2010 09:49:33 -0700
parents
children fbc033540a34
comparison
equal deleted inserted replaced
43:ff7f771a9baf 45:069a739d88ad
1 #!/usr/bin/env python
2
3 import sys
4 from lxml import etree
5
6 from lsex import lsex # local import
7 executables = set([i.rsplit('/', 1)[-1] for i in lsex() ])
8
9 def printmenu(dl, output, top=True):
10
11 # XXX should do more checking
12 for child in dl.iterchildren():
13 if not top and child.tag == 'a':
14 print >> output, '[submenu] (%s)' % child.text
15 if child.tag == 'dt':
16 label = ' '.join([ i.strip() for i in child.itertext() if i.strip() ])
17 if child.tag == 'dd':
18 command = ' '.join([ i.strip() for i in child.itertext() if i.strip() ])
19 executable = command.split()[0]
20 if executable in executables:
21 print >> output, '[exec] (%s) {%s}' % (label, command)
22 if child.tag == 'dl':
23 printmenu(child, output, top=False)
24 if not top:
25 print >> output, '[end]'
26
27 def main(args = sys.argv[1:]):
28
29 # setup input, output
30 if args:
31 htmlfile = file(args[0])
32 else:
33 htmlfile = sys.stdin
34 html = htmlfile.read()
35 fluxout = sys.stdout
36
37 # get first element
38 dom = etree.fromstring(html)
39 dl = dom.find('.//dl')
40
41 printmenu(dl, fluxout)
42
43 if __name__ == '__main__':
44 main()