Updated for 2.1a3
[python/dscho.git] / Lib / xml / dom / domreg.py
blob14b87d6a99821e94d205d9a30c0a9274a467dd7f
1 """Registration facilities for DOM. This module should not be used
2 directly. Instead, the functions getDOMImplementation and
3 registerDOMImplementation should be imported from xml.dom."""
5 # This is a list of well-known implementations. Well-known names
6 # should be published by posting to xml-sig@python.org, and are
7 # subsequently recorded in this file.
9 well_known_implementations = {
10 'minidom':'xml.dom.minidom',
11 '4DOM': 'xml.dom.DOMImplementation',
14 # DOM implementations not officially registered should register
15 # themselves with their
17 registered = {}
19 def registerDOMImplementation(name, factory):
20 """registerDOMImplementation(name, factory)
22 Register the factory function with the name. The factory function
23 should return an object which implements the DOMImplementation
24 interface. The factory function can either return the same object,
25 or a new one (e.g. if that implementation supports some
26 customization)."""
28 registered[name] = factory
30 def _good_enough(dom, features):
31 "_good_enough(dom, features) -> Return 1 if the dom offers the features"
32 for f,v in features:
33 if not dom.hasFeature(f,v):
34 return 0
35 return 1
37 def getDOMImplementation(name = None, features = ()):
38 """getDOMImplementation(name = None, features = ()) -> DOM implementation.
40 Return a suitable DOM implementation. The name is either
41 well-known, the module name of a DOM implementation, or None. If
42 it is not None, imports the corresponding module and returns
43 DOMImplementation object if the import succeeds.
45 If name is not given, consider the available implementations to
46 find one with the required feature set. If no implementation can
47 be found, raise an ImportError. The features list must be a sequence
48 of (feature, version) pairs which are passed to hasFeature."""
50 import os
51 creator = None
52 mod = well_known_implementations.get(name)
53 if mod:
54 mod = __import__(mod, {}, {}, ['getDOMImplementation'])
55 return mod.getDOMImplementation()
56 elif name:
57 return registered[name]()
58 elif os.environ.has_key("PYTHON_DOM"):
59 return getDOMImplementation(name = os.environ["PYTHON_DOM"])
61 # User did not specify a name, try implementations in arbitrary
62 # order, returning the one that has the required features
63 for creator in registered.values():
64 dom = creator()
65 if _good_enough(dom, features):
66 return dom
68 for creator in well_known_implementations.keys():
69 try:
70 dom = getDOMImplementation(name = creator)
71 except StandardError: # typically ImportError, or AttributeError
72 continue
73 if _good_enough(dom, features):
74 return dom
76 raise ImportError,"no suitable DOM implementation found"