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