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.7KB

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