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