1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2025-10-29 18:49:03 +01:00

Documentation of the lodel.plugin.extensions and lodel.plugin.hooks modules

This commit is contained in:
Roland Haroutiounian 2017-03-24 16:07:21 +01:00
commit bc68848600
2 changed files with 28 additions and 19 deletions

View file

@ -1,3 +1,6 @@
## @package lodel.plugin.extensions A package to manage the Extension plugins
from lodel.context import LodelContext from lodel.context import LodelContext
LodelContext.expose_modules(globals(), { LodelContext.expose_modules(globals(), {
'lodel.plugin.plugins': ['Plugin'], 'lodel.plugin.plugins': ['Plugin'],
@ -7,9 +10,12 @@ LodelContext.expose_modules(globals(), {
_glob_typename = 'extension' _glob_typename = 'extension'
## @brief A class representing a basic Extension plugin
#
# This class will be extended for each plugin of this type.
class Extension(Plugin): class Extension(Plugin):
## @brief Specifies the settings linked to this plugin
_plist_confspecs = { _plist_confspecs = {
'section': 'lodel2', 'section': 'lodel2',
'key': 'extensions', 'key': 'extensions',
@ -20,6 +26,8 @@ class Extension(Plugin):
'ptype': _glob_typename, 'ptype': _glob_typename,
'none_is_valid': False}) 'none_is_valid': False})
} }
## @brief A property defining the type's name of this plugin.
# By default, it's the global type name ("extension" here).
_type_conf_name = _glob_typename _type_conf_name = _glob_typename

View file

@ -1,20 +1,22 @@
#-*- coding: utf-8 -*- #-*- coding: utf-8 -*-
## @package lodel.plugin.hooks This module deals with the Hook management in Lodel
import os import os
import copy import copy
from lodel.context import LodelContext from lodel.context import LodelContext
##@brief Class designed to handle a hook's callback with a priority ## @brief Class designed to handle a hook's callback with a priority
class DecoratedWrapper(object): class DecoratedWrapper(object):
##@brief Constructor ##
# @param hook function : the function to wrap # @param hook function : the function to wrap
# @param priority int : the callbacl priority # @param priority int : the callbacl priority
def __init__(self, hook, priority): def __init__(self, hook, priority):
self._priority = priority self._priority = priority
self._hook = hook self._hook = hook
##@brief Call the callback ## @brief Calls the callback
# @param hook_name str : The name of the called hook # @param hook_name str : The name of the called hook
# @param caller * : The caller (depends on the hook) # @param caller * : The caller (depends on the hook)
# @param payload * : Datas that depends on the hook # @param payload * : Datas that depends on the hook
@ -22,12 +24,14 @@ class DecoratedWrapper(object):
def __call__(self, hook_name, caller, payload): def __call__(self, hook_name, caller, payload):
return self._hook(hook_name, caller, payload) return self._hook(hook_name, caller, payload)
## @brief Returns the string representation of the class
# It shows the name and the priority of the hook
def __str__(self): def __str__(self):
return "<LodelHook '%s' priority = %s>" % ( return "<LodelHook '%s' priority = %s>" % (
self._hook.__name__, self._priority) self._hook.__name__, self._priority)
##@brief Decorator designed to register hook's callbacks ## @brief Decorator designed to register hook's callbacks
#@ingroup lodel2_plugins # @ingroup lodel2_plugins
# #
# @note Decorated functions are expected to take 3 arguments : # @note Decorated functions are expected to take 3 arguments :
#  - hook_name : the called hook name #  - hook_name : the called hook name
@ -35,17 +39,17 @@ class DecoratedWrapper(object):
# - payload : datas depending on the hook # - payload : datas depending on the hook
class LodelHook(object): class LodelHook(object):
##@brief Stores all hooks (DecoratedWrapper instances) ## @brief Stores all hooks (DecoratedWrapper instances)
_hooks = dict() _hooks = dict()
##@brief Decorator constructor ##
# @param hook_name str : the name of the hook to register to # @param hook_name str : the name of the hook to register to
# @param priority int : the hook priority # @param priority int : the hook priority (default value : None)
def __init__(self, hook_name, priority = None): def __init__(self, hook_name, priority = None):
self._hook_name = hook_name self._hook_name = hook_name
self._priority = 0xFFFF if priority is None else priority self._priority = 0xFFFF if priority is None else priority
##@brief called just after __init__ ## @brief called just after __init__
# @param hook function : the decorated function # @param hook function : the decorated function
# @return the hook argument # @return the hook argument
def __call__(self, hook): def __call__(self, hook):
@ -56,11 +60,10 @@ class LodelHook(object):
self._hooks[self._hook_name] = sorted(self._hooks[self._hook_name], key = lambda h: h._priority) self._hooks[self._hook_name] = sorted(self._hooks[self._hook_name], key = lambda h: h._priority)
return hook return hook
##@brief Call hooks ## @brief Calls a hook
# @param hook_name str : the hook's name # @param hook_name str : the hook's name
# @param caller * : the hook caller (depends on the hook) # @param caller * : the hook caller (depends on the hook)
# @param payload * : datas for the hook # @param payload * : datas for the hook
# @param cls
# @return modified payload # @return modified payload
@classmethod @classmethod
def call_hook(cls, hook_name, caller, payload): def call_hook(cls, hook_name, caller, payload):
@ -73,10 +76,9 @@ class LodelHook(object):
payload = hook(hook_name, caller, payload) payload = hook(hook_name, caller, payload)
return payload return payload
##@brief Fetch registered hooks ## @brief Fetches registered hooks
# @param names list | None : optionnal filter on name # @param names list | None : optionnal filter on name (default value : None)
# @param cls # @return dict containing for each name a list of the hooks and their priorities
# @return a list of functions
@classmethod @classmethod
def hook_list(cls, names = None): def hook_list(cls, names = None):
res = None res = None
@ -86,8 +88,7 @@ class LodelHook(object):
res = copy.copy(cls._hooks) res = copy.copy(cls._hooks)
return { name: [(hook._hook, hook._priority) for hook in hooks] for name, hooks in res.items() } return { name: [(hook._hook, hook._priority) for hook in hooks] for name, hooks in res.items() }
##@brief Unregister all hooks ## @brief Unregister all hooks
# @param cls
# @warning REALLY NOT a good idea ! # @warning REALLY NOT a good idea !
# @note implemented for testing purpose # @note implemented for testing purpose
@classmethod @classmethod