annotate python/html2flux.py @ 924:fc88a12acf48 default tip

add lfs filter
author Jeff Hammel <k0scist@gmail.com>
date Wed, 04 Dec 2024 15:50:53 -0800
parents e5861cbbc4bb
children
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
882
c26ec5ea9b9a minor fixes
Jeff Hammel <k0scist@gmail.com>
parents: 649
diff changeset
53 try:
c26ec5ea9b9a minor fixes
Jeff Hammel <k0scist@gmail.com>
parents: 649
diff changeset
54 # python2
c26ec5ea9b9a minor fixes
Jeff Hammel <k0scist@gmail.com>
parents: 649
diff changeset
55 string = (str, unicode)
c26ec5ea9b9a minor fixes
Jeff Hammel <k0scist@gmail.com>
parents: 649
diff changeset
56 except NameError:
c26ec5ea9b9a minor fixes
Jeff Hammel <k0scist@gmail.com>
parents: 649
diff changeset
57 # python3
c26ec5ea9b9a minor fixes
Jeff Hammel <k0scist@gmail.com>
parents: 649
diff changeset
58 string = (str,)
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
59
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
60 # available executables
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
61 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
62
620
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
63 # TODO: next generation
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
64 # class HtmlMenu
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
65 # def _init__(self, html):
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
66 # if isinstance (html, string):
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
67 # html =
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
68 # for item in html
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
69
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
70 # class Command
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
71 # classname = ''
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
72
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
73 # class Sudo(Command)
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
74 # classname = 'sudo'
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
75 # programs = (('gksudo',),)
a43d0205f80b STUB: python/html2flux.py
Jeff Hammel <k0scist@gmail.com>
parents: 296
diff changeset
76
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
77 def readmenu(dl, output, top=True):
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
78 """read menu from an <dl> tag"""
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
79 # TODO: probably don't really need lxml
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
80
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
81 menu_items = []
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
82 name = None # menu name
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
83 firstchild = True
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
84 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
85 for child in dl.iterchildren():
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
86
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
87 if not top and child.tag == 'a' and firstchild:
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
88 # TODO: better way of labeling this!
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
89 name = child.text.strip()
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
90
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
91 if child.tag == 'dt':
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
92 # item label
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
93 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
94 if child.tag == 'dd':
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
95 # command
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
96 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
97 # 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
98 executable = command.split()[0]
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
99 if executable in executables or os.path.isabs(executable):
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
100 menu_items.append((label, command))
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
101
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
102 # submenu
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
103 if child.tag == 'dl':
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
104 menu_items.append(readmenu(child, output, top=False))
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
105
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
106 return (name, menu_items)
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
107
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
108
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
109 def printflux(name, menu, output, top=True):
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
110 """
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
111 print menu in fluxbox notation
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
112
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
113 - output: file-like object for writing
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
114 """
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
115
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
116 if not menu:
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
117 return
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
118
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
119 # print [submenu] tag for this menu
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
120 name = name or ''
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
121 if not top:
883
3b7f67021877 python3
Jeff Hammel <k0scist@gmail.com>
parents: 882
diff changeset
122 output.write('[submenu] (%s)\n' % name)
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
123
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
124 # print menu items
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
125 for name, item in menu:
883
3b7f67021877 python3
Jeff Hammel <k0scist@gmail.com>
parents: 882
diff changeset
126 if isinstance(item, string):
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
127 # command
884
e5861cbbc4bb python3
Jeff Hammel <k0scist@gmail.com>
parents: 883
diff changeset
128 output.write('[exec] (%s) {%s}\n' % (name, item))
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
129 else:
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
130 # submenu
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
131 printflux(name, item, output, top=False)
296
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
132
ee3c1b65d6d1 random fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 294
diff changeset
133 # 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
134 if not top:
884
e5861cbbc4bb python3
Jeff Hammel <k0scist@gmail.com>
parents: 883
diff changeset
135 output.write('[end]\n')
e5861cbbc4bb python3
Jeff Hammel <k0scist@gmail.com>
parents: 883
diff changeset
136
45
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
137
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
138 def printmenu(dl, output):
294
a0d830fd8a42 refactor this bad boy
Jeff Hammel <jhammel@mozilla.com>
parents: 293
diff changeset
139 name, menu = readmenu(dl, output)
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
140 if isinstance(output, string):
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
141 with open(output, 'w') as f:
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
142 printflux(name, menu, f)
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
143 else:
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
144 # file-like object
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
145 printflux(name, menu, output)
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
146
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
147 def main(args=sys.argv[1:]):
293
45b970048ae2 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 292
diff changeset
148 """command line interface"""
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
149
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
150 # parse command line
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
151 parser = argparse.ArgumentParser(description=__doc__)
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
152 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
153 type=argparse.FileType('r'), default=sys.stdin,
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
154 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
155 parser.add_argument('--collapse', dest='collapse',
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
156 action='store_true', default=False,
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
157 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
158 parser.add_argument('-o', '--output', dest='output',
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
159 help="output file [Default: <stdout>]")
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
160 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
161
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
162 # read input
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
163 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
164
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
165 # 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
166 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
167 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
168
292
fbc033540a34 resurrect this ole beast
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
169 # print to stdout
649
fbfc02ea7d8e make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents: 620
diff changeset
170 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
171
069a739d88ad get fluxbox menu from a webpage, i.e. http://k0s.org/programs.html
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
172 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
173 main()