author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 17 Feb 2010 12:21:28 +0100 | |
branch | stable |
changeset 4607 | 55eab66c6592 |
parent 4522 | 271f201e3735 |
child 4699 | 8757fca2c308 |
permissions | -rw-r--r-- |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
1 |
"""Hooks management |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
2 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
3 |
This module defined the `Hook` class and registry and a set of abstract classes |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
4 |
for operations. |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
5 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
6 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
7 |
Hooks are called before / after any individual update of entities / relations |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
8 |
in the repository and on special events such as server startup or shutdown. |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
9 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
10 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
11 |
Operations may be registered by hooks during a transaction, which will be |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
12 |
fired when the pool is commited or rollbacked. |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
13 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
14 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
15 |
Entity hooks (eg before_add_entity, after_add_entity, before_update_entity, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
16 |
after_update_entity, before_delete_entity, after_delete_entity) all have an |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
17 |
`entity` attribute |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
18 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
19 |
Relation (eg before_add_relation, after_add_relation, before_delete_relation, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
20 |
after_delete_relation) all have `eidfrom`, `rtype`, `eidto` attributes. |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
21 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
22 |
Server start/stop hooks (eg server_startup, server_shutdown) have a `repo` |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
23 |
attribute, but *their `_cw` attribute is None*. |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
24 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
25 |
Backup/restore hooks (eg server_backup, server_restore) have a `repo` and a |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
26 |
`timestamp` attributes, but *their `_cw` attribute is None*. |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
27 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
28 |
Session hooks (eg session_open, session_close) have no special attribute. |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
29 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
30 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
31 |
:organization: Logilab |
4307
7fba9c34c88f
update copyright
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4106
diff
changeset
|
32 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
33 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
34 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
35 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
36 |
__docformat__ = "restructuredtext en" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
37 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
38 |
from warnings import warn |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
39 |
from logging import getLogger |
3660
7b41a6ba7400
fix/prepare propagation hooks usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
40 |
from itertools import chain |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
41 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
42 |
from logilab.common.decorators import classproperty |
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
43 |
from logilab.common.deprecation import deprecated |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
44 |
from logilab.common.logging_ext import set_log_methods |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
45 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
46 |
from cubicweb.cwvreg import CWRegistry, VRegistry |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
47 |
from cubicweb.selectors import (objectify_selector, lltrace, match_search_state, |
4075
e710f4052bd6
use implements instead of entity_implements
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4025
diff
changeset
|
48 |
implements) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
49 |
from cubicweb.appobject import AppObject |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
50 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
51 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
52 |
ENTITIES_HOOKS = set(('before_add_entity', 'after_add_entity', |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
53 |
'before_update_entity', 'after_update_entity', |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
54 |
'before_delete_entity', 'after_delete_entity')) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
55 |
RELATIONS_HOOKS = set(('before_add_relation', 'after_add_relation' , |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
56 |
'before_delete_relation','after_delete_relation')) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
57 |
SYSTEM_HOOKS = set(('server_backup', 'server_restore', |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
58 |
'server_startup', 'server_shutdown', |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
59 |
'session_open', 'session_close')) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
60 |
ALL_HOOKS = ENTITIES_HOOKS | RELATIONS_HOOKS | SYSTEM_HOOKS |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
61 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
62 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
63 |
class HooksRegistry(CWRegistry): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
64 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
65 |
def register(self, obj, **kwargs): |
2840
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
66 |
try: |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
67 |
iter(obj.events) |
3394
51a25bdd7bdc
[hooks] fix check for .events attribute in hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3195
diff
changeset
|
68 |
except AttributeError: |
51a25bdd7bdc
[hooks] fix check for .events attribute in hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3195
diff
changeset
|
69 |
raise |
2840
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
70 |
except: |
3415
ae884253edeb
nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3396
diff
changeset
|
71 |
raise Exception('bad .events attribute %s on %s.%s' % ( |
ae884253edeb
nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3396
diff
changeset
|
72 |
obj.events, obj.__module__, obj.__name__)) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
73 |
for event in obj.events: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
74 |
if event not in ALL_HOOKS: |
3415
ae884253edeb
nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3396
diff
changeset
|
75 |
raise Exception('bad event %s on %s.%s' % ( |
ae884253edeb
nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3396
diff
changeset
|
76 |
event, obj.__module__, obj.__name__)) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
77 |
super(HooksRegistry, self).register(obj, **kwargs) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
78 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
79 |
def call_hooks(self, event, req=None, **kwargs): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
80 |
kwargs['event'] = event |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
81 |
for hook in sorted(self.possible_objects(req, **kwargs), key=lambda x: x.order): |
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
82 |
if hook.enabled: |
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
83 |
hook() |
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
84 |
else: |
4339
fe93b670343d
some fixes so that deprecation warning are properly localized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4307
diff
changeset
|
85 |
warn('[3.6] %s: enabled is deprecated' % hook.__class__) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
86 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
87 |
VRegistry.REGISTRY_FACTORY['hooks'] = HooksRegistry |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
88 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
89 |
|
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4008
diff
changeset
|
90 |
def entity_oldnewvalue(entity, attr): |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4008
diff
changeset
|
91 |
"""returns the couple (old attr value, new attr value) |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4008
diff
changeset
|
92 |
NOTE: will only work in a before_update_entity hook |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4008
diff
changeset
|
93 |
""" |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4008
diff
changeset
|
94 |
# get new value and remove from local dict to force a db query to |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4008
diff
changeset
|
95 |
# fetch old value |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4008
diff
changeset
|
96 |
newvalue = entity.pop(attr, None) |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4008
diff
changeset
|
97 |
oldvalue = getattr(entity, attr) |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4008
diff
changeset
|
98 |
if newvalue is not None: |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4008
diff
changeset
|
99 |
entity[attr] = newvalue |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4008
diff
changeset
|
100 |
return oldvalue, newvalue |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4008
diff
changeset
|
101 |
|
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4008
diff
changeset
|
102 |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
103 |
# some hook specific selectors ################################################# |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
104 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
105 |
@objectify_selector |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
106 |
@lltrace |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
107 |
def match_event(cls, req, **kwargs): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
108 |
if kwargs.get('event') in cls.events: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
109 |
return 1 |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
110 |
return 0 |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
111 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
112 |
@objectify_selector |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
113 |
@lltrace |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
114 |
def enabled_category(cls, req, **kwargs): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
115 |
if req is None: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
116 |
# server startup / shutdown event |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
117 |
config = kwargs['repo'].config |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
118 |
else: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
119 |
config = req.vreg.config |
4106
ea4e93f9b151
more advanced hooks control
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4104
diff
changeset
|
120 |
return config.is_hook_activated(cls) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
121 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
122 |
@objectify_selector |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
123 |
@lltrace |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
124 |
def regular_session(cls, req, **kwargs): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
125 |
if req is None or req.is_super_session: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
126 |
return 0 |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
127 |
return 1 |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
128 |
|
3660
7b41a6ba7400
fix/prepare propagation hooks usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
129 |
|
7b41a6ba7400
fix/prepare propagation hooks usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
130 |
class rechain(object): |
7b41a6ba7400
fix/prepare propagation hooks usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
131 |
def __init__(self, *iterators): |
7b41a6ba7400
fix/prepare propagation hooks usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
132 |
self.iterators = iterators |
7b41a6ba7400
fix/prepare propagation hooks usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
133 |
def __iter__(self): |
7b41a6ba7400
fix/prepare propagation hooks usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
134 |
return iter(chain(*self.iterators)) |
7b41a6ba7400
fix/prepare propagation hooks usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
135 |
|
7b41a6ba7400
fix/prepare propagation hooks usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
136 |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
137 |
class match_rtype(match_search_state): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
138 |
"""accept if parameters specified as initializer arguments are specified |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
139 |
in named arguments given to the selector |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
140 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
141 |
:param *expected: parameters (eg `basestring`) which are expected to be |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
142 |
found in named arguments (kwargs) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
143 |
""" |
4341
2d2aa2526c56
match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
144 |
def __init__(self, *expected, **more): |
3660
7b41a6ba7400
fix/prepare propagation hooks usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
145 |
self.expected = expected |
4341
2d2aa2526c56
match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
146 |
self.frometypes = more.pop('frometypes', None) |
2d2aa2526c56
match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
147 |
self.toetypes = more.pop('toetypes', None) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
148 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
149 |
@lltrace |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
150 |
def __call__(self, cls, req, *args, **kwargs): |
4341
2d2aa2526c56
match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
151 |
if kwargs.get('rtype') not in self.expected: |
2d2aa2526c56
match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
152 |
return 0 |
2d2aa2526c56
match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
153 |
if self.frometypes is not None and \ |
2d2aa2526c56
match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
154 |
req.describe(kwargs['eidfrom'])[0] not in self.frometypes: |
2d2aa2526c56
match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
155 |
return 0 |
2d2aa2526c56
match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
156 |
if self.toetypes is not None and \ |
2d2aa2526c56
match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
157 |
req.describe(kwargs['eidto'])[0] not in self.toetypes: |
2d2aa2526c56
match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
158 |
return 0 |
2d2aa2526c56
match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
159 |
return 1 |
2d2aa2526c56
match_rtype hook selector now accepts frometypes/toetypes optional arguments to match only given subject/object entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
160 |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
161 |
|
4086
9b96126e0b14
add hook selector
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4075
diff
changeset
|
162 |
class match_rtype_sets(match_search_state): |
9b96126e0b14
add hook selector
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4075
diff
changeset
|
163 |
"""accept if parameters specified as initializer arguments are specified |
9b96126e0b14
add hook selector
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4075
diff
changeset
|
164 |
in named arguments given to the selector |
9b96126e0b14
add hook selector
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4075
diff
changeset
|
165 |
""" |
9b96126e0b14
add hook selector
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4075
diff
changeset
|
166 |
|
9b96126e0b14
add hook selector
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4075
diff
changeset
|
167 |
def __init__(self, *expected): |
9b96126e0b14
add hook selector
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4075
diff
changeset
|
168 |
self.expected = expected |
9b96126e0b14
add hook selector
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4075
diff
changeset
|
169 |
|
9b96126e0b14
add hook selector
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4075
diff
changeset
|
170 |
@lltrace |
9b96126e0b14
add hook selector
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4075
diff
changeset
|
171 |
def __call__(self, cls, req, *args, **kwargs): |
9b96126e0b14
add hook selector
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4075
diff
changeset
|
172 |
for rel_set in self.expected: |
9b96126e0b14
add hook selector
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4075
diff
changeset
|
173 |
if kwargs.get('rtype') in rel_set: |
9b96126e0b14
add hook selector
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4075
diff
changeset
|
174 |
return 1 |
9b96126e0b14
add hook selector
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4075
diff
changeset
|
175 |
return 0 |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
176 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
177 |
# base class for hook ########################################################## |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
178 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
179 |
class Hook(AppObject): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
180 |
__registry__ = 'hooks' |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
181 |
__select__ = match_event() & enabled_category() |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
182 |
# set this in derivated classes |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
183 |
events = None |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
184 |
category = None |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
185 |
order = 0 |
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
186 |
# XXX deprecated |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
187 |
enabled = True |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
188 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
189 |
@classproperty |
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3195
diff
changeset
|
190 |
def __regid__(cls): |
3383
c6aff16e5aed
nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
191 |
warn('[3.6] %s.%s: please specify an id for your hook' |
c6aff16e5aed
nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
192 |
% (cls.__module__, cls.__name__), DeprecationWarning) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
193 |
return str(id(cls)) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
194 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
195 |
@classmethod |
4490
d45cde54d464
backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4341
diff
changeset
|
196 |
def __registered__(cls, reg): |
d45cde54d464
backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4341
diff
changeset
|
197 |
super(Hook, cls).__registered__(reg) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
198 |
if getattr(cls, 'accepts', None): |
3383
c6aff16e5aed
nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
199 |
warn('[3.6] %s.%s: accepts is deprecated, define proper __select__' |
c6aff16e5aed
nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
200 |
% (cls.__module__, cls.__name__), DeprecationWarning) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
201 |
rtypes = [] |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
202 |
for ertype in cls.accepts: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
203 |
if ertype.islower(): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
204 |
rtypes.append(ertype) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
205 |
else: |
4075
e710f4052bd6
use implements instead of entity_implements
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4025
diff
changeset
|
206 |
cls.__select__ = cls.__select__ & implements(ertype) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
207 |
if rtypes: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
208 |
cls.__select__ = cls.__select__ & match_rtype(*rtypes) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
209 |
return cls |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
210 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
211 |
known_args = set(('entity', 'rtype', 'eidfrom', 'eidto', 'repo', 'timestamp')) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
212 |
def __init__(self, req, event, **kwargs): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
213 |
for arg in self.known_args: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
214 |
if arg in kwargs: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
215 |
setattr(self, arg, kwargs.pop(arg)) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
216 |
super(Hook, self).__init__(req, **kwargs) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
217 |
self.event = event |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
218 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
219 |
def __call__(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
220 |
if hasattr(self, 'call'): |
3383
c6aff16e5aed
nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
221 |
cls = self.__class__ |
c6aff16e5aed
nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
222 |
warn('[3.6] %s.%s: call is deprecated, implements __call__' |
c6aff16e5aed
nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
223 |
% (cls.__module__, cls.__name__), DeprecationWarning) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
224 |
if self.event.endswith('_relation'): |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
225 |
self.call(self._cw, self.eidfrom, self.rtype, self.eidto) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
226 |
elif 'delete' in self.event: |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
227 |
self.call(self._cw, self.entity.eid) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
228 |
elif self.event.startswith('server_'): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
229 |
self.call(self.repo) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
230 |
elif self.event.startswith('session_'): |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
231 |
self.call(self._cw) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
232 |
else: |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
233 |
self.call(self._cw, self.entity) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
234 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
235 |
set_log_methods(Hook, getLogger('cubicweb.hook')) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
236 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
237 |
|
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
238 |
# base classes for relation propagation ######################################## |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
239 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
240 |
class PropagateSubjectRelationHook(Hook): |
4522
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
241 |
"""propagate some `main_rtype` relation on entities linked as object of |
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
242 |
`subject_relations` or as subject of `object_relations` (the watched |
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
243 |
relations). |
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
244 |
|
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
245 |
This hook ensure that when one of the watched relation is added, the |
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
246 |
`main_rtype` relation is added to the target entity of the relation. |
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
247 |
""" |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
248 |
events = ('after_add_relation',) |
3660
7b41a6ba7400
fix/prepare propagation hooks usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
249 |
|
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
250 |
# to set in concrete class |
3660
7b41a6ba7400
fix/prepare propagation hooks usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
251 |
main_rtype = None |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
252 |
subject_relations = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
253 |
object_relations = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
254 |
|
3659 | 255 |
def __call__(self): |
4522
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
256 |
assert self.main_rtype |
3659 | 257 |
for eid in (self.eidfrom, self.eidto): |
258 |
etype = self._cw.describe(eid)[0] |
|
4522
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
259 |
if self.main_rtype not in self._cw.vreg.schema.eschema(etype).subjrels: |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
260 |
return |
3659 | 261 |
if self.rtype in self.subject_relations: |
262 |
meid, seid = self.eidfrom, self.eidto |
|
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
263 |
else: |
3659 | 264 |
assert self.rtype in self.object_relations |
265 |
meid, seid = self.eidto, self.eidfrom |
|
266 |
self._cw.unsafe_execute( |
|
3090
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
267 |
'SET E %s P WHERE X %s P, X eid %%(x)s, E eid %%(e)s, NOT E %s P'\ |
3659 | 268 |
% (self.main_rtype, self.main_rtype, self.main_rtype), |
3090
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
269 |
{'x': meid, 'e': seid}, ('x', 'e')) |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
270 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
271 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
272 |
class PropagateSubjectRelationAddHook(Hook): |
4522
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
273 |
"""propagate to entities at the end of watched relations when a `main_rtype` |
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
274 |
relation is added |
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
275 |
""" |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
276 |
events = ('after_add_relation',) |
3660
7b41a6ba7400
fix/prepare propagation hooks usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
277 |
|
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
278 |
# to set in concrete class |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
279 |
subject_relations = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
280 |
object_relations = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
281 |
|
3659 | 282 |
def __call__(self): |
4104
7e478d7caf20
[mq]: small_api_changes_for_3.6
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
4086
diff
changeset
|
283 |
eschema = self._cw.vreg.schema.eschema(self._cw.describe(self.eidfrom)[0]) |
3659 | 284 |
execute = self._cw.unsafe_execute |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
285 |
for rel in self.subject_relations: |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
286 |
if rel in eschema.subjrels: |
3090
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
287 |
execute('SET R %s P WHERE X eid %%(x)s, P eid %%(p)s, ' |
3659 | 288 |
'X %s R, NOT R %s P' % (self.rtype, rel, self.rtype), |
289 |
{'x': self.eidfrom, 'p': self.eidto}, 'x') |
|
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
290 |
for rel in self.object_relations: |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
291 |
if rel in eschema.objrels: |
3090
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
292 |
execute('SET R %s P WHERE X eid %%(x)s, P eid %%(p)s, ' |
3659 | 293 |
'R %s X, NOT R %s P' % (self.rtype, rel, self.rtype), |
294 |
{'x': self.eidfrom, 'p': self.eidto}, 'x') |
|
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
295 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
296 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
297 |
class PropagateSubjectRelationDelHook(Hook): |
4522
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
298 |
"""propagate to entities at the end of watched relations when a `main_rtype` |
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
299 |
relation is deleted |
271f201e3735
propagation hooks cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
300 |
""" |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
301 |
events = ('after_delete_relation',) |
3660
7b41a6ba7400
fix/prepare propagation hooks usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
302 |
|
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
303 |
# to set in concrete class |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
304 |
subject_relations = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
305 |
object_relations = None |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
306 |
|
3659 | 307 |
def __call__(self): |
4104
7e478d7caf20
[mq]: small_api_changes_for_3.6
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
4086
diff
changeset
|
308 |
eschema = self._cw.vreg.schema.eschema(self._cw.describe(self.eidfrom)[0]) |
3659 | 309 |
execute = self._cw.unsafe_execute |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
310 |
for rel in self.subject_relations: |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
311 |
if rel in eschema.subjrels: |
3090
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
312 |
execute('DELETE R %s P WHERE X eid %%(x)s, P eid %%(p)s, ' |
3659 | 313 |
'X %s R' % (self.rtype, rel), |
314 |
{'x': self.eidfrom, 'p': self.eidto}, 'x') |
|
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
315 |
for rel in self.object_relations: |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
316 |
if rel in eschema.objrels: |
3090
8184bec7414d
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3087
diff
changeset
|
317 |
execute('DELETE R %s P WHERE X eid %%(x)s, P eid %%(p)s, ' |
3659 | 318 |
'R %s X' % (self.rtype, rel), |
319 |
{'x': self.eidfrom, 'p': self.eidto}, 'x') |
|
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
320 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
321 |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
322 |
# abstract classes for operation ############################################### |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
323 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
324 |
class Operation(object): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
325 |
"""an operation is triggered on connections pool events related to |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
326 |
commit / rollback transations. Possible events are: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
327 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
328 |
precommit: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
329 |
the pool is preparing to commit. You shouldn't do anything things which |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
330 |
has to be reverted if the commit fail at this point, but you can freely |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
331 |
do any heavy computation or raise an exception if the commit can't go. |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
332 |
You can add some new operation during this phase but their precommit |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
333 |
event won't be triggered |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
334 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
335 |
commit: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
336 |
the pool is preparing to commit. You should avoid to do to expensive |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
337 |
stuff or something that may cause an exception in this event |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
338 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
339 |
revertcommit: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
340 |
if an operation failed while commited, this event is triggered for |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
341 |
all operations which had their commit event already to let them |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
342 |
revert things (including the operation which made fail the commit) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
343 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
344 |
rollback: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
345 |
the transaction has been either rollbacked either |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
346 |
* intentionaly |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
347 |
* a precommit event failed, all operations are rollbacked |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
348 |
* a commit event failed, all operations which are not been triggered for |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
349 |
commit are rollbacked |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
350 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
351 |
order of operations may be important, and is controlled according to: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
352 |
* operation's class |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
353 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
354 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
355 |
def __init__(self, session, **kwargs): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
356 |
self.session = session |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
357 |
self.__dict__.update(kwargs) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
358 |
self.register(session) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
359 |
# execution information |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
360 |
self.processed = None # 'precommit', 'commit' |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
361 |
self.failed = False |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
362 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
363 |
def register(self, session): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
364 |
session.add_operation(self, self.insert_index()) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
365 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
366 |
def insert_index(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
367 |
"""return the index of the lastest instance which is not a |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
368 |
LateOperation instance |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
369 |
""" |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
370 |
# faster by inspecting operation in reverse order for heavy transactions |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
371 |
i = None |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
372 |
for i, op in enumerate(reversed(self.session.pending_operations)): |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
373 |
if isinstance(op, (LateOperation, SingleLastOperation)): |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
374 |
continue |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
375 |
return -i or None |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
376 |
if i is None: |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
377 |
return None |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
378 |
return -(i + 1) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
379 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
380 |
def handle_event(self, event): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
381 |
"""delegate event handling to the opertaion""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
382 |
getattr(self, event)() |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
383 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
384 |
def precommit_event(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
385 |
"""the observed connections pool is preparing a commit""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
386 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
387 |
def revertprecommit_event(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
388 |
"""an error went when pre-commiting this operation or a later one |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
389 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
390 |
should revert pre-commit's changes but take care, they may have not |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
391 |
been all considered if it's this operation which failed |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
392 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
393 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
394 |
def commit_event(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
395 |
"""the observed connections pool is commiting""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
396 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
397 |
def revertcommit_event(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
398 |
"""an error went when commiting this operation or a later one |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
399 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
400 |
should revert commit's changes but take care, they may have not |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
401 |
been all considered if it's this operation which failed |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
402 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
403 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
404 |
def rollback_event(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
405 |
"""the observed connections pool has been rollbacked |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
406 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
407 |
do nothing by default, the operation will just be removed from the pool |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
408 |
operation list |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
409 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
410 |
|
3998
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3720
diff
changeset
|
411 |
def postcommit_event(self): |
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3720
diff
changeset
|
412 |
"""the observed connections pool has committed""" |
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3720
diff
changeset
|
413 |
|
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
414 |
@property |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
415 |
@deprecated('[3.6] use self.session.user') |
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
416 |
def user(self): |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
417 |
return self.session.user |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
418 |
|
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
419 |
@property |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
420 |
@deprecated('[3.6] use self.session.repo') |
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
421 |
def repo(self): |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
422 |
return self.session.repo |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
423 |
|
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
424 |
@property |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
425 |
@deprecated('[3.6] use self.session.vreg.schema') |
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
426 |
def schema(self): |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
427 |
return self.session.repo.schema |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
428 |
|
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
429 |
@property |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
430 |
@deprecated('[3.6] use self.session.vreg.config') |
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
431 |
def config(self): |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
432 |
return self.session.repo.config |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
433 |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
434 |
set_log_methods(Operation, getLogger('cubicweb.session')) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
435 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
436 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
437 |
class LateOperation(Operation): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
438 |
"""special operation which should be called after all possible (ie non late) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
439 |
operations |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
440 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
441 |
def insert_index(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
442 |
"""return the index of the lastest instance which is not a |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
443 |
SingleLastOperation instance |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
444 |
""" |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
445 |
# faster by inspecting operation in reverse order for heavy transactions |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
446 |
i = None |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
447 |
for i, op in enumerate(reversed(self.session.pending_operations)): |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
448 |
if isinstance(op, SingleLastOperation): |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
449 |
continue |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
450 |
return -i or None |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
451 |
if i is None: |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
452 |
return None |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
453 |
return -(i + 1) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
454 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
455 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
456 |
class SingleOperation(Operation): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
457 |
"""special operation which should be called once""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
458 |
def register(self, session): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
459 |
"""override register to handle cases where this operation has already |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
460 |
been added |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
461 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
462 |
operations = session.pending_operations |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
463 |
index = self.equivalent_index(operations) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
464 |
if index is not None: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
465 |
equivalent = operations.pop(index) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
466 |
else: |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
467 |
equivalent = None |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
468 |
session.add_operation(self, self.insert_index()) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
469 |
return equivalent |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
470 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
471 |
def equivalent_index(self, operations): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
472 |
"""return the index of the equivalent operation if any""" |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
473 |
for i, op in enumerate(reversed(operations)): |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
474 |
if op.__class__ is self.__class__: |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3660
diff
changeset
|
475 |
return -(i+1) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
476 |
return None |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
477 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
478 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
479 |
class SingleLastOperation(SingleOperation): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
480 |
"""special operation which should be called once and after all other |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
481 |
operations |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
482 |
""" |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
483 |
def insert_index(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
484 |
return None |
2840
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
485 |
|
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
486 |
|
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
487 |
class SendMailOp(SingleLastOperation): |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
488 |
def __init__(self, session, msg=None, recipients=None, **kwargs): |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
489 |
# may not specify msg yet, as |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
490 |
# `cubicweb.sobjects.supervision.SupervisionMailOp` |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
491 |
if msg is not None: |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
492 |
assert recipients |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
493 |
self.to_send = [(msg, recipients)] |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
494 |
else: |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
495 |
assert recipients is None |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
496 |
self.to_send = [] |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
497 |
super(SendMailOp, self).__init__(session, **kwargs) |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
498 |
|
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
499 |
def register(self, session): |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
500 |
previous = super(SendMailOp, self).register(session) |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
501 |
if previous: |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
502 |
self.to_send = previous.to_send + self.to_send |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
503 |
|
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
504 |
def commit_event(self): |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3396
diff
changeset
|
505 |
self.session.repo.threaded_task(self.sendmails) |
2840
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
506 |
|
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
507 |
def sendmails(self): |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3396
diff
changeset
|
508 |
self.session.vreg.config.sendmails(self.to_send) |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
509 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
510 |
|
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
511 |
class RQLPrecommitOperation(Operation): |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
512 |
def precommit_event(self): |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
513 |
execute = self.session.unsafe_execute |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
514 |
for rql in self.rqls: |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2855
diff
changeset
|
515 |
execute(*rql) |