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