author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 23 Sep 2009 09:35:28 +0200 | |
changeset 3382 | 435286b8fe10 |
parent 3369 | 7b88d12b4ee2 |
child 3401 | f893f4f199fc |
permissions | -rw-r--r-- |
0 | 1 |
"""classes to define schemas for CubicWeb |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1498
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1498
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
2142
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
9 |
_ = unicode |
0 | 10 |
|
11 |
import re |
|
2147
476a75ede2cc
merge and add missing import in schema.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2142
diff
changeset
|
12 |
from os.path import join |
0 | 13 |
from logging import getLogger |
1133 | 14 |
from warnings import warn |
0 | 15 |
|
624
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
16 |
from logilab.common.decorators import cached, clear_cache, monkeypatch |
2730
bb6fcb8c5d71
to make cw schemas importable, they have to be installed w/ cw code, not in /usr/share/cubicweb/schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2718
diff
changeset
|
17 |
from logilab.common.logging_ext import set_log_methods |
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2531
diff
changeset
|
18 |
from logilab.common.deprecation import deprecated |
2926
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
19 |
from logilab.common.graph import get_cycles |
0 | 20 |
from logilab.common.compat import any |
21 |
||
2531
531ea4e7013e
[cleanup] nicer imports
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2526
diff
changeset
|
22 |
from yams import BadSchemaDefinition, buildobjs as ybo |
0 | 23 |
from yams.schema import Schema, ERSchema, EntitySchema, RelationSchema |
2531
531ea4e7013e
[cleanup] nicer imports
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2526
diff
changeset
|
24 |
from yams.constraints import (BaseConstraint, StaticVocabularyConstraint, |
531ea4e7013e
[cleanup] nicer imports
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2526
diff
changeset
|
25 |
FormatConstraint) |
531ea4e7013e
[cleanup] nicer imports
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2526
diff
changeset
|
26 |
from yams.reader import (CONSTRAINTS, PyFileReader, SchemaLoader, |
531ea4e7013e
[cleanup] nicer imports
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2526
diff
changeset
|
27 |
obsolete as yobsolete, cleanup_sys_modules) |
0 | 28 |
|
29 |
from rql import parse, nodes, RQLSyntaxError, TypeResolverException |
|
30 |
||
2730
bb6fcb8c5d71
to make cw schemas importable, they have to be installed w/ cw code, not in /usr/share/cubicweb/schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2718
diff
changeset
|
31 |
import cubicweb |
0 | 32 |
from cubicweb import ETYPE_NAME_MAP, ValidationError, Unauthorized |
33 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
745
diff
changeset
|
34 |
# XXX <3.2 bw compat |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
745
diff
changeset
|
35 |
from yams import schema |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
745
diff
changeset
|
36 |
schema.use_py_datetime() |
1451 | 37 |
nodes.use_py_datetime() |
0 | 38 |
|
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2531
diff
changeset
|
39 |
PURE_VIRTUAL_RTYPES = set(('identity', 'has_text',)) |
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2531
diff
changeset
|
40 |
VIRTUAL_RTYPES = set(('eid', 'identity', 'has_text',)) |
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2531
diff
changeset
|
41 |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
42 |
# set of meta-relations available for every entity types |
2622
3c7edaa6c6d2
oops, should have been in a earlier commit (META_RELATION_TYPES renaming)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
43 |
META_RTYPES = set(( |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
44 |
'owned_by', 'created_by', 'is', 'is_instance_of', 'identity', |
2456
aa25d6b244c8
new cwuri metadata + a few tests fixes on the way
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2437
diff
changeset
|
45 |
'eid', 'creation_date', 'modification_date', 'has_text', 'cwuri', |
2184 | 46 |
)) |
3287
19c1011736a6
tweak catalog generation by adding a new SYSTEM_RTYPES set to schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3284
diff
changeset
|
47 |
SYSTEM_RTYPES = set(('require_permission', 'custom_workflow', 'in_state', 'wf_info_for')) |
0 | 48 |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
49 |
# set of entity and relation types used to build the schema |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
50 |
SCHEMA_TYPES = set(( |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
51 |
'CWEType', 'CWRType', 'CWAttribute', 'CWRelation', |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
52 |
'CWConstraint', 'CWConstraintType', 'RQLExpression', |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
53 |
'relation_type', 'from_entity', 'to_entity', |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
54 |
'constrained_by', 'cstrtype', |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
55 |
)) |
0 | 56 |
|
2142
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
57 |
_LOGGER = getLogger('cubicweb.schemaloader') |
0 | 58 |
|
59 |
# schema entities created from serialized schema have an eid rproperty |
|
60 |
ybo.ETYPE_PROPERTIES += ('eid',) |
|
61 |
ybo.RTYPE_PROPERTIES += ('eid',) |
|
62 |
ybo.RDEF_PROPERTIES += ('eid',) |
|
63 |
||
2926
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
64 |
|
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
65 |
# XXX same algorithm as in reorder_cubes and probably other place, |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
66 |
# may probably extract a generic function |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
67 |
def order_eschemas(eschemas): |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
68 |
"""return entity schemas ordered such that entity types which specializes an |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
69 |
other one appears after that one |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
70 |
""" |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
71 |
graph = {} |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
72 |
for eschema in eschemas: |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
73 |
if eschema.specializes(): |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
74 |
graph[eschema] = set((eschema.specializes(),)) |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
75 |
else: |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
76 |
graph[eschema] = set() |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
77 |
cycles = get_cycles(graph) |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
78 |
if cycles: |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
79 |
cycles = '\n'.join(' -> '.join(cycle) for cycle in cycles) |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
80 |
raise Exception('cycles in entity schema specialization: %s' |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
81 |
% cycles) |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
82 |
eschemas = [] |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
83 |
while graph: |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
84 |
# sorted to get predictable results |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
85 |
for eschema, deps in sorted(graph.items()): |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
86 |
if not deps: |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
87 |
eschemas.append(eschema) |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
88 |
del graph[eschema] |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
89 |
for deps in graph.itervalues(): |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
90 |
try: |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
91 |
deps.remove(eschema) |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
92 |
except KeyError: |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
93 |
continue |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
94 |
return eschemas |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
95 |
|
0 | 96 |
def bw_normalize_etype(etype): |
97 |
if etype in ETYPE_NAME_MAP: |
|
98 |
msg = '%s has been renamed to %s, please update your code' % ( |
|
1451 | 99 |
etype, ETYPE_NAME_MAP[etype]) |
0 | 100 |
warn(msg, DeprecationWarning, stacklevel=4) |
101 |
etype = ETYPE_NAME_MAP[etype] |
|
102 |
return etype |
|
103 |
||
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
104 |
def display_name(req, key, form='', context=None): |
0 | 105 |
"""return a internationalized string for the key (schema entity or relation |
106 |
name) in a given form |
|
107 |
""" |
|
108 |
assert form in ('', 'plural', 'subject', 'object') |
|
109 |
if form == 'subject': |
|
110 |
form = '' |
|
111 |
if form: |
|
112 |
key = key + '_' + form |
|
113 |
# ensure unicode |
|
3284
036cf5a25714
ensure pgettext return unicode as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
114 |
# .lower() in case no translation are available XXX done whatever a translation is there or not! |
036cf5a25714
ensure pgettext return unicode as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
115 |
if context is not None: |
036cf5a25714
ensure pgettext return unicode as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
116 |
return unicode(req.pgettext(context, key)).lower() |
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
117 |
else: |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
118 |
return unicode(req._(key)).lower() |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
119 |
|
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2782
diff
changeset
|
120 |
__builtins__['display_name'] = deprecated('[3.4] display_name should be imported from cubicweb.schema')(display_name) |
0 | 121 |
|
122 |
def ERSchema_display_name(self, req, form=''): |
|
123 |
"""return a internationalized string for the entity/relation type name in |
|
124 |
a given form |
|
125 |
""" |
|
126 |
return display_name(req, self.type, form) |
|
127 |
ERSchema.display_name = ERSchema_display_name |
|
128 |
||
129 |
@cached |
|
130 |
def ERSchema_get_groups(self, action): |
|
131 |
"""return the groups authorized to perform <action> on entities of |
|
132 |
this type |
|
133 |
||
134 |
:type action: str |
|
135 |
:param action: the name of a permission |
|
136 |
||
137 |
:rtype: tuple |
|
138 |
:return: names of the groups with the given permission |
|
139 |
""" |
|
140 |
assert action in self.ACTIONS, action |
|
141 |
#assert action in self._groups, '%s %s' % (self, action) |
|
142 |
try: |
|
143 |
return frozenset(g for g in self._groups[action] if isinstance(g, basestring)) |
|
144 |
except KeyError: |
|
145 |
return () |
|
146 |
ERSchema.get_groups = ERSchema_get_groups |
|
147 |
||
148 |
def ERSchema_set_groups(self, action, groups): |
|
149 |
"""set the groups allowed to perform <action> on entities of this type. Don't |
|
150 |
change rql expressions for the same action. |
|
151 |
||
152 |
:type action: str |
|
153 |
:param action: the name of a permission |
|
154 |
||
155 |
:type groups: list or tuple |
|
156 |
:param groups: names of the groups granted to do the given action |
|
157 |
""" |
|
158 |
assert action in self.ACTIONS, action |
|
159 |
clear_cache(self, 'ERSchema_get_groups') |
|
160 |
self._groups[action] = tuple(groups) + self.get_rqlexprs(action) |
|
161 |
ERSchema.set_groups = ERSchema_set_groups |
|
162 |
||
163 |
@cached |
|
164 |
def ERSchema_get_rqlexprs(self, action): |
|
165 |
"""return the rql expressions representing queries to check the user is allowed |
|
166 |
to perform <action> on entities of this type |
|
167 |
||
168 |
:type action: str |
|
169 |
:param action: the name of a permission |
|
170 |
||
171 |
:rtype: tuple |
|
172 |
:return: the rql expressions with the given permission |
|
173 |
""" |
|
174 |
assert action in self.ACTIONS, action |
|
175 |
#assert action in self._rqlexprs, '%s %s' % (self, action) |
|
176 |
try: |
|
177 |
return tuple(g for g in self._groups[action] if not isinstance(g, basestring)) |
|
178 |
except KeyError: |
|
179 |
return () |
|
180 |
ERSchema.get_rqlexprs = ERSchema_get_rqlexprs |
|
181 |
||
182 |
def ERSchema_set_rqlexprs(self, action, rqlexprs): |
|
183 |
"""set the rql expression allowing to perform <action> on entities of this type. Don't |
|
184 |
change groups for the same action. |
|
185 |
||
186 |
:type action: str |
|
187 |
:param action: the name of a permission |
|
188 |
||
189 |
:type rqlexprs: list or tuple |
|
190 |
:param rqlexprs: the rql expressions allowing the given action |
|
191 |
""" |
|
192 |
assert action in self.ACTIONS, action |
|
193 |
clear_cache(self, 'ERSchema_get_rqlexprs') |
|
194 |
self._groups[action] = tuple(self.get_groups(action)) + tuple(rqlexprs) |
|
195 |
ERSchema.set_rqlexprs = ERSchema_set_rqlexprs |
|
196 |
||
197 |
def ERSchema_set_permissions(self, action, permissions): |
|
198 |
"""set the groups and rql expressions allowing to perform <action> on |
|
199 |
entities of this type |
|
200 |
||
201 |
:type action: str |
|
202 |
:param action: the name of a permission |
|
203 |
||
204 |
:type permissions: tuple |
|
205 |
:param permissions: the groups and rql expressions allowing the given action |
|
206 |
""" |
|
207 |
assert action in self.ACTIONS, action |
|
208 |
clear_cache(self, 'ERSchema_get_rqlexprs') |
|
209 |
clear_cache(self, 'ERSchema_get_groups') |
|
210 |
self._groups[action] = tuple(permissions) |
|
211 |
ERSchema.set_permissions = ERSchema_set_permissions |
|
212 |
||
213 |
def ERSchema_has_perm(self, session, action, *args, **kwargs): |
|
214 |
"""return true if the action is granted globaly or localy""" |
|
215 |
try: |
|
216 |
self.check_perm(session, action, *args, **kwargs) |
|
217 |
return True |
|
218 |
except Unauthorized: |
|
219 |
return False |
|
220 |
ERSchema.has_perm = ERSchema_has_perm |
|
221 |
||
222 |
def ERSchema_has_local_role(self, action): |
|
223 |
"""return true if the action *may* be granted localy (eg either rql |
|
224 |
expressions or the owners group are used in security definition) |
|
225 |
||
226 |
XXX this method is only there since we don't know well how to deal with |
|
227 |
'add' action checking. Also find a better name would be nice. |
|
228 |
""" |
|
229 |
assert action in self.ACTIONS, action |
|
230 |
if self.get_rqlexprs(action): |
|
231 |
return True |
|
232 |
if action in ('update', 'delete'): |
|
233 |
return self.has_group(action, 'owners') |
|
234 |
return False |
|
235 |
ERSchema.has_local_role = ERSchema_has_local_role |
|
236 |
||
237 |
||
238 |
def system_etypes(schema): |
|
239 |
"""return system entity types only: skip final, schema and application entities |
|
240 |
""" |
|
241 |
for eschema in schema.entities(): |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
242 |
if eschema.is_final() or eschema.schema_entity(): |
0 | 243 |
continue |
244 |
yield eschema.type |
|
245 |
||
246 |
# Schema objects definition ################################################### |
|
247 |
||
248 |
class CubicWebEntitySchema(EntitySchema): |
|
249 |
"""a entity has a type, a set of subject and or object relations |
|
250 |
the entity schema defines the possible relations for a given type and some |
|
251 |
constraints on those relations |
|
252 |
""" |
|
253 |
def __init__(self, schema=None, edef=None, eid=None, **kwargs): |
|
254 |
super(CubicWebEntitySchema, self).__init__(schema, edef, **kwargs) |
|
255 |
if eid is None and edef is not None: |
|
256 |
eid = getattr(edef, 'eid', None) |
|
257 |
self.eid = eid |
|
258 |
# take care: no _groups attribute when deep-copying |
|
1451 | 259 |
if getattr(self, '_groups', None): |
0 | 260 |
for groups in self._groups.itervalues(): |
261 |
for group_or_rqlexpr in groups: |
|
262 |
if isinstance(group_or_rqlexpr, RRQLExpression): |
|
263 |
msg = "can't use RRQLExpression on an entity type, use an ERQLExpression (%s)" |
|
264 |
raise BadSchemaDefinition(msg % self.type) |
|
1451 | 265 |
|
0 | 266 |
def attribute_definitions(self): |
267 |
"""return an iterator on attribute definitions |
|
1451 | 268 |
|
0 | 269 |
attribute relations are a subset of subject relations where the |
270 |
object's type is a final entity |
|
1451 | 271 |
|
0 | 272 |
an attribute definition is a 2-uple : |
273 |
* name of the relation |
|
274 |
* schema of the destination entity type |
|
275 |
""" |
|
276 |
iter = super(CubicWebEntitySchema, self).attribute_definitions() |
|
277 |
for rschema, attrschema in iter: |
|
278 |
if rschema.type == 'has_text': |
|
279 |
continue |
|
280 |
yield rschema, attrschema |
|
1451 | 281 |
|
2128
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
282 |
def main_attribute(self): |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
283 |
"""convenience method that returns the *main* (i.e. the first non meta) |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
284 |
attribute defined in the entity schema |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
285 |
""" |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
286 |
for rschema, _ in self.attribute_definitions(): |
2622
3c7edaa6c6d2
oops, should have been in a earlier commit (META_RELATION_TYPES renaming)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
287 |
if not (rschema in META_RTYPES |
2128
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
288 |
or self.is_metadata(rschema)): |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
289 |
return rschema |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
290 |
|
0 | 291 |
def add_subject_relation(self, rschema): |
292 |
"""register the relation schema as possible subject relation""" |
|
293 |
super(CubicWebEntitySchema, self).add_subject_relation(rschema) |
|
294 |
self._update_has_text() |
|
295 |
||
296 |
def del_subject_relation(self, rtype): |
|
297 |
super(CubicWebEntitySchema, self).del_subject_relation(rtype) |
|
2632
920bfaff60a4
B [schema update] may remove has_text unexpectedly, give another argument to fix it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2622
diff
changeset
|
298 |
self._update_has_text(True) |
1451 | 299 |
|
2632
920bfaff60a4
B [schema update] may remove has_text unexpectedly, give another argument to fix it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2622
diff
changeset
|
300 |
def _update_has_text(self, deletion=False): |
0 | 301 |
may_need_has_text, has_has_text = False, False |
2632
920bfaff60a4
B [schema update] may remove has_text unexpectedly, give another argument to fix it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2622
diff
changeset
|
302 |
need_has_text = None |
0 | 303 |
for rschema in self.subject_relations(): |
304 |
if rschema.is_final(): |
|
305 |
if rschema == 'has_text': |
|
306 |
has_has_text = True |
|
307 |
elif self.rproperty(rschema, 'fulltextindexed'): |
|
308 |
may_need_has_text = True |
|
309 |
elif rschema.fulltext_container: |
|
310 |
if rschema.fulltext_container == 'subject': |
|
311 |
may_need_has_text = True |
|
312 |
else: |
|
313 |
need_has_text = False |
|
314 |
for rschema in self.object_relations(): |
|
315 |
if rschema.fulltext_container: |
|
316 |
if rschema.fulltext_container == 'object': |
|
317 |
may_need_has_text = True |
|
318 |
else: |
|
319 |
need_has_text = False |
|
320 |
if need_has_text is None: |
|
321 |
need_has_text = may_need_has_text |
|
2632
920bfaff60a4
B [schema update] may remove has_text unexpectedly, give another argument to fix it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2622
diff
changeset
|
322 |
if need_has_text and not has_has_text and not deletion: |
0 | 323 |
rdef = ybo.RelationDefinition(self.type, 'has_text', 'String') |
324 |
self.schema.add_relation_def(rdef) |
|
325 |
elif not need_has_text and has_has_text: |
|
326 |
self.schema.del_relation_def(self.type, 'has_text', 'String') |
|
1451 | 327 |
|
0 | 328 |
def schema_entity(self): |
329 |
"""return True if this entity type is used to build the schema""" |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
330 |
return self.type in SCHEMA_TYPES |
0 | 331 |
|
332 |
def check_perm(self, session, action, eid=None): |
|
333 |
# NB: session may be a server session or a request object |
|
334 |
user = session.user |
|
335 |
# check user is in an allowed group, if so that's enough |
|
336 |
# internal sessions should always stop there |
|
337 |
if user.matching_groups(self.get_groups(action)): |
|
338 |
return |
|
339 |
# if 'owners' in allowed groups, check if the user actually owns this |
|
340 |
# object, if so that's enough |
|
341 |
if eid is not None and 'owners' in self.get_groups(action) and \ |
|
342 |
user.owns(eid): |
|
343 |
return |
|
344 |
# else if there is some rql expressions, check them |
|
345 |
if any(rqlexpr.check(session, eid) |
|
346 |
for rqlexpr in self.get_rqlexprs(action)): |
|
1451 | 347 |
return |
0 | 348 |
raise Unauthorized(action, str(self)) |
349 |
||
350 |
def rql_expression(self, expression, mainvars=None, eid=None): |
|
351 |
"""rql expression factory""" |
|
352 |
return ERQLExpression(expression, mainvars, eid) |
|
1451 | 353 |
|
2252 | 354 |
|
0 | 355 |
class CubicWebRelationSchema(RelationSchema): |
356 |
RelationSchema._RPROPERTIES['eid'] = None |
|
357 |
_perms_checked = False |
|
1451 | 358 |
|
0 | 359 |
def __init__(self, schema=None, rdef=None, eid=None, **kwargs): |
360 |
if rdef is not None: |
|
361 |
# if this relation is inlined |
|
362 |
self.inlined = rdef.inlined |
|
363 |
super(CubicWebRelationSchema, self).__init__(schema, rdef, **kwargs) |
|
364 |
if eid is None and rdef is not None: |
|
365 |
eid = getattr(rdef, 'eid', None) |
|
366 |
self.eid = eid |
|
1451 | 367 |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
368 |
@property |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
369 |
def meta(self): |
2622
3c7edaa6c6d2
oops, should have been in a earlier commit (META_RELATION_TYPES renaming)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
370 |
return self.type in META_RTYPES |
1451 | 371 |
|
0 | 372 |
def update(self, subjschema, objschema, rdef): |
373 |
super(CubicWebRelationSchema, self).update(subjschema, objschema, rdef) |
|
374 |
if not self._perms_checked and self._groups: |
|
375 |
for action, groups in self._groups.iteritems(): |
|
376 |
for group_or_rqlexpr in groups: |
|
377 |
if action == 'read' and \ |
|
378 |
isinstance(group_or_rqlexpr, RQLExpression): |
|
379 |
msg = "can't use rql expression for read permission of "\ |
|
380 |
"a relation type (%s)" |
|
381 |
raise BadSchemaDefinition(msg % self.type) |
|
382 |
elif self.final and isinstance(group_or_rqlexpr, RRQLExpression): |
|
383 |
if self.schema.reading_from_database: |
|
384 |
# we didn't have final relation earlier, so turn |
|
385 |
# RRQLExpression into ERQLExpression now |
|
386 |
rqlexpr = group_or_rqlexpr |
|
387 |
newrqlexprs = [x for x in self.get_rqlexprs(action) if not x is rqlexpr] |
|
388 |
newrqlexprs.append(ERQLExpression(rqlexpr.expression, |
|
389 |
rqlexpr.mainvars, |
|
390 |
rqlexpr.eid)) |
|
1451 | 391 |
self.set_rqlexprs(action, newrqlexprs) |
0 | 392 |
else: |
393 |
msg = "can't use RRQLExpression on a final relation "\ |
|
394 |
"type (eg attribute relation), use an ERQLExpression (%s)" |
|
395 |
raise BadSchemaDefinition(msg % self.type) |
|
396 |
elif not self.final and \ |
|
397 |
isinstance(group_or_rqlexpr, ERQLExpression): |
|
398 |
msg = "can't use ERQLExpression on a relation type, use "\ |
|
399 |
"a RRQLExpression (%s)" |
|
400 |
raise BadSchemaDefinition(msg % self.type) |
|
401 |
self._perms_checked = True |
|
1451 | 402 |
|
0 | 403 |
def cardinality(self, subjtype, objtype, target): |
404 |
card = self.rproperty(subjtype, objtype, 'cardinality') |
|
405 |
return (target == 'subject' and card[0]) or \ |
|
406 |
(target == 'object' and card[1]) |
|
1451 | 407 |
|
0 | 408 |
def schema_relation(self): |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
409 |
"""return True if this relation type is used to build the schema""" |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
410 |
return self.type in SCHEMA_TYPES |
1451 | 411 |
|
0 | 412 |
def physical_mode(self): |
413 |
"""return an appropriate mode for physical storage of this relation type: |
|
414 |
* 'subjectinline' if every possible subject cardinalities are 1 or ? |
|
415 |
* 'objectinline' if 'subjectinline' mode is not possible but every |
|
416 |
possible object cardinalities are 1 or ? |
|
417 |
* None if neither 'subjectinline' and 'objectinline' |
|
418 |
""" |
|
419 |
assert not self.final |
|
420 |
return self.inlined and 'subjectinline' or None |
|
421 |
||
422 |
def check_perm(self, session, action, *args, **kwargs): |
|
423 |
# NB: session may be a server session or a request object check user is |
|
424 |
# in an allowed group, if so that's enough internal sessions should |
|
425 |
# always stop there |
|
426 |
if session.user.matching_groups(self.get_groups(action)): |
|
1451 | 427 |
return |
0 | 428 |
# else if there is some rql expressions, check them |
429 |
if any(rqlexpr.check(session, *args, **kwargs) |
|
430 |
for rqlexpr in self.get_rqlexprs(action)): |
|
431 |
return |
|
432 |
raise Unauthorized(action, str(self)) |
|
433 |
||
434 |
def rql_expression(self, expression, mainvars=None, eid=None): |
|
435 |
"""rql expression factory""" |
|
436 |
if self.is_final(): |
|
437 |
return ERQLExpression(expression, mainvars, eid) |
|
438 |
return RRQLExpression(expression, mainvars, eid) |
|
439 |
||
1451 | 440 |
|
0 | 441 |
class CubicWebSchema(Schema): |
442 |
"""set of entities and relations schema defining the possible data sets |
|
443 |
used in an application |
|
444 |
||
445 |
||
446 |
:type name: str |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2460
diff
changeset
|
447 |
:ivar name: name of the schema, usually the instance identifier |
1451 | 448 |
|
0 | 449 |
:type base: str |
450 |
:ivar base: path of the directory where the schema is defined |
|
451 |
""" |
|
1451 | 452 |
reading_from_database = False |
0 | 453 |
entity_class = CubicWebEntitySchema |
454 |
relation_class = CubicWebRelationSchema |
|
2958
44e5446b649b
no inference for the identity relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2926
diff
changeset
|
455 |
no_specialization_inference = ('identity',) |
0 | 456 |
|
457 |
def __init__(self, *args, **kwargs): |
|
458 |
self._eid_index = {} |
|
459 |
super(CubicWebSchema, self).__init__(*args, **kwargs) |
|
460 |
ybo.register_base_types(self) |
|
2300
c8151d004e06
meta has been drop
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
461 |
rschema = self.add_relation_type(ybo.RelationType('eid')) |
0 | 462 |
rschema.final = True |
463 |
rschema.set_default_groups() |
|
2300
c8151d004e06
meta has been drop
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
464 |
rschema = self.add_relation_type(ybo.RelationType('has_text')) |
0 | 465 |
rschema.final = True |
466 |
rschema.set_default_groups() |
|
2300
c8151d004e06
meta has been drop
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
467 |
rschema = self.add_relation_type(ybo.RelationType('identity')) |
0 | 468 |
rschema.final = False |
469 |
rschema.set_default_groups() |
|
1451 | 470 |
|
0 | 471 |
def add_entity_type(self, edef): |
472 |
edef.name = edef.name.encode() |
|
473 |
edef.name = bw_normalize_etype(edef.name) |
|
474 |
assert re.match(r'[A-Z][A-Za-z0-9]*[a-z]+[0-9]*$', edef.name), repr(edef.name) |
|
475 |
eschema = super(CubicWebSchema, self).add_entity_type(edef) |
|
476 |
if not eschema.is_final(): |
|
477 |
# automatically add the eid relation to non final entity types |
|
478 |
rdef = ybo.RelationDefinition(eschema.type, 'eid', 'Int', |
|
479 |
cardinality='11', uid=True) |
|
480 |
self.add_relation_def(rdef) |
|
481 |
rdef = ybo.RelationDefinition(eschema.type, 'identity', eschema.type) |
|
482 |
self.add_relation_def(rdef) |
|
483 |
self._eid_index[eschema.eid] = eschema |
|
484 |
return eschema |
|
1451 | 485 |
|
0 | 486 |
def add_relation_type(self, rdef): |
487 |
rdef.name = rdef.name.lower().encode() |
|
488 |
rschema = super(CubicWebSchema, self).add_relation_type(rdef) |
|
489 |
self._eid_index[rschema.eid] = rschema |
|
490 |
return rschema |
|
1451 | 491 |
|
0 | 492 |
def add_relation_def(self, rdef): |
493 |
"""build a part of a relation schema |
|
494 |
(i.e. add a relation between two specific entity's types) |
|
495 |
||
496 |
:type subject: str |
|
497 |
:param subject: entity's type that is subject of the relation |
|
498 |
||
499 |
:type rtype: str |
|
500 |
:param rtype: the relation's type (i.e. the name of the relation) |
|
501 |
||
502 |
:type obj: str |
|
503 |
:param obj: entity's type that is object of the relation |
|
504 |
||
505 |
:rtype: RelationSchema |
|
506 |
:param: the newly created or just completed relation schema |
|
507 |
""" |
|
508 |
rdef.name = rdef.name.lower() |
|
509 |
rdef.subject = bw_normalize_etype(rdef.subject) |
|
510 |
rdef.object = bw_normalize_etype(rdef.object) |
|
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
511 |
if super(CubicWebSchema, self).add_relation_def(rdef): |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
512 |
try: |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
513 |
self._eid_index[rdef.eid] = (self.eschema(rdef.subject), |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
514 |
self.rschema(rdef.name), |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
515 |
self.eschema(rdef.object)) |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
516 |
except AttributeError: |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
517 |
pass # not a serialized schema |
1451 | 518 |
|
0 | 519 |
def del_relation_type(self, rtype): |
520 |
rschema = self.rschema(rtype) |
|
521 |
self._eid_index.pop(rschema.eid, None) |
|
522 |
super(CubicWebSchema, self).del_relation_type(rtype) |
|
1451 | 523 |
|
0 | 524 |
def del_relation_def(self, subjtype, rtype, objtype): |
525 |
for k, v in self._eid_index.items(): |
|
526 |
if v == (subjtype, rtype, objtype): |
|
527 |
del self._eid_index[k] |
|
2718
3a56b87bb5d6
[schema] break when found
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2716
diff
changeset
|
528 |
break |
0 | 529 |
super(CubicWebSchema, self).del_relation_def(subjtype, rtype, objtype) |
1451 | 530 |
|
0 | 531 |
def del_entity_type(self, etype): |
532 |
eschema = self.eschema(etype) |
|
533 |
self._eid_index.pop(eschema.eid, None) |
|
534 |
# deal with has_text first, else its automatic deletion (see above) |
|
535 |
# may trigger an error in ancestor's del_entity_type method |
|
536 |
if 'has_text' in eschema.subject_relations(): |
|
537 |
self.del_relation_def(etype, 'has_text', 'String') |
|
538 |
super(CubicWebSchema, self).del_entity_type(etype) |
|
1451 | 539 |
|
0 | 540 |
def schema_by_eid(self, eid): |
541 |
return self._eid_index[eid] |
|
542 |
||
543 |
||
544 |
# Possible constraints ######################################################## |
|
545 |
||
546 |
class RQLVocabularyConstraint(BaseConstraint): |
|
547 |
"""the rql vocabulary constraint : |
|
548 |
||
549 |
limit the proposed values to a set of entities returned by a rql query, |
|
550 |
but this is not enforced at the repository level |
|
1451 | 551 |
|
0 | 552 |
restriction is additional rql restriction that will be added to |
553 |
a predefined query, where the S and O variables respectivly represent |
|
554 |
the subject and the object of the relation |
|
555 |
""" |
|
1451 | 556 |
|
0 | 557 |
def __init__(self, restriction): |
558 |
self.restriction = restriction |
|
559 |
||
2716
2f6edb0e9626
[schema] yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2691
diff
changeset
|
560 |
def check_consistency(self, subjschema, objschema, rdef): |
2691
ade319674170
add consistency check on constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2632
diff
changeset
|
561 |
if objschema.is_final(): |
ade319674170
add consistency check on constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2632
diff
changeset
|
562 |
raise BadSchemaDefinition("unique constraint doesn't apply to " |
ade319674170
add consistency check on constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2632
diff
changeset
|
563 |
"final entity type") |
ade319674170
add consistency check on constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2632
diff
changeset
|
564 |
|
0 | 565 |
def serialize(self): |
566 |
return self.restriction |
|
1451 | 567 |
|
0 | 568 |
def deserialize(cls, value): |
569 |
return cls(value) |
|
570 |
deserialize = classmethod(deserialize) |
|
1451 | 571 |
|
0 | 572 |
def check(self, entity, rtype, value): |
573 |
"""return true if the value satisfy the constraint, else false""" |
|
574 |
# implemented as a hook in the repository |
|
575 |
return 1 |
|
576 |
||
577 |
def repo_check(self, session, eidfrom, rtype, eidto): |
|
578 |
"""raise ValidationError if the relation doesn't satisfy the constraint |
|
579 |
""" |
|
580 |
pass # this is a vocabulary constraint, not enforce |
|
1451 | 581 |
|
0 | 582 |
def __str__(self): |
583 |
return self.restriction |
|
584 |
||
585 |
def __repr__(self): |
|
586 |
return '<%s : %s>' % (self.__class__.__name__, repr(self.restriction)) |
|
587 |
||
588 |
||
589 |
class RQLConstraint(RQLVocabularyConstraint): |
|
590 |
"""the rql constraint is similar to the RQLVocabularyConstraint but |
|
591 |
are also enforced at the repository level |
|
592 |
""" |
|
593 |
def exec_query(self, session, eidfrom, eidto): |
|
594 |
rql = 'Any S,O WHERE S eid %(s)s, O eid %(o)s, ' + self.restriction |
|
595 |
return session.unsafe_execute(rql, {'s': eidfrom, 'o': eidto}, |
|
596 |
('s', 'o'), build_descr=False) |
|
597 |
def error(self, eid, rtype, msg): |
|
598 |
raise ValidationError(eid, {rtype: msg}) |
|
1451 | 599 |
|
0 | 600 |
def repo_check(self, session, eidfrom, rtype, eidto): |
601 |
"""raise ValidationError if the relation doesn't satisfy the constraint |
|
602 |
""" |
|
603 |
if not self.exec_query(session, eidfrom, eidto): |
|
604 |
# XXX at this point dunno if the validation error `occured` on |
|
605 |
# eidfrom or eidto (from user interface point of view) |
|
606 |
self.error(eidfrom, rtype, 'constraint %s failed' % self) |
|
607 |
||
608 |
||
609 |
class RQLUniqueConstraint(RQLConstraint): |
|
610 |
"""the unique rql constraint check that the result of the query isn't |
|
611 |
greater than one |
|
612 |
""" |
|
613 |
def repo_check(self, session, eidfrom, rtype, eidto): |
|
614 |
"""raise ValidationError if the relation doesn't satisfy the constraint |
|
615 |
""" |
|
616 |
if len(self.exec_query(session, eidfrom, eidto)) > 1: |
|
617 |
# XXX at this point dunno if the validation error `occured` on |
|
618 |
# eidfrom or eidto (from user interface point of view) |
|
619 |
self.error(eidfrom, rtype, 'unique constraint %s failed' % self) |
|
620 |
||
1451 | 621 |
|
0 | 622 |
def split_expression(rqlstring): |
623 |
for expr in rqlstring.split(','): |
|
624 |
for word in expr.split(): |
|
625 |
yield word |
|
1451 | 626 |
|
0 | 627 |
def normalize_expression(rqlstring): |
628 |
"""normalize an rql expression to ease schema synchronization (avoid |
|
629 |
suppressing and reinserting an expression if only a space has been added/removed |
|
630 |
for instance) |
|
631 |
""" |
|
632 |
return u', '.join(' '.join(expr.split()) for expr in rqlstring.split(',')) |
|
633 |
||
634 |
||
635 |
class RQLExpression(object): |
|
636 |
def __init__(self, expression, mainvars, eid): |
|
637 |
self.eid = eid # eid of the entity representing this rql expression |
|
638 |
if not isinstance(mainvars, unicode): |
|
639 |
mainvars = unicode(mainvars) |
|
640 |
self.mainvars = mainvars |
|
641 |
self.expression = normalize_expression(expression) |
|
642 |
try: |
|
643 |
self.rqlst = parse(self.full_rql, print_errors=False).children[0] |
|
644 |
except RQLSyntaxError: |
|
645 |
raise RQLSyntaxError(expression) |
|
646 |
for mainvar in mainvars.split(','): |
|
647 |
if len(self.rqlst.defined_vars[mainvar].references()) <= 2: |
|
2142
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
648 |
_LOGGER.warn('You did not use the %s variable in your RQL ' |
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
649 |
'expression %s', mainvar, self) |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
650 |
# syntax tree used by read security (inserted in queries when necessary |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
651 |
self.snippet_rqlst = parse(self.minimal_rql, print_errors=False).children[0] |
1451 | 652 |
|
0 | 653 |
def __str__(self): |
654 |
return self.full_rql |
|
655 |
def __repr__(self): |
|
656 |
return '%s(%s)' % (self.__class__.__name__, self.full_rql) |
|
1451 | 657 |
|
0 | 658 |
def __deepcopy__(self, memo): |
659 |
return self.__class__(self.expression, self.mainvars) |
|
660 |
def __getstate__(self): |
|
661 |
return (self.expression, self.mainvars) |
|
662 |
def __setstate__(self, state): |
|
663 |
self.__init__(*state) |
|
1451 | 664 |
|
0 | 665 |
@cached |
666 |
def transform_has_permission(self): |
|
667 |
found = None |
|
668 |
rqlst = self.rqlst |
|
669 |
for var in rqlst.defined_vars.itervalues(): |
|
670 |
for varref in var.references(): |
|
671 |
rel = varref.relation() |
|
672 |
if rel is None: |
|
673 |
continue |
|
674 |
try: |
|
675 |
prefix, action, suffix = rel.r_type.split('_') |
|
676 |
except ValueError: |
|
677 |
continue |
|
678 |
if prefix != 'has' or suffix != 'permission' or \ |
|
679 |
not action in ('add', 'delete', 'update', 'read'): |
|
680 |
continue |
|
681 |
if found is None: |
|
682 |
found = [] |
|
683 |
rqlst.save_state() |
|
684 |
assert rel.children[0].name == 'U' |
|
685 |
objvar = rel.children[1].children[0].variable |
|
686 |
rqlst.remove_node(rel) |
|
687 |
selected = [v.name for v in rqlst.get_selected_variables()] |
|
688 |
if objvar.name not in selected: |
|
689 |
colindex = len(selected) |
|
690 |
rqlst.add_selected(objvar) |
|
691 |
else: |
|
692 |
colindex = selected.index(objvar.name) |
|
693 |
found.append((action, objvar, colindex)) |
|
694 |
# remove U eid %(u)s if U is not used in any other relation |
|
695 |
uvrefs = rqlst.defined_vars['U'].references() |
|
696 |
if len(uvrefs) == 1: |
|
697 |
rqlst.remove_node(uvrefs[0].relation()) |
|
698 |
if found is not None: |
|
699 |
rql = rqlst.as_string() |
|
700 |
if len(rqlst.selection) == 1 and isinstance(rqlst.where, nodes.Relation): |
|
701 |
# only "Any X WHERE X eid %(x)s" remaining, no need to execute the rql |
|
702 |
keyarg = rqlst.selection[0].name.lower() |
|
703 |
else: |
|
704 |
keyarg = None |
|
705 |
rqlst.recover() |
|
706 |
return rql, found, keyarg |
|
707 |
return rqlst.as_string(), None, None |
|
1451 | 708 |
|
0 | 709 |
def _check(self, session, **kwargs): |
710 |
"""return True if the rql expression is matching the given relation |
|
711 |
between fromeid and toeid |
|
712 |
||
713 |
session may actually be a request as well |
|
714 |
""" |
|
715 |
if self.eid is not None: |
|
716 |
key = (self.eid, tuple(sorted(kwargs.iteritems()))) |
|
717 |
try: |
|
718 |
return session.local_perm_cache[key] |
|
719 |
except KeyError: |
|
720 |
pass |
|
721 |
rql, has_perm_defs, keyarg = self.transform_has_permission() |
|
722 |
if keyarg is None: |
|
723 |
# on the server side, use unsafe_execute, but this is not available |
|
724 |
# on the client side (session is actually a request) |
|
725 |
execute = getattr(session, 'unsafe_execute', session.execute) |
|
726 |
# XXX what if 'u' in kwargs |
|
727 |
cachekey = kwargs.keys() |
|
728 |
kwargs['u'] = session.user.eid |
|
729 |
try: |
|
730 |
rset = execute(rql, kwargs, cachekey, build_descr=True) |
|
731 |
except NotImplementedError: |
|
732 |
self.critical('cant check rql expression, unsupported rql %s', rql) |
|
733 |
if self.eid is not None: |
|
734 |
session.local_perm_cache[key] = False |
|
735 |
return False |
|
736 |
except TypeResolverException, ex: |
|
737 |
# some expression may not be resolvable with current kwargs |
|
738 |
# (type conflict) |
|
739 |
self.warning('%s: %s', rql, str(ex)) |
|
740 |
if self.eid is not None: |
|
741 |
session.local_perm_cache[key] = False |
|
742 |
return False |
|
743 |
else: |
|
744 |
rset = session.eid_rset(kwargs[keyarg]) |
|
745 |
# if no special has_*_permission relation in the rql expression, just |
|
746 |
# check the result set contains something |
|
747 |
if has_perm_defs is None: |
|
748 |
if rset: |
|
749 |
if self.eid is not None: |
|
750 |
session.local_perm_cache[key] = True |
|
751 |
return True |
|
752 |
elif rset: |
|
753 |
# check every special has_*_permission relation is satisfied |
|
754 |
get_eschema = session.vreg.schema.eschema |
|
755 |
try: |
|
756 |
for eaction, var, col in has_perm_defs: |
|
757 |
for i in xrange(len(rset)): |
|
758 |
eschema = get_eschema(rset.description[i][col]) |
|
759 |
eschema.check_perm(session, eaction, rset[i][col]) |
|
760 |
if self.eid is not None: |
|
761 |
session.local_perm_cache[key] = True |
|
762 |
return True |
|
763 |
except Unauthorized: |
|
764 |
pass |
|
765 |
if self.eid is not None: |
|
766 |
session.local_perm_cache[key] = False |
|
767 |
return False |
|
1451 | 768 |
|
0 | 769 |
@property |
770 |
def minimal_rql(self): |
|
771 |
return 'Any %s WHERE %s' % (self.mainvars, self.expression) |
|
772 |
||
773 |
||
774 |
class ERQLExpression(RQLExpression): |
|
775 |
def __init__(self, expression, mainvars=None, eid=None): |
|
776 |
RQLExpression.__init__(self, expression, mainvars or 'X', eid) |
|
777 |
||
778 |
@property |
|
779 |
def full_rql(self): |
|
780 |
rql = self.minimal_rql |
|
781 |
rqlst = getattr(self, 'rqlst', None) # may be not set yet |
|
782 |
if rqlst is not None: |
|
783 |
defined = rqlst.defined_vars |
|
784 |
else: |
|
785 |
defined = set(split_expression(self.expression)) |
|
786 |
if 'X' in defined: |
|
787 |
rql += ', X eid %(x)s' |
|
788 |
if 'U' in defined: |
|
789 |
rql += ', U eid %(u)s' |
|
790 |
return rql |
|
1451 | 791 |
|
0 | 792 |
def check(self, session, eid=None): |
793 |
if 'X' in self.rqlst.defined_vars: |
|
794 |
if eid is None: |
|
795 |
return False |
|
796 |
return self._check(session, x=eid) |
|
797 |
return self._check(session) |
|
1451 | 798 |
|
2301
a9e9582d5fb6
obsolete implicit context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2300
diff
changeset
|
799 |
PyFileReader.context['ERQLExpression'] = yobsolete(ERQLExpression) |
1451 | 800 |
|
0 | 801 |
class RRQLExpression(RQLExpression): |
802 |
def __init__(self, expression, mainvars=None, eid=None): |
|
803 |
if mainvars is None: |
|
804 |
defined = set(split_expression(expression)) |
|
805 |
mainvars = [] |
|
806 |
if 'S' in defined: |
|
807 |
mainvars.append('S') |
|
808 |
if 'O' in defined: |
|
809 |
mainvars.append('O') |
|
810 |
if not mainvars: |
|
811 |
raise Exception('unable to guess selection variables') |
|
812 |
mainvars = ','.join(mainvars) |
|
813 |
RQLExpression.__init__(self, expression, mainvars, eid) |
|
814 |
||
815 |
@property |
|
816 |
def full_rql(self): |
|
817 |
rql = self.minimal_rql |
|
818 |
rqlst = getattr(self, 'rqlst', None) # may be not set yet |
|
819 |
if rqlst is not None: |
|
820 |
defined = rqlst.defined_vars |
|
821 |
else: |
|
822 |
defined = set(split_expression(self.expression)) |
|
823 |
if 'S' in defined: |
|
824 |
rql += ', S eid %(s)s' |
|
825 |
if 'O' in defined: |
|
826 |
rql += ', O eid %(o)s' |
|
827 |
if 'U' in defined: |
|
828 |
rql += ', U eid %(u)s' |
|
829 |
return rql |
|
1451 | 830 |
|
0 | 831 |
def check(self, session, fromeid=None, toeid=None): |
832 |
kwargs = {} |
|
833 |
if 'S' in self.rqlst.defined_vars: |
|
834 |
if fromeid is None: |
|
835 |
return False |
|
836 |
kwargs['s'] = fromeid |
|
837 |
if 'O' in self.rqlst.defined_vars: |
|
838 |
if toeid is None: |
|
839 |
return False |
|
840 |
kwargs['o'] = toeid |
|
841 |
return self._check(session, **kwargs) |
|
1451 | 842 |
|
2301
a9e9582d5fb6
obsolete implicit context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2300
diff
changeset
|
843 |
PyFileReader.context['RRQLExpression'] = yobsolete(RRQLExpression) |
0 | 844 |
|
629
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
845 |
# workflow extensions ######################################################### |
2926
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
846 |
|
2460
ce1a7ffc6c90
fix a couple NameErrors
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2459
diff
changeset
|
847 |
from yams.buildobjs import _add_relation as yams_add_relation |
629
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
848 |
|
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
849 |
class workflowable_definition(ybo.metadefinition): |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
850 |
"""extends default EntityType's metaclass to add workflow relations |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
851 |
(i.e. in_state and wf_info_for). |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
852 |
This is the default metaclass for WorkflowableEntityType |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
853 |
""" |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
854 |
def __new__(mcs, name, bases, classdict): |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
855 |
abstract = classdict.pop('__abstract__', False) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
856 |
cls = super(workflowable_definition, mcs).__new__(mcs, name, bases, |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
857 |
classdict) |
629
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
858 |
if not abstract: |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
859 |
make_workflowable(cls) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
860 |
return cls |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
861 |
|
2957
2daabf4c646e
[schema] make_workflowable now take an option description for the in_state relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2931
diff
changeset
|
862 |
def make_workflowable(cls, in_state_descr=None): |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
863 |
existing_rels = set(rdef.name for rdef in cls.__relations__) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
864 |
# let relation types defined in cw.schemas.workflow carrying |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
865 |
# cardinality, constraints and other relation definition properties |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
866 |
if 'custom_workflow' not in existing_rels: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
867 |
rdef = ybo.SubjectRelation('Workflow') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
868 |
yams_add_relation(cls.__relations__, rdef, 'custom_workflow') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
869 |
if 'in_state' not in existing_rels: |
2957
2daabf4c646e
[schema] make_workflowable now take an option description for the in_state relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2931
diff
changeset
|
870 |
rdef = ybo.SubjectRelation('State', description=in_state_descr) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
871 |
yams_add_relation(cls.__relations__, rdef, 'in_state') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
872 |
if 'wf_info_for' not in existing_rels: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
873 |
rdef = ybo.ObjectRelation('TrInfo') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
874 |
yams_add_relation(cls.__relations__, rdef, 'wf_info_for') |
629
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
875 |
|
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
876 |
class WorkflowableEntityType(ybo.EntityType): |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
877 |
__metaclass__ = workflowable_definition |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
878 |
__abstract__ = True |
1451 | 879 |
|
629
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
880 |
PyFileReader.context['WorkflowableEntityType'] = WorkflowableEntityType |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
881 |
|
0 | 882 |
# schema loading ############################################################## |
883 |
||
884 |
CONSTRAINTS['RQLConstraint'] = RQLConstraint |
|
885 |
CONSTRAINTS['RQLUniqueConstraint'] = RQLUniqueConstraint |
|
886 |
CONSTRAINTS['RQLVocabularyConstraint'] = RQLVocabularyConstraint |
|
887 |
PyFileReader.context.update(CONSTRAINTS) |
|
888 |
||
889 |
||
890 |
class BootstrapSchemaLoader(SchemaLoader): |
|
891 |
"""cubicweb specific schema loader, loading only schema necessary to read |
|
892 |
the persistent schema |
|
893 |
""" |
|
894 |
schemacls = CubicWebSchema |
|
895 |
||
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
896 |
def load(self, config, path=(), **kwargs): |
0 | 897 |
"""return a Schema instance from the schema definition read |
898 |
from <directory> |
|
899 |
""" |
|
900 |
return super(BootstrapSchemaLoader, self).load( |
|
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
901 |
path, config.appid, register_base_types=False, **kwargs) |
1451 | 902 |
|
0 | 903 |
def _load_definition_files(self, cubes=None): |
904 |
# bootstraping, ignore cubes |
|
2735
39c942241b48
no need for lib_directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
905 |
filepath = join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'bootstrap.py') |
2142
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
906 |
self.info('loading %s', filepath) |
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
907 |
self.handle_file(filepath) |
1451 | 908 |
|
0 | 909 |
def unhandled_file(self, filepath): |
910 |
"""called when a file without handler associated has been found""" |
|
911 |
self.warning('ignoring file %r', filepath) |
|
912 |
||
913 |
||
914 |
class CubicWebSchemaLoader(BootstrapSchemaLoader): |
|
915 |
"""cubicweb specific schema loader, automatically adding metadata to the |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2460
diff
changeset
|
916 |
instance's schema |
0 | 917 |
""" |
918 |
||
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
919 |
def load(self, config, **kwargs): |
0 | 920 |
"""return a Schema instance from the schema definition read |
921 |
from <directory> |
|
922 |
""" |
|
923 |
self.info('loading %s schemas', ', '.join(config.cubes())) |
|
2782
998f24dabd0d
compute extrapath, necessary for proper schema module detection when CW_CUBES_PATH is set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2742
diff
changeset
|
924 |
self.extrapath = {} |
998f24dabd0d
compute extrapath, necessary for proper schema module detection when CW_CUBES_PATH is set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2742
diff
changeset
|
925 |
for cubesdir in config.cubes_search_path(): |
998f24dabd0d
compute extrapath, necessary for proper schema module detection when CW_CUBES_PATH is set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2742
diff
changeset
|
926 |
if cubesdir != config.CUBES_DIR: |
998f24dabd0d
compute extrapath, necessary for proper schema module detection when CW_CUBES_PATH is set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2742
diff
changeset
|
927 |
self.extrapath[cubesdir] = 'cubes' |
372
a8a975a88368
check apphome is not None
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
928 |
if config.apphome: |
2598
a66fe74bd9fc
[R schema] dont give an iterator, it's a trap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
929 |
path = tuple(reversed([config.apphome] + config.cubes_path())) |
372
a8a975a88368
check apphome is not None
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
930 |
else: |
2598
a66fe74bd9fc
[R schema] dont give an iterator, it's a trap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
931 |
path = tuple(reversed(config.cubes_path())) |
2526
40e41eb34a7d
we've to call cleanup_sys_modules manually for proper schema modules cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
932 |
try: |
40e41eb34a7d
we've to call cleanup_sys_modules manually for proper schema modules cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
933 |
return super(CubicWebSchemaLoader, self).load(config, path=path, **kwargs) |
40e41eb34a7d
we've to call cleanup_sys_modules manually for proper schema modules cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
934 |
finally: |
40e41eb34a7d
we've to call cleanup_sys_modules manually for proper schema modules cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
935 |
# we've to cleanup modules imported from cubicweb.schemas as well |
2735
39c942241b48
no need for lib_directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
936 |
cleanup_sys_modules([join(cubicweb.CW_SOFTWARE_ROOT, 'schemas')]) |
0 | 937 |
|
938 |
def _load_definition_files(self, cubes): |
|
2741
148d0cea1fb5
[schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2736
diff
changeset
|
939 |
for filepath in (join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'bootstrap.py'), |
148d0cea1fb5
[schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2736
diff
changeset
|
940 |
join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'base.py'), |
148d0cea1fb5
[schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2736
diff
changeset
|
941 |
join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'workflow.py'), |
148d0cea1fb5
[schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2736
diff
changeset
|
942 |
join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'Bookmark.py')): |
0 | 943 |
self.info('loading %s', filepath) |
944 |
self.handle_file(filepath) |
|
945 |
for cube in cubes: |
|
946 |
for filepath in self.get_schema_files(cube): |
|
947 |
self.info('loading %s', filepath) |
|
948 |
self.handle_file(filepath) |
|
949 |
||
950 |
||
2459
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
951 |
set_log_methods(CubicWebSchemaLoader, getLogger('cubicweb.schemaloader')) |
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
952 |
set_log_methods(BootstrapSchemaLoader, getLogger('cubicweb.bootstrapschemaloader')) |
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
953 |
set_log_methods(RQLExpression, getLogger('cubicweb.schema')) |
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
954 |
|
0 | 955 |
# _() is just there to add messages to the catalog, don't care about actual |
956 |
# translation |
|
957 |
PERM_USE_TEMPLATE_FORMAT = _('use_template_format') |
|
2459
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
958 |
NEED_PERM_FORMATS = [_('text/cubicweb-page-template')] |
0 | 959 |
|
2531
531ea4e7013e
[cleanup] nicer imports
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2526
diff
changeset
|
960 |
@monkeypatch(FormatConstraint) |
3347
428f95118556
fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3287
diff
changeset
|
961 |
def vocabulary(self, entity=None, form=None): |
428f95118556
fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3287
diff
changeset
|
962 |
req = None |
428f95118556
fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3287
diff
changeset
|
963 |
if form is None and entity is not None: |
2459
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
964 |
req = entity.req |
3347
428f95118556
fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3287
diff
changeset
|
965 |
elif form is not None: |
428f95118556
fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3287
diff
changeset
|
966 |
req = form.req |
2459
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
967 |
if req is not None and req.user.has_permission(PERM_USE_TEMPLATE_FORMAT): |
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
968 |
return self.regular_formats + tuple(NEED_PERM_FORMATS) |
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
969 |
return self.regular_formats |
0 | 970 |
|
971 |
# XXX monkey patch PyFileReader.import_erschema until bw_normalize_etype is |
|
972 |
# necessary |
|
973 |
orig_import_erschema = PyFileReader.import_erschema |
|
974 |
def bw_import_erschema(self, ertype, schemamod=None, instantiate=True): |
|
975 |
return orig_import_erschema(self, bw_normalize_etype(ertype), schemamod, instantiate) |
|
976 |
PyFileReader.import_erschema = bw_import_erschema |
|
1451 | 977 |
|
0 | 978 |
# XXX itou for some Statement methods |
979 |
from rql import stmts |
|
980 |
orig_get_etype = stmts.ScopeNode.get_etype |
|
981 |
def bw_get_etype(self, name): |
|
982 |
return orig_get_etype(self, bw_normalize_etype(name)) |
|
983 |
stmts.ScopeNode.get_etype = bw_get_etype |
|
984 |
||
985 |
orig_add_main_variable_delete = stmts.Delete.add_main_variable |
|
986 |
def bw_add_main_variable_delete(self, etype, vref): |
|
987 |
return orig_add_main_variable_delete(self, bw_normalize_etype(etype), vref) |
|
988 |
stmts.Delete.add_main_variable = bw_add_main_variable_delete |
|
989 |
||
990 |
orig_add_main_variable_insert = stmts.Insert.add_main_variable |
|
991 |
def bw_add_main_variable_insert(self, etype, vref): |
|
992 |
return orig_add_main_variable_insert(self, bw_normalize_etype(etype), vref) |
|
993 |
stmts.Insert.add_main_variable = bw_add_main_variable_insert |
|
994 |
||
995 |
orig_set_statement_type = stmts.Select.set_statement_type |
|
996 |
def bw_set_statement_type(self, etype): |
|
997 |
return orig_set_statement_type(self, bw_normalize_etype(etype)) |
|
998 |
stmts.Select.set_statement_type = bw_set_statement_type |
|
2736
c5048502409f
restore backward compat, still imported a lot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2735
diff
changeset
|
999 |
|
c5048502409f
restore backward compat, still imported a lot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2735
diff
changeset
|
1000 |
# XXX deprecated |
c5048502409f
restore backward compat, still imported a lot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2735
diff
changeset
|
1001 |
from yams.constraints import format_constraint |
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2782
diff
changeset
|
1002 |
format_constraint = deprecated('[3.4] use RichString instead of format_constraint')(format_constraint) |
2736
c5048502409f
restore backward compat, still imported a lot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2735
diff
changeset
|
1003 |
from yams.buildobjs import RichString |
2742
9439c61eb84c
[bw compat] restore format_constraint into schema file context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2741
diff
changeset
|
1004 |
PyFileReader.context['format_constraint'] = format_constraint |