Mercurial > hg > config
annotate python/html2flux.py @ 782:f643cb5e020a
docker-machine is so last month
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 24 Aug 2016 14:02:22 -0700 |
parents | fbfc02ea7d8e |
children | c26ec5ea9b9a |
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')]) | |
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 | 45 """ |
46 | |
649
fbfc02ea7d8e
make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents:
620
diff
changeset
|
47 import argparse |
294 | 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 | 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 | 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 | 58 # TODO: next generation |
59 # class HtmlMenu | |
60 # def _init__(self, html): | |
61 # if isinstance (html, string): | |
62 # html = | |
63 # for item in html | |
64 | |
65 # class Command | |
66 # classname = '' | |
67 | |
68 # class Sudo(Command) | |
69 # classname = 'sudo' | |
70 # programs = (('gksudo',),) | |
71 | |
293 | 72 def readmenu(dl, output, top=True): |
296 | 73 """read menu from an <dl> tag""" |
74 # TODO: probably don't really need lxml | |
292 | 75 |
76 menu_items = [] | |
77 name = None # menu name | |
294 | 78 firstchild = True |
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 | 81 |
294 | 82 if not top and child.tag == 'a' and firstchild: |
292 | 83 # TODO: better way of labeling this! |
294 | 84 name = child.text.strip() |
292 | 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 | 87 # item label |
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 | 90 # command |
292 | 91 command = ' '.join([i.strip() for i in child.itertext() if i.strip()]) |
294 | 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 | 94 if executable in executables or os.path.isabs(executable): |
294 | 95 menu_items.append((label, command)) |
96 | |
97 # submenu | |
98 if child.tag == 'dl': | |
99 menu_items.append(readmenu(child, output, top=False)) | |
100 | |
101 return (name, menu_items) | |
292 | 102 |
649
fbfc02ea7d8e
make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents:
620
diff
changeset
|
103 |
294 | 104 def printflux(name, menu, output, top=True): |
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 | 108 - output: file-like object for writing |
109 """ | |
296 | 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 | 114 # print [submenu] tag for this menu |
294 | 115 name = name or '' |
296 | 116 if not top: |
117 print >> output, '[submenu] (%s)' % name | |
118 | |
119 # print menu items | |
294 | 120 for name, item in menu: |
121 if isinstance(item, basestring): | |
122 # command | |
123 print >> output, '[exec] (%s) {%s}' % (name, item) | |
124 else: | |
125 # submenu | |
126 printflux(name, item, output, top=False) | |
296 | 127 |
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 | 132 def printmenu(dl, output): |
294 | 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 | 140 |
292 | 141 def main(args=sys.argv[1:]): |
293 | 142 """command line interface""" |
292 | 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 | 150 action='store_true', default=False, |
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 | 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 | 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() |