--- /dev/null
+"""Python version compatibility support for minidom."""\r
+\r
+# This module should only be imported using "import *".\r
+#\r
+# The following names are defined:\r
+#\r
+# NodeList -- lightest possible NodeList implementation\r
+#\r
+# EmptyNodeList -- lightest possible NodeList that is guaranteed to\r
+# remain empty (immutable)\r
+#\r
+# StringTypes -- tuple of defined string types\r
+#\r
+# defproperty -- function used in conjunction with GetattrMagic;\r
+# using these together is needed to make them work\r
+# as efficiently as possible in both Python 2.2+\r
+# and older versions. For example:\r
+#\r
+# class MyClass(GetattrMagic):\r
+# def _get_myattr(self):\r
+# return something\r
+#\r
+# defproperty(MyClass, "myattr",\r
+# "return some value")\r
+#\r
+# For Python 2.2 and newer, this will construct a\r
+# property object on the class, which avoids\r
+# needing to override __getattr__(). It will only\r
+# work for read-only attributes.\r
+#\r
+# For older versions of Python, inheriting from\r
+# GetattrMagic will use the traditional\r
+# __getattr__() hackery to achieve the same effect,\r
+# but less efficiently.\r
+#\r
+# defproperty() should be used for each version of\r
+# the relevant _get_<property>() function.\r
+\r
+__all__ = ["NodeList", "EmptyNodeList", "StringTypes", "defproperty"]\r
+\r
+import xml.dom\r
+\r
+try:\r
+ unicode\r
+except NameError:\r
+ StringTypes = type(''),\r
+else:\r
+ StringTypes = type(''), type(unicode(''))\r
+\r
+\r
+class NodeList(list):\r
+ __slots__ = ()\r
+\r
+ def item(self, index):\r
+ if 0 <= index < len(self):\r
+ return self[index]\r
+\r
+ def _get_length(self):\r
+ return len(self)\r
+\r
+ def _set_length(self, value):\r
+ raise xml.dom.NoModificationAllowedErr(\r
+ "attempt to modify read-only attribute 'length'")\r
+\r
+ length = property(_get_length, _set_length,\r
+ doc="The number of nodes in the NodeList.")\r
+\r
+ def __getstate__(self):\r
+ return list(self)\r
+\r
+ def __setstate__(self, state):\r
+ self[:] = state\r
+\r
+\r
+class EmptyNodeList(tuple):\r
+ __slots__ = ()\r
+\r
+ def __add__(self, other):\r
+ NL = NodeList()\r
+ NL.extend(other)\r
+ return NL\r
+\r
+ def __radd__(self, other):\r
+ NL = NodeList()\r
+ NL.extend(other)\r
+ return NL\r
+\r
+ def item(self, index):\r
+ return None\r
+\r
+ def _get_length(self):\r
+ return 0\r
+\r
+ def _set_length(self, value):\r
+ raise xml.dom.NoModificationAllowedErr(\r
+ "attempt to modify read-only attribute 'length'")\r
+\r
+ length = property(_get_length, _set_length,\r
+ doc="The number of nodes in the NodeList.")\r
+\r
+\r
+def defproperty(klass, name, doc):\r
+ get = getattr(klass, ("_get_" + name)).im_func\r
+ def set(self, value, name=name):\r
+ raise xml.dom.NoModificationAllowedErr(\r
+ "attempt to modify read-only attribute " + repr(name))\r
+ assert not hasattr(klass, "_set_" + name), \\r
+ "expected not to find _set_" + name\r
+ prop = property(get, set, doc=doc)\r
+ setattr(klass, name, prop)\r