Mercurial > p > roundup > code
annotate roundup/template_parser.py @ 931:3dba266f94d8
this oughta be better
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Fri, 02 Aug 2002 04:43:53 +0000 |
| parents | |
| children | a43fa69c1b70 |
| rev | line source |
|---|---|
|
931
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1 import htmllib, formatter |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
2 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
3 class Require: |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
4 ''' Encapsulates a parsed <require attributes>...[<else>...]</require> |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
5 ''' |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
6 def __init__(self, attributes): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
7 self.attributes = attributes |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
8 self.current = self.ok = [] |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
9 self.fail = [] |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
10 def __len__(self): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
11 return len(self.current) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
12 def __getitem__(self, n): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
13 return self.current[n] |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
14 def __setitem__(self, n, data): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
15 self.current[n] = data |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
16 def append(self, data): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
17 self.current.append(data) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
18 def elseMode(self): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
19 self.current = self.fail |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
20 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
21 class Display: |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
22 ''' Encapsulates a parsed <display attributes> |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
23 ''' |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
24 def __init__(self, attributes): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
25 self.attributes = attributes |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
26 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
27 class Property: |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
28 ''' Encapsulates a parsed <property attributes> |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
29 ''' |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
30 def __init__(self, attributes): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
31 self.attributes = attributes |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
32 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
33 class RoundupTemplateParser(htmllib.HTMLParser): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
34 ''' Parse Roundup's HTML template structure into a list of components: |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
35 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
36 'string': this is just plain data to be displayed |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
37 Display : instances indicate that display functions are to be called |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
38 Require : if/else style check using the conditions in the attributes, |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
39 displaying the "ok" list of components or "fail" list |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
40 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
41 ''' |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
42 def __init__(self): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
43 htmllib.HTMLParser.__init__(self, formatter.NullFormatter()) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
44 self.current = self.structure = [] |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
45 self.stack = [] |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
46 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
47 def handle_data(self, data): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
48 self.append_data(data) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
49 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
50 def append_data(self, data): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
51 if self.current and isinstance(self.current[-1], type('')): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
52 self.current[-1] = self.current[-1] + data |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
53 else: |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
54 self.current.append(data) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
55 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
56 def unknown_starttag(self, tag, attributes): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
57 s = '' |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
58 s = s + '<%s' % tag |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
59 for name, value in attributes: |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
60 s = s + ' %s="%s"' % (name, value) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
61 s = s + '>' |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
62 self.append_data(s) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
63 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
64 def handle_starttag(self, tag, method, attributes): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
65 if tag in ('require', 'else', 'display', 'property'): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
66 method(attributes) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
67 else: |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
68 self.unknown_starttag(tag, attributes) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
69 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
70 def unknown_endtag(self, tag): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
71 if tag == 'require': |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
72 self.current = self.stack.pop() |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
73 else: |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
74 self.append_data('</%s>'%tag) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
75 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
76 def handle_endtag(self, tag, method): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
77 self.unknown_endtag(tag) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
78 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
79 def close(self): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
80 htmllib.HTMLParser.close(self) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
81 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
82 def do_display(self, attributes): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
83 self.current.append(Display(attributes)) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
84 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
85 def do_property(self, attributes): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
86 self.current.append(Property(attributes)) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
87 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
88 def do_require(self, attributes): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
89 r = Require(attributes) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
90 self.current.append(r) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
91 self.stack.append(self.current) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
92 self.current = r |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
93 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
94 def do_else(self, attributes): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
95 self.current.elseMode() |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
96 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
97 def display(structure, indent=''): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
98 ''' Pretty-print the parsed structure for debugging |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
99 ''' |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
100 for entry in structure: |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
101 if isinstance(entry, type('')): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
102 print "%s%r"%(indent, entry[:50]) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
103 elif isinstance(entry, Require): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
104 print '%sTEST: %r'%(indent, entry.attributes) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
105 print '%sOK...'%indent |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
106 display(entry.ok, indent+' ') |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
107 if entry.fail: |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
108 print '%sFAIL...'%indent |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
109 display(entry.fail, indent+' ') |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
110 elif isinstance(entry, Display): |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
111 print '%sDISPLAY: %r'%(indent, entry.attributes) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
112 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
113 if __name__ == '__main__': |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
114 import sys |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
115 parser = RoundupTemplateParser() |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
116 parser.feed(open(sys.argv[1], 'r').read()) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
117 display(parser.structure) |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
118 |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
119 # |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
120 # $Log: not supported by cvs2svn $ |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
121 # |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
122 # vim: set filetype=python ts=4 sw=4 et si |
|
3dba266f94d8
this oughta be better
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
123 |
