author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 17 Jun 2010 15:15:52 +0200 | |
changeset 5774 | 0d792bceb25d |
parent 5424 | 8ecbcbff9777 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
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:
4212
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:
4212
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:
4212
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:
4212
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:
4212
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:
4212
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:
4212
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:
4212
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:
4212
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:
4212
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:
4212
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:
4212
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:
4212
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:
4212
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""extends yams to be able to load google appengine's schemas |
19 |
||
20 |
MISSING FEATURES: |
|
21 |
- ListProperty, StringList, EmailProperty, etc. (XXX) |
|
22 |
- ReferenceProperty.verbose_name, collection_name, etc. (XXX) |
|
23 |
||
24 |
XXX proprify this knowing we'll use goa.db |
|
25 |
""" |
|
26 |
||
27 |
from os.path import join |
|
28 |
from datetime import datetime, date, time |
|
29 |
||
30 |
from google.appengine.ext import db |
|
31 |
from google.appengine.api import datastore_types |
|
32 |
||
33 |
from yams.buildobjs import (String, Int, Float, Boolean, Date, Time, Datetime, |
|
1132 | 34 |
Bytes, SubjectRelation) |
0 | 35 |
from yams.buildobjs import metadefinition, EntityType |
36 |
||
37 |
from cubicweb.schema import CubicWebSchemaLoader |
|
38 |
from cubicweb.goa import db as goadb |
|
39 |
||
40 |
# db.Model -> yams ############################################################ |
|
41 |
||
42 |
DBM2Y_TYPESMAP = { |
|
43 |
basestring: String, |
|
44 |
datastore_types.Text: String, |
|
45 |
int: Int, |
|
46 |
float: Float, |
|
47 |
bool: Boolean, |
|
48 |
time: Time, |
|
49 |
date: Date, |
|
50 |
datetime: Datetime, |
|
51 |
datastore_types.Blob: Bytes, |
|
52 |
} |
|
53 |
||
54 |
||
55 |
def dbm2y_default_factory(prop, **kwargs): |
|
56 |
"""just wraps the default types map to set |
|
57 |
basic constraints like `required`, `default`, etc. |
|
58 |
""" |
|
59 |
yamstype = DBM2Y_TYPESMAP[prop.data_type] |
|
60 |
if 'default' not in kwargs: |
|
61 |
default = prop.default_value() |
|
62 |
if default is not None: |
|
63 |
kwargs['default'] = default |
|
64 |
if prop.required: |
|
65 |
kwargs['required'] = True |
|
66 |
return yamstype(**kwargs) |
|
67 |
||
68 |
def dbm2y_string_factory(prop): |
|
69 |
"""like dbm2y_default_factory but also deals with `maxsize` and `vocabulary`""" |
|
70 |
kwargs = {} |
|
71 |
if prop.data_type is basestring: |
|
72 |
kwargs['maxsize'] = 500 |
|
73 |
if prop.choices is not None: |
|
74 |
kwargs['vocabulary'] = prop.choices |
|
75 |
return dbm2y_default_factory(prop, **kwargs) |
|
76 |
||
77 |
def dbm2y_date_factory(prop): |
|
78 |
"""like dbm2y_default_factory but also deals with today / now definition""" |
|
79 |
kwargs = {} |
|
80 |
if prop.auto_now_add: |
|
81 |
if prop.data_type is datetime: |
|
82 |
kwargs['default'] = 'now' |
|
83 |
else: |
|
84 |
kwargs['default'] = 'today' |
|
85 |
# XXX no equivalent to Django's `auto_now` |
|
86 |
return dbm2y_default_factory(prop, **kwargs) |
|
87 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
88 |
|
0 | 89 |
def dbm2y_relation_factory(etype, prop, multiple=False): |
90 |
"""called if `prop` is a `db.ReferenceProperty`""" |
|
91 |
if multiple: |
|
92 |
cardinality = '**' |
|
93 |
elif prop.required: |
|
94 |
cardinality = '1*' |
|
95 |
else: |
|
96 |
cardinality = '?*' |
|
97 |
# XXX deal with potential kwargs of ReferenceProperty.__init__() |
|
98 |
try: |
|
99 |
return SubjectRelation(prop.data_type.kind(), cardinality=cardinality) |
|
100 |
except AttributeError, ex: |
|
101 |
# hack, data_type is still _SELF_REFERENCE_MARKER |
|
102 |
return SubjectRelation(etype, cardinality=cardinality) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
103 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
104 |
|
0 | 105 |
DBM2Y_FACTORY = { |
106 |
basestring: dbm2y_string_factory, |
|
107 |
datastore_types.Text: dbm2y_string_factory, |
|
108 |
int: dbm2y_default_factory, |
|
109 |
float: dbm2y_default_factory, |
|
110 |
bool: dbm2y_default_factory, |
|
111 |
time: dbm2y_date_factory, |
|
112 |
date: dbm2y_date_factory, |
|
113 |
datetime: dbm2y_date_factory, |
|
114 |
datastore_types.Blob: dbm2y_default_factory, |
|
115 |
} |
|
116 |
||
117 |
||
118 |
class GaeSchemaLoader(CubicWebSchemaLoader): |
|
119 |
"""Google appengine schema loader class""" |
|
120 |
def __init__(self, *args, **kwargs): |
|
121 |
self.use_gauthservice = kwargs.pop('use_gauthservice', False) |
|
122 |
super(GaeSchemaLoader, self).__init__(*args, **kwargs) |
|
123 |
self.defined = {} |
|
124 |
self.created = [] |
|
935 | 125 |
self.loaded_files = [] |
0 | 126 |
self._instantiate_handlers() |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
127 |
|
0 | 128 |
def finalize(self, register_base_types=False): |
129 |
return self._build_schema('google-appengine', register_base_types) |
|
130 |
||
131 |
def load_dbmodel(self, name, props): |
|
132 |
clsdict = {} |
|
133 |
ordered_props = sorted(props.items(), |
|
134 |
key=lambda x: x[1].creation_counter) |
|
135 |
for pname, prop in ordered_props: |
|
136 |
if isinstance(prop, db.ListProperty): |
|
137 |
if not issubclass(prop.item_type, db.Model): |
|
138 |
self.error('ignoring list property with %s item type' |
|
139 |
% prop.item_type) |
|
140 |
continue |
|
141 |
rdef = dbm2y_relation_factory(name, prop, multiple=True) |
|
142 |
else: |
|
143 |
try: |
|
144 |
if isinstance(prop, (db.ReferenceProperty, |
|
145 |
goadb.ReferencePropertyStub)): |
|
146 |
rdef = dbm2y_relation_factory(name, prop) |
|
147 |
else: |
|
148 |
rdef = DBM2Y_FACTORY[prop.data_type](prop) |
|
149 |
except KeyError, ex: |
|
150 |
import traceback |
|
151 |
traceback.print_exc() |
|
152 |
self.error('ignoring property %s (keyerror on %s)' % (pname, ex)) |
|
153 |
continue |
|
154 |
rdef.creation_rank = prop.creation_counter |
|
155 |
clsdict[pname] = rdef |
|
156 |
edef = metadefinition(name, (EntityType,), clsdict) |
|
157 |
self.add_definition(self, edef()) |
|
158 |
||
159 |
def error(self, msg): |
|
160 |
print 'ERROR:', msg |
|
161 |
||
162 |
def import_yams_schema(self, ertype, schemamod): |
|
163 |
erdef = self.pyreader.import_erschema(ertype, schemamod) |
|
164 |
||
165 |
def import_yams_cube_schema(self, templpath): |
|
166 |
for filepath in self.get_schema_files(templpath): |
|
167 |
self.handle_file(filepath) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
168 |
|
0 | 169 |
@property |
170 |
def pyreader(self): |
|
171 |
return self._live_handlers['.py'] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
172 |
|
0 | 173 |
import os |
174 |
from cubicweb import CW_SOFTWARE_ROOT |
|
175 |
||
176 |
def load_schema(config, schemaclasses=None, extrahook=None): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
177 |
"""high level method to load all the schema for a lax instance""" |
0 | 178 |
# IMPORTANT NOTE: dbmodel schemas must be imported **BEFORE** |
179 |
# the loader is instantiated because this is where the dbmodels |
|
180 |
# are registered in the yams schema |
|
181 |
for compname in config['included-cubes']: |
|
1133 | 182 |
__import__('%s.schema' % compname) |
0 | 183 |
loader = GaeSchemaLoader(use_gauthservice=config['use-google-auth'], db=db) |
184 |
if schemaclasses is not None: |
|
185 |
for cls in schemaclasses: |
|
186 |
loader.load_dbmodel(cls.__name__, goadb.extract_dbmodel(cls)) |
|
187 |
elif config['schema-type'] == 'dbmodel': |
|
188 |
import schema as appschema |
|
1133 | 189 |
for obj in vars(appschema).values(): |
0 | 190 |
if isinstance(obj, type) and issubclass(obj, goadb.Model) and obj.__module__ == appschema.__name__: |
191 |
loader.load_dbmodel(obj.__name__, goadb.extract_dbmodel(obj)) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1133
diff
changeset
|
192 |
for erschema in ('CWGroup', 'CWEType', 'CWRType', 'RQLExpression', |
0 | 193 |
'is_', 'is_instance_of', |
194 |
'read_permission', 'add_permission', |
|
195 |
'delete_permission', 'update_permission'): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
196 |
loader.import_yams_schema(erschema, 'bootstrap') |
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:
2476
diff
changeset
|
197 |
loader.handle_file(join(CW_SOFTWARE_ROOT, 'schemas', 'base.py')) |
0 | 198 |
cubes = config['included-yams-cubes'] |
199 |
for cube in reversed(config.expand_cubes(cubes)): |
|
200 |
config.info('loading cube %s', cube) |
|
201 |
loader.import_yams_cube_schema(config.cube_dir(cube)) |
|
202 |
if config['schema-type'] == 'yams': |
|
203 |
loader.import_yams_cube_schema('.') |
|
204 |
if extrahook is not None: |
|
205 |
extrahook(loader) |
|
206 |
if config['use-google-auth']: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1133
diff
changeset
|
207 |
loader.defined['CWUser'].remove_relation('upassword') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1133
diff
changeset
|
208 |
loader.defined['CWUser'].permissions['add'] = () |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1133
diff
changeset
|
209 |
loader.defined['CWUser'].permissions['delete'] = () |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1133
diff
changeset
|
210 |
for etype in ('CWGroup', 'RQLExpression'): |
0 | 211 |
read_perm_rel = loader.defined[etype].get_relations('read_permission').next() |
212 |
read_perm_rel.cardinality = '**' |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1133
diff
changeset
|
213 |
# XXX not yet ready for CWUser workflow |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1133
diff
changeset
|
214 |
loader.defined['CWUser'].remove_relation('in_state') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1133
diff
changeset
|
215 |
loader.defined['CWUser'].remove_relation('wf_info_for') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1133
diff
changeset
|
216 |
# remove RQLConstraint('NOT O name "owners"') on CWUser in_group CWGroup |
0 | 217 |
# since "owners" group is not persistent with gae |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1133
diff
changeset
|
218 |
loader.defined['CWUser'].get_relations('in_group').next().constraints = [] |
0 | 219 |
# return the full schema including the cubes' schema |
220 |
for ertype in loader.defined.values(): |
|
221 |
if getattr(ertype, 'inlined', False): |
|
222 |
ertype.inlined = False |
|
223 |
return loader.finalize() |