domreg.py 3.3 KB

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