annotate Demo/xml/rss2html.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents a5d7b5277951
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
56488
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
1 """
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
2 A demo that reads in an RSS XML document and emits an HTML file containing
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
3 a list of the individual items in the feed.
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
4 """
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
5
15241
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6 import sys
56488
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
7 import codecs
15241
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 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
10
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
11 # --- Templates
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12
56488
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
13 top = """\
15241
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
56488
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
15 <html>
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
16 <head>
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
17 <title>%s</title>
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
18 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
19 </head>
15241
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20
56488
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
21 <body>
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
22 <h1>%s</h1>
15241
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23 """
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24
56488
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
25 bottom = """
15241
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26 </ul>
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
27
56488
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
28 <hr>
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
29 <address>
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
30 Converted to HTML by rss2html.py.
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
31 </address>
15241
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
32
56488
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
33 </body>
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
34 </html>
15241
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
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37 # --- 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
38
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39 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
40
56488
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
41 def __init__(self, out=sys.stdout):
15241
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 handler.ContentHandler.__init__(self)
56488
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
43 self._out = codecs.getwriter('utf-8')(out)
15241
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44
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._text = ""
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46 self._parent = None
56488
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
47 self._list_started = False
15241
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48 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
49 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
50 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
51
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52 # 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
53
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54 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
55 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
56 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
57
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._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
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60 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
61 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
62 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
63 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
64 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
65 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
66
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 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
68 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
69 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
70 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
71 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
72 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
73 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
74 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
75 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
76 self._out.write("<ul>\n")
56488
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
77 self._list_started = True
15241
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
78
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._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
80 (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
81
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._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
83 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
84 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
85
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86 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
87 self._out.write(bottom)
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 15241
diff changeset
88
15241
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
89 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
90 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
91
657750c1d783 Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
92 # --- 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
93
56488
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
94 if __name__ == '__main__':
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
95 parser = make_parser()
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
96 parser.setContentHandler(RSSHandler())
a5d7b5277951 Overhaul of Demo/xml.
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
97 parser.parse(sys.argv[1])