Package ptree :: Module etreeimpl
[hide private]
[frames] | no frames]

Source Code for Module ptree.etreeimpl

  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 
 31   
32 -class ElementTreeImplementation(object):
33 """ 34 Instances of this class can be used as drop-in replacements for 35 the xml.etree.ElementTree module. 36 """
37 - def __init__(self, ElementClass):
38 self._Element = ElementClass
39
40 - def Element(self, tag, attrib={}, **extra):
41 attrib = attrib.copy() 42 attrib.update(extra) 43 return self._Element(tag, attrib)
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. 51
52 - def iterparse(self, source, events=None):
53 # [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 iterator
58 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. 78
79 - def TreeBuilder(self, element_factory=None):
80 if element_factory is None: 81 element_factory = self._Element 82 return ET.TreeBuilder(element_factory)
83
84 - def XMLTreeBuilder(self, html=0, target=None):
85 if target is None: 86 target = self.TreeBuilder() 87 return ET.XMLTreeBuilder(html, target)
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. 99
100 - def ElementTree(self, element=None, file=None):
101 return self._ElementTree(self, element, file)
102 - class _ElementTree(ET.ElementTree):
103 - def __init__(self, etbase, element, file):
104 self.__default_parser_class = etbase.XMLTreeBuilder 105 ET.ElementTree.__init__(self, element, file)
106 - def parse(self, source, parser=None):
107 if not parser: 108 parser = self.__default_parser_class() 109 ET.ElementTree.parse(self, source, parser)
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
122 - def Comment(self, text=None):
123 element = self.Element(Comment) 124 element.text = text 125 return element
126
127 - def ProcessingInstruction(self, target, text=None):
128 element = self.Element(self.ProcessingInstruction) 129 element.text = target 130 if text: 131 element.text = element.text + " " + text 132 return element
133 134 PI = ProcessingInstruction 135
136 - def parse(self, source, parser=None):
137 tree = self.ElementTree() 138 tree.parse(source, parser) 139 return tree
140
141 - def XML(self, text):
142 parser = self.XMLTreeBuilder() 143 parser.feed(text) 144 return parser.close()
145
146 - def XMLID(self, text):
147 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, ids
156 157 fromstring = XML
158