@@ -17,20 +17,36 @@ def append(self, data):
1717 self .current .append (data )
1818 def elseMode (self ):
1919 self .current = self .fail
20+ def __repr__ (self ):
21+ return '<Require %r ok:%r fail:%r>' % (self .attributes , self .ok ,
22+ self .fail )
2023
2124class Display :
2225 ''' Encapsulates a parsed <display attributes>
2326 '''
2427 def __init__ (self , attributes ):
2528 self .attributes = attributes
29+ def __repr__ (self ):
30+ return '<Display %r>' % self .attributes
2631
2732class Property :
2833 ''' Encapsulates a parsed <property attributes>
2934 '''
3035 def __init__ (self , attributes ):
3136 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 )
3248
33- class RoundupTemplateParser (htmllib .HTMLParser ):
49+ class RoundupTemplate (htmllib .HTMLParser ):
3450 ''' Parse Roundup's HTML template structure into a list of components:
3551
3652 'string': this is just plain data to be displayed
@@ -68,7 +84,7 @@ def handle_starttag(self, tag, method, attributes):
6884 self .unknown_starttag (tag , attributes )
6985
7086 def unknown_endtag (self , tag ):
71- if tag == 'require' :
87+ if tag in ( 'require' , 'property' ) :
7288 self .current = self .stack .pop ()
7389 else :
7490 self .append_data ('</%s>' % tag )
@@ -83,7 +99,10 @@ def do_display(self, attributes):
8399 self .current .append (Display (attributes ))
84100
85101 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
87106
88107 def do_require (self , attributes ):
89108 r = Require (attributes )
@@ -94,30 +113,33 @@ def do_require(self, attributes):
94113 def do_else (self , attributes ):
95114 self .current .elseMode ()
96115
116+ def __repr__ (self ):
117+ return '<RoundupTemplate %r>' % self .structure
118+
97119def display (structure , indent = '' ):
98120 ''' Pretty-print the parsed structure for debugging
99121 '''
122+ l = []
100123 for entry in structure :
101124 if isinstance (entry , type ('' )):
102- print "%s%r " % (indent , entry [: 50 ] )
125+ l . append ( "%s%s " % (indent , entry ) )
103126 elif isinstance (entry , Require ):
104- print '%sTEST: %r' % (indent , entry .attributes )
105- print '%sOK...' % indent
106- display (entry .ok , indent + ' ' )
127+ l . append ( '%sTEST: %r\n ' % (indent , entry .attributes ) )
128+ l . append ( '%sOK...' % indent )
129+ l . append ( display (entry .ok , indent + ' ' ) )
107130 if entry .fail :
108- print '%sFAIL...' % indent
109- display (entry .fail , indent + ' ' )
131+ l . append ( '%sFAIL...' % indent )
132+ l . append ( display (entry .fail , indent + ' ' ) )
110133 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 )
112139
113140if __name__ == '__main__' :
114141 import sys
115- parser = RoundupTemplateParser ()
142+ parser = RoundupTemplate ()
116143 parser .feed (open (sys .argv [1 ], 'r' ).read ())
117- display (parser .structure )
118-
119- #
120- # $Log: not supported by cvs2svn $
121- #
122- # vim: set filetype=python ts=4 sw=4 et si
144+ print display (parser .structure )
123145
0 commit comments