comparison roundup/template_parser.py @ 932:a43fa69c1b70

fixes to the new template parser
author Richard Jones <richard@users.sourceforge.net>
date Fri, 02 Aug 2002 23:45:41 +0000
parents 3dba266f94d8
children fdcf16b444a9
comparison
equal deleted inserted replaced
931:3dba266f94d8 932:a43fa69c1b70
15 self.current[n] = data 15 self.current[n] = data
16 def append(self, data): 16 def append(self, data):
17 self.current.append(data) 17 self.current.append(data)
18 def elseMode(self): 18 def elseMode(self):
19 self.current = self.fail 19 self.current = self.fail
20 def __repr__(self):
21 return '<Require %r ok:%r fail:%r>'%(self.attributes, self.ok,
22 self.fail)
20 23
21 class Display: 24 class Display:
22 ''' Encapsulates a parsed <display attributes> 25 ''' Encapsulates a parsed <display attributes>
23 ''' 26 '''
24 def __init__(self, attributes): 27 def __init__(self, attributes):
25 self.attributes = attributes 28 self.attributes = attributes
29 def __repr__(self):
30 return '<Display %r>'%self.attributes
26 31
27 class Property: 32 class Property:
28 ''' Encapsulates a parsed <property attributes> 33 ''' Encapsulates a parsed <property attributes>
29 ''' 34 '''
30 def __init__(self, attributes): 35 def __init__(self, attributes):
31 self.attributes = attributes 36 self.attributes = attributes
37 self.current = self.structure = []
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)
32 48
33 class RoundupTemplateParser(htmllib.HTMLParser): 49 class RoundupTemplate(htmllib.HTMLParser):
34 ''' Parse Roundup's HTML template structure into a list of components: 50 ''' Parse Roundup's HTML template structure into a list of components:
35 51
36 'string': this is just plain data to be displayed 52 'string': this is just plain data to be displayed
37 Display : instances indicate that display functions are to be called 53 Display : instances indicate that display functions are to be called
38 Require : if/else style check using the conditions in the attributes, 54 Require : if/else style check using the conditions in the attributes,
66 method(attributes) 82 method(attributes)
67 else: 83 else:
68 self.unknown_starttag(tag, attributes) 84 self.unknown_starttag(tag, attributes)
69 85
70 def unknown_endtag(self, tag): 86 def unknown_endtag(self, tag):
71 if tag == 'require': 87 if tag in ('require','property'):
72 self.current = self.stack.pop() 88 self.current = self.stack.pop()
73 else: 89 else:
74 self.append_data('</%s>'%tag) 90 self.append_data('</%s>'%tag)
75 91
76 def handle_endtag(self, tag, method): 92 def handle_endtag(self, tag, method):
81 97
82 def do_display(self, attributes): 98 def do_display(self, attributes):
83 self.current.append(Display(attributes)) 99 self.current.append(Display(attributes))
84 100
85 def do_property(self, attributes): 101 def do_property(self, attributes):
86 self.current.append(Property(attributes)) 102 p = Property(attributes)
103 self.current.append(p)
104 self.stack.append(self.current)
105 self.current = p
87 106
88 def do_require(self, attributes): 107 def do_require(self, attributes):
89 r = Require(attributes) 108 r = Require(attributes)
90 self.current.append(r) 109 self.current.append(r)
91 self.stack.append(self.current) 110 self.stack.append(self.current)
92 self.current = r 111 self.current = r
93 112
94 def do_else(self, attributes): 113 def do_else(self, attributes):
95 self.current.elseMode() 114 self.current.elseMode()
96 115
116 def __repr__(self):
117 return '<RoundupTemplate %r>'%self.structure
118
97 def display(structure, indent=''): 119 def display(structure, indent=''):
98 ''' Pretty-print the parsed structure for debugging 120 ''' Pretty-print the parsed structure for debugging
99 ''' 121 '''
122 l = []
100 for entry in structure: 123 for entry in structure:
101 if isinstance(entry, type('')): 124 if isinstance(entry, type('')):
102 print "%s%r"%(indent, entry[:50]) 125 l.append("%s%s"%(indent, entry))
103 elif isinstance(entry, Require): 126 elif isinstance(entry, Require):
104 print '%sTEST: %r'%(indent, entry.attributes) 127 l.append('%sTEST: %r\n'%(indent, entry.attributes))
105 print '%sOK...'%indent 128 l.append('%sOK...'%indent)
106 display(entry.ok, indent+' ') 129 l.append(display(entry.ok, indent+' '))
107 if entry.fail: 130 if entry.fail:
108 print '%sFAIL...'%indent 131 l.append('%sFAIL...'%indent)
109 display(entry.fail, indent+' ') 132 l.append(display(entry.fail, indent+' '))
110 elif isinstance(entry, Display): 133 elif isinstance(entry, Display):
111 print '%sDISPLAY: %r'%(indent, entry.attributes) 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.structure, indent+' '))
138 return ''.join(l)
112 139
113 if __name__ == '__main__': 140 if __name__ == '__main__':
114 import sys 141 import sys
115 parser = RoundupTemplateParser() 142 parser = RoundupTemplate()
116 parser.feed(open(sys.argv[1], 'r').read()) 143 parser.feed(open(sys.argv[1], 'r').read())
117 display(parser.structure) 144 print display(parser.structure)
118 145
119 #
120 # $Log: not supported by cvs2svn $
121 #
122 # vim: set filetype=python ts=4 sw=4 et si
123

Roundup Issue Tracker: http://roundup-tracker.org/