__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. _property = property
  2. import sys
  3. from odml import doc
  4. from odml import property
  5. from odml import section
  6. from odml import value
  7. from odml.dtypes import DType
  8. class odml_implementation(object):
  9. name = None
  10. provides = []
  11. Value = None
  12. Property = None
  13. Section = None
  14. Document = None
  15. class BasicImplementation(odml_implementation):
  16. name = 'basic'
  17. provides = ['basic']
  18. @_property
  19. def Value(self):
  20. return value.BaseValue
  21. @_property
  22. def Section(self):
  23. return section.BaseSection
  24. @_property
  25. def Property(self):
  26. return property.BaseProperty
  27. @_property
  28. def Document(self):
  29. return doc.BaseDocument
  30. # here the available implementations are stored
  31. impls = {}
  32. # the default implementation
  33. current_implementation = BasicImplementation()
  34. minimum_implementation = current_implementation
  35. def addImplementation(implementation, make_minimum=False, make_default=False, key=None):
  36. """register a new available implementation"""
  37. impls[implementation.name] = implementation
  38. if make_minimum and key is not None:
  39. setMinimumImplementation(key)
  40. if make_default and key is not None:
  41. setDefaultImplementation(key)
  42. def getImplementation(key=None):
  43. """retrieve a implementation named *key*"""
  44. if key is None:
  45. return current_implementation
  46. implementation = impls[key]
  47. return implementation
  48. def setDefaultImplementation(key):
  49. """
  50. set a new default implementation
  51. if it does not fulfill the minimum requirements, a TypeError is raised
  52. """
  53. global current_implementation
  54. if minimum_implementation.name not in impls[key].provides:
  55. raise TypeError(
  56. "Cannot set default odml-implementation to '%s', because %s-capabilities are required which are not provided (provides: %s)" %
  57. (key, minimum_implementation.name, ', '.join(impls[key].provides)))
  58. current_implementation = impls[key]
  59. def setMinimumImplementation(key):
  60. """
  61. Set a new minimum requirement for a default implementation.
  62. This can only be increased, i.e. 'downgrades' are not possible.
  63. If the current_implementation does not provide the requested capability,
  64. make the minimum implementation the default.
  65. """
  66. global minimum_implementation
  67. if key in minimum_implementation.provides:
  68. return # the minimum implementation is already capable of this feature
  69. if minimum_implementation.name not in impls[key].provides:
  70. raise TypeError(
  71. "Cannot set new minimum odml-implementation to '%s', because %s-capabilities are already required which are not provided (provides: %s)" %
  72. (key, minimum_implementation.name, ', '.join(impls[key].provides)))
  73. if key not in current_implementation.provides:
  74. setDefaultImplementation(key)
  75. minimum_implementation = impls[key]
  76. addImplementation(current_implementation)
  77. def Value(*args, **kwargs):
  78. return current_implementation.Value(*args, **kwargs)
  79. def Property(*args, **kwargs):
  80. return current_implementation.Property(*args, **kwargs)
  81. def Section(*args, **kwargs):
  82. return current_implementation.Section(*args, **kwargs)
  83. def Document(*args, **kwargs):
  84. return current_implementation.Document(*args, **kwargs)
  85. #__all__ = [Value, Property, Section, Document]