ยปCore Development>Code coverage>Lib/xml/dom/domreg.py

Python code coverage for Lib/xml/dom/domreg.py

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