# HG changeset patch # User Jeff Hammel # Date 1395476126 25200 # Node ID fbfc02ea7d8eb787c78b78590396c0fc2138e178 # Parent c6d3bfb2a21c32a42946ff80128569239503f712 make our menu generator slightly less vulgar diff -r c6d3bfb2a21c -r fbfc02ea7d8e .bashrc --- a/.bashrc Sat Mar 15 16:44:31 2014 -0700 +++ b/.bashrc Sat Mar 22 01:15:26 2014 -0700 @@ -618,18 +618,17 @@ . ${COGNET_BASHRC} fi -### regenerate fluxbox menus here for convenience if type deactivate &> /dev/null then deactivate fi + +### regenerate fluxbox menus here for convenience MENU=~/web/site/programs.html regeneratefluxmenu() { - if [ -e $MENU ] + if [ -e "${MENU}" ] then - # XXX could be safer - # XXX ...along with the fluxbox menu option o_O - html2flux.py $MENU > ~/.fluxbox/applications + html2flux.py "${MENU}" -o ~/.fluxbox/applications fi } regeneratefluxmenu diff -r c6d3bfb2a21c -r fbfc02ea7d8e python/html2flux.py --- a/python/html2flux.py Sat Mar 15 16:44:31 2014 -0700 +++ b/python/html2flux.py Sat Mar 22 01:15:26 2014 -0700 @@ -13,16 +13,47 @@ ('submenu name': [('program label', 'command'), ('another program label', 'command2')]) + + - add graphterm: graphterm (should be in ~/k0s/) + * if a menu has a single item, that is *the* implementor to that interface! :) + - display fluxbox menu items by `title` attribute, if possible + * (see class-based usage below) + - something about && for joining + - class-based usage (class="shell|...") + * and is the shell persistent? small? etc. + * also, sudo! (or other access related thing) + * resolve: xmessage v gmessage v xosd_cat etc + * classes: shell, root, message + - an easier way of editing o_O + * add an item to a menu + * submenu + - utility to find missing commands + - utility to add command + (e.g. ubuntu) install + - python package for all of this + * ...which also sets up the whole html2flux thing + - ....and the 50 billion dollar problem is: + How to integrate $(this) and http://k0s.org/portfolio/ubuntu-packages.txt + * these should have classifiers and ultimately work in to toolbox + - can a workflow be mapped to a (X; and, tbf, hierarchal) menu?!? + * YOU'RE SEEING IT, RIGHT? + - that said, a hierachal menu...ain't the best; maybe something like + dasher.... + - to that effect, amonst others, menu items of the same set + should be added multiple places. that is to say, the menu item + indicated as `[foo][bar]fleem` could also live under + `[bar][foo]fleem` """ -import optparse +import argparse import os import sys from lxml import etree from lsex import lsex # local import +string = (str, unicode) + # available executables -executables = set([i.rsplit('/', 1)[-1] for i in lsex() ]) +executables = set([os.path.basename(i) for i in lsex() ]) # TODO: next generation # class HtmlMenu @@ -69,11 +100,17 @@ return (name, menu_items) + def printflux(name, menu, output, top=True): """ + print menu in fluxbox notation + - output: file-like object for writing """ + if not menu: + return + # print [submenu] tag for this menu name = name or '' if not top: @@ -94,36 +131,37 @@ def printmenu(dl, output): name, menu = readmenu(dl, output) - printflux(name, menu, output) + if isinstance(output, string): + with open(output, 'w') as f: + printflux(name, menu, f) + else: + # file-like object + printflux(name, menu, output) def main(args=sys.argv[1:]): """command line interface""" - # parse command line option - usage = '%prog [options] [menu.html]' - parser = optparse.OptionParser(usage=usage, - description=__doc__) - parser.add_option('--collapse', dest='collapse', + # parse command line + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('htmlfile', metavar='programs.html', nargs='?', + type=argparse.FileType('r'), default=sys.stdin, + help='input file, or read from stdin if ommitted') + parser.add_argument('--collapse', dest='collapse', action='store_true', default=False, help="collapse menus with a single item to that item") - parser.add_option('-o', '--output', dest='output', + parser.add_argument('-o', '--output', dest='output', help="output file [Default: ]") - options, args = parser.parse_args(args) + options = parser.parse_args(args) - # setup input, output - if args: - htmlfile = file(args[0]) - else: - htmlfile = sys.stdin - html = htmlfile.read() - fluxout = sys.stdout + # read input + html = options.htmlfile.read() # get first element dom = etree.fromstring(html) dl = dom.find('.//dl') # print to stdout - printmenu(dl, fluxout) + printmenu(dl, options.output or sys.stdout) if __name__ == '__main__': main()