Andrew Kuchling | 98a0d06 | 2013-11-12 10:25:15 -0500 | [diff] [blame] | 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 | """ |
Martin v. Löwis | fc5fec7 | 2003-01-25 15:11:07 +0000 | [diff] [blame] | 6 | |
| 7 | # This module should only be imported using "import *". |
| 8 | # |
| 9 | # The following names are defined: |
| 10 | # |
Martin v. Löwis | fc5fec7 | 2003-01-25 15:11:07 +0000 | [diff] [blame] | 11 | # NodeList -- lightest possible NodeList implementation |
| 12 | # |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 13 | # EmptyNodeList -- lightest possible NodeList that is guaranteed to |
Martin v. Löwis | fc5fec7 | 2003-01-25 15:11:07 +0000 | [diff] [blame] | 14 | # remain empty (immutable) |
| 15 | # |
| 16 | # StringTypes -- tuple of defined string types |
| 17 | # |
Martin v. Löwis | fc5fec7 | 2003-01-25 15:11:07 +0000 | [diff] [blame] | 18 | # defproperty -- function used in conjunction with GetattrMagic; |
| 19 | # using these together is needed to make them work |
| 20 | # as efficiently as possible in both Python 2.2+ |
| 21 | # and older versions. For example: |
| 22 | # |
| 23 | # class MyClass(GetattrMagic): |
| 24 | # def _get_myattr(self): |
| 25 | # return something |
| 26 | # |
| 27 | # defproperty(MyClass, "myattr", |
| 28 | # "return some value") |
| 29 | # |
| 30 | # For Python 2.2 and newer, this will construct a |
| 31 | # property object on the class, which avoids |
| 32 | # needing to override __getattr__(). It will only |
| 33 | # work for read-only attributes. |
| 34 | # |
| 35 | # For older versions of Python, inheriting from |
| 36 | # GetattrMagic will use the traditional |
| 37 | # __getattr__() hackery to achieve the same effect, |
| 38 | # but less efficiently. |
| 39 | # |
| 40 | # defproperty() should be used for each version of |
| 41 | # the relevant _get_<property>() function. |
Martin v. Löwis | fc5fec7 | 2003-01-25 15:11:07 +0000 | [diff] [blame] | 42 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 43 | __all__ = ["NodeList", "EmptyNodeList", "StringTypes", "defproperty"] |
Martin v. Löwis | fc5fec7 | 2003-01-25 15:11:07 +0000 | [diff] [blame] | 44 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 45 | import xml.dom |
Martin v. Löwis | fc5fec7 | 2003-01-25 15:11:07 +0000 | [diff] [blame] | 46 | |
Walter Dörwald | 5de48bd | 2007-06-11 21:38:39 +0000 | [diff] [blame] | 47 | StringTypes = (str,) |
Martin v. Löwis | fc5fec7 | 2003-01-25 15:11:07 +0000 | [diff] [blame] | 48 | |
| 49 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | def __getstate__(self): |
| 68 | return list(self) |
| 69 | |
| 70 | def __setstate__(self, state): |
| 71 | self[:] = state |
| 72 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 73 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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.") |
Martin v. Löwis | fc5fec7 | 2003-01-25 15:11:07 +0000 | [diff] [blame] | 99 | |
| 100 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 101 | def defproperty(klass, name, doc): |
Christian Heimes | 4a22b5d | 2007-11-25 09:39:14 +0000 | [diff] [blame] | 102 | get = getattr(klass, ("_get_" + name)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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) |