Mercurial > hg > config
annotate python/html2flux.py @ 620:a43d0205f80b
STUB: python/html2flux.py
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 05 Feb 2014 08:33:16 -0800 |
parents | ee3c1b65d6d1 |
children | fbfc02ea7d8e |
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 | |
294 | 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 | 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 | 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 |
620 | 27 # TODO: next generation |
28 # class HtmlMenu | |
29 # def _init__(self, html): | |
30 # if isinstance (html, string): | |
31 # html = | |
32 # for item in html | |
33 | |
34 # class Command | |
35 # classname = '' | |
36 | |
37 # class Sudo(Command) | |
38 # classname = 'sudo' | |
39 # programs = (('gksudo',),) | |
40 | |
293 | 41 def readmenu(dl, output, top=True): |
296 | 42 """read menu from an <dl> tag""" |
43 # TODO: probably don't really need lxml | |
292 | 44 |
45 menu_items = [] | |
46 name = None # menu name | |
294 | 47 firstchild = True |
48 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
|
49 for child in dl.iterchildren(): |
292 | 50 |
294 | 51 if not top and child.tag == 'a' and firstchild: |
292 | 52 # TODO: better way of labeling this! |
294 | 53 name = child.text.strip() |
292 | 54 |
45
069a739d88ad
get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
55 if child.tag == 'dt': |
292 | 56 # item label |
57 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
|
58 if child.tag == 'dd': |
294 | 59 # command |
292 | 60 command = ' '.join([i.strip() for i in child.itertext() if i.strip()]) |
294 | 61 # 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
|
62 executable = command.split()[0] |
292 | 63 if executable in executables or os.path.isabs(executable): |
294 | 64 menu_items.append((label, command)) |
65 | |
66 # submenu | |
67 if child.tag == 'dl': | |
68 menu_items.append(readmenu(child, output, top=False)) | |
69 | |
70 return (name, menu_items) | |
292 | 71 |
294 | 72 def printflux(name, menu, output, top=True): |
73 """ | |
74 - output: file-like object for writing | |
75 """ | |
296 | 76 |
77 # print [submenu] tag for this menu | |
294 | 78 name = name or '' |
296 | 79 if not top: |
80 print >> output, '[submenu] (%s)' % name | |
81 | |
82 # print menu items | |
294 | 83 for name, item in menu: |
84 if isinstance(item, basestring): | |
85 # command | |
86 print >> output, '[exec] (%s) {%s}' % (name, item) | |
87 else: | |
88 # submenu | |
89 printflux(name, item, output, top=False) | |
296 | 90 |
91 # 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
|
92 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
|
93 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
|
94 |
293 | 95 def printmenu(dl, output): |
294 | 96 name, menu = readmenu(dl, output) |
97 printflux(name, menu, output) | |
293 | 98 |
292 | 99 def main(args=sys.argv[1:]): |
293 | 100 """command line interface""" |
292 | 101 |
102 # parse command line option | |
103 usage = '%prog [options] [menu.html]' | |
104 parser = optparse.OptionParser(usage=usage, | |
105 description=__doc__) | |
106 parser.add_option('--collapse', dest='collapse', | |
107 action='store_true', default=False, | |
108 help="collapse menus with a single item to that item") | |
109 parser.add_option('-o', '--output', dest='output', | |
110 help="output file [Default: <stdout>]") | |
111 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
|
112 |
069a739d88ad
get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
113 # 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
|
114 if args: |
069a739d88ad
get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
115 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
|
116 else: |
069a739d88ad
get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
117 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
|
118 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
|
119 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
|
120 |
069a739d88ad
get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
121 # 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
|
122 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
|
123 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
|
124 |
292 | 125 # 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
|
126 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
|
127 |
069a739d88ad
get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
128 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
|
129 main() |