author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 11 Jun 2009 19:07:47 +0200 | |
branch | stable |
changeset 2101 | 08003e0354a7 |
parent 1977 | 606923dff11b |
child 3689 | deb13e88e037 |
permissions | -rw-r--r-- |
0 | 1 |
"""cubicweb on google appengine |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
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:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
||
11 |
try: |
|
12 |
# WARNING: do not import the google's db module here since it will take |
|
13 |
# precedence over our own db submodule |
|
14 |
from google.appengine.api.datastore import Key, Get, Query |
|
15 |
from google.appengine.api.datastore_errors import BadKeyError |
|
16 |
except ImportError: |
|
17 |
# not in google app environment |
|
18 |
pass |
|
19 |
else: |
|
20 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
21 |
import os |
0 | 22 |
_SS = os.environ.get('SERVER_SOFTWARE') |
23 |
if _SS is None: |
|
24 |
MODE = 'test' |
|
25 |
elif _SS.startswith('Dev'): |
|
26 |
MODE = 'dev' |
|
27 |
else: |
|
28 |
MODE = 'prod' |
|
29 |
||
30 |
from cubicweb.server import SOURCE_TYPES |
|
31 |
from cubicweb.goa.gaesource import GAESource |
|
32 |
SOURCE_TYPES['gae'] = GAESource |
|
33 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
34 |
|
0 | 35 |
def do_monkey_patch(): |
36 |
||
37 |
# monkey patch yams Bytes validator since it should take a bytes string with gae |
|
38 |
# and not a StringIO |
|
39 |
def check_bytes(eschema, value): |
|
40 |
"""check value is a bytes string""" |
|
41 |
return isinstance(value, str) |
|
42 |
from yams import constraints |
|
43 |
constraints.BASE_CHECKERS['Bytes'] = check_bytes |
|
44 |
||
45 |
def rql_for_eid(eid): |
|
46 |
return 'Any X WHERE X eid "%s"' % eid |
|
47 |
from cubicweb.common import uilib |
|
48 |
uilib.rql_for_eid = rql_for_eid |
|
49 |
||
50 |
def typed_eid(eid): |
|
51 |
try: |
|
52 |
return str(Key(eid)) |
|
53 |
except BadKeyError: |
|
54 |
raise ValueError(eid) |
|
55 |
import cubicweb |
|
56 |
cubicweb.typed_eid = typed_eid |
|
57 |
||
58 |
# XXX monkey patch cubicweb.schema.CubicWebSchema to have string eid with |
|
59 |
# optional cardinality (since eid is set after the validation) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
60 |
|
0 | 61 |
import re |
62 |
from yams import buildobjs as ybo |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
63 |
|
0 | 64 |
def add_entity_type(self, edef): |
65 |
edef.name = edef.name.encode() |
|
66 |
assert re.match(r'[A-Z][A-Za-z0-9]*[a-z]+[0-9]*$', edef.name), repr(edef.name) |
|
67 |
eschema = super(CubicWebSchema, self).add_entity_type(edef) |
|
68 |
if not eschema.is_final(): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
69 |
# automatically add the eid relation to non final entity types |
0 | 70 |
rdef = ybo.RelationDefinition(eschema.type, 'eid', 'Bytes', |
71 |
cardinality='?1', uid=True) |
|
72 |
self.add_relation_def(rdef) |
|
73 |
rdef = ybo.RelationDefinition(eschema.type, 'identity', eschema.type) |
|
74 |
self.add_relation_def(rdef) |
|
75 |
self._eid_index[eschema.eid] = eschema |
|
76 |
return eschema |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
77 |
|
0 | 78 |
from cubicweb.schema import CubicWebSchema |
79 |
CubicWebSchema.add_entity_type = add_entity_type |
|
80 |
||
81 |
||
82 |
# don't reset vreg on repository set_schema |
|
83 |
from cubicweb.server import repository |
|
84 |
orig_set_schema = repository.Repository.set_schema |
|
85 |
def set_schema(self, schema, resetvreg=True): |
|
86 |
orig_set_schema(self, schema, False) |
|
87 |
repository.Repository.set_schema = set_schema |
|
88 |
# deactivate function ensuring relation cardinality consistency |
|
89 |
repository.del_existing_rel_if_needed = lambda *args: None |
|
90 |
||
91 |
def get_cubes(self): |
|
92 |
"""return the list of top level cubes used by this instance""" |
|
93 |
config = self.config |
|
94 |
cubes = config['included-cubes'] + config['included-yams-cubes'] |
|
95 |
return config.expand_cubes(cubes) |
|
96 |
repository.Repository.get_cubes = get_cubes |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
97 |
|
0 | 98 |
from rql import RQLHelper |
1133 | 99 |
RQLHelper.simplify = lambda x, r: None |
0 | 100 |
|
101 |
# activate entity caching on the server side |
|
102 |
||
103 |
def set_entity_cache(self, entity): |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
104 |
self.transaction_data.setdefault('_eid_cache', {})[entity.eid] = entity |
0 | 105 |
|
106 |
def entity_cache(self, eid): |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
107 |
return self.transaction_data['_eid_cache'][eid] |
0 | 108 |
|
109 |
def drop_entity_cache(self, eid=None): |
|
110 |
if eid is None: |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
111 |
self.transaction_data['_eid_cache'] = {} |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
112 |
elif '_eid_cache' in self.transaction_data: |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
113 |
self.transaction_data['_eid_cache'].pop(eid, None) |
0 | 114 |
|
115 |
def datastore_get(self, key): |
|
116 |
if isinstance(key, basestring): |
|
117 |
key = Key(key) |
|
118 |
try: |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
119 |
gentity = self.transaction_data['_key_cache'][key] |
0 | 120 |
#self.critical('cached %s', gentity) |
121 |
except KeyError: |
|
122 |
gentity = Get(key) |
|
123 |
#self.critical('Get %s', gentity) |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
124 |
self.transaction_data.setdefault('_key_cache', {})[key] = gentity |
0 | 125 |
return gentity |
126 |
||
127 |
def clear_datastore_cache(self, key=None): |
|
128 |
if key is None: |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
129 |
self.transaction_data['_key_cache'] = {} |
0 | 130 |
else: |
131 |
if isinstance(key, basestring): |
|
132 |
key = Key(key) |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
133 |
self.transaction_data['_key_cache'].pop(key, None) |
0 | 134 |
|
135 |
from cubicweb.server.session import Session |
|
136 |
Session.set_entity_cache = set_entity_cache |
|
137 |
Session.entity_cache = entity_cache |
|
138 |
Session.drop_entity_cache = drop_entity_cache |
|
139 |
Session.datastore_get = datastore_get |
|
140 |
Session.clear_datastore_cache = clear_datastore_cache |
|
141 |
||
142 |
from docutils.frontend import OptionParser |
|
143 |
# avoid a call to expanduser which is not available under gae |
|
144 |
def get_standard_config_files(self): |
|
145 |
return self.standard_config_files |
|
146 |
OptionParser.get_standard_config_files = get_standard_config_files |