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