Mercurial > hg > config
diff python/html2flux.py @ 294:a0d830fd8a42
refactor this bad boy
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sat, 11 May 2013 03:49:06 -0700 |
parents | 45b970048ae2 |
children | ee3c1b65d6d1 |
line wrap: on
line diff
--- a/python/html2flux.py Sat May 11 03:24:47 2013 -0700 +++ b/python/html2flux.py Sat May 11 03:49:06 2013 -0700 @@ -16,6 +16,7 @@ """ import optparse +import os import sys from lxml import etree from lsex import lsex # local import @@ -27,31 +28,50 @@ menu_items = [] name = None # menu name + firstchild = True + label = None for child in dl.iterchildren(): - if not top and child.tag == 'a': + if not top and child.tag == 'a' and firstchild: # TODO: better way of labeling this! - print >> output, '[submenu] (%s)' % child.text + name = child.text.strip() if child.tag == 'dt': # item label label = ' '.join([i.strip() for i in child.itertext() if i.strip()]) if child.tag == 'dd': + # command command = ' '.join([i.strip() for i in child.itertext() if i.strip()]) + # TODO: classes executable = command.split()[0] if executable in executables or os.path.isabs(executable): - print >> output, '[exec] (%s) {%s}' % (label, command) + menu_items.append((label, command)) + + # submenu + if child.tag == 'dl': + menu_items.append(readmenu(child, output, top=False)) + + return (name, menu_items) - if child.tag == 'dl': - printmenu(child, output, top=False) +def printflux(name, menu, output, top=True): + """ + - output: file-like object for writing + """ + name = name or '' + print >> output, '[submenu] (%s)' % name + for name, item in menu: + if isinstance(item, basestring): + # command + print >> output, '[exec] (%s) {%s}' % (name, item) + else: + # submenu + printflux(name, item, output, top=False) if not top: print >> output, '[end]' def printmenu(dl, output): - """ - - output: file-like object for writing - """ - menu = readmenu(dl, output) + name, menu = readmenu(dl, output) + printflux(name, menu, output) def main(args=sys.argv[1:]): """command line interface"""