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