Make element.Element implement the xml.etree.ElementTree._Element_Py by itself
authorNick Downing <nick@ndcode.org>
Mon, 28 Jan 2019 11:52:32 +0000 (22:52 +1100)
committerNick Downing <nick@ndcode.org>
Mon, 28 Jan 2019 11:52:32 +0000 (22:52 +1100)
skel/element.py

index d622c41..7b92345 100644 (file)
 
 import xml.etree.ElementTree
 
-class Element(xml.etree.ElementTree._Element_Py):
+class Element:
+  #def __init__(self, tag, attrib={}, **extra):
+  #  if not isinstance(attrib, dict):
+  #    raise TypeError("attrib must be dict, not %s" % (
+  #      attrib.__class__.__name__,))
+  #  attrib = attrib.copy()
+  #  attrib.update(extra)
+  #  self.tag = tag
+  #  self.attrib = attrib
+  #  self.children = []
+
+  #def __repr__(self):
+  #  return "<%s %r at %#x>" % (self.__class__.__name__, self.tag, id(self))
+
+  #def makeelement(self, tag, attrib):
+  #  return self.__class__(tag, attrib)
+
+  #def copy(self):
+  #  elem = self.makeelement(self.tag, self.attrib)
+  #  elem.text = self.text
+  #  elem.tail = self.tail
+  #  elem[:] = self
+  #  return elem
+
+  def __len__(self):
+    return len(self.children)
+
+  #def __bool__(self):
+  #  warnings.warn(
+  #    "The behavior of this method will change in future versions.  "
+  #    "Use specific 'len(elem)' or 'elem is not None' test instead.",
+  #    FutureWarning, stacklevel=2
+  #    )
+  #  return len(self.children) != 0 # emulate old behaviour, for now
+
+  def __getitem__(self, index):
+    return self.children[index]
+
+  def __setitem__(self, index, element):
+    # if isinstance(index, slice):
+    #   for elt in element:
+    #     assert iselement(elt)
+    # else:
+    #   assert iselement(element)
+    self.children[index] = element
+
+  def __delitem__(self, index):
+    del self.children[index]
+
+  def append(self, subelement):
+    #self._assert_is_element(subelement)
+    self.children.append(subelement)
+
+  def extend(self, elements):
+    #for element in elements:
+    #  self._assert_is_element(element)
+    self.children.extend(elements)
+
+  def insert(self, index, subelement):
+    #self._assert_is_element(subelement)
+    self.children.insert(index, subelement)
+
+  #def _assert_is_element(self, e):
+  #  # Need to refer to the actual Python implementation, not the
+  #  # shadowing C implementation.
+  #  if not isinstance(e, _Element_Py):
+  #    raise TypeError('expected an Element, not %s' % type(e).__name__)
+
+  #def remove(self, subelement):
+  #  # assert iselement(element)
+  #  self.children.remove(subelement)
+
+  #def getchildren(self):
+  #  warnings.warn(
+  #    "This method will be removed in future versions.  "
+  #    "Use 'list(elem)' or iteration over elem instead.",
+  #    DeprecationWarning, stacklevel=2
+  #    )
+  #  return self.children
+
+  #def find(self, path, namespaces=None):
+  #  return ElementPath.find(self, path, namespaces)
+
+  #def findtext(self, path, default=None, namespaces=None):
+  #  return ElementPath.findtext(self, path, default, namespaces)
+
+  #def findall(self, path, namespaces=None):
+  #  return ElementPath.findall(self, path, namespaces)
+
+  #def iterfind(self, path, namespaces=None):
+  #  return ElementPath.iterfind(self, path, namespaces)
+
+  #def clear(self):
+  #  self.attrib.clear()
+  #  self.children = []
+  #  self.text = self.tail = None
+
+  def get(self, key, default=None):
+    return self.attrib.get(key, default)
+
+  def set(self, key, value):
+    self.attrib[key] = value
+
+  #def keys(self):
+  #  return self.attrib.keys()
+
+  def items(self):
+    return self.attrib.items()
+
+  def iter(self, tag=None):
+    if tag == "*":
+      tag = None
+    if tag is None or self.tag == tag:
+      yield self
+    for e in self.children:
+      yield from e.iter(tag)
+
+  ## compatibility
+  #def getiterator(self, tag=None):
+  #  # Change for a DeprecationWarning in 1.4
+  #  warnings.warn(
+  #    "This method will be removed in future versions.  "
+  #    "Use 'elem.iter()' or 'list(elem.iter())' instead.",
+  #    PendingDeprecationWarning, stacklevel=2
+  #  )
+  #  return list(self.iter(tag))
+
+  #def itertext(self):
+  #  tag = self.tag
+  #  if not isinstance(tag, str) and tag is not None:
+  #    return
+  #  t = self.text
+  #  if t:
+  #    yield t
+  #  for e in self:
+  #    yield from e.itertext()
+  #    t = e.tail
+  #    if t:
+  #      yield t
+
   def __init__(self, tag = 'Element', attrib = {}, text = '', children = []):
-    xml.etree.ElementTree._Element_Py.__init__(self, tag, attrib)
+    self.tag = tag
+    self.attrib = attrib.copy()
+    self.tail = None
     self.ref = -1
     self.seen = False
     set_text(self, 0, text)
-    self[:] = children
+    self.children = children.copy()
   def serialize(self, ref_list):
     for i in self:
       # parented, enforce that child can only be parented at most once