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