Mercurial > hg > config
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 |
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 | 3 """ |
4 transform an HTML <dl> file into a fluxbox menu | |
5 if no file give, read from stdin | |
6 | |
7 <dl><a>submenu name</a> | |
8 <dt>program label</dt><dd>command</dd> | |
9 <dt>another program label</dt><dd>command2</dd> | |
10 </dl> | |
11 | |
12 x-form -> internal format: | |
13 | |
14 ('submenu name': [('program label', 'command'), | |
15 ('another program label', 'command2')]) | |
16 """ | |
17 | |
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 | 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 | 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 | 26 def readmenu(dl, output, top=True): |
292 | 27 |
28 menu_items = [] | |
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 | 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 | 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 | 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 | 37 # item label |
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 | 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 | 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 | 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 | 50 def printmenu(dl, output): |
51 """ | |
52 - output: file-like object for writing | |
53 """ | |
54 menu = readmenu(dl, output) | |
55 | |
292 | 56 def main(args=sys.argv[1:]): |
293 | 57 """command line interface""" |
292 | 58 |
59 # parse command line option | |
60 usage = '%prog [options] [menu.html]' | |
61 parser = optparse.OptionParser(usage=usage, | |
62 description=__doc__) | |
63 parser.add_option('--collapse', dest='collapse', | |
64 action='store_true', default=False, | |
65 help="collapse menus with a single item to that item") | |
66 parser.add_option('-o', '--output', dest='output', | |
67 help="output file [Default: <stdout>]") | |
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 | 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() |