registration facilities for dom this module should not be used directly instead the functions getdomimplementation and registerdomimplementation should be imported from xml dom this is a list of wellknown implementations wellknown names should be published by posting to xmlsigpython org and are subsequently recorded in this file import sys wellknownimplementations minidom xml dom minidom 4dom xml dom domimplementation dom implementations not officially registered should register themselves with their registered def registerdomimplementationname factory getdomimplementationname none features dom implementation return a suitable dom implementation the name is either wellknown the module name of a dom implementation or none if it is not none imports the corresponding module and returns domimplementation object if the import succeeds if name is not given consider the available implementations to find one with the required feature set if no implementation can be found raise an importerror the features list must be a sequence of feature version pairs which are passed to hasfeature import os creator none mod wellknownimplementations getname if mod mod importmod getdomimplementation return mod getdomimplementation elif name return registeredname elif not sys flags ignoreenvironment and pythondom in os environ return getdomimplementationname os environpythondom user did not specify a name try implementations in arbitrary order returning the one that has the required features if isinstancefeatures str features parsefeaturestringfeatures for creator in registered values dom creator if goodenoughdom features return dom for creator in wellknownimplementations keys try dom getdomimplementationname creator except exception typically importerror or attributeerror continue if goodenoughdom features return dom raise importerrorno suitable dom implementation found def parsefeaturestrings features parts s split i 0 length lenparts while i length feature partsi if feature0 in 0123456789 raise valueerrorbad feature name r feature i i 1 version none if i length v partsi if v0 in 0123456789 i i 1 version v features appendfeature version return tuplefeatures this is a list of well known implementations well known names should be published by posting to xml sig python org and are subsequently recorded in this file dom implementations not officially registered should register themselves with their registerdomimplementation name factory register the factory function with the name the factory function should return an object which implements the domimplementation interface the factory function can either return the same object or a new one e g if that implementation supports some customization getdomimplementation name none features dom implementation return a suitable dom implementation the name is either well known the module name of a dom implementation or none if it is not none imports the corresponding module and returns domimplementation object if the import succeeds if name is not given consider the available implementations to find one with the required feature set if no implementation can be found raise an importerror the features list must be a sequence of feature version pairs which are passed to hasfeature user did not specify a name try implementations in arbitrary order returning the one that has the required features typically importerror or attributeerror
import sys

well_known_implementations = {
    'minidom':'xml.dom.minidom',
    '4DOM': 'xml.dom.DOMImplementation',
    }




registered = {}

def registerDOMImplementation(name, factory):
    

    registered[name] = factory

def _good_enough(dom, features):
    "_good_enough(dom, features) -> Return 1 if the dom offers the features"
    for f,v in features:
        if not dom.hasFeature(f,v):
            return 0
    return 1

def getDOMImplementation(name=None, features=()):
    

    import os
    creator = None
    mod = well_known_implementations.get(name)
    if mod:
        mod = __import__(mod, {}, {}, ['getDOMImplementation'])
        return mod.getDOMImplementation()
    elif name:
        return registered[name]()
    elif not sys.flags.ignore_environment and "PYTHON_DOM" in os.environ:
        return getDOMImplementation(name = os.environ["PYTHON_DOM"])

    
    
    if isinstance(features, str):
        features = _parse_feature_string(features)
    for creator in registered.values():
        dom = creator()
        if _good_enough(dom, features):
            return dom

    for creator in well_known_implementations.keys():
        try:
            dom = getDOMImplementation(name = creator)
        except Exception: 
            continue
        if _good_enough(dom, features):
            return dom

    raise ImportError("no suitable DOM implementation found")

def _parse_feature_string(s):
    features = []
    parts = s.split()
    i = 0
    length = len(parts)
    while i < length:
        feature = parts[i]
        if feature[0] in "0123456789":
            raise ValueError("bad feature name: %r" % (feature,))
        i = i + 1
        version = None
        if i < length:
            v = parts[i]
            if v[0] in "0123456789":
                i = i + 1
                version = v
        features.append((feature, version))
    return tuple(features)