author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 18 Dec 2009 10:26:47 +0100 | |
branch | stable |
changeset 4132 | 440d383367eb |
parent 3749 | a84e798b99f6 |
child 4053 | 7cc66b1d9183 |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: iso-8859-1 -*- |
2 |
"""This modules defines func / methods for creating test repositories |
|
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
: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
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
11 |
from random import randint, choice |
|
12 |
from copy import deepcopy |
|
2118
0012d997b992
should use time here, not timedelta
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
13 |
from datetime import datetime, date, time#timedelta |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
428
diff
changeset
|
14 |
from decimal import Decimal |
0 | 15 |
|
16 |
from yams.constraints import (SizeConstraint, StaticVocabularyConstraint, |
|
17 |
IntervalBoundConstraint) |
|
18 |
from rql.utils import decompose_b26 as base_decompose_b26 |
|
19 |
||
20 |
from cubicweb import Binary |
|
21 |
from cubicweb.schema import RQLConstraint |
|
22 |
||
23 |
def decompose_b26(index, ascii=False): |
|
24 |
"""return a letter (base-26) decomposition of index""" |
|
25 |
if ascii: |
|
26 |
return base_decompose_b26(index) |
|
27 |
return base_decompose_b26(index, u'ïż½abcdefghijklmnopqrstuvwxyz') |
|
28 |
||
29 |
def get_choices(eschema, attrname): |
|
30 |
"""returns possible choices for 'attrname' |
|
31 |
if attrname doesn't have ChoiceConstraint, return None |
|
32 |
""" |
|
33 |
for cst in eschema.constraints(attrname): |
|
34 |
if isinstance(cst, StaticVocabularyConstraint): |
|
35 |
return cst.vocabulary() |
|
36 |
return None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
37 |
|
0 | 38 |
|
39 |
def get_max_length(eschema, attrname): |
|
40 |
"""returns the maximum length allowed for 'attrname'""" |
|
41 |
for cst in eschema.constraints(attrname): |
|
42 |
if isinstance(cst, SizeConstraint) and cst.max: |
|
43 |
return cst.max |
|
44 |
return 300 |
|
45 |
#raise AttributeError('No Size constraint on attribute "%s"' % attrname) |
|
46 |
||
47 |
def get_bounds(eschema, attrname): |
|
48 |
for cst in eschema.constraints(attrname): |
|
49 |
if isinstance(cst, IntervalBoundConstraint): |
|
50 |
return cst.minvalue, cst.maxvalue |
|
51 |
return None, None |
|
52 |
||
53 |
||
54 |
_GENERATED_VALUES = {} |
|
55 |
||
56 |
class _ValueGenerator(object): |
|
57 |
"""generates integers / dates / strings / etc. to fill a DB table""" |
|
58 |
||
59 |
def __init__(self, eschema, choice_func=None): |
|
60 |
"""<choice_func> is a function that returns a list of possible |
|
61 |
choices for a given entity type and an attribute name. It should |
|
62 |
looks like : |
|
63 |
def values_for(etype, attrname): |
|
64 |
# some stuff ... |
|
65 |
return alist_of_acceptable_values # or None |
|
66 |
""" |
|
67 |
self.e_schema = eschema |
|
68 |
self.choice_func = choice_func |
|
69 |
||
70 |
def _generate_value(self, attrname, index, **kwargs): |
|
71 |
if not self.e_schema.has_unique_values(attrname): |
|
72 |
return self.__generate_value(attrname, index, **kwargs) |
|
73 |
value = self.__generate_value(attrname, index, **kwargs) |
|
74 |
while value in _GENERATED_VALUES.get((self.e_schema.type, attrname), ()): |
|
75 |
index += 1 |
|
76 |
value = self.__generate_value(attrname, index, **kwargs) |
|
77 |
_GENERATED_VALUES.setdefault((self.e_schema.type, attrname), set()).add(value) |
|
78 |
return value |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
79 |
|
0 | 80 |
def __generate_value(self, attrname, index, **kwargs): |
81 |
"""generates a consistent value for 'attrname'""" |
|
82 |
attrtype = str(self.e_schema.destination(attrname)).lower() |
|
83 |
# Before calling generate_%s functions, try to find values domain |
|
84 |
etype = self.e_schema.type |
|
85 |
if self.choice_func is not None: |
|
86 |
values_domain = self.choice_func(etype, attrname) |
|
87 |
if values_domain is not None: |
|
88 |
return choice(values_domain) |
|
89 |
gen_func = getattr(self, 'generate_%s_%s' % (self.e_schema.type, attrname), None) |
|
90 |
if gen_func is None: |
|
91 |
gen_func = getattr(self, 'generate_Any_%s' % attrname, None) |
|
92 |
if gen_func is not None: |
|
93 |
return gen_func(index, **kwargs) |
|
94 |
# If no specific values domain, then generate a dummy value |
|
95 |
gen_func = getattr(self, 'generate_%s' % (attrtype)) |
|
96 |
return gen_func(attrname, index, **kwargs) |
|
97 |
||
98 |
def generate_choice(self, attrname, index): |
|
99 |
"""generates a consistent value for 'attrname' if it's a choice""" |
|
100 |
choices = get_choices(self.e_schema, attrname) |
|
101 |
if choices is None: |
|
102 |
return None |
|
103 |
return unicode(choice(choices)) # FIXME |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
104 |
|
0 | 105 |
def generate_string(self, attrname, index, format=None): |
106 |
"""generates a consistent value for 'attrname' if it's a string""" |
|
107 |
# First try to get choices |
|
108 |
choosed = self.generate_choice(attrname, index) |
|
109 |
if choosed is not None: |
|
110 |
return choosed |
|
111 |
# All other case, generate a default string |
|
112 |
attrlength = get_max_length(self.e_schema, attrname) |
|
113 |
num_len = numlen(index) |
|
114 |
if num_len >= attrlength: |
|
115 |
ascii = self.e_schema.rproperty(attrname, 'internationalizable') |
|
116 |
return ('&'+decompose_b26(index, ascii))[:attrlength] |
|
117 |
# always use plain text when no format is specified |
|
118 |
attrprefix = attrname[:max(attrlength-num_len-1, 0)] |
|
119 |
if format == 'text/html': |
|
120 |
value = u'<span>ïż½%s<b>%d</b></span>' % (attrprefix, index) |
|
121 |
elif format == 'text/rest': |
|
122 |
value = u""" |
|
123 |
title |
|
124 |
----- |
|
125 |
||
126 |
* %s |
|
127 |
* %d |
|
128 |
* ïż½& |
|
129 |
""" % (attrprefix, index) |
|
130 |
else: |
|
131 |
value = u'ïż½&%s%d' % (attrprefix, index) |
|
132 |
return value[:attrlength] |
|
133 |
||
134 |
def generate_password(self, attrname, index): |
|
135 |
"""generates a consistent value for 'attrname' if it's a password""" |
|
136 |
return u'toto' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
137 |
|
0 | 138 |
def generate_integer(self, attrname, index): |
139 |
"""generates a consistent value for 'attrname' if it's an integer""" |
|
428
7d80331a91d7
consider vocabulary of Int fields
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
140 |
choosed = self.generate_choice(attrname, index) |
7d80331a91d7
consider vocabulary of Int fields
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
141 |
if choosed is not None: |
7d80331a91d7
consider vocabulary of Int fields
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
142 |
return choosed |
0 | 143 |
minvalue, maxvalue = get_bounds(self.e_schema, attrname) |
144 |
if maxvalue is not None and maxvalue <= 0 and minvalue is None: |
|
145 |
minvalue = maxvalue - index # i.e. randint(-index, 0) |
|
146 |
else: |
|
147 |
maxvalue = maxvalue or index |
|
148 |
return randint(minvalue or 0, maxvalue) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
149 |
|
0 | 150 |
generate_int = generate_integer |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
151 |
|
0 | 152 |
def generate_float(self, attrname, index): |
153 |
"""generates a consistent value for 'attrname' if it's a float""" |
|
154 |
return float(randint(-index, index)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
155 |
|
0 | 156 |
def generate_decimal(self, attrname, index): |
157 |
"""generates a consistent value for 'attrname' if it's a float""" |
|
158 |
return Decimal(str(self.generate_float(attrname, index))) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
159 |
|
0 | 160 |
def generate_date(self, attrname, index): |
161 |
"""generates a random date (format is 'yyyy-mm-dd')""" |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
428
diff
changeset
|
162 |
return date(randint(2000, 2004), randint(1, 12), randint(1, 28)) |
0 | 163 |
|
164 |
def generate_time(self, attrname, index): |
|
165 |
"""generates a random time (format is ' HH:MM')""" |
|
2118
0012d997b992
should use time here, not timedelta
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
166 |
return time(11, index%60) #'11:%02d' % (index % 60) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
167 |
|
0 | 168 |
def generate_datetime(self, attrname, index): |
169 |
"""generates a random date (format is 'yyyy-mm-dd HH:MM')""" |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
428
diff
changeset
|
170 |
return datetime(randint(2000, 2004), randint(1, 12), randint(1, 28), 11, index%60) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
171 |
|
0 | 172 |
|
173 |
def generate_bytes(self, attrname, index, format=None): |
|
174 |
# modpython way |
|
175 |
fakefile = Binary("%s%s" % (attrname, index)) |
|
3541
63ba0a669c4f
unicode file name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
176 |
fakefile.filename = u"file_%s" % attrname |
0 | 177 |
fakefile.value = fakefile.getvalue() |
178 |
return fakefile |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
179 |
|
0 | 180 |
def generate_boolean(self, attrname, index): |
181 |
"""generates a consistent value for 'attrname' if it's a boolean""" |
|
182 |
return index % 2 == 0 |
|
183 |
||
184 |
def generate_Any_data_format(self, index, **kwargs): |
|
185 |
# data_format attribute of Image/File has no vocabulary constraint, we |
|
186 |
# need this method else stupid values will be set which make mtconverter |
|
187 |
# raise exception |
|
188 |
return u'application/octet-stream' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
189 |
|
0 | 190 |
def generate_Any_content_format(self, index, **kwargs): |
191 |
# content_format attribute of EmailPart has no vocabulary constraint, we |
|
192 |
# need this method else stupid values will be set which make mtconverter |
|
193 |
# raise exception |
|
194 |
return u'text/plain' |
|
195 |
||
196 |
def generate_Image_data_format(self, index, **kwargs): |
|
197 |
# data_format attribute of Image/File has no vocabulary constraint, we |
|
198 |
# need this method else stupid values will be set which make mtconverter |
|
199 |
# raise exception |
|
200 |
return u'image/png' |
|
201 |
||
202 |
||
203 |
class autoextend(type): |
|
204 |
def __new__(mcs, name, bases, classdict): |
|
205 |
for attrname, attrvalue in classdict.items(): |
|
206 |
if callable(attrvalue): |
|
207 |
if attrname.startswith('generate_') and \ |
|
208 |
attrvalue.func_code.co_argcount < 2: |
|
209 |
raise TypeError('generate_xxx must accept at least 1 argument') |
|
210 |
setattr(_ValueGenerator, attrname, attrvalue) |
|
211 |
return type.__new__(mcs, name, bases, classdict) |
|
212 |
||
213 |
class ValueGenerator(_ValueGenerator): |
|
214 |
__metaclass__ = autoextend |
|
215 |
||
216 |
||
217 |
def _default_choice_func(etype, attrname): |
|
218 |
"""default choice_func for insert_entity_queries""" |
|
219 |
return None |
|
220 |
||
221 |
def insert_entity_queries(etype, schema, vreg, entity_num, |
|
222 |
choice_func=_default_choice_func): |
|
223 |
"""returns a list of 'add entity' queries (couples query, args) |
|
224 |
:type etype: str |
|
225 |
:param etype: the entity's type |
|
226 |
||
227 |
:type schema: cubicweb.schema.Schema |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2118
diff
changeset
|
228 |
:param schema: the instance schema |
0 | 229 |
|
230 |
:type entity_num: int |
|
231 |
:param entity_num: the number of entities to insert |
|
232 |
||
233 |
XXX FIXME: choice_func is here for *historical* reasons, it should |
|
234 |
probably replaced by a nicer way to specify choices |
|
235 |
:type choice_func: function |
|
236 |
:param choice_func: a function that takes an entity type, an attrname and |
|
237 |
returns acceptable values for this attribute |
|
238 |
""" |
|
239 |
# XXX HACK, remove or fix asap |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
240 |
if etype in (('String', 'Int', 'Float', 'Boolean', 'Date', 'CWGroup', 'CWUser')): |
0 | 241 |
return [] |
242 |
queries = [] |
|
243 |
for index in xrange(entity_num): |
|
244 |
restrictions = [] |
|
245 |
args = {} |
|
246 |
for attrname, value in make_entity(etype, schema, vreg, index, choice_func).items(): |
|
247 |
restrictions.append('X %s %%(%s)s' % (attrname, attrname)) |
|
248 |
args[attrname] = value |
|
249 |
if restrictions: |
|
250 |
queries.append(('INSERT %s X: %s' % (etype, ', '.join(restrictions)), |
|
251 |
args)) |
|
252 |
assert not 'eid' in args, args |
|
253 |
else: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
254 |
queries.append(('INSERT %s X' % etype, {})) |
0 | 255 |
return queries |
256 |
||
257 |
||
258 |
def make_entity(etype, schema, vreg, index=0, choice_func=_default_choice_func, |
|
259 |
form=False): |
|
260 |
"""generates a random entity and returns it as a dict |
|
261 |
||
262 |
by default, generate an entity to be inserted in the repository |
|
263 |
elif form, generate an form dictionnary to be given to a web controller |
|
264 |
""" |
|
265 |
eschema = schema.eschema(etype) |
|
266 |
valgen = ValueGenerator(eschema, choice_func) |
|
267 |
entity = {} |
|
268 |
# preprocessing to deal with _format fields |
|
269 |
attributes = [] |
|
270 |
relatedfields = {} |
|
271 |
for rschema, attrschema in eschema.attribute_definitions(): |
|
272 |
attrname = rschema.type |
|
273 |
if attrname == 'eid': |
|
274 |
# don't specify eids ! |
|
275 |
continue |
|
276 |
if attrname.endswith('_format') and attrname[:-7] in eschema.subject_relations(): |
|
277 |
relatedfields[attrname[:-7]] = attrschema |
|
278 |
else: |
|
279 |
attributes.append((attrname, attrschema)) |
|
280 |
for attrname, attrschema in attributes: |
|
281 |
if attrname in relatedfields: |
|
282 |
# first generate a format and record it |
|
283 |
format = valgen._generate_value(attrname + '_format', index) |
|
284 |
entity[attrname + '_format'] = format |
|
285 |
# then a value coherent with this format |
|
286 |
value = valgen._generate_value(attrname, index, format=format) |
|
287 |
else: |
|
288 |
value = valgen._generate_value(attrname, index) |
|
289 |
if form: # need to encode values |
|
290 |
if attrschema.type == 'Bytes': |
|
291 |
# twisted way |
|
292 |
fakefile = value |
|
293 |
filename = value.filename |
|
294 |
value = (filename, u"text/plain", fakefile) |
|
295 |
elif attrschema.type == 'Date': |
|
296 |
value = value.strftime(vreg.property_value('ui.date-format')) |
|
297 |
elif attrschema.type == 'Datetime': |
|
298 |
value = value.strftime(vreg.property_value('ui.datetime-format')) |
|
299 |
elif attrschema.type == 'Time': |
|
300 |
value = value.strftime(vreg.property_value('ui.time-format')) |
|
301 |
elif attrschema.type == 'Float': |
|
302 |
fmt = vreg.property_value('ui.float-format') |
|
303 |
value = fmt % value |
|
304 |
else: |
|
305 |
value = unicode(value) |
|
306 |
entity[attrname] = value |
|
307 |
return entity |
|
308 |
||
309 |
||
310 |
||
3742
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
311 |
def select(constraints, cursor, selectvar='O', objtype=None): |
0 | 312 |
"""returns list of eids matching <constraints> |
313 |
||
314 |
<selectvar> should be either 'O' or 'S' to match schema definitions |
|
315 |
""" |
|
316 |
try: |
|
3742
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
317 |
rql = 'Any %s WHERE %s' % (selectvar, constraints) |
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
318 |
if objtype: |
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
319 |
rql += ', %s is %s' % (selectvar, objtype) |
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
320 |
rset = cursor.execute(rql) |
0 | 321 |
except: |
322 |
print "could restrict eid_list with given constraints (%r)" % constraints |
|
323 |
return [] |
|
324 |
return set(eid for eid, in rset.rows) |
|
325 |
||
326 |
||
327 |
||
328 |
def make_relations_queries(schema, edict, cursor, ignored_relations=(), |
|
329 |
existingrels=None): |
|
330 |
"""returns a list of generated RQL queries for relations |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2118
diff
changeset
|
331 |
:param schema: The instance schema |
0 | 332 |
|
333 |
:param e_dict: mapping between etypes and eids |
|
334 |
||
335 |
:param ignored_relations: list of relations to ignore (i.e. don't try |
|
336 |
to generate insert queries for these relations) |
|
337 |
""" |
|
338 |
gen = RelationsQueriesGenerator(schema, cursor, existingrels) |
|
339 |
return gen.compute_queries(edict, ignored_relations) |
|
340 |
||
3742
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
341 |
def composite_relation(rschema): |
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
342 |
for obj in rschema.objects(): |
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
343 |
if obj.objrproperty(rschema, 'composite') == 'subject': |
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
344 |
return True |
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
345 |
for obj in rschema.subjects(): |
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
346 |
if obj.subjrproperty(rschema, 'composite') == 'object': |
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
347 |
return True |
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
348 |
return False |
0 | 349 |
|
350 |
class RelationsQueriesGenerator(object): |
|
351 |
rql_tmpl = 'SET S %s O WHERE S eid %%(subjeid)s, O eid %%(objeid)s' |
|
352 |
def __init__(self, schema, cursor, existing=None): |
|
353 |
self.schema = schema |
|
354 |
self.cursor = cursor |
|
355 |
self.existingrels = existing or {} |
|
356 |
||
357 |
def compute_queries(self, edict, ignored_relations): |
|
358 |
queries = [] |
|
359 |
# 1/ skip final relations and explictly ignored relations |
|
3742
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
360 |
rels = sorted([rschema for rschema in self.schema.relations() |
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
361 |
if not (rschema.final or rschema in ignored_relations)], |
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
362 |
key=lambda x:not composite_relation(x)) |
0 | 363 |
# for each relation |
364 |
# 2/ take each possible couple (subj, obj) |
|
365 |
# 3/ analyze cardinality of relation |
|
366 |
# a/ if relation is mandatory, insert one relation |
|
367 |
# b/ else insert N relations where N is the mininum |
|
368 |
# of 20 and the number of existing targetable entities |
|
369 |
for rschema in rels: |
|
370 |
sym = set() |
|
371 |
sedict = deepcopy(edict) |
|
372 |
oedict = deepcopy(edict) |
|
373 |
delayed = [] |
|
374 |
# for each couple (subjschema, objschema), insert relations |
|
375 |
for subj, obj in rschema.iter_rdefs(): |
|
376 |
sym.add( (subj, obj) ) |
|
377 |
if rschema.symetric and (obj, subj) in sym: |
|
378 |
continue |
|
379 |
subjcard, objcard = rschema.rproperty(subj, obj, 'cardinality') |
|
380 |
# process mandatory relations first |
|
3742
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
381 |
if subjcard in '1+' or objcard in '1+' or composite_relation(rschema): |
3749
a84e798b99f6
turn make_relations_query into a true generator (this allows interleaving of actual entities linking and query generation)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3747
diff
changeset
|
382 |
for query, args in self.make_relation_queries(sedict, oedict, |
a84e798b99f6
turn make_relations_query into a true generator (this allows interleaving of actual entities linking and query generation)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3747
diff
changeset
|
383 |
rschema, subj, obj): |
a84e798b99f6
turn make_relations_query into a true generator (this allows interleaving of actual entities linking and query generation)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3747
diff
changeset
|
384 |
yield query, args |
0 | 385 |
else: |
386 |
delayed.append( (subj, obj) ) |
|
387 |
for subj, obj in delayed: |
|
3749
a84e798b99f6
turn make_relations_query into a true generator (this allows interleaving of actual entities linking and query generation)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3747
diff
changeset
|
388 |
for query, args in self.make_relation_queries(sedict, oedict, rschema, |
a84e798b99f6
turn make_relations_query into a true generator (this allows interleaving of actual entities linking and query generation)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3747
diff
changeset
|
389 |
subj, obj): |
a84e798b99f6
turn make_relations_query into a true generator (this allows interleaving of actual entities linking and query generation)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3747
diff
changeset
|
390 |
yield query, args |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
391 |
|
0 | 392 |
def qargs(self, subjeids, objeids, subjcard, objcard, subjeid, objeid): |
393 |
if subjcard in '?1': |
|
394 |
subjeids.remove(subjeid) |
|
395 |
if objcard in '?1': |
|
396 |
objeids.remove(objeid) |
|
397 |
return {'subjeid' : subjeid, 'objeid' : objeid} |
|
398 |
||
399 |
def make_relation_queries(self, sedict, oedict, rschema, subj, obj): |
|
400 |
subjcard, objcard = rschema.rproperty(subj, obj, 'cardinality') |
|
401 |
subjeids = sedict.get(subj, frozenset()) |
|
402 |
used = self.existingrels[rschema.type] |
|
403 |
preexisting_subjrels = set(subj for subj, obj in used) |
|
404 |
preexisting_objrels = set(obj for subj, obj in used) |
|
405 |
# if there are constraints, only select appropriate objeids |
|
406 |
q = self.rql_tmpl % rschema.type |
|
407 |
constraints = [c for c in rschema.rproperty(subj, obj, 'constraints') |
|
408 |
if isinstance(c, RQLConstraint)] |
|
409 |
if constraints: |
|
410 |
restrictions = ', '.join(c.restriction for c in constraints) |
|
411 |
q += ', %s' % restrictions |
|
412 |
# restrict object eids if possible |
|
3711
486a29d66f2a
work around a problem in constraint application
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3689
diff
changeset
|
413 |
# XXX the attempt to restrict below in completely wrong |
486a29d66f2a
work around a problem in constraint application
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3689
diff
changeset
|
414 |
# disabling it for now |
3742
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3712
diff
changeset
|
415 |
objeids = select(restrictions, self.cursor, objtype=obj) |
3712
4b8d7838d74d
revert change, but there's something fishy here
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3711
diff
changeset
|
416 |
else: |
4b8d7838d74d
revert change, but there's something fishy here
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3711
diff
changeset
|
417 |
objeids = oedict.get(obj, frozenset()) |
0 | 418 |
if subjcard in '?1' or objcard in '?1': |
419 |
for subjeid, objeid in used: |
|
420 |
if subjcard in '?1' and subjeid in subjeids: |
|
421 |
subjeids.remove(subjeid) |
|
3673
9342e6783bd2
[testlib] hummm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3541
diff
changeset
|
422 |
# XXX why? |
9342e6783bd2
[testlib] hummm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3541
diff
changeset
|
423 |
#if objeid in objeids: |
9342e6783bd2
[testlib] hummm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3541
diff
changeset
|
424 |
# objeids.remove(objeid) |
0 | 425 |
if objcard in '?1' and objeid in objeids: |
426 |
objeids.remove(objeid) |
|
3673
9342e6783bd2
[testlib] hummm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3541
diff
changeset
|
427 |
# XXX why? |
9342e6783bd2
[testlib] hummm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3541
diff
changeset
|
428 |
#if subjeid in subjeids: |
9342e6783bd2
[testlib] hummm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3541
diff
changeset
|
429 |
# subjeids.remove(subjeid) |
0 | 430 |
if not subjeids: |
431 |
check_card_satisfied(objcard, objeids, subj, rschema, obj) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
432 |
return |
0 | 433 |
if not objeids: |
434 |
check_card_satisfied(subjcard, subjeids, subj, rschema, obj) |
|
435 |
return |
|
436 |
if subjcard in '?1+': |
|
437 |
for subjeid in tuple(subjeids): |
|
438 |
# do not insert relation if this entity already has a relation |
|
439 |
if subjeid in preexisting_subjrels: |
|
440 |
continue |
|
441 |
objeid = choose_eid(objeids, subjeid) |
|
442 |
if objeid is None or (subjeid, objeid) in used: |
|
443 |
continue |
|
444 |
yield q, self.qargs(subjeids, objeids, subjcard, objcard, |
|
445 |
subjeid, objeid) |
|
446 |
used.add( (subjeid, objeid) ) |
|
447 |
if not objeids: |
|
448 |
check_card_satisfied(subjcard, subjeids, subj, rschema, obj) |
|
449 |
break |
|
450 |
elif objcard in '?1+': |
|
451 |
for objeid in tuple(objeids): |
|
452 |
# do not insert relation if this entity already has a relation |
|
453 |
if objeid in preexisting_objrels: |
|
454 |
continue |
|
455 |
subjeid = choose_eid(subjeids, objeid) |
|
456 |
if subjeid is None or (subjeid, objeid) in used: |
|
457 |
continue |
|
458 |
yield q, self.qargs(subjeids, objeids, subjcard, objcard, |
|
459 |
subjeid, objeid) |
|
460 |
used.add( (subjeid, objeid) ) |
|
461 |
if not subjeids: |
|
462 |
check_card_satisfied(objcard, objeids, subj, rschema, obj) |
|
463 |
break |
|
464 |
else: |
|
465 |
# FIXME: 20 should be read from config |
|
466 |
subjeidsiter = [choice(tuple(subjeids)) for i in xrange(min(len(subjeids), 20))] |
|
467 |
objeidsiter = [choice(tuple(objeids)) for i in xrange(min(len(objeids), 20))] |
|
468 |
for subjeid, objeid in zip(subjeidsiter, objeidsiter): |
|
469 |
if subjeid != objeid and not (subjeid, objeid) in used: |
|
470 |
used.add( (subjeid, objeid) ) |
|
471 |
yield q, self.qargs(subjeids, objeids, subjcard, objcard, |
|
472 |
subjeid, objeid) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
473 |
|
0 | 474 |
def check_card_satisfied(card, remaining, subj, rschema, obj): |
475 |
if card in '1+' and remaining: |
|
3749
a84e798b99f6
turn make_relations_query into a true generator (this allows interleaving of actual entities linking and query generation)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3747
diff
changeset
|
476 |
raise Exception("can't satisfy cardinality %s for relation %s %s %s" % |
a84e798b99f6
turn make_relations_query into a true generator (this allows interleaving of actual entities linking and query generation)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3747
diff
changeset
|
477 |
(card, subj, rschema, obj)) |
a84e798b99f6
turn make_relations_query into a true generator (this allows interleaving of actual entities linking and query generation)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3747
diff
changeset
|
478 |
|
0 | 479 |
|
480 |
def choose_eid(values, avoid): |
|
481 |
values = tuple(values) |
|
482 |
if len(values) == 1 and values[0] == avoid: |
|
483 |
return None |
|
484 |
objeid = choice(values) |
|
485 |
while objeid == avoid: # avoid infinite recursion like in X comment X |
|
486 |
objeid = choice(values) |
|
487 |
return objeid |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
488 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
489 |
|
0 | 490 |
|
491 |
# UTILITIES FUNCS ############################################################## |
|
492 |
def make_tel(num_tel): |
|
493 |
"""takes an integer, converts is as a string and inserts |
|
494 |
white spaces each 2 chars (french notation) |
|
495 |
""" |
|
496 |
num_list = list(str(num_tel)) |
|
497 |
for index in (6, 4, 2): |
|
498 |
num_list.insert(index, ' ') |
|
499 |
||
500 |
return ''.join(num_list) |
|
501 |
||
502 |
||
503 |
def numlen(number): |
|
504 |
"""returns the number's length""" |
|
505 |
return len(str(number)) |