1 import htmllib, formatter
3 class Require:
4 ''' Encapsulates a parsed <require attributes>...[<else>...]</require>
5 '''
6 def __init__(self, attributes):
7 self.attributes = attributes
8 self.current = self.ok = []
9 self.fail = []
10 def __len__(self):
11 return len(self.current)
12 def __getitem__(self, n):
13 return self.current[n]
14 def __setitem__(self, n, data):
15 self.current[n] = data
16 def append(self, data):
17 self.current.append(data)
18 def elseMode(self):
19 self.current = self.fail
20 def __repr__(self):
21 return '<Require %r ok:%r fail:%r>'%(self.attributes, self.ok,
22 self.fail)
24 class Display:
25 ''' Encapsulates a parsed <display attributes>
26 '''
27 def __init__(self, attributes):
28 self.attributes = attributes
29 def __repr__(self):
30 return '<Display %r>'%self.attributes
32 class Property:
33 ''' Encapsulates a parsed <property attributes>
34 '''
35 def __init__(self, attributes):
36 self.attributes = attributes
37 self.current = self.ok = []
38 def __len__(self):
39 return len(self.current)
40 def __getitem__(self, n):
41 return self.current[n]
42 def __setitem__(self, n, data):
43 self.current[n] = data
44 def append(self, data):
45 self.current.append(data)
46 def __repr__(self):
47 return '<Property %r %r>'%(self.attributes, self.structure)
49 class RoundupTemplate(htmllib.HTMLParser):
50 ''' Parse Roundup's HTML template structure into a list of components:
52 'string': this is just plain data to be displayed
53 Display : instances indicate that display functions are to be called
54 Require : if/else style check using the conditions in the attributes,
55 displaying the "ok" list of components or "fail" list
57 '''
58 def __init__(self):
59 htmllib.HTMLParser.__init__(self, formatter.NullFormatter())
60 self.current = self.structure = []
61 self.stack = []
63 def handle_data(self, data):
64 self.append_data(data)
66 def append_data(self, data):
67 if self.current and isinstance(self.current[-1], type('')):
68 self.current[-1] = self.current[-1] + data
69 else:
70 self.current.append(data)
72 def unknown_starttag(self, tag, attributes):
73 s = ''
74 s = s + '<%s' % tag
75 for name, value in attributes:
76 s = s + ' %s="%s"' % (name, value)
77 s = s + '>'
78 self.append_data(s)
80 def handle_starttag(self, tag, method, attributes):
81 if tag in ('require', 'else', 'display', 'property'):
82 method(attributes)
83 else:
84 self.unknown_starttag(tag, attributes)
86 def unknown_endtag(self, tag):
87 if tag in ('require','property'):
88 self.current = self.stack.pop()
89 else:
90 self.append_data('</%s>'%tag)
92 def handle_endtag(self, tag, method):
93 self.unknown_endtag(tag)
95 def close(self):
96 htmllib.HTMLParser.close(self)
98 def do_display(self, attributes):
99 self.current.append(Display(attributes))
101 def do_property(self, attributes):
102 p = Property(attributes)
103 self.current.append(p)
104 self.stack.append(self.current)
105 self.current = p
107 def do_require(self, attributes):
108 r = Require(attributes)
109 self.current.append(r)
110 self.stack.append(self.current)
111 self.current = r
113 def do_else(self, attributes):
114 self.current.elseMode()
116 def __repr__(self):
117 return '<RoundupTemplate %r>'%self.structure
119 def display(structure, indent=''):
120 ''' Pretty-print the parsed structure for debugging
121 '''
122 l = []
123 for entry in structure:
124 if isinstance(entry, type('')):
125 l.append("%s%s"%(indent, entry))
126 elif isinstance(entry, Require):
127 l.append('%sTEST: %r\n'%(indent, entry.attributes))
128 l.append('%sOK...'%indent)
129 l.append(display(entry.ok, indent+' '))
130 if entry.fail:
131 l.append('%sFAIL...'%indent)
132 l.append(display(entry.fail, indent+' '))
133 elif isinstance(entry, Display):
134 l.append('%sDISPLAY: %r'%(indent, entry.attributes))
135 elif isinstance(entry, Property):
136 l.append('%sPROPERTY: %r'%(indent, entry.attributes))
137 l.append(display(entry.ok, indent+' '))
138 return ''.join(l)
140 if __name__ == '__main__':
141 import sys
142 parser = RoundupTemplate()
143 parser.feed(open(sys.argv[1], 'r').read())
144 print display(parser.structure)