Home | Trees | Indices | Help |
|
---|
|
1 # 2 # ptree.etreeimpl -- Class for defining new etree implementations 3 # 4 # -------------------------------------------------------------------- 5 # The ptree package is 6 # 7 # Copyright (c) 2007 by Edward Loper 8 # 9 # By obtaining, using, and/or copying this software and/or its 10 # associated documentation, you agree that you have read, understood, 11 # and will comply with the following terms and conditions: 12 # 13 # Permission to use, copy, modify, and distribute this software and 14 # its associated documentation for any purpose and without fee is 15 # hereby granted, provided that the above copyright notice appears in 16 # all copies, and that both that copyright notice and this permission 17 # notice appear in supporting documentation. 18 # 19 # THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 20 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN 21 # NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR 22 # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 23 # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 24 # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 25 # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 26 # -------------------------------------------------------------------- 27 28 __all__ = ['ElementTreeImplementation'] 29 30 from xml.etree import ElementTree as ET 3133 """ 34 Instances of this class can be used as drop-in replacements for 35 the xml.etree.ElementTree module. 36 """ 39 44 45 ################################################################## 46 # Subclasses w/ new constructors 47 #----------------------------------------------------------------- 48 # These classes needed to have their constructer overridden, to 49 # change the default values of tree-building functions to point 50 # to the versions in *this file, not in ElementTree. 5115853 # [XXX] We can't do this without poking around in other 54 # people's internals! 55 iterator = ET.iterparse(source, events) 56 iterator._parser._target = TreeBuilder() 57 return iterator58 59 ################################################################## 60 # No dependencies on Element class 61 #----------------------------------------------------------------- 62 # These functions & classes do not depend, directly or indirectly, 63 # on the class used to implement elements; so we can just copy 64 # them. 65 66 SubElement = staticmethod(ET.SubElement) 67 QName = ET.QName 68 iselement = staticmethod(ET.iselement) 69 dump = staticmethod(ET.dump) 70 tostring= staticmethod(ET.tostring) 71 72 ################################################################## 73 # Modified Constructor Defaults 74 #----------------------------------------------------------------- 75 # These classes have default constructor parameter that depend on 76 # the class used to implement elements; so we wrap them in 77 # functions that provide new defaults. 7880 if element_factory is None: 81 element_factory = self._Element 82 return ET.TreeBuilder(element_factory)83 88 89 XMLParser = XMLTreeBuilder 90 91 ################################################################## 92 # Modified Method Defaults 93 #----------------------------------------------------------------- 94 # The ElementTree class has a method parameter whose default value 95 # depends on the class used to implement elements; so we replace 96 # it with a new subclass (_ElementTree) that stores a private 97 # pointer back to the ElementTreeImplementation instance; and uses it 98 # to construct an appropriate default for the method parameter. 99101 return self._ElementTree(self, element, file)110 111 ################################################################## 112 # Indirect dependencies on Element class 113 #----------------------------------------------------------------- 114 # These functions depend indirectly on the class used to implement 115 # elements; so we need to redefine them. Each method in this 116 # section is copied verbatim from etree/ElementTree.py, with the 117 # following exceptions: a 'self' parameter is added. And global 118 # variables that depend on the Element class are replaced with 119 # local versions. E.g., "XMLTreeBuilder" is replaced with 120 # "self.XMLTreeBuilder". 121 126104 self.__default_parser_class = etbase.XMLTreeBuilder 105 ET.ElementTree.__init__(self, element, file)107 if not parser: 108 parser = self.__default_parser_class() 109 ET.ElementTree.parse(self, source, parser)128 element = self.Element(self.ProcessingInstruction) 129 element.text = target 130 if text: 131 element.text = element.text + " " + text 132 return element133 134 PI = ProcessingInstruction 135 140 145147 parser = self.XMLTreeBuilder() 148 parser.feed(text) 149 tree = parser.close() 150 ids = {} 151 for elem in tree.getiterator(): 152 id = elem.get("id") 153 if id: 154 ids[id] = elem 155 return tree, ids156 157 fromstring = XML
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Tue Dec 11 20:41:38 2012 | http://epydoc.sourceforge.net |