Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 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 xml-sig@python.org, 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).""" |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 27 | |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 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 | |
Georg Brandl | fe99105 | 2009-09-16 15:54:04 +0000 | [diff] [blame] | 37 | def getDOMImplementation(name=None, features=()): |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 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.""" |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 49 | |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 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]() |
Guido van Rossum | 1b01e5c | 2006-08-19 02:45:06 +0000 | [diff] [blame] | 58 | elif "PYTHON_DOM" in os.environ: |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 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 |
Christian Heimes | c9543e4 | 2007-11-28 08:28:28 +0000 | [diff] [blame] | 63 | if isinstance(features, str): |
Martin v. Löwis | f8de21c | 2003-01-26 09:04:35 +0000 | [diff] [blame] | 64 | features = _parse_feature_string(features) |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 65 | for creator in registered.values(): |
| 66 | dom = creator() |
| 67 | if _good_enough(dom, features): |
| 68 | return dom |
| 69 | |
| 70 | for creator in well_known_implementations.keys(): |
| 71 | try: |
| 72 | dom = getDOMImplementation(name = creator) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 73 | except Exception: # typically ImportError, or AttributeError |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 74 | continue |
| 75 | if _good_enough(dom, features): |
| 76 | return dom |
| 77 | |
Collin Winter | 70e7980 | 2007-08-24 18:57:22 +0000 | [diff] [blame] | 78 | raise ImportError("no suitable DOM implementation found") |
Martin v. Löwis | f8de21c | 2003-01-26 09:04:35 +0000 | [diff] [blame] | 79 | |
| 80 | def _parse_feature_string(s): |
| 81 | features = [] |
| 82 | parts = s.split() |
| 83 | i = 0 |
| 84 | length = len(parts) |
| 85 | while i < length: |
| 86 | feature = parts[i] |
| 87 | if feature[0] in "0123456789": |
Collin Winter | 70e7980 | 2007-08-24 18:57:22 +0000 | [diff] [blame] | 88 | raise ValueError("bad feature name: %r" % (feature,)) |
Martin v. Löwis | f8de21c | 2003-01-26 09:04:35 +0000 | [diff] [blame] | 89 | i = i + 1 |
| 90 | version = None |
| 91 | if i < length: |
| 92 | v = parts[i] |
| 93 | if v[0] in "0123456789": |
| 94 | i = i + 1 |
| 95 | version = v |
| 96 | features.append((feature, version)) |
| 97 | return tuple(features) |