| 1 |
"""Python version compatibility support for minidom. |
| 2 |
|
| 3 |
This module contains internal implementation details and |
| 4 |
should not be imported; use xml.dom.minidom instead. |
| 5 |
""" |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
__all__ = ["NodeList", "EmptyNodeList", "StringTypes", "defproperty"] |
| 44 |
|
| 45 |
import xml.dom |
| 46 |
|
| 47 |
StringTypes = (str,) |
| 48 |
|
| 49 |
|
| 50 |
class NodeList(list): |
| 51 |
__slots__ = () |
| 52 |
|
| 53 |
def item(self, index): |
| 54 |
if 0 <= index < len(self): |
| 55 |
return self[index] |
| 56 |
|
| 57 |
def _get_length(self): |
| 58 |
return len(self) |
| 59 |
|
| 60 |
def _set_length(self, value): |
| 61 |
raise xml.dom.NoModificationAllowedErr( |
| 62 |
"attempt to modify read-only attribute 'length'") |
| 63 |
|
| 64 |
length = property(_get_length, _set_length, |
| 65 |
doc="The number of nodes in the NodeList.") |
| 66 |
|
| 67 |
|
| 68 |
def __setstate__(self, state): |
| 69 |
if state is None: |
| 70 |
state = [] |
| 71 |
self[:] = state |
| 72 |
|
| 73 |
|
| 74 |
class EmptyNodeList(tuple): |
| 75 |
__slots__ = () |
| 76 |
|
| 77 |
def __add__(self, other): |
| 78 |
NL = NodeList() |
| 79 |
NL.extend(other) |
| 80 |
return NL |
| 81 |
|
| 82 |
def __radd__(self, other): |
| 83 |
NL = NodeList() |
| 84 |
NL.extend(other) |
| 85 |
return NL |
| 86 |
|
| 87 |
def item(self, index): |
| 88 |
return None |
| 89 |
|
| 90 |
def _get_length(self): |
| 91 |
return 0 |
| 92 |
|
| 93 |
def _set_length(self, value): |
| 94 |
raise xml.dom.NoModificationAllowedErr( |
| 95 |
"attempt to modify read-only attribute 'length'") |
| 96 |
|
| 97 |
length = property(_get_length, _set_length, |
| 98 |
doc="The number of nodes in the NodeList.") |
| 99 |
|
| 100 |
|
| 101 |
def defproperty(klass, name, doc): |
| 102 |
get = getattr(klass, ("_get_" + name)) |
| 103 |
def set(self, value, name=name): |
| 104 |
raise xml.dom.NoModificationAllowedErr( |
| 105 |
"attempt to modify read-only attribute " + repr(name)) |
| 106 |
assert not hasattr(klass, "_set_" + name), \ |
| 107 |
"expected not to find _set_" + name |
| 108 |
prop = property(get, set, doc=doc) |
| 109 |
setattr(klass, name, prop) |