Mercurial > p > roundup > code
annotate roundup/cgi/TAL/XMLParser.py @ 5418:55f09ca366c4
Python 3 preparation: StringIO.
This generally arranges for StringIO and cStringIO references to use
io.StringIO for Python 3 but io.BytesIO for Python 2, consistent with
the string representations generally used in Roundup. A special
FasterStringIO in the TAL code, which referenced internals of the old
Python 2 StringIO module, is cut down so it doesn't actually do
anything beyond the StringIO class it inherits from (it would also be
reasonable to remove FasterStringIO completely). One place in
roundup_server.py clearly needing binary I/O is made to use io.BytesIO
unconditionally.
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Wed, 25 Jul 2018 09:08:29 +0000 |
| parents | 88dbacd11cd1 |
| children | 14a61eabcea8 |
| rev | line source |
|---|---|
| 1049 | 1 ############################################################################## |
| 2 # | |
| 3 # Copyright (c) 2001, 2002 Zope Corporation and Contributors. | |
| 4 # All Rights Reserved. | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
5 # |
| 1049 | 6 # This software is subject to the provisions of the Zope Public License, |
| 7 # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. | |
| 8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | |
| 9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | |
| 11 # FOR A PARTICULAR PURPOSE | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
12 # |
| 1049 | 13 ############################################################################## |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
14 # Modifications for Roundup: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
15 # 1. commented out zLOG references |
|
5402
88dbacd11cd1
Python 3 preparation: update urllib / urllib2 / urlparse imports.
Joseph Myers <jsm@polyomino.org.uk>
parents:
2348
diff
changeset
|
16 # 2. use roundup.anypy.urllib_ |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
17 """ |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
18 Generic expat-based XML parser base class. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
19 """ |
|
1071
c08b3820edd1
Adhering to ZPL
Richard Jones <richard@users.sourceforge.net>
parents:
1049
diff
changeset
|
20 |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
21 #import zLOG |
| 1049 | 22 |
| 23 class XMLParser: | |
| 24 | |
| 25 ordered_attributes = 0 | |
| 26 | |
| 27 handler_names = [ | |
| 28 "StartElementHandler", | |
| 29 "EndElementHandler", | |
| 30 "ProcessingInstructionHandler", | |
| 31 "CharacterDataHandler", | |
| 32 "UnparsedEntityDeclHandler", | |
| 33 "NotationDeclHandler", | |
| 34 "StartNamespaceDeclHandler", | |
| 35 "EndNamespaceDeclHandler", | |
| 36 "CommentHandler", | |
| 37 "StartCdataSectionHandler", | |
| 38 "EndCdataSectionHandler", | |
| 39 "DefaultHandler", | |
| 40 "DefaultHandlerExpand", | |
| 41 "NotStandaloneHandler", | |
| 42 "ExternalEntityRefHandler", | |
| 43 "XmlDeclHandler", | |
| 44 "StartDoctypeDeclHandler", | |
| 45 "EndDoctypeDeclHandler", | |
| 46 "ElementDeclHandler", | |
| 47 "AttlistDeclHandler" | |
| 48 ] | |
| 49 | |
| 50 def __init__(self, encoding=None): | |
| 51 self.parser = p = self.createParser() | |
| 52 if self.ordered_attributes: | |
| 53 try: | |
| 54 self.parser.ordered_attributes = self.ordered_attributes | |
| 55 except AttributeError: | |
| 56 #zLOG.LOG("TAL.XMLParser", zLOG.INFO, | |
| 57 # "Can't set ordered_attributes") | |
| 58 self.ordered_attributes = 0 | |
| 59 for name in self.handler_names: | |
| 60 method = getattr(self, name, None) | |
| 61 if method is not None: | |
| 62 try: | |
| 63 setattr(p, name, method) | |
| 64 except AttributeError: | |
| 65 #zLOG.LOG("TAL.XMLParser", zLOG.PROBLEM, | |
| 66 # "Can't set expat handler %s" % name) | |
| 67 pass | |
| 68 | |
| 69 def createParser(self, encoding=None): | |
| 70 global XMLParseError | |
| 71 try: | |
| 72 from Products.ParsedXML.Expat import pyexpat | |
| 73 XMLParseError = pyexpat.ExpatError | |
| 74 return pyexpat.ParserCreate(encoding, ' ') | |
| 75 except ImportError: | |
| 76 from xml.parsers import expat | |
| 77 XMLParseError = expat.ExpatError | |
| 78 return expat.ParserCreate(encoding, ' ') | |
| 79 | |
| 80 def parseFile(self, filename): | |
| 81 self.parseStream(open(filename)) | |
| 82 | |
| 83 def parseString(self, s): | |
| 84 self.parser.Parse(s, 1) | |
| 85 | |
| 86 def parseURL(self, url): | |
|
5402
88dbacd11cd1
Python 3 preparation: update urllib / urllib2 / urlparse imports.
Joseph Myers <jsm@polyomino.org.uk>
parents:
2348
diff
changeset
|
87 import roundup.anypy.urllib_ |
|
88dbacd11cd1
Python 3 preparation: update urllib / urllib2 / urlparse imports.
Joseph Myers <jsm@polyomino.org.uk>
parents:
2348
diff
changeset
|
88 self.parseStream(roundup.anypy.urllib_.urlopen(url)) |
| 1049 | 89 |
| 90 def parseStream(self, stream): | |
| 91 self.parser.ParseFile(stream) | |
| 92 | |
| 93 def parseFragment(self, s, end=0): | |
| 94 self.parser.Parse(s, end) |
