Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

datasource_plugin.py 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from .plugins import Plugin
  2. from .exceptions import *
  3. from lodel.settings.validator import SettingValidator
  4. ##@brief Designed to handles datasources plugins
  5. #
  6. #A datasource provide data access to LeAPI typically a connector on a DB
  7. #or an API
  8. #@note For the moment implementation is done with a retro-compatibilities
  9. #priority and not with a convenience priority.
  10. #@todo Refactor and rewrite lodel2 datasource handling
  11. class DatasourcePlugin(Plugin):
  12. ##@brief Stores confspecs indicating where DatasourcePlugin list is stored
  13. _plist_confspecs = {
  14. 'section': 'lodel2',
  15. 'key': 'datasource_connectors',
  16. 'default': None,
  17. 'validator': SettingValidator('strip', none_is_valid = False) }
  18. def __init__(self, name):
  19. super().__init__(name)
  20. self.__datasource_cls = None
  21. def datasource_cls(self):
  22. if self.__datasource_cls is None:
  23. self.__datasource_cls = self.loader_module().Datasource
  24. return self.__datasource_cls
  25. def migration_handler_cls(self):
  26. return self.loader_module().migration_handler_class()
  27. ##@brief Return an initialized Datasource instance
  28. #@param ds_name str : The name of the datasource to instanciate
  29. #@param ro bool
  30. #@return A properly initialized Datasource instance
  31. #@throw SettingsError if an error occurs in settings
  32. #@throw DatasourcePluginError for various errors
  33. @classmethod
  34. def init_datasource(cls, ds_name, ro):
  35. plugin_name, ds_identifier = cls.plugin_name(ds_name, ro)
  36. ds_conf = cls._get_ds_connection_conf(ds_identifier, plugin_name)
  37. ds_cls = cls.get_datasource(plugin_name)
  38. return ds_cls(**ds_conf)
  39. ##@brief Return an initialized MigrationHandler instance
  40. #@param ds_name str : The datasource name
  41. #@return A properly initialized MigrationHandler instance
  42. @classmethod
  43. def init_migration_handler(cls, ds_name):
  44. plugin_name, ds_identifier = cls.plugin_name(ds_name, False)
  45. ds_conf = cls._get_ds_connection_conf(ds_identifier, plugin_name)
  46. mh_cls = cls.get_migration_handler(plugin_name)
  47. if 'read_only' in ds_conf:
  48. if ds_conf['read_only']:
  49. raise PluginError("A read only datasource was given to \
  50. migration handler !!!")
  51. del(ds_conf['read_only'])
  52. return mh_cls(**ds_conf)
  53. ##@brief Given a datasource name returns a DatasourcePlugin name
  54. #@param ds_name str : datasource name
  55. #@param ro bool : if true consider the datasource as readonly
  56. #@return a DatasourcePlugin name
  57. #@throw PluginError if datasource name not found
  58. #@throw DatasourcePermError if datasource is read_only but ro flag arg is
  59. #false
  60. @staticmethod
  61. def plugin_name(ds_name, ro):
  62. from lodel.settings import Settings
  63. # fetching connection identifier given datasource name
  64. try:
  65. ds_identifier = getattr(Settings.datasources, ds_name)
  66. except (NameError, AttributeError):
  67. raise DatasourcePluginError("Unknown or unconfigured datasource \
  68. '%s'" % ds_name)
  69. # fetching read_only flag
  70. try:
  71. read_only = getattr(ds_identifier, 'read_only')
  72. except (NameError, AttributeError):
  73. raise SettingsError("Malformed datasource configuration for '%s' \
  74. : missing read_only key" % ds_name)
  75. # fetching datasource identifier
  76. try:
  77. ds_identifier = getattr(ds_identifier, 'identifier')
  78. except (NameError,AttributeError) as e:
  79. raise SettingsError("Malformed datasource configuration for '%s' \
  80. : missing identifier key" % ds_name)
  81. # settings and ro arg consistency check
  82. if read_only and not ro:
  83. raise DatasourcePluginError("ro argument was set to False but \
  84. True found in settings for datasource '%s'" % ds_name)
  85. res = ds_identifier.split('.')
  86. if len(res) != 2:
  87. raise SettingsError("expected value for identifier is like \
  88. DS_PLUGIN_NAME.DS_INSTANCE_NAME. But got %s" % ds_identifier)
  89. return res
  90. ##@brief Try to fetch a datasource configuration
  91. #@param ds_identifier str : datasource name
  92. #@param ds_plugin_name : datasource plugin name
  93. #@return a dict containing datasource initialisation options
  94. #@throw NameError if a datasource plugin or instance cannot be found
  95. @staticmethod
  96. def _get_ds_connection_conf(ds_identifier,ds_plugin_name):
  97. from lodel.settings import Settings
  98. if ds_plugin_name not in Settings.datasource._fields:
  99. msg = "Unknown or unconfigured datasource plugin %s"
  100. msg %= ds_plugin
  101. raise DatasourcePluginError(msg)
  102. ds_conf = getattr(Settings.datasource, ds_plugin_name)
  103. if ds_identifier not in ds_conf._fields:
  104. msg = "Unknown or unconfigured datasource instance %s"
  105. msg %= ds_identifier
  106. raise DatasourcePluginError(msg)
  107. ds_conf = getattr(ds_conf, ds_identifier)
  108. return {k: getattr(ds_conf,k) for k in ds_conf._fields }
  109. ##@brief DatasourcePlugin instance accessor
  110. #@param ds_name str : plugin name
  111. #@return a DatasourcePlugin instance
  112. #@throw PluginError if no plugin named ds_name found
  113. #@throw PluginTypeError if ds_name ref to a plugin that is not a
  114. #DatasourcePlugin
  115. @classmethod
  116. def get(cls, ds_name):
  117. pinstance = super().get(ds_name) #Will raise PluginError if bad name
  118. if not isinstance(pinstance, DatasourcePlugin):
  119. raise PluginTypeErrror("A name of a DatasourcePlugin was excepted \
  120. but %s is a %s" % (ds_name, pinstance.__class__.__name__))
  121. return pinstance
  122. ##@brief Return a datasource class given a datasource name
  123. #@param ds_name str : datasource plugin name
  124. #@throw PluginError if ds_name is not an existing plugin name
  125. #@throw PluginTypeError if ds_name is not the name of a DatasourcePlugin
  126. @classmethod
  127. def get_datasource(cls, ds_plugin_name):
  128. return cls.get(ds_plugin_name).datasource_cls()
  129. @classmethod
  130. def get_migration_handler(cls, ds_plugin_name):
  131. return cls.get(ds_plugin_name).migration_handler_cls()