272 SYSTEM_HOOKS = set(('server_backup', 'server_restore', |
272 SYSTEM_HOOKS = set(('server_backup', 'server_restore', |
273 'server_startup', 'server_maintenance', 'server_shutdown', |
273 'server_startup', 'server_maintenance', 'server_shutdown', |
274 'session_open', 'session_close')) |
274 'session_open', 'session_close')) |
275 ALL_HOOKS = ENTITIES_HOOKS | RELATIONS_HOOKS | SYSTEM_HOOKS |
275 ALL_HOOKS = ENTITIES_HOOKS | RELATIONS_HOOKS | SYSTEM_HOOKS |
276 |
276 |
|
277 def _iter_kwargs(entities, kwargs): |
|
278 if not entities: |
|
279 yield kwargs |
|
280 else: |
|
281 for entity in entities: |
|
282 kwargs['entity'] = entity |
|
283 yield kwargs |
|
284 |
277 |
285 |
278 class HooksRegistry(CWRegistry): |
286 class HooksRegistry(CWRegistry): |
279 def initialization_completed(self): |
287 def initialization_completed(self): |
280 for appobjects in self.values(): |
288 for appobjects in self.values(): |
281 for cls in appobjects: |
289 for cls in appobjects: |
286 def register(self, obj, **kwargs): |
294 def register(self, obj, **kwargs): |
287 obj.check_events() |
295 obj.check_events() |
288 super(HooksRegistry, self).register(obj, **kwargs) |
296 super(HooksRegistry, self).register(obj, **kwargs) |
289 |
297 |
290 def call_hooks(self, event, session=None, **kwargs): |
298 def call_hooks(self, event, session=None, **kwargs): |
|
299 """call `event` hooks for an entity or a list of entities (passed |
|
300 respectively as the `entity` or ``entities`` keyword argument). |
|
301 """ |
291 kwargs['event'] = event |
302 kwargs['event'] = event |
292 if session is None: |
303 if session is None: # True for events such as server_start |
293 for hook in sorted(self.possible_objects(session, **kwargs), |
304 for hook in sorted(self.possible_objects(session, **kwargs), |
294 key=lambda x: x.order): |
305 key=lambda x: x.order): |
295 hook() |
306 hook() |
296 else: |
307 else: |
|
308 if 'entities' in kwargs: |
|
309 assert 'entity' not in kwargs, \ |
|
310 'can\'t pass "entities" and "entity" arguments simultaneously' |
|
311 entities = kwargs.pop('entities') |
|
312 else: |
|
313 entities = [] |
297 # by default, hooks are executed with security turned off |
314 # by default, hooks are executed with security turned off |
298 with security_enabled(session, read=False): |
315 with security_enabled(session, read=False): |
299 hooks = sorted(self.possible_objects(session, **kwargs), |
316 for _kwargs in _iter_kwargs(entities, kwargs): |
300 key=lambda x: x.order) |
317 hooks = sorted(self.possible_objects(session, **_kwargs), |
301 with security_enabled(session, write=False): |
318 key=lambda x: x.order) |
302 for hook in hooks: |
319 with security_enabled(session, write=False): |
303 #print hook.category, hook.__regid__ |
320 for hook in hooks: |
304 hook() |
321 #print hook.category, hook.__regid__ |
|
322 hook() |
305 |
323 |
306 class HooksManager(object): |
324 class HooksManager(object): |
307 def __init__(self, vreg): |
325 def __init__(self, vreg): |
308 self.vreg = vreg |
326 self.vreg = vreg |
309 |
327 |