Debian Codegraph @

cpython27/Lib/xml/dom/domreg.py

raw symbols SCIP
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."""
4
5 from xml.dom.minicompat import * # isinstance, StringTypes
6
7 # This is a list of well-known implementations. Well-known names
8 # should be published by posting to [email protected], and are
9 # subsequently recorded in this file.
10
11 import sys
12
13 well_known_implementations = {
14 'minidom':'xml.dom.minidom',
15 '4DOM': 'xml.dom.DOMImplementation',
16 }
17
18 # DOM implementations not officially registered should register
19 # themselves with their
20
21 registered = {}
22
23 def registerDOMImplementation(name, factory):
24 """registerDOMImplementation(name, factory)
25
26 Register the factory function with the name. The factory function
27 should return an object which implements the DOMImplementation
28 interface. The factory function can either return the same object,
29 or a new one (e.g. if that implementation supports some
30 customization)."""
31
32 registered[name] = factory
33
34 def _good_enough(dom, features):
35 "_good_enough(dom, features) -> Return 1 if the dom offers the features"
36 for f,v in features:
37 if not dom.hasFeature(f,v):
38 return 0
39 return 1
40
41 def getDOMImplementation(name = None, features = ()):
42 """getDOMImplementation(name = None, features = ()) -> DOM implementation.
43
44 Return a suitable DOM implementation. The name is either
45 well-known, the module name of a DOM implementation, or None. If
46 it is not None, imports the corresponding module and returns
47 DOMImplementation object if the import succeeds.
48
49 If name is not given, consider the available implementations to
50 find one with the required feature set. If no implementation can
51 be found, raise an ImportError. The features list must be a sequence
52 of (feature, version) pairs which are passed to hasFeature."""
53
54 import os
55 creator = None
56 mod = well_known_implementations.get(name)
57 if mod:
58 mod = __import__(mod, {}, {}, ['getDOMImplementation'])
59 return mod.getDOMImplementation()
60 elif name:
61 return registered[name]()
62 elif not sys.flags.ignore_environment and "PYTHON_DOM" in os.environ:
63 return getDOMImplementation(name = os.environ["PYTHON_DOM"])
64
65 # User did not specify a name, try implementations in arbitrary
66 # order, returning the one that has the required features
67 if isinstance(features, StringTypes):
68 features = _parse_feature_string(features)
69 for creator in registered.values():
70 dom = creator()
71 if _good_enough(dom, features):
72 return dom
73
74 for creator in well_known_implementations.keys():
75 try:
76 dom = getDOMImplementation(name = creator)
77 except StandardError: # typically ImportError, or AttributeError
78 continue
79 if _good_enough(dom, features):
80 return dom
81
82 raise ImportError,"no suitable DOM implementation found"
83
84 def _parse_feature_string(s):
85 features = []
86 parts = s.split()
87 i = 0
88 length = len(parts)
89 while i < length:
90 feature = parts[i]
91 if feature[0] in "0123456789":
92 raise ValueError, "bad feature name: %r" % (feature,)
93 i = i + 1
94 version = None
95 if i < length:
96 v = parts[i]
97 if v[0] in "0123456789":
98 i = i + 1
99 version = v
100 features.append((feature, version))
101 return tuple(features)