No Description
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.

core_hooks.py 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #-*- coding: utf-8 -*-
  2. from lodel.plugin import LodelHook
  3. from lodel.settings import Setting
  4. from lodel.logger import logger
  5. ## @package lodel.plugin.core_hooks
  6. # @brief Lodel2 internal hooks declaration
  7. # @ingroup lodel2_plugins
  8. ## @brief Bootstrap hook that checks datasources configuration
  9. # @param hook_name str
  10. # @param caller * : the hook's caller
  11. # @param payload * : data to be given to the hook
  12. # @throw NameError when : a set datasource family name can not be found or a datasource identifier does not match with a configured datasource.
  13. @LodelHook('lodel2_bootstraped')
  14. def datasources_bootstrap_hook(hook_name, caller, payload):
  15. for ds_name in Settings.datasources._fields:
  16. ds_conf = getattr(Settings.datasources, ds_name)
  17. identifier = getattr(ds_conf, 'identifier')
  18. # Now we are trying to fetch the datasource given the identifier
  19. ds_family_name, ds_name = identifier.split('.',1)
  20. try:
  21. ds_fam = getattr(Settings.datasource, ds_family_name)
  22. except AttributeError:
  23. msg = "No datasource family named '%s' found"
  24. msg %= ds_family_name
  25. raise NameError(msg)
  26. try:
  27. ds = getattr(ds_fam, ds_name)
  28. except AttributeError:
  29. msg = "No datasource configured for identifier '%s' found"
  30. msg %= identifier
  31. raise NameError(msg)
  32. log_msg = "Found a datasource named '%s' identified by '%s'"
  33. log_msg %= (ds_name, identifier)
  34. logger.debug(log_msg)
  35. ## @brief Bootstrap hook that prints debug infos about registered hooks
  36. # @param name str
  37. # @param caller * : the hook's caller
  38. # @param payload * : data to be given to the hook
  39. @LodelHook('lodel2_bootstraped')
  40. def list_hook_debug_hook(name, caller, payload):
  41. from lodel.logger import logger
  42. hlist = LodelHook.hook_list()
  43. for name, reg_hooks in hlist.items():
  44. for hook, priority in reg_hooks:
  45. logger.debug("{modname}.{funname} is registered as hook \
  46. {hookname} with priority {priority}".format(
  47. modname = hook.__module__,
  48. funname = hook.__name__,
  49. priority = priority,
  50. hookname = name))
  51. ## @brief Hook that triggers custom methods injection in dynamic classes
  52. # @param caller * : the hook's caller
  53. # @param dynclasses list : a list of classes in which the injection will occur
  54. @LodelHook("lodel2_dyncode_loaded")
  55. def lodel2_plugins_custom_methods(self, caller, dynclasses):
  56. from lodel.plugin.plugins import CustomMethod
  57. CustomMethod.set_registered(dynclasses)