author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 17 Jun 2009 20:46:37 +0200 | |
branch | stable |
changeset 2114 | 862f27d94af4 |
parent 1977 | 606923dff11b |
child 2476 | 1294a6bdf3bf |
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 |
if os.environ.get('APYCOT_ROOT'): |
|
164 |
SCHEMAS_LIB_DIRECTORY = join(os.environ['APYCOT_ROOT'], |
|
165 |
'local', 'share', 'cubicweb', 'schemas') |
|
166 |
else: |
|
167 |
SCHEMAS_LIB_DIRECTORY = join(CW_SOFTWARE_ROOT, 'schemas') |
|
168 |
||
169 |
def load_schema(config, schemaclasses=None, extrahook=None): |
|
170 |
"""high level method to load all the schema for a lax application""" |
|
171 |
# IMPORTANT NOTE: dbmodel schemas must be imported **BEFORE** |
|
172 |
# the loader is instantiated because this is where the dbmodels |
|
173 |
# are registered in the yams schema |
|
174 |
for compname in config['included-cubes']: |
|
1133 | 175 |
__import__('%s.schema' % compname) |
0 | 176 |
loader = GaeSchemaLoader(use_gauthservice=config['use-google-auth'], db=db) |
177 |
loader.lib_directory = SCHEMAS_LIB_DIRECTORY |
|
178 |
if schemaclasses is not None: |
|
179 |
for cls in schemaclasses: |
|
180 |
loader.load_dbmodel(cls.__name__, goadb.extract_dbmodel(cls)) |
|
181 |
elif config['schema-type'] == 'dbmodel': |
|
182 |
import schema as appschema |
|
1133 | 183 |
for obj in vars(appschema).values(): |
0 | 184 |
if isinstance(obj, type) and issubclass(obj, goadb.Model) and obj.__module__ == appschema.__name__: |
185 |
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
|
186 |
for erschema in ('CWGroup', 'CWEType', 'CWRType', 'RQLExpression', |
0 | 187 |
'is_', 'is_instance_of', |
188 |
'read_permission', 'add_permission', |
|
189 |
'delete_permission', 'update_permission'): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
190 |
loader.import_yams_schema(erschema, 'bootstrap') |
0 | 191 |
loader.handle_file(join(SCHEMAS_LIB_DIRECTORY, 'base.py')) |
192 |
cubes = config['included-yams-cubes'] |
|
193 |
for cube in reversed(config.expand_cubes(cubes)): |
|
194 |
config.info('loading cube %s', cube) |
|
195 |
loader.import_yams_cube_schema(config.cube_dir(cube)) |
|
196 |
if config['schema-type'] == 'yams': |
|
197 |
loader.import_yams_cube_schema('.') |
|
198 |
if extrahook is not None: |
|
199 |
extrahook(loader) |
|
200 |
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
|
201 |
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
|
202 |
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
|
203 |
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
|
204 |
for etype in ('CWGroup', 'RQLExpression'): |
0 | 205 |
read_perm_rel = loader.defined[etype].get_relations('read_permission').next() |
206 |
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
|
207 |
# 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
|
208 |
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
|
209 |
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
|
210 |
# remove RQLConstraint('NOT O name "owners"') on CWUser in_group CWGroup |
0 | 211 |
# 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
|
212 |
loader.defined['CWUser'].get_relations('in_group').next().constraints = [] |
0 | 213 |
# return the full schema including the cubes' schema |
214 |
for ertype in loader.defined.values(): |
|
215 |
if getattr(ertype, 'inlined', False): |
|
216 |
ertype.inlined = False |
|
217 |
return loader.finalize() |