|
@@ -1,20 +1,22 @@
|
1
|
1
|
#-*- coding: utf-8 -*-
|
2
|
2
|
|
|
3
|
+## @package lodel.plugin.hooks This module deals with the Hook management in Lodel
|
|
4
|
+
|
3
|
5
|
import os
|
4
|
6
|
import copy
|
5
|
7
|
from lodel.context import LodelContext
|
6
|
8
|
|
7
|
9
|
|
8
|
|
-##@brief Class designed to handle a hook's callback with a priority
|
|
10
|
+## @brief Class designed to handle a hook's callback with a priority
|
9
|
11
|
class DecoratedWrapper(object):
|
10
|
|
- ##@brief Constructor
|
|
12
|
+ ##
|
11
|
13
|
# @param hook function : the function to wrap
|
12
|
14
|
# @param priority int : the callbacl priority
|
13
|
15
|
def __init__(self, hook, priority):
|
14
|
16
|
self._priority = priority
|
15
|
17
|
self._hook = hook
|
16
|
18
|
|
17
|
|
- ##@brief Call the callback
|
|
19
|
+ ## @brief Calls the callback
|
18
|
20
|
# @param hook_name str : The name of the called hook
|
19
|
21
|
# @param caller * : The caller (depends on the hook)
|
20
|
22
|
# @param payload * : Datas that depends on the hook
|
|
@@ -22,12 +24,14 @@ class DecoratedWrapper(object):
|
22
|
24
|
def __call__(self, hook_name, caller, payload):
|
23
|
25
|
return self._hook(hook_name, caller, payload)
|
24
|
26
|
|
|
27
|
+ ## @brief Returns the string representation of the class
|
|
28
|
+ # It shows the name and the priority of the hook
|
25
|
29
|
def __str__(self):
|
26
|
30
|
return "<LodelHook '%s' priority = %s>" % (
|
27
|
31
|
self._hook.__name__, self._priority)
|
28
|
32
|
|
29
|
|
-##@brief Decorator designed to register hook's callbacks
|
30
|
|
-#@ingroup lodel2_plugins
|
|
33
|
+## @brief Decorator designed to register hook's callbacks
|
|
34
|
+# @ingroup lodel2_plugins
|
31
|
35
|
#
|
32
|
36
|
# @note Decorated functions are expected to take 3 arguments :
|
33
|
37
|
# - hook_name : the called hook name
|
|
@@ -35,17 +39,17 @@ class DecoratedWrapper(object):
|
35
|
39
|
# - payload : datas depending on the hook
|
36
|
40
|
class LodelHook(object):
|
37
|
41
|
|
38
|
|
- ##@brief Stores all hooks (DecoratedWrapper instances)
|
|
42
|
+ ## @brief Stores all hooks (DecoratedWrapper instances)
|
39
|
43
|
_hooks = dict()
|
40
|
44
|
|
41
|
|
- ##@brief Decorator constructor
|
|
45
|
+ ##
|
42
|
46
|
# @param hook_name str : the name of the hook to register to
|
43
|
|
- # @param priority int : the hook priority
|
|
47
|
+ # @param priority int : the hook priority (default value : None)
|
44
|
48
|
def __init__(self, hook_name, priority = None):
|
45
|
49
|
self._hook_name = hook_name
|
46
|
50
|
self._priority = 0xFFFF if priority is None else priority
|
47
|
51
|
|
48
|
|
- ##@brief called just after __init__
|
|
52
|
+ ## @brief called just after __init__
|
49
|
53
|
# @param hook function : the decorated function
|
50
|
54
|
# @return the hook argument
|
51
|
55
|
def __call__(self, hook):
|
|
@@ -56,11 +60,10 @@ class LodelHook(object):
|
56
|
60
|
self._hooks[self._hook_name] = sorted(self._hooks[self._hook_name], key = lambda h: h._priority)
|
57
|
61
|
return hook
|
58
|
62
|
|
59
|
|
- ##@brief Call hooks
|
|
63
|
+ ## @brief Calls a hook
|
60
|
64
|
# @param hook_name str : the hook's name
|
61
|
65
|
# @param caller * : the hook caller (depends on the hook)
|
62
|
66
|
# @param payload * : datas for the hook
|
63
|
|
- # @param cls
|
64
|
67
|
# @return modified payload
|
65
|
68
|
@classmethod
|
66
|
69
|
def call_hook(cls, hook_name, caller, payload):
|
|
@@ -73,10 +76,9 @@ class LodelHook(object):
|
73
|
76
|
payload = hook(hook_name, caller, payload)
|
74
|
77
|
return payload
|
75
|
78
|
|
76
|
|
- ##@brief Fetch registered hooks
|
77
|
|
- # @param names list | None : optionnal filter on name
|
78
|
|
- # @param cls
|
79
|
|
- # @return a list of functions
|
|
79
|
+ ## @brief Fetches registered hooks
|
|
80
|
+ # @param names list | None : optionnal filter on name (default value : None)
|
|
81
|
+ # @return dict containing for each name a list of the hooks and their priorities
|
80
|
82
|
@classmethod
|
81
|
83
|
def hook_list(cls, names = None):
|
82
|
84
|
res = None
|
|
@@ -86,8 +88,7 @@ class LodelHook(object):
|
86
|
88
|
res = copy.copy(cls._hooks)
|
87
|
89
|
return { name: [(hook._hook, hook._priority) for hook in hooks] for name, hooks in res.items() }
|
88
|
90
|
|
89
|
|
- ##@brief Unregister all hooks
|
90
|
|
- # @param cls
|
|
91
|
+ ## @brief Unregister all hooks
|
91
|
92
|
# @warning REALLY NOT a good idea !
|
92
|
93
|
# @note implemented for testing purpose
|
93
|
94
|
@classmethod
|