annotate Demo/xml/rss2html.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 657750c1d783
children c3a096bfbd6b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15241
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 import sys
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3 from xml.sax import make_parser, handler
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
5 # --- Templates
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7 top = \
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
8 """
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10 <HTML>
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
11 <HEAD>
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12 <TITLE>%s</TITLE>
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
13 </HEAD>
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15 <BODY>
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16 <H1>%s</H1>
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17 """
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 bottom = \
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20 """
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21 </ul>
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23 <HR>
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24 <ADDRESS>
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25 Converted to HTML by sax_rss2html.py.
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26 </ADDRESS>
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
27
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
28 </BODY>
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
29 </HTML>
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
30 """
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
32 # --- The ContentHandler
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34 class RSSHandler(handler.ContentHandler):
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 def __init__(self, out = sys.stdout):
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37 handler.ContentHandler.__init__(self)
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38 self._out = out
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 self._text = ""
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41 self._parent = None
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 self._list_started = 0
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43 self._title = None
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44 self._link = None
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45 self._descr = ""
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47 # ContentHandler methods
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49 def startElement(self, name, attrs):
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 if name == "channel" or name == "image" or name == "item":
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51 self._parent = name
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
53 self._text = ""
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 def endElement(self, name):
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56 if self._parent == "channel":
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 if name == "title":
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58 self._out.write(top % (self._text, self._text))
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 elif name == "description":
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60 self._out.write("<p>%s</p>\n" % self._text)
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
62 elif self._parent == "item":
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63 if name == "title":
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64 self._title = self._text
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 elif name == "link":
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 self._link = self._text
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67 elif name == "description":
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 self._descr = self._text
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
69 elif name == "item":
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
70 if not self._list_started:
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
71 self._out.write("<ul>\n")
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
72 self._list_started = 1
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
73
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
74 self._out.write(' <li><a href="%s">%s</a> %s\n' %
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
75 (self._link, self._title, self._descr))
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
76
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
77 self._title = None
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
78 self._link = None
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
79 self._descr = ""
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
80
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
81 if name == "rss":
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
82 self._out.write(bottom)
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
83
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
84 def characters(self, content):
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
85 self._text = self._text + content
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
87 # --- Main program
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
88
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
89 parser = make_parser()
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
90 parser.setContentHandler(RSSHandler())
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
91 parser.parse(sys.argv[1])