author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 10 Apr 2012 16:59:50 +0200 | |
branch | stable |
changeset 8348 | 1a88d201675c |
parent 8234 | 83fd9ff90a9d |
child 8236 | cc70da744f43 |
permissions | -rw-r--r-- |
7879
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6518
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5081
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""CubicWeb is a generic framework to quickly build applications which describes |
19 |
relations between entitites. |
|
5881
57387070f612
[selectors] use before-registry-reset event to init is_instance cache: cleaner code and avoid reloading bug (making hooks test fail for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5451
diff
changeset
|
20 |
""" |
8132
460472499d6d
missing import statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8131
diff
changeset
|
21 |
from __future__ import with_statement |
0 | 22 |
|
23 |
__docformat__ = "restructuredtext en" |
|
24 |
||
5451
7b2e9f774028
ignore annoying warnings about pygments already being imported and stuff being added to python path
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5424
diff
changeset
|
25 |
# ignore the pygments UserWarnings |
7b2e9f774028
ignore annoying warnings about pygments already being imported and stuff being added to python path
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5424
diff
changeset
|
26 |
import warnings |
7b2e9f774028
ignore annoying warnings about pygments already being imported and stuff being added to python path
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5424
diff
changeset
|
27 |
warnings.filterwarnings('ignore', category=UserWarning, |
7b2e9f774028
ignore annoying warnings about pygments already being imported and stuff being added to python path
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5424
diff
changeset
|
28 |
message='.*was already imported', |
7b2e9f774028
ignore annoying warnings about pygments already being imported and stuff being added to python path
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5424
diff
changeset
|
29 |
module='.*pygments') |
7b2e9f774028
ignore annoying warnings about pygments already being imported and stuff being added to python path
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5424
diff
changeset
|
30 |
|
7b2e9f774028
ignore annoying warnings about pygments already being imported and stuff being added to python path
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5424
diff
changeset
|
31 |
|
0 | 32 |
import __builtin__ |
33 |
# '_' is available in builtins to mark internationalized string but should |
|
34 |
# not be used to do the actual translation |
|
35 |
if not hasattr(__builtin__, '_'): |
|
36 |
__builtin__._ = unicode |
|
37 |
||
38 |
CW_SOFTWARE_ROOT = __path__[0] |
|
39 |
||
40 |
import sys, os, logging |
|
41 |
from StringIO import StringIO |
|
42 |
||
1741 | 43 |
from logilab.common.logging_ext import set_log_methods |
0 | 44 |
|
3669
4eb33ee29c84
nicer create_entity implementation (and test)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3667
diff
changeset
|
45 |
|
0 | 46 |
if os.environ.get('APYCOT_ROOT'): |
47 |
logging.basicConfig(level=logging.CRITICAL) |
|
48 |
else: |
|
49 |
logging.basicConfig() |
|
50 |
||
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2790
diff
changeset
|
51 |
from cubicweb.__pkginfo__ import version as __version__ |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2790
diff
changeset
|
52 |
|
0 | 53 |
|
54 |
set_log_methods(sys.modules[__name__], logging.getLogger('cubicweb')) |
|
55 |
||
56 |
# make all exceptions accessible from the package |
|
57 |
from cubicweb._exceptions import * |
|
58 |
||
59 |
# convert eid to the right type, raise ValueError if it's not a valid eid |
|
60 |
typed_eid = int |
|
61 |
||
62 |
||
63 |
#def log_thread(f, w, a): |
|
64 |
# print f.f_code.co_filename, f.f_code.co_name |
|
65 |
#import threading |
|
66 |
#threading.settrace(log_thread) |
|
67 |
||
68 |
class Binary(StringIO): |
|
69 |
"""customize StringIO to make sure we don't use unicode""" |
|
1954 | 70 |
def __init__(self, buf=''): |
0 | 71 |
assert isinstance(buf, (str, buffer)), \ |
72 |
"Binary objects must use raw strings, not %s" % buf.__class__ |
|
73 |
StringIO.__init__(self, buf) |
|
74 |
||
75 |
def write(self, data): |
|
76 |
assert isinstance(data, (str, buffer)), \ |
|
77 |
"Binary objects must use raw strings, not %s" % data.__class__ |
|
78 |
StringIO.write(self, data) |
|
79 |
||
8131
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
80 |
def to_file(self, filename): |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
81 |
"""write a binary to disk |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
82 |
|
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
83 |
the writing is performed in a safe way for files stored on |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
84 |
Windows SMB shares |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
85 |
""" |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
86 |
pos = self.tell() |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
87 |
with open(filename, 'wb') as fobj: |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
88 |
self.seek(0) |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
89 |
if sys.platform == 'win32': |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
90 |
while True: |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
91 |
# the 16kB chunksize comes from the shutil module |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
92 |
# in stdlib |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
93 |
chunk = self.read(16*1024) |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
94 |
if not chunk: |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
95 |
break |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
96 |
fobj.write(chunk) |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
97 |
else: |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
98 |
fobj.write(self.read()) |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
99 |
self.seek(pos) |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
100 |
|
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
101 |
@staticmethod |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
102 |
def from_file(filename): |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
103 |
"""read a file and returns its contents in a Binary |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
104 |
|
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
105 |
the reading is performed in a safe way for files stored on |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
106 |
Windows SMB shares |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
107 |
""" |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
108 |
binary = Binary() |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
109 |
with open(filename, 'rb') as fobj: |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
110 |
if sys.platform == 'win32': |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
111 |
while True: |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
112 |
# the 16kB chunksize comes from the shutil module |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
113 |
# in stdlib |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
114 |
chunk = fobj.read(16*1024) |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
115 |
if not chunk: |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
116 |
break |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
117 |
binary.write(chunk) |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
118 |
else: |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
119 |
binary.write(fobj.read()) |
8234
83fd9ff90a9d
[binary] use seek(0) in `from_file` Binary creation method (closes #2189183)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
8132
diff
changeset
|
120 |
binary.seek(0) |
8131
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
121 |
return binary |
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
122 |
|
a6654712ad50
fix potential problems when BFSS uses a Windows SMB share (closes #2131435)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7879
diff
changeset
|
123 |
|
4475
37c413a07216
kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
124 |
# use this dictionary to rename entity types while keeping bw compat |
37c413a07216
kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
125 |
ETYPE_NAME_MAP = {} |
0 | 126 |
|
4475
37c413a07216
kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
127 |
# XXX cubic web cube migration map. See if it's worth keeping this mecanism |
37c413a07216
kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
128 |
# to help in cube renaming |
37c413a07216
kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
129 |
CW_MIGRATION_MAP = {} |
231
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
130 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
131 |
def neg_role(role): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
132 |
if role == 'subject': |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
133 |
return 'object' |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
134 |
return 'subject' |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
135 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
136 |
def role(obj): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
137 |
try: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
138 |
return obj.role |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
139 |
except AttributeError: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
140 |
return neg_role(obj.target) |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
141 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
142 |
def target(obj): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
143 |
try: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
144 |
return obj.target |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
145 |
except AttributeError: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
146 |
return neg_role(obj.role) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
147 |
|
2683
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
148 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
149 |
class CubicWebEventManager(object): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
150 |
"""simple event / callback manager. |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
151 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
152 |
Typical usage to register a callback:: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
153 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
154 |
>>> from cubicweb import CW_EVENT_MANAGER |
2705
30bcdbd92820
[events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2689
diff
changeset
|
155 |
>>> CW_EVENT_MANAGER.bind('after-registry-reload', mycallback) |
2683
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
156 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
157 |
Typical usage to emit an event:: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
158 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
159 |
>>> from cubicweb import CW_EVENT_MANAGER |
2705
30bcdbd92820
[events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2689
diff
changeset
|
160 |
>>> CW_EVENT_MANAGER.emit('after-registry-reload') |
2683
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
161 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
162 |
emit() accepts an additional context parameter that will be passed |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
163 |
to the callback if specified (and only in that case) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
164 |
""" |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
165 |
def __init__(self): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
166 |
self.callbacks = {} |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
167 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
168 |
def bind(self, event, callback, *args, **kwargs): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
169 |
self.callbacks.setdefault(event, []).append( (callback, args, kwargs) ) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
170 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
171 |
def emit(self, event, context=None): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
172 |
for callback, args, kwargs in self.callbacks.get(event, ()): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
173 |
if context is None: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
174 |
callback(*args, **kwargs) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
175 |
else: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
176 |
callback(context, *args, **kwargs) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
177 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
178 |
CW_EVENT_MANAGER = CubicWebEventManager() |
2689
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
179 |
|
5081
2ea98b8512dd
[events manager] onevent decorator may be given extra arguments to give to em.bind
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
180 |
def onevent(event, *args, **kwargs): |
2689
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
181 |
"""decorator to ease event / callback binding |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
182 |
|
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
183 |
>>> from cubicweb import onevent |
2705
30bcdbd92820
[events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2689
diff
changeset
|
184 |
>>> @onevent('before-registry-reload') |
2689
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
185 |
... def mycallback(): |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
186 |
... print 'hello' |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
187 |
... |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
188 |
>>> |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
189 |
""" |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
190 |
def _decorator(func): |
5081
2ea98b8512dd
[events manager] onevent decorator may be given extra arguments to give to em.bind
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
191 |
CW_EVENT_MANAGER.bind(event, func, *args, **kwargs) |
2689
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
192 |
return func |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
193 |
return _decorator |