Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7edbd4f

Browse files
committed
Patch #103885: Add dynamic registration and lookup of DOM implementations.
1 parent f5d3ea0 commit 7edbd4f

4 files changed

Lines changed: 111 additions & 0 deletions

File tree

Doc/lib/xmldom.tex

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,36 @@ \section{\module{xml.dom} ---
8585
{This specifies the mapping from OMG IDL to Python.}
8686
\end{seealso}
8787

88+
\subsection{Module Contents}
89+
90+
The \module{xml.dom} contains the following functions:
91+
92+
\begin{funcdesc}{registerDOMImplementation}{name, factory}
93+
Register the \var{factory} function with the \var{name}. The factory
94+
function should return an object which implements the
95+
\code{DOMImplementation| interface. The factory function can either return
96+
the same object, or a new one (e.g. if that implementation supports
97+
some customization).
98+
\end{funcdesc}
99+
100+
\begin{funcdesc}{getDOMImplementation}{name = None, features = ()}
101+
Return a suitable DOM implementation. The \var{name} is either
102+
well-known, the module name of a DOM implementation, or
103+
\code{None}. If it is not \code{None}, imports the corresponding module and
104+
returns a \class{DOMImplementation} object if the import succeeds. If
105+
no name is given, and if the environment variable \code{PYTHON_DOM} is
106+
set, this variable is used to find the implementation.
107+
108+
If name is not given, consider the available implementations to find
109+
one with the required feature set. If no implementation can be found,
110+
raise an \exception{ImportError}. The features list must be a sequence of
111+
(feature, version) pairs which are passed to hasFeature.
112+
\end{funcdesc}
113+
114+
% Should the Node documentation go here?
115+
116+
In addition, \module{xml.dom} contains the \class{Node}, and the DOM
117+
exceptions.
88118

89119
\subsection{Objects in the DOM \label{dom-objects}}
90120

Lib/xml/dom/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,5 @@ class NamespaceErr(DOMException):
115115

116116
class InvalidAccessErr(DOMException):
117117
code = INVALID_ACCESS_ERR
118+
119+
from domreg import getDOMImplementation,registerDOMImplementation

Lib/xml/dom/domreg.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
# This is a list of well-known implementations. Well-known names
6+
# should be published by posting to [email protected], and are
7+
# subsequently recorded in this file.
8+
9+
well_known_implementations = {
10+
'minidom':'xml.dom.minidom',
11+
'4DOM': 'xml.dom.DOMImplementation',
12+
}
13+
14+
# DOM implementations not officially registered should register
15+
# themselves with their
16+
17+
registered = {}
18+
19+
def registerDOMImplementation(name, factory):
20+
"""registerDOMImplementation(name, factory)
21+
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)."""
27+
28+
registered[name] = factory
29+
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
36+
37+
def getDOMImplementation(name = None, features = ()):
38+
"""getDOMImplementation(name = None, features = ()) -> DOM implementation.
39+
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.
44+
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."""
49+
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"])
60+
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
67+
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
75+
76+
raise ImportError,"no suitable DOM implementation found"

Lib/xml/dom/minidom.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,3 +782,6 @@ def parseString(*args, **kwargs):
782782
"""Parse a file into a DOM from a string."""
783783
from xml.dom import pulldom
784784
return _doparse(pulldom.parseString, args, kwargs)
785+
786+
def getDOMImplementation():
787+
return Document.implementation

0 commit comments

Comments
 (0)