annotate python/html2flux.py @ 293:45b970048ae2

resurrect this ole beast
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 11 May 2013 03:24:47 -0700
parents fbc033540a34
children a0d830fd8a42
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
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 import sys
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 from lxml import etree
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
21 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
22
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
23 # 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
24 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
25
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
26 def readmenu(dl, output, top=True):
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
27
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
28 menu_items = []
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
29 name = None # menu name
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 for child in dl.iterchildren():
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
31
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 if not top and child.tag == 'a':
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
33 # TODO: better way of labeling this!
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 print >> output, '[submenu] (%s)' % child.text
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
35
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 if child.tag == 'dt':
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
37 # item label
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
38 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
39 if child.tag == 'dd':
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
40 command = ' '.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
41 executable = command.split()[0]
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
42 if executable in executables or os.path.isabs(executable):
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 print >> output, '[exec] (%s) {%s}' % (label, command)
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
44
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 if child.tag == 'dl':
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 printmenu(child, output, top=False)
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 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
48 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
49
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
50 def printmenu(dl, output):
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
51 """
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
52 - output: file-like object for writing
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
53 """
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
54 menu = readmenu(dl, output)
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
55
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
56 def main(args=sys.argv[1:]):
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
57 """command line interface"""
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
58
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
59 # parse command line option
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
60 usage = '%prog [options] [menu.html]'
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
61 parser = optparse.OptionParser(usage=usage,
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
62 description=__doc__)
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
63 parser.add_option('--collapse', dest='collapse',
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
64 action='store_true', default=False,
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
65 help="collapse menus with a single item to that item")
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
66 parser.add_option('-o', '--output', dest='output',
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
67 help="output file [Default: <stdout>]")
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
68 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
69
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70 # 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
71 if args:
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 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
73 else:
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 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
75 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
76 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
77
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78 # 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
79 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
80 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
81
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
82 # 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
83 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
84
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
85 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
86 main()