python version compatibility support for minidom this module contains internal implementation details and should not be imported use xml dom minidom instead this module should only be imported using import the following names are defined nodelist lightest possible nodelist implementation emptynodelist lightest possible nodelist that is guaranteed to remain empty immutable stringtypes tuple of defined string types defproperty function used in conjunction with getattrmagic using these together is needed to make them work as efficiently as possible in both python 2 2 and older versions for example class myclassgetattrmagic def getmyattrself return something defpropertymyclass myattr return some value for python 2 2 and newer this will construct a property object on the class which avoids needing to override getattr it will only work for readonly attributes for older versions of python inheriting from getattrmagic will use the traditional getattr hackery to achieve the same effect but less efficiently defproperty should be used for each version of the relevant getproperty function for backward compatibility this module should only be imported using import the following names are defined nodelist lightest possible nodelist implementation emptynodelist lightest possible nodelist that is guaranteed to remain empty immutable stringtypes tuple of defined string types defproperty function used in conjunction with getattrmagic using these together is needed to make them work as efficiently as possible in both python 2 2 and older versions for example class myclass getattrmagic def _get_myattr self return something defproperty myclass myattr return some value for python 2 2 and newer this will construct a property object on the class which avoids needing to override __getattr__ it will only work for read only attributes for older versions of python inheriting from getattrmagic will use the traditional __getattr__ hackery to achieve the same effect but less efficiently defproperty should be used for each version of the relevant _get_ property function for backward compatibility
__all__ = ["NodeList", "EmptyNodeList", "StringTypes", "defproperty"]

import xml.dom

StringTypes = (str,)


class NodeList(list):
    __slots__ = ()

    def item(self, index):
        if 0 <= index < len(self):
            return self[index]

    def _get_length(self):
        return len(self)

    def _set_length(self, value):
        raise xml.dom.NoModificationAllowedErr(
            "attempt to modify read-only attribute 'length'")

    length = property(_get_length, _set_length,
                      doc="The number of nodes in the NodeList.")

    
    def __setstate__(self, state):
        if state is None:
            state = []
        self[:] = state


class EmptyNodeList(tuple):
    __slots__ = ()

    def __add__(self, other):
        NL = NodeList()
        NL.extend(other)
        return NL

    def __radd__(self, other):
        NL = NodeList()
        NL.extend(other)
        return NL

    def item(self, index):
        return None

    def _get_length(self):
        return 0

    def _set_length(self, value):
        raise xml.dom.NoModificationAllowedErr(
            "attempt to modify read-only attribute 'length'")

    length = property(_get_length, _set_length,
                      doc="The number of nodes in the NodeList.")


def defproperty(klass, name, doc):
    get = getattr(klass, ("_get_" + name))
    def set(self, value, name=name):
        raise xml.dom.NoModificationAllowedErr(
            "attempt to modify read-only attribute " + repr(name))
    assert not hasattr(klass, "_set_" + name), \
           "expected not to find _set_" + name
    prop = property(get, set, doc=doc)
    setattr(klass, name, prop)