Mercurial > hg > config
annotate python/html2flux.py @ 910:98bcf207cda3
remove -dev from k0s-requirements.txt; not compatible with new pip
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 12 Apr 2024 07:32:37 -0700 |
parents | e5861cbbc4bb |
children |
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 |
882 | 53 try: |
54 # python2 | |
55 string = (str, unicode) | |
56 except NameError: | |
57 # python3 | |
58 string = (str,) | |
649
fbfc02ea7d8e
make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents:
620
diff
changeset
|
59 |
292 | 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 | 63 # TODO: next generation |
64 # class HtmlMenu | |
65 # def _init__(self, html): | |
66 # if isinstance (html, string): | |
67 # html = | |
68 # for item in html | |
69 | |
70 # class Command | |
71 # classname = '' | |
72 | |
73 # class Sudo(Command) | |
74 # classname = 'sudo' | |
75 # programs = (('gksudo',),) | |
76 | |
293 | 77 def readmenu(dl, output, top=True): |
296 | 78 """read menu from an <dl> tag""" |
79 # TODO: probably don't really need lxml | |
292 | 80 |
81 menu_items = [] | |
82 name = None # menu name | |
294 | 83 firstchild = True |
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 | 86 |
294 | 87 if not top and child.tag == 'a' and firstchild: |
292 | 88 # TODO: better way of labeling this! |
294 | 89 name = child.text.strip() |
292 | 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 | 92 # item label |
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 | 95 # command |
292 | 96 command = ' '.join([i.strip() for i in child.itertext() if i.strip()]) |
294 | 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 | 99 if executable in executables or os.path.isabs(executable): |
294 | 100 menu_items.append((label, command)) |
101 | |
102 # submenu | |
103 if child.tag == 'dl': | |
104 menu_items.append(readmenu(child, output, top=False)) | |
105 | |
106 return (name, menu_items) | |
292 | 107 |
649
fbfc02ea7d8e
make our menu generator slightly less vulgar
Jeff Hammel <k0scist@gmail.com>
parents:
620
diff
changeset
|
108 |
294 | 109 def printflux(name, menu, output, top=True): |
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 | 113 - output: file-like object for writing |
114 """ | |
296 | 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 | 119 # print [submenu] tag for this menu |
294 | 120 name = name or '' |
296 | 121 if not top: |
883 | 122 output.write('[submenu] (%s)\n' % name) |
296 | 123 |
124 # print menu items | |
294 | 125 for name, item in menu: |
883 | 126 if isinstance(item, string): |
294 | 127 # command |
884 | 128 output.write('[exec] (%s) {%s}\n' % (name, item)) |
294 | 129 else: |
130 # submenu | |
131 printflux(name, item, output, top=False) | |
296 | 132 |
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 | 135 output.write('[end]\n') |
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 | 138 def printmenu(dl, output): |
294 | 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 | 146 |
292 | 147 def main(args=sys.argv[1:]): |
293 | 148 """command line interface""" |
292 | 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 | 156 action='store_true', default=False, |
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 | 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 | 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() |