annotate python/html2flux.py @ 651:66a7729aa049

fix this function
author Jeff Hammel <k0scist@gmail.com>
date Fri, 28 Mar 2014 18:55:49 -0700
parents fbfc02ea7d8e
children c26ec5ea9b9a
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')])
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
16
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
17 - add graphterm: graphterm (should be in ~/k0s/)
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
18 * if a menu has a single item, that is *the* implementor to that interface! :)
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
19 - display fluxbox menu items by `title` attribute, if possible
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
20 * (see class-based usage below)
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
21 - something about && for joining
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
22 - class-based usage (class="shell|...")
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
23 * and is the shell persistent? small? etc.
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
24 * also, sudo! (or other access related thing)
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
25 * resolve: xmessage v gmessage v xosd_cat etc
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
26 * classes: shell, root, message
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
27 - an easier way of editing o_O
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
28 * add an item to a menu
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
29 * submenu
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
30 - utility to find missing commands
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
31 - utility to add command + (e.g. ubuntu) install
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
32 - python package for all of this
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
33 * ...which also sets up the whole html2flux thing
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
34 - ....and the 50 billion dollar problem is:
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
35 How to integrate $(this) and http://k0s.org/portfolio/ubuntu-packages.txt
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
36 * these should have classifiers and ultimately work in to toolbox
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
37 - can a workflow be mapped to a (X; and, tbf, hierarchal) menu?!?
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
38 * YOU'RE SEEING IT, RIGHT?
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
39 - that said, a hierachal menu...ain't the best; maybe something like
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
40 dasher....
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
41 - to that effect, amonst others, menu items of the same set
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
42 should be added multiple places. that is to say, the menu item
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
43 indicated as `[foo][bar]fleem` could also live under
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
44 `[bar][foo]fleem`
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
45 """
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
46
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
47 import argparse
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
48 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
49 import sys
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 from lxml import etree
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
51 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
52
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
53 string = (str, unicode)
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
54
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
55 # available executables
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
56 executables = set([os.path.basename(i) for i in lsex() ])
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57
620
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
58 # TODO: next generation
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
59 # class HtmlMenu
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
60 # def _init__(self, html):
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
61 # if isinstance (html, string):
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
62 # html =
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
63 # for item in html
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
64
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
65 # class Command
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
66 # classname = ''
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
67
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
68 # class Sudo(Command)
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
69 # classname = 'sudo'
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
70 # programs = (('gksudo',),)
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
71
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
72 def readmenu(dl, output, top=True):
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
73 """read menu from an <dl> tag"""
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
74 # TODO: probably don't really need lxml
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
75
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
76 menu_items = []
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
77 name = None # menu name
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
78 firstchild = True
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
79 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
80 for child in dl.iterchildren():
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
81
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
82 if not top and child.tag == 'a' and firstchild:
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
83 # TODO: better way of labeling this!
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
84 name = child.text.strip()
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
85
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
86 if child.tag == 'dt':
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
87 # item label
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
88 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
89 if child.tag == 'dd':
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
90 # command
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
91 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
92 # 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
93 executable = command.split()[0]
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
94 if executable in executables or os.path.isabs(executable):
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
95 menu_items.append((label, command))
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
96
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
97 # submenu
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
98 if child.tag == 'dl':
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
99 menu_items.append(readmenu(child, output, top=False))
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
100
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
101 return (name, menu_items)
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
102
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
103
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
104 def printflux(name, menu, output, top=True):
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
105 """
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
106 print menu in fluxbox notation
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
107
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
108 - output: file-like object for writing
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
109 """
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
110
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
111 if not menu:
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
112 return
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
113
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
114 # print [submenu] tag for this menu
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
115 name = name or ''
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
116 if not top:
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
117 print >> output, '[submenu] (%s)' % name
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
118
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
119 # print menu items
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
120 for name, item in menu:
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
121 if isinstance(item, basestring):
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
122 # command
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
123 print >> output, '[exec] (%s) {%s}' % (name, item)
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
124 else:
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
125 # submenu
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
126 printflux(name, item, output, top=False)
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
127
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
128 # 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
129 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
130 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
131
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
132 def printmenu(dl, output):
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
133 name, menu = readmenu(dl, output)
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
134 if isinstance(output, string):
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
135 with open(output, 'w') as f:
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
136 printflux(name, menu, f)
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
137 else:
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
138 # file-like object
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
139 printflux(name, menu, output)
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
140
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
141 def main(args=sys.argv[1:]):
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
142 """command line interface"""
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
143
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
144 # parse command line
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
145 parser = argparse.ArgumentParser(description=__doc__)
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
146 parser.add_argument('htmlfile', metavar='programs.html', nargs='?',
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
147 type=argparse.FileType('r'), default=sys.stdin,
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
148 help='input file, or read from stdin if ommitted')
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
149 parser.add_argument('--collapse', dest='collapse',
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
150 action='store_true', default=False,
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
151 help="collapse menus with a single item to that item")
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
152 parser.add_argument('-o', '--output', dest='output',
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
153 help="output file [Default: <stdout>]")
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
154 options = 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
155
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
156 # read input
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
157 html = options.htmlfile.read()
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
158
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
159 # 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
160 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
161 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
162
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
163 # print to stdout
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
164 printmenu(dl, options.output or sys.stdout)
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
165
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
166 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
167 main()