annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/xml/dom/minicompat.py @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 """Python version compatibility support for minidom.
jpayne@68 2
jpayne@68 3 This module contains internal implementation details and
jpayne@68 4 should not be imported; use xml.dom.minidom instead.
jpayne@68 5 """
jpayne@68 6
jpayne@68 7 # This module should only be imported using "import *".
jpayne@68 8 #
jpayne@68 9 # The following names are defined:
jpayne@68 10 #
jpayne@68 11 # NodeList -- lightest possible NodeList implementation
jpayne@68 12 #
jpayne@68 13 # EmptyNodeList -- lightest possible NodeList that is guaranteed to
jpayne@68 14 # remain empty (immutable)
jpayne@68 15 #
jpayne@68 16 # StringTypes -- tuple of defined string types
jpayne@68 17 #
jpayne@68 18 # defproperty -- function used in conjunction with GetattrMagic;
jpayne@68 19 # using these together is needed to make them work
jpayne@68 20 # as efficiently as possible in both Python 2.2+
jpayne@68 21 # and older versions. For example:
jpayne@68 22 #
jpayne@68 23 # class MyClass(GetattrMagic):
jpayne@68 24 # def _get_myattr(self):
jpayne@68 25 # return something
jpayne@68 26 #
jpayne@68 27 # defproperty(MyClass, "myattr",
jpayne@68 28 # "return some value")
jpayne@68 29 #
jpayne@68 30 # For Python 2.2 and newer, this will construct a
jpayne@68 31 # property object on the class, which avoids
jpayne@68 32 # needing to override __getattr__(). It will only
jpayne@68 33 # work for read-only attributes.
jpayne@68 34 #
jpayne@68 35 # For older versions of Python, inheriting from
jpayne@68 36 # GetattrMagic will use the traditional
jpayne@68 37 # __getattr__() hackery to achieve the same effect,
jpayne@68 38 # but less efficiently.
jpayne@68 39 #
jpayne@68 40 # defproperty() should be used for each version of
jpayne@68 41 # the relevant _get_<property>() function.
jpayne@68 42
jpayne@68 43 __all__ = ["NodeList", "EmptyNodeList", "StringTypes", "defproperty"]
jpayne@68 44
jpayne@68 45 import xml.dom
jpayne@68 46
jpayne@68 47 StringTypes = (str,)
jpayne@68 48
jpayne@68 49
jpayne@68 50 class NodeList(list):
jpayne@68 51 __slots__ = ()
jpayne@68 52
jpayne@68 53 def item(self, index):
jpayne@68 54 if 0 <= index < len(self):
jpayne@68 55 return self[index]
jpayne@68 56
jpayne@68 57 def _get_length(self):
jpayne@68 58 return len(self)
jpayne@68 59
jpayne@68 60 def _set_length(self, value):
jpayne@68 61 raise xml.dom.NoModificationAllowedErr(
jpayne@68 62 "attempt to modify read-only attribute 'length'")
jpayne@68 63
jpayne@68 64 length = property(_get_length, _set_length,
jpayne@68 65 doc="The number of nodes in the NodeList.")
jpayne@68 66
jpayne@68 67 # For backward compatibility
jpayne@68 68 def __setstate__(self, state):
jpayne@68 69 if state is None:
jpayne@68 70 state = []
jpayne@68 71 self[:] = state
jpayne@68 72
jpayne@68 73
jpayne@68 74 class EmptyNodeList(tuple):
jpayne@68 75 __slots__ = ()
jpayne@68 76
jpayne@68 77 def __add__(self, other):
jpayne@68 78 NL = NodeList()
jpayne@68 79 NL.extend(other)
jpayne@68 80 return NL
jpayne@68 81
jpayne@68 82 def __radd__(self, other):
jpayne@68 83 NL = NodeList()
jpayne@68 84 NL.extend(other)
jpayne@68 85 return NL
jpayne@68 86
jpayne@68 87 def item(self, index):
jpayne@68 88 return None
jpayne@68 89
jpayne@68 90 def _get_length(self):
jpayne@68 91 return 0
jpayne@68 92
jpayne@68 93 def _set_length(self, value):
jpayne@68 94 raise xml.dom.NoModificationAllowedErr(
jpayne@68 95 "attempt to modify read-only attribute 'length'")
jpayne@68 96
jpayne@68 97 length = property(_get_length, _set_length,
jpayne@68 98 doc="The number of nodes in the NodeList.")
jpayne@68 99
jpayne@68 100
jpayne@68 101 def defproperty(klass, name, doc):
jpayne@68 102 get = getattr(klass, ("_get_" + name))
jpayne@68 103 def set(self, value, name=name):
jpayne@68 104 raise xml.dom.NoModificationAllowedErr(
jpayne@68 105 "attempt to modify read-only attribute " + repr(name))
jpayne@68 106 assert not hasattr(klass, "_set_" + name), \
jpayne@68 107 "expected not to find _set_" + name
jpayne@68 108 prop = property(get, set, doc=doc)
jpayne@68 109 setattr(klass, name, prop)