__init__.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import warnings
  2. from sys import version_info as _python_version
  3. _property = property
  4. from . import doc
  5. from . import property
  6. from . import section
  7. from .dtypes import DType
  8. from .fileio import load, save, display
  9. from .info import VERSION
  10. from .tools.parser_utils import SUPPORTED_PARSERS as PARSERS
  11. def _format_warning(warn_msg, *args, **kwargs):
  12. """
  13. Used to provide users with deprecation warnings via the warnings module
  14. but without spamming them with full stack traces.
  15. """
  16. final_msg = "%s\n" % str(warn_msg)
  17. # If available add category name to the message
  18. if args and hasattr(args[0], "__name__"):
  19. final_msg = "%s: %s" % (args[0].__name__, final_msg)
  20. return final_msg
  21. # Monkey patch formatting 'warnings' messages for the whole module.
  22. warnings.formatwarning = _format_warning
  23. if _python_version.major < 3:
  24. msg = "Python 2 has been deprecated.\n\todML support for Python 2 will be dropped August 2020."
  25. warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
  26. elif _python_version.major == 3 and _python_version.minor < 6:
  27. msg = "The '%s' package is not tested with your Python version. " % __name__
  28. msg += "\n\tPlease consider upgrading to the latest Python distribution."
  29. warnings.warn(msg)
  30. __version__ = VERSION
  31. class odml_implementation(object):
  32. name = None
  33. provides = []
  34. Property = None
  35. Section = None
  36. Document = None
  37. class BasicImplementation(odml_implementation):
  38. name = 'basic'
  39. provides = ['basic']
  40. @_property
  41. def Section(self):
  42. return section.BaseSection
  43. @_property
  44. def Property(self):
  45. return property.BaseProperty
  46. @_property
  47. def Document(self):
  48. return doc.BaseDocument
  49. # here the available implementations are stored
  50. impls = {}
  51. # the default implementation
  52. current_implementation = BasicImplementation()
  53. minimum_implementation = current_implementation
  54. def addImplementation(implementation, make_minimum=False,
  55. make_default=False, key=None):
  56. """register a new available implementation"""
  57. impls[implementation.name] = implementation
  58. if make_minimum and key is not None:
  59. setMinimumImplementation(key)
  60. if make_default and key is not None:
  61. setDefaultImplementation(key)
  62. def getImplementation(key=None):
  63. """retrieve a implementation named *key*"""
  64. if key is None:
  65. return current_implementation
  66. implementation = impls[key]
  67. return implementation
  68. def setDefaultImplementation(key):
  69. """
  70. set a new default implementation
  71. if it does not fulfill the minimum requirements, a TypeError is raised
  72. """
  73. global current_implementation
  74. if minimum_implementation.name not in impls[key].provides:
  75. raise TypeError(
  76. "Cannot set default odml-implementation to '%s', "
  77. "because %s-capabilities are required which are not "
  78. "provided (provides: %s)" %
  79. (key, minimum_implementation.name, ', '.join(impls[key].provides)))
  80. current_implementation = impls[key]
  81. def setMinimumImplementation(key):
  82. """
  83. Set a new minimum requirement for a default implementation.
  84. This can only be increased, i.e. 'downgrades' are not possible.
  85. If the current_implementation does not provide the requested capability,
  86. make the minimum implementation the default.
  87. """
  88. global minimum_implementation
  89. if key in minimum_implementation.provides:
  90. return # the minimum implementation is already capable of this feature
  91. if minimum_implementation.name not in impls[key].provides:
  92. raise TypeError(
  93. "Cannot set new minimum odml-implementation to '%s', "
  94. "because %s-capabilities are already required which are "
  95. "not provided (provides: %s)" %
  96. (key, minimum_implementation.name, ', '.join(impls[key].provides)))
  97. if key not in current_implementation.provides:
  98. setDefaultImplementation(key)
  99. minimum_implementation = impls[key]
  100. addImplementation(current_implementation)
  101. def Property(*args, **kwargs):
  102. return current_implementation.Property(*args, **kwargs)
  103. def Section(*args, **kwargs):
  104. return current_implementation.Section(*args, **kwargs)
  105. def Document(*args, **kwargs):
  106. return current_implementation.Document(*args, **kwargs)
  107. # __all__ = [Property, Section, Document]