Mercurial > cpython-lru_cache-weakref
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 |
| rev | line source |
|---|---|
| 56488 | 1 """ |
| 2 A demo that reads in an RSS XML document and emits an HTML file containing | |
| 3 a list of the individual items in the feed. | |
| 4 """ | |
| 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 | 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 | 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 | 15 <html> |
| 16 <head> | |
| 17 <title>%s</title> | |
| 18 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
| 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 | 21 <body> |
| 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 | 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 | 28 <hr> |
| 29 <address> | |
| 30 Converted to HTML by rss2html.py. | |
| 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 | 33 </body> |
| 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 | 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 | 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 | 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 | 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 | 94 if __name__ == '__main__': |
| 95 parser = make_parser() | |
| 96 parser.setContentHandler(RSSHandler()) | |
| 97 parser.parse(sys.argv[1]) |
