author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 27 Jul 2009 10:57:33 +0200 | |
changeset 2508 | b1958ebed570 |
parent 2493 | 9806571ea790 |
child 2579 | e69b2342bd8b |
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') |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2492
diff
changeset
|
37 |
SYSTEM_HOOKS = ('server_backup', 'server_restore', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2492
diff
changeset
|
38 |
'server_startup', 'server_shutdown', |
0 | 39 |
'session_open', 'session_close') |
40 |
||
41 |
ALL_HOOKS = frozenset(ENTITIES_HOOKS + RELATIONS_HOOKS + SYSTEM_HOOKS) |
|
42 |
||
43 |
class HooksManager(object): |
|
44 |
"""handle hooks registration and calls |
|
45 |
""" |
|
46 |
verification_hooks_activated = True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
47 |
|
0 | 48 |
def __init__(self, schema): |
49 |
self.set_schema(schema) |
|
50 |
||
51 |
def set_schema(self, schema): |
|
52 |
self._hooks = {} |
|
53 |
self.schema = schema |
|
54 |
self._init_hooks(schema) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
55 |
|
0 | 56 |
def register_hooks(self, hooks): |
57 |
"""register a dictionary of hooks : |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
58 |
|
0 | 59 |
{'event': {'entity or relation type': [callbacks list]}} |
60 |
""" |
|
61 |
for event, subevents in hooks.items(): |
|
62 |
for subevent, callbacks in subevents.items(): |
|
63 |
for callback in callbacks: |
|
64 |
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
|
65 |
|
0 | 66 |
def register_hook(self, function, event, etype=''): |
67 |
"""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
|
68 |
|
0 | 69 |
<etype> is an entity/relation type or an empty string. |
70 |
If etype is the empty string, the function will be called at each |
|
71 |
event, else the function will be called only when event occurs on an |
|
72 |
entity/relation of the given type. |
|
73 |
""" |
|
74 |
assert event in ALL_HOOKS, '%r NOT IN %r' % (event, ALL_HOOKS) |
|
75 |
assert (not event in SYSTEM_HOOKS or not etype), (event, etype) |
|
76 |
etype = etype or '' |
|
77 |
try: |
|
78 |
self._hooks[event][etype].append(function) |
|
79 |
self.debug('registered hook %s on %s (%s)', event, etype or 'any', |
|
80 |
function.func_name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
81 |
|
0 | 82 |
except KeyError: |
83 |
self.error('can\'t register hook %s on %s (%s)', |
|
84 |
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
|
85 |
|
0 | 86 |
def unregister_hook(self, function, event, etype=''): |
87 |
"""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
|
88 |
|
0 | 89 |
<etype> is an entity/relation type or an empty string. |
90 |
If etype is the empty string, the function will be called at each |
|
91 |
event, else the function will be called only when event occurs on an |
|
92 |
entity/relation of the given type. |
|
93 |
""" |
|
94 |
assert event in ALL_HOOKS, event |
|
95 |
etype = etype or '' |
|
96 |
self.info('unregister hook %s on %s (%s)', event, etype, |
|
97 |
function.func_name) |
|
98 |
self._hooks[event][etype].remove(function) |
|
99 |
||
100 |
def call_hooks(self, __event, __type='', *args, **kwargs): |
|
101 |
"""call hook matching event and optional type""" |
|
102 |
if __type: |
|
103 |
self.info('calling hooks for event %s (%s)', __event, __type) |
|
104 |
else: |
|
105 |
self.info('calling hooks for event %s', __event) |
|
106 |
# call generic hooks first |
|
107 |
for hook in self._hooks[__event]['']: |
|
108 |
#print '[generic]', hook.__name__ |
|
109 |
hook(*args, **kwargs) |
|
110 |
if __type: |
|
111 |
for hook in self._hooks[__event][__type]: |
|
112 |
#print '[%s]'%__type, hook.__name__ |
|
113 |
hook(*args, **kwargs) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
114 |
|
0 | 115 |
def _init_hooks(self, schema): |
116 |
"""initialize the hooks map""" |
|
117 |
for hook_event in ENTITIES_HOOKS: |
|
118 |
self._hooks[hook_event] = {'': []} |
|
119 |
for etype in schema.entities(): |
|
120 |
self._hooks[hook_event][etype] = [] |
|
121 |
for hook_event in RELATIONS_HOOKS: |
|
122 |
self._hooks[hook_event] = {'': []} |
|
123 |
for r_type in schema.relations(): |
|
124 |
self._hooks[hook_event][r_type] = [] |
|
125 |
for hook_event in SYSTEM_HOOKS: |
|
126 |
self._hooks[hook_event] = {'': []} |
|
127 |
||
128 |
def register_system_hooks(self, config): |
|
129 |
"""register system hooks according to the configuration""" |
|
130 |
self.info('register core hooks') |
|
131 |
from cubicweb.server.hooks import _register_metadata_hooks, _register_wf_hooks |
|
132 |
_register_metadata_hooks(self) |
|
133 |
self.info('register workflow hooks') |
|
134 |
_register_wf_hooks(self) |
|
135 |
if config.core_hooks: |
|
136 |
from cubicweb.server.hooks import _register_core_hooks |
|
137 |
_register_core_hooks(self) |
|
138 |
if config.schema_hooks: |
|
139 |
from cubicweb.server.schemahooks import _register_schema_hooks |
|
140 |
self.info('register schema hooks') |
|
141 |
_register_schema_hooks(self) |
|
142 |
if config.usergroup_hooks: |
|
143 |
from cubicweb.server.hooks import _register_usergroup_hooks |
|
144 |
from cubicweb.server.hooks import _register_eproperty_hooks |
|
145 |
self.info('register user/group hooks') |
|
146 |
_register_usergroup_hooks(self) |
|
147 |
_register_eproperty_hooks(self) |
|
148 |
if config.security_hooks: |
|
149 |
from cubicweb.server.securityhooks import register_security_hooks |
|
150 |
self.info('register security hooks') |
|
151 |
register_security_hooks(self) |
|
152 |
if not self.verification_hooks_activated: |
|
153 |
self.deactivate_verification_hooks() |
|
154 |
||
155 |
def deactivate_verification_hooks(self): |
|
156 |
from cubicweb.server.hooks import (cardinalitycheck_after_add_entity, |
|
157 |
cardinalitycheck_before_del_relation, |
|
158 |
cstrcheck_after_add_relation, |
|
159 |
uniquecstrcheck_before_modification) |
|
160 |
self.warning('deactivating verification hooks') |
|
161 |
self.verification_hooks_activated = False |
|
162 |
self.unregister_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '') |
|
163 |
self.unregister_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '') |
|
164 |
self.unregister_hook(cstrcheck_after_add_relation, 'after_add_relation', '') |
|
165 |
self.unregister_hook(uniquecstrcheck_before_modification, 'before_add_entity', '') |
|
166 |
self.unregister_hook(uniquecstrcheck_before_modification, 'before_update_entity', '') |
|
167 |
# self.unregister_hook(tidy_html_fields('before_add_entity'), 'before_add_entity', '') |
|
168 |
# 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
|
169 |
|
0 | 170 |
def reactivate_verification_hooks(self): |
171 |
from cubicweb.server.hooks import (cardinalitycheck_after_add_entity, |
|
172 |
cardinalitycheck_before_del_relation, |
|
173 |
cstrcheck_after_add_relation, |
|
174 |
uniquecstrcheck_before_modification) |
|
175 |
self.warning('reactivating verification hooks') |
|
176 |
self.verification_hooks_activated = True |
|
177 |
self.register_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '') |
|
178 |
self.register_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '') |
|
179 |
self.register_hook(cstrcheck_after_add_relation, 'after_add_relation', '') |
|
180 |
self.register_hook(uniquecstrcheck_before_modification, 'before_add_entity', '') |
|
181 |
self.register_hook(uniquecstrcheck_before_modification, 'before_update_entity', '') |
|
182 |
# self.register_hook(tidy_html_fields('before_add_entity'), 'before_add_entity', '') |
|
183 |
# 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
|
184 |
|
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
|
185 |
from cubicweb.selectors import yes |
722 | 186 |
from cubicweb.appobject import AppObject |
0 | 187 |
|
712
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
188 |
class autoid(type): |
0 | 189 |
"""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
|
190 |
# XXX is this metaclass really necessary ? |
0 | 191 |
def __new__(mcs, name, bases, classdict): |
192 |
cls = super(autoid, mcs).__new__(mcs, name, bases, classdict) |
|
193 |
cls.id = str(id(cls)) |
|
194 |
return cls |
|
195 |
||
196 |
class Hook(AppObject): |
|
197 |
__metaclass__ = autoid |
|
198 |
__registry__ = 'hooks' |
|
730 | 199 |
__select__ = yes() |
0 | 200 |
# set this in derivated classes |
201 |
events = None |
|
202 |
accepts = None |
|
203 |
enabled = True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
204 |
|
0 | 205 |
def __init__(self, event=None): |
206 |
super(Hook, self).__init__() |
|
207 |
self.event = event |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
208 |
|
0 | 209 |
@classmethod |
210 |
def registered(cls, vreg): |
|
211 |
super(Hook, cls).registered(vreg) |
|
212 |
return cls() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
213 |
|
0 | 214 |
@classmethod |
215 |
def register_to(cls): |
|
216 |
if not cls.enabled: |
|
217 |
cls.warning('%s hook has been disabled', cls) |
|
218 |
return |
|
219 |
done = set() |
|
2350
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
220 |
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
|
221 |
'%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
|
222 |
cls, type(cls.events)) |
0 | 223 |
for event in cls.events: |
2492
c51be1cf8317
should check for every system hooks, not only server_startup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2452
diff
changeset
|
224 |
if event in SYSTEM_HOOKS: |
2350
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
225 |
assert not cls.accepts or cls.accepts == ('Any',), \ |
2492
c51be1cf8317
should check for every system hooks, not only server_startup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2452
diff
changeset
|
226 |
'%s doesnt make sense on %s' % (cls.accepts, event) |
2350
209a816a5fb4
added some assertion on expected hooks class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
227 |
cls.accepts = ('Any',) |
0 | 228 |
for ertype in cls.accepts: |
229 |
if (event, ertype) in done: |
|
230 |
continue |
|
231 |
yield event, ertype |
|
232 |
done.add((event, ertype)) |
|
233 |
try: |
|
234 |
eschema = cls.schema.eschema(ertype) |
|
235 |
except KeyError: |
|
236 |
# relation schema |
|
237 |
pass |
|
238 |
else: |
|
239 |
for eetype in eschema.specialized_by(): |
|
240 |
if (event, eetype) in done: |
|
241 |
continue |
|
242 |
yield event, str(eetype) |
|
243 |
done.add((event, eetype)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
244 |
|
0 | 245 |
|
246 |
def make_callback(self, event): |
|
247 |
if len(self.events) == 1: |
|
248 |
return self.call |
|
249 |
return self.__class__(event=event).call |
|
250 |
||
251 |
def call(self): |
|
252 |
raise NotImplementedError |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
730
diff
changeset
|
253 |
|
0 | 254 |
class SystemHook(Hook): |
2452
868e0c75a57d
fix SystemHook declaration, accepts=('',) doesn't mean anything
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2350
diff
changeset
|
255 |
accepts = () |
0 | 256 |
|
257 |
from logging import getLogger |
|
258 |
from cubicweb import set_log_methods |
|
259 |
set_log_methods(HooksManager, getLogger('cubicweb.hooksmanager')) |
|
260 |
set_log_methods(Hook, getLogger('cubicweb.hooks')) |