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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. from lodel.context import LodelContext
  20. LodelContext.expose_modules(globals(), {
  21. 'lodel.plugin': ['LodelHook'],
  22. 'lodel.settings': ['Settings'],
  23. 'lodel.logger': 'logger'})
  24. ## @package lodel.plugin.core_hooks
  25. # @brief Lodel2 internal hooks declaration
  26. # @ingroup lodel2_plugins
  27. ## @brief Bootstrap hook that checks datasources configuration
  28. # @param hook_name str
  29. # @param caller * : the hook's caller
  30. # @param payload * : data to be given to the hook
  31. # @throw NameError when : a set datasource family name can not be found or a datasource identifier does not match with a configured datasource.
  32. @LodelHook('lodel2_bootstraped')
  33. def datasources_bootstrap_hook(hook_name, caller, payload):
  34. for ds_name in Settings.datasources._fields:
  35. ds_conf = getattr(Settings.datasources, ds_name)
  36. identifier = getattr(ds_conf, 'identifier')
  37. # Now we are trying to fetch the datasource given the identifier
  38. ds_family_name, ds_name = identifier.split('.',1)
  39. try:
  40. ds_fam = getattr(Settings.datasource, ds_family_name)
  41. except AttributeError:
  42. msg = "No datasource family named '%s' found"
  43. msg %= ds_family_name
  44. raise NameError(msg)
  45. try:
  46. ds = getattr(ds_fam, ds_name)
  47. except AttributeError:
  48. msg = "No datasource configured for identifier '%s' found"
  49. msg %= identifier
  50. raise NameError(msg)
  51. log_msg = "Found a datasource named '%s' identified by '%s'"
  52. log_msg %= (ds_name, identifier)
  53. logger.debug(log_msg)
  54. ## @brief Bootstrap hook that prints debug infos about registered hooks
  55. # @param name str
  56. # @param caller * : the hook's caller
  57. # @param payload * : data to be given to the hook
  58. @LodelHook('lodel2_bootstraped')
  59. def list_hook_debug_hook(name, caller, payload):
  60. LodelContext.expose_modules(globals(), {
  61. 'lodel.logger': 'logger'})
  62. hlist = LodelHook.hook_list()
  63. for name, reg_hooks in hlist.items():
  64. for hook, priority in reg_hooks:
  65. logger.debug("{modname}.{funname} is registered as hook \
  66. {hookname} with priority {priority}".format(
  67. modname = hook.__module__,
  68. funname = hook.__name__,
  69. priority = priority,
  70. hookname = name))
  71. ## @brief Hook that triggers custom methods injection in dynamic classes
  72. # @param caller * : the hook's caller
  73. # @param dynclasses list : a list of classes in which the injection will occur
  74. @LodelHook("lodel2_dyncode_loaded")
  75. def lodel2_plugins_custom_methods(self, caller, dynclasses):
  76. LodelContext.expose_modules(globals(), {
  77. 'lodel.plugin.plugins': ['CustomMethod']})
  78. CustomMethod.set_registered(dynclasses)