author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 21 Jul 2009 23:09:27 +0200 | |
branch | stable |
changeset 2413 | b1bea4cab594 |
parent 2350 | 209a816a5fb4 |
child 2452 | 868e0c75a57d |
permissions | -rw-r--r-- |
0 | 1 |
"""Hooks management |
2 |
||
3 |
Hooks are called before / after any individual update of entities / relations |
|
4 |
in the repository. |
|
5 |
||
6 |
Here is the prototype of the different hooks: |
|
7 |
||
8 |
* filtered on the entity's type: |
|
9 |
||
10 |
before_add_entity (session, entity) |
|
11 |
after_add_entity (session, entity) |
|
12 |
before_update_entity (session, entity) |
|
13 |
after_update_entity (session, entity) |
|
14 |
before_delete_entity (session, eid) |
|
15 |
after_delete_entity (session, eid) |
|
16 |
||
17 |
* filtered on the relation's type: |
|
18 |
||
19 |
before_add_relation (session, fromeid, rtype, toeid) |
|
20 |
after_add_relation (session, fromeid, rtype, toeid) |
|
21 |
before_delete_relation (session, fromeid, rtype, toeid) |
|
22 |
after_delete_relation (session, fromeid, rtype, toeid) |
|
23 |
||
24 |
||
25 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
26 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 27 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
28 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 29 |
""" |
30 |
__docformat__ = "restructuredtext en" |
|
31 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
32 |
ENTITIES_HOOKS = ('before_add_entity', 'after_add_entity', |
0 | 33 |
'before_update_entity', 'after_update_entity', |
34 |
'before_delete_entity', 'after_delete_entity') |
|
35 |
RELATIONS_HOOKS = ('before_add_relation', 'after_add_relation' , |
|
36 |
'before_delete_relation','after_delete_relation') |
|
37 |
SYSTEM_HOOKS = ('server_startup', 'server_shutdown', |
|
38 |
'session_open', 'session_close') |
|
39 |
||
40 |
ALL_HOOKS = frozenset(ENTITIES_HOOKS + RELATIONS_HOOKS + SYSTEM_HOOKS) |
|
41 |
||
42 |
class HooksManager(object): |
|
43 |
"""handle hooks registration and calls |
|
44 |
""" |
|
45 |
verification_hooks_activated = True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
46 |
|
0 | 47 |
def __init__(self, schema): |
48 |
self.set_schema(schema) |
|
49 |
||
50 |
def set_schema(self, schema): |
|
51 |
self._hooks = {} |
|
52 |
self.schema = schema |
|
53 |
self._init_hooks(schema) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
54 |
|
0 | 55 |
def register_hooks(self, hooks): |
56 |
"""register a dictionary of hooks : |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
57 |
|
0 | 58 |
{'event': {'entity or relation type': [callbacks list]}} |
59 |
""" |
|
60 |
for event, subevents in hooks.items(): |
|
61 |
for subevent, callbacks in subevents.items(): |
|
62 |
for callback in callbacks: |
|
63 |
self.register_hook(callback, event, subevent) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
64 |
|
0 | 65 |
def register_hook(self, function, event, etype=''): |
66 |
"""register a function to call when <event> occurs |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
67 |
|
0 | 68 |
<etype> is an entity/relation type or an empty string. |
69 |
If etype is the empty string, the function will be called at each |
|
70 |
event, else the function will be called only when event occurs on an |
|
71 |
entity/relation of the given type. |
|
72 |
""" |
|
73 |
assert event in ALL_HOOKS, '%r NOT IN %r' % (event, ALL_HOOKS) |
|
74 |
assert (not event in SYSTEM_HOOKS or not etype), (event, etype) |
|
75 |
etype = etype or '' |
|
76 |
try: |
|
77 |
self._hooks[event][etype].append(function) |
|
78 |
self.debug('registered hook %s on %s (%s)', event, etype or 'any', |
|
79 |
function.func_name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
80 |
|
0 | 81 |
except KeyError: |
82 |
self.error('can\'t register hook %s on %s (%s)', |
|
83 |
event, etype or 'any', function.func_name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
84 |
|
0 | 85 |
def unregister_hook(self, function, event, etype=''): |
86 |
"""register a function to call when <event> occurs |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
87 |
|
0 | 88 |
<etype> is an entity/relation type or an empty string. |
89 |
If etype is the empty string, the function will be called at each |
|
90 |
event, else the function will be called only when event occurs on an |
|
91 |
entity/relation of the given type. |
|
92 |
""" |
|
93 |
assert event in ALL_HOOKS, event |
|
94 |
etype = etype or '' |
|
95 |
self.info('unregister hook %s on %s (%s)', event, etype, |
|
96 |
function.func_name) |
|
97 |
self._hooks[event][etype].remove(function) |
|
98 |
||
99 |
def call_hooks(self, __event, __type='', *args, **kwargs): |
|
100 |
"""call hook matching event and optional type""" |
|
101 |
if __type: |
|
102 |
self.info('calling hooks for event %s (%s)', __event, __type) |
|
103 |
else: |
|
104 |
self.info('calling hooks for event %s', __event) |
|
105 |
# call generic hooks first |
|
106 |
for hook in self._hooks[__event]['']: |
|
107 |
#print '[generic]', hook.__name__ |
|
108 |
hook(*args, **kwargs) |
|
109 |
if __type: |
|
110 |
for hook in self._hooks[__event][__type]: |
|
111 |
#print '[%s]'%__type, hook.__name__ |
|
112 |
hook(*args, **kwargs) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
113 |
|
0 | 114 |
def _init_hooks(self, schema): |
115 |
"""initialize the hooks map""" |
|
116 |
for hook_event in ENTITIES_HOOKS: |
|
117 |
self._hooks[hook_event] = {'': []} |
|
118 |
for etype in schema.entities(): |
|
119 |
self._hooks[hook_event][etype] = [] |
|
120 |
for hook_event in RELATIONS_HOOKS: |
|
121 |
self._hooks[hook_event] = {'': []} |
|
122 |
for r_type in schema.relations(): |
|
123 |
self._hooks[hook_event][r_type] = [] |
|
124 |
for hook_event in SYSTEM_HOOKS: |
|
125 |
self._hooks[hook_event] = {'': []} |
|
126 |
||
127 |
def register_system_hooks(self, config): |
|
128 |
"""register system hooks according to the configuration""" |
|
129 |
self.info('register core hooks') |
|
130 |
from cubicweb.server.hooks import _register_metadata_hooks, _register_wf_hooks |
|
131 |
_register_metadata_hooks(self) |
|
132 |
self.info('register workflow hooks') |
|
133 |
_register_wf_hooks(self) |
|
134 |
if config.core_hooks: |
|
135 |
from cubicweb.server.hooks import _register_core_hooks |
|
136 |
_register_core_hooks(self) |
|
137 |
if config.schema_hooks: |
|
138 |
from cubicweb.server.schemahooks import _register_schema_hooks |
|
139 |
self.info('register schema hooks') |
|
140 |
_register_schema_hooks(self) |
|
141 |
if config.usergroup_hooks: |
|
142 |
from cubicweb.server.hooks import _register_usergroup_hooks |
|
143 |
from cubicweb.server.hooks import _register_eproperty_hooks |
|
144 |
self.info('register user/group hooks') |
|
145 |
_register_usergroup_hooks(self) |
|
146 |
_register_eproperty_hooks(self) |
|
147 |
if config.security_hooks: |
|
148 |
from cubicweb.server.securityhooks import register_security_hooks |
|
149 |
self.info('register security hooks') |
|
150 |
register_security_hooks(self) |
|
151 |
if not self.verification_hooks_activated: |
|
152 |
self.deactivate_verification_hooks() |
|
153 |
||
154 |
def deactivate_verification_hooks(self): |
|
155 |
from cubicweb.server.hooks import (cardinalitycheck_after_add_entity, |
|
156 |
cardinalitycheck_before_del_relation, |
|
157 |
cstrcheck_after_add_relation, |
|
158 |
uniquecstrcheck_before_modification) |
|
159 |
self.warning('deactivating verification hooks') |
|
160 |
self.verification_hooks_activated = False |
|
161 |
self.unregister_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '') |
|
162 |
self.unregister_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '') |
|
163 |
self.unregister_hook(cstrcheck_after_add_relation, 'after_add_relation', '') |
|
164 |
self.unregister_hook(uniquecstrcheck_before_modification, 'before_add_entity', '') |
|
165 |
self.unregister_hook(uniquecstrcheck_before_modification, 'before_update_entity', '') |
|
166 |
# self.unregister_hook(tidy_html_fields('before_add_entity'), 'before_add_entity', '') |
|
167 |
# self.unregister_hook(tidy_html_fields('before_update_entity'), 'before_update_entity', '') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
168 |
|
0 | 169 |
def reactivate_verification_hooks(self): |
170 |
from cubicweb.server.hooks import (cardinalitycheck_after_add_entity, |
|
171 |
cardinalitycheck_before_del_relation, |
|
172 |
cstrcheck_after_add_relation, |
|
173 |
uniquecstrcheck_before_modification) |
|
174 |
self.warning('reactivating verification hooks') |
|
175 |
self.verification_hooks_activated = True |
|
176 |
self.register_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '') |
|
177 |
self.register_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '') |
|
178 |
self.register_hook(cstrcheck_after_add_relation, 'after_add_relation', '') |
|
179 |
self.register_hook(uniquecstrcheck_before_modification, 'before_add_entity', '') |
|
180 |
self.register_hook(uniquecstrcheck_before_modification, 'before_update_entity', '') |
|
181 |
# self.register_hook(tidy_html_fields('before_add_entity'), 'before_add_entity', '') |
|
182 |
# self.register_hook(tidy_html_fields('before_update_entity'), 'before_update_entity', '') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
183 |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
672
diff
changeset
|
184 |
from cubicweb.selectors import yes |
722 | 185 |
from cubicweb.appobject import AppObject |
0 | 186 |
|
712
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
187 |
class autoid(type): |
0 | 188 |
"""metaclass to create an unique 'id' attribute on the class using it""" |
712
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
189 |
# XXX is this metaclass really necessary ? |
0 | 190 |
def __new__(mcs, name, bases, classdict): |
191 |
cls = super(autoid, mcs).__new__(mcs, name, bases, classdict) |
|
192 |
cls.id = str(id(cls)) |
|
193 |
return cls |
|
194 |
||
195 |
class Hook(AppObject): |
|
196 |
__metaclass__ = autoid |
|
197 |
__registry__ = 'hooks' |
|
730 | 198 |
__select__ = yes() |
0 | 199 |
# set this in derivated classes |
200 |
events = None |
|
201 |
accepts = None |
|
202 |
enabled = True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
203 |
|
0 | 204 |
def __init__(self, event=None): |
205 |
super(Hook, self).__init__() |
|
206 |
self.event = event |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
207 |
|
0 | 208 |
@classmethod |
209 |
def registered(cls, vreg): |
|
210 |
super(Hook, cls).registered(vreg) |
|
211 |
return cls() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
212 |
|
0 | 213 |
@classmethod |
214 |
def register_to(cls): |
|
215 |
if not cls.enabled: |
|
216 |
cls.warning('%s hook has been disabled', cls) |
|
217 |
return |
|
218 |
done = set() |
|
2350
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
219 |
assert isinstance(cls.events, (tuple, list)), \ |
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
220 |
'%s: events is expected to be a tuple, not %s' % ( |
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
221 |
cls, type(cls.events)) |
0 | 222 |
for event in cls.events: |
2350
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
223 |
if event == 'server_startup': |
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
224 |
assert not cls.accepts or cls.accepts == ('Any',), \ |
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
225 |
'%s doesnt make sense on server_startup' % cls.accepts |
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
226 |
cls.accepts = ('Any',) |
0 | 227 |
for ertype in cls.accepts: |
228 |
if (event, ertype) in done: |
|
229 |
continue |
|
230 |
yield event, ertype |
|
231 |
done.add((event, ertype)) |
|
232 |
try: |
|
233 |
eschema = cls.schema.eschema(ertype) |
|
234 |
except KeyError: |
|
235 |
# relation schema |
|
236 |
pass |
|
237 |
else: |
|
238 |
for eetype in eschema.specialized_by(): |
|
239 |
if (event, eetype) in done: |
|
240 |
continue |
|
241 |
yield event, str(eetype) |
|
242 |
done.add((event, eetype)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
243 |
|
0 | 244 |
|
245 |
def make_callback(self, event): |
|
246 |
if len(self.events) == 1: |
|
247 |
return self.call |
|
248 |
return self.__class__(event=event).call |
|
249 |
||
250 |
def call(self): |
|
251 |
raise NotImplementedError |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
252 |
|
0 | 253 |
class SystemHook(Hook): |
254 |
accepts = ('',) |
|
255 |
||
256 |
from logging import getLogger |
|
257 |
from cubicweb import set_log_methods |
|
258 |
set_log_methods(HooksManager, getLogger('cubicweb.hooksmanager')) |
|
259 |
set_log_methods(Hook, getLogger('cubicweb.hooks')) |