comparison 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
comparison
equal deleted inserted replaced
293:45b970048ae2 294:a0d830fd8a42
14 ('submenu name': [('program label', 'command'), 14 ('submenu name': [('program label', 'command'),
15 ('another program label', 'command2')]) 15 ('another program label', 'command2')])
16 """ 16 """
17 17
18 import optparse 18 import optparse
19 import os
19 import sys 20 import sys
20 from lxml import etree 21 from lxml import etree
21 from lsex import lsex # local import 22 from lsex import lsex # local import
22 23
23 # available executables 24 # available executables
25 26
26 def readmenu(dl, output, top=True): 27 def readmenu(dl, output, top=True):
27 28
28 menu_items = [] 29 menu_items = []
29 name = None # menu name 30 name = None # menu name
31 firstchild = True
32 label = None
30 for child in dl.iterchildren(): 33 for child in dl.iterchildren():
31 34
32 if not top and child.tag == 'a': 35 if not top and child.tag == 'a' and firstchild:
33 # TODO: better way of labeling this! 36 # TODO: better way of labeling this!
34 print >> output, '[submenu] (%s)' % child.text 37 name = child.text.strip()
35 38
36 if child.tag == 'dt': 39 if child.tag == 'dt':
37 # item label 40 # item label
38 label = ' '.join([i.strip() for i in child.itertext() if i.strip()]) 41 label = ' '.join([i.strip() for i in child.itertext() if i.strip()])
39 if child.tag == 'dd': 42 if child.tag == 'dd':
43 # command
40 command = ' '.join([i.strip() for i in child.itertext() if i.strip()]) 44 command = ' '.join([i.strip() for i in child.itertext() if i.strip()])
45 # TODO: classes
41 executable = command.split()[0] 46 executable = command.split()[0]
42 if executable in executables or os.path.isabs(executable): 47 if executable in executables or os.path.isabs(executable):
43 print >> output, '[exec] (%s) {%s}' % (label, command) 48 menu_items.append((label, command))
44 49
50 # submenu
45 if child.tag == 'dl': 51 if child.tag == 'dl':
46 printmenu(child, output, top=False) 52 menu_items.append(readmenu(child, output, top=False))
53
54 return (name, menu_items)
55
56 def printflux(name, menu, output, top=True):
57 """
58 - output: file-like object for writing
59 """
60 name = name or ''
61 print >> output, '[submenu] (%s)' % name
62 for name, item in menu:
63 if isinstance(item, basestring):
64 # command
65 print >> output, '[exec] (%s) {%s}' % (name, item)
66 else:
67 # submenu
68 printflux(name, item, output, top=False)
47 if not top: 69 if not top:
48 print >> output, '[end]' 70 print >> output, '[end]'
49 71
50 def printmenu(dl, output): 72 def printmenu(dl, output):
51 """ 73 name, menu = readmenu(dl, output)
52 - output: file-like object for writing 74 printflux(name, menu, output)
53 """
54 menu = readmenu(dl, output)
55 75
56 def main(args=sys.argv[1:]): 76 def main(args=sys.argv[1:]):
57 """command line interface""" 77 """command line interface"""
58 78
59 # parse command line option 79 # parse command line option