annotate python/html2flux.py @ 295:e2564dd51334

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