__init__.py 3.3 KB

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