PyXR

c:\python24\lib \ xml \ dom \ domreg.py



0001 """Registration facilities for DOM. This module should not be used
0002 directly. Instead, the functions getDOMImplementation and
0003 registerDOMImplementation should be imported from xml.dom."""
0004 
0005 from xml.dom.minicompat import *  # isinstance, StringTypes
0006 
0007 # This is a list of well-known implementations.  Well-known names
0008 # should be published by posting to xml-sig@python.org, and are
0009 # subsequently recorded in this file.
0010 
0011 well_known_implementations = {
0012     'minidom':'xml.dom.minidom',
0013     '4DOM': 'xml.dom.DOMImplementation',
0014     }
0015 
0016 # DOM implementations not officially registered should register
0017 # themselves with their
0018 
0019 registered = {}
0020 
0021 def registerDOMImplementation(name, factory):
0022     """registerDOMImplementation(name, factory)
0023 
0024     Register the factory function with the name. The factory function
0025     should return an object which implements the DOMImplementation
0026     interface. The factory function can either return the same object,
0027     or a new one (e.g. if that implementation supports some
0028     customization)."""
0029 
0030     registered[name] = factory
0031 
0032 def _good_enough(dom, features):
0033     "_good_enough(dom, features) -> Return 1 if the dom offers the features"
0034     for f,v in features:
0035         if not dom.hasFeature(f,v):
0036             return 0
0037     return 1
0038 
0039 def getDOMImplementation(name = None, features = ()):
0040     """getDOMImplementation(name = None, features = ()) -> DOM implementation.
0041 
0042     Return a suitable DOM implementation. The name is either
0043     well-known, the module name of a DOM implementation, or None. If
0044     it is not None, imports the corresponding module and returns
0045     DOMImplementation object if the import succeeds.
0046 
0047     If name is not given, consider the available implementations to
0048     find one with the required feature set. If no implementation can
0049     be found, raise an ImportError. The features list must be a sequence
0050     of (feature, version) pairs which are passed to hasFeature."""
0051 
0052     import os
0053     creator = None
0054     mod = well_known_implementations.get(name)
0055     if mod:
0056         mod = __import__(mod, {}, {}, ['getDOMImplementation'])
0057         return mod.getDOMImplementation()
0058     elif name:
0059         return registered[name]()
0060     elif os.environ.has_key("PYTHON_DOM"):
0061         return getDOMImplementation(name = os.environ["PYTHON_DOM"])
0062 
0063     # User did not specify a name, try implementations in arbitrary
0064     # order, returning the one that has the required features
0065     if isinstance(features, StringTypes):
0066         features = _parse_feature_string(features)
0067     for creator in registered.values():
0068         dom = creator()
0069         if _good_enough(dom, features):
0070             return dom
0071 
0072     for creator in well_known_implementations.keys():
0073         try:
0074             dom = getDOMImplementation(name = creator)
0075         except StandardError: # typically ImportError, or AttributeError
0076             continue
0077         if _good_enough(dom, features):
0078             return dom
0079 
0080     raise ImportError,"no suitable DOM implementation found"
0081 
0082 def _parse_feature_string(s):
0083     features = []
0084     parts = s.split()
0085     i = 0
0086     length = len(parts)
0087     while i < length:
0088         feature = parts[i]
0089         if feature[0] in "0123456789":
0090             raise ValueError, "bad feature name: %r" % (feature,)
0091         i = i + 1
0092         version = None
0093         if i < length:
0094             v = parts[i]
0095             if v[0] in "0123456789":
0096                 i = i + 1
0097                 version = v
0098         features.append((feature, version))
0099     return tuple(features)
0100 

Generated by PyXR 0.9.4
SourceForge.net Logo