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