domreg.py 3.4 KB

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