author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 28 Sep 2009 18:58:12 +0200 | |
branch | stable |
changeset 3506 | f0ec5d17f470 |
parent 2476 | 1294a6bdf3bf |
child 3541 | 63ba0a669c4f |
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)) |
|
176 |
fakefile.filename = "file_%s" % attrname |
|
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 |
||
311 |
def select(constraints, cursor, selectvar='O'): |
|
312 |
"""returns list of eids matching <constraints> |
|
313 |
||
314 |
<selectvar> should be either 'O' or 'S' to match schema definitions |
|
315 |
""" |
|
316 |
try: |
|
317 |
rset = cursor.execute('Any %s WHERE %s' % (selectvar, constraints)) |
|
318 |
except: |
|
319 |
print "could restrict eid_list with given constraints (%r)" % constraints |
|
320 |
return [] |
|
321 |
return set(eid for eid, in rset.rows) |
|
322 |
||
323 |
||
324 |
||
325 |
def make_relations_queries(schema, edict, cursor, ignored_relations=(), |
|
326 |
existingrels=None): |
|
327 |
"""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
|
328 |
:param schema: The instance schema |
0 | 329 |
|
330 |
:param e_dict: mapping between etypes and eids |
|
331 |
||
332 |
:param ignored_relations: list of relations to ignore (i.e. don't try |
|
333 |
to generate insert queries for these relations) |
|
334 |
""" |
|
335 |
gen = RelationsQueriesGenerator(schema, cursor, existingrels) |
|
336 |
return gen.compute_queries(edict, ignored_relations) |
|
337 |
||
338 |
||
339 |
class RelationsQueriesGenerator(object): |
|
340 |
rql_tmpl = 'SET S %s O WHERE S eid %%(subjeid)s, O eid %%(objeid)s' |
|
341 |
def __init__(self, schema, cursor, existing=None): |
|
342 |
self.schema = schema |
|
343 |
self.cursor = cursor |
|
344 |
self.existingrels = existing or {} |
|
345 |
||
346 |
def compute_queries(self, edict, ignored_relations): |
|
347 |
queries = [] |
|
348 |
# 1/ skip final relations and explictly ignored relations |
|
349 |
rels = [rschema for rschema in self.schema.relations() |
|
350 |
if not (rschema.is_final() or rschema in ignored_relations)] |
|
351 |
# for each relation |
|
352 |
# 2/ take each possible couple (subj, obj) |
|
353 |
# 3/ analyze cardinality of relation |
|
354 |
# a/ if relation is mandatory, insert one relation |
|
355 |
# b/ else insert N relations where N is the mininum |
|
356 |
# of 20 and the number of existing targetable entities |
|
357 |
for rschema in rels: |
|
358 |
sym = set() |
|
359 |
sedict = deepcopy(edict) |
|
360 |
oedict = deepcopy(edict) |
|
361 |
delayed = [] |
|
362 |
# for each couple (subjschema, objschema), insert relations |
|
363 |
for subj, obj in rschema.iter_rdefs(): |
|
364 |
sym.add( (subj, obj) ) |
|
365 |
if rschema.symetric and (obj, subj) in sym: |
|
366 |
continue |
|
367 |
subjcard, objcard = rschema.rproperty(subj, obj, 'cardinality') |
|
368 |
# process mandatory relations first |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
369 |
if subjcard in '1+' or objcard in '1+': |
0 | 370 |
queries += self.make_relation_queries(sedict, oedict, |
371 |
rschema, subj, obj) |
|
372 |
else: |
|
373 |
delayed.append( (subj, obj) ) |
|
374 |
for subj, obj in delayed: |
|
375 |
queries += self.make_relation_queries(sedict, oedict, rschema, |
|
376 |
subj, obj) |
|
377 |
return queries |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
378 |
|
0 | 379 |
def qargs(self, subjeids, objeids, subjcard, objcard, subjeid, objeid): |
380 |
if subjcard in '?1': |
|
381 |
subjeids.remove(subjeid) |
|
382 |
if objcard in '?1': |
|
383 |
objeids.remove(objeid) |
|
384 |
return {'subjeid' : subjeid, 'objeid' : objeid} |
|
385 |
||
386 |
def make_relation_queries(self, sedict, oedict, rschema, subj, obj): |
|
387 |
subjcard, objcard = rschema.rproperty(subj, obj, 'cardinality') |
|
388 |
subjeids = sedict.get(subj, frozenset()) |
|
389 |
used = self.existingrels[rschema.type] |
|
390 |
preexisting_subjrels = set(subj for subj, obj in used) |
|
391 |
preexisting_objrels = set(obj for subj, obj in used) |
|
392 |
# if there are constraints, only select appropriate objeids |
|
393 |
q = self.rql_tmpl % rschema.type |
|
394 |
constraints = [c for c in rschema.rproperty(subj, obj, 'constraints') |
|
395 |
if isinstance(c, RQLConstraint)] |
|
396 |
if constraints: |
|
397 |
restrictions = ', '.join(c.restriction for c in constraints) |
|
398 |
q += ', %s' % restrictions |
|
399 |
# restrict object eids if possible |
|
400 |
objeids = select(restrictions, self.cursor) |
|
401 |
else: |
|
402 |
objeids = oedict.get(obj, frozenset()) |
|
403 |
if subjcard in '?1' or objcard in '?1': |
|
404 |
for subjeid, objeid in used: |
|
405 |
if subjcard in '?1' and subjeid in subjeids: |
|
406 |
subjeids.remove(subjeid) |
|
407 |
if objeid in objeids: |
|
408 |
objeids.remove(objeid) |
|
409 |
if objcard in '?1' and objeid in objeids: |
|
410 |
objeids.remove(objeid) |
|
411 |
if subjeid in subjeids: |
|
412 |
subjeids.remove(subjeid) |
|
413 |
if not subjeids: |
|
414 |
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
|
415 |
return |
0 | 416 |
if not objeids: |
417 |
check_card_satisfied(subjcard, subjeids, subj, rschema, obj) |
|
418 |
return |
|
419 |
if subjcard in '?1+': |
|
420 |
for subjeid in tuple(subjeids): |
|
421 |
# do not insert relation if this entity already has a relation |
|
422 |
if subjeid in preexisting_subjrels: |
|
423 |
continue |
|
424 |
objeid = choose_eid(objeids, subjeid) |
|
425 |
if objeid is None or (subjeid, objeid) in used: |
|
426 |
continue |
|
427 |
yield q, self.qargs(subjeids, objeids, subjcard, objcard, |
|
428 |
subjeid, objeid) |
|
429 |
used.add( (subjeid, objeid) ) |
|
430 |
if not objeids: |
|
431 |
check_card_satisfied(subjcard, subjeids, subj, rschema, obj) |
|
432 |
break |
|
433 |
elif objcard in '?1+': |
|
434 |
for objeid in tuple(objeids): |
|
435 |
# do not insert relation if this entity already has a relation |
|
436 |
if objeid in preexisting_objrels: |
|
437 |
continue |
|
438 |
subjeid = choose_eid(subjeids, objeid) |
|
439 |
if subjeid is None or (subjeid, objeid) in used: |
|
440 |
continue |
|
441 |
yield q, self.qargs(subjeids, objeids, subjcard, objcard, |
|
442 |
subjeid, objeid) |
|
443 |
used.add( (subjeid, objeid) ) |
|
444 |
if not subjeids: |
|
445 |
check_card_satisfied(objcard, objeids, subj, rschema, obj) |
|
446 |
break |
|
447 |
else: |
|
448 |
# FIXME: 20 should be read from config |
|
449 |
subjeidsiter = [choice(tuple(subjeids)) for i in xrange(min(len(subjeids), 20))] |
|
450 |
objeidsiter = [choice(tuple(objeids)) for i in xrange(min(len(objeids), 20))] |
|
451 |
for subjeid, objeid in zip(subjeidsiter, objeidsiter): |
|
452 |
if subjeid != objeid and not (subjeid, objeid) in used: |
|
453 |
used.add( (subjeid, objeid) ) |
|
454 |
yield q, self.qargs(subjeids, objeids, subjcard, objcard, |
|
455 |
subjeid, objeid) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
456 |
|
0 | 457 |
def check_card_satisfied(card, remaining, subj, rschema, obj): |
458 |
if card in '1+' and remaining: |
|
459 |
raise Exception("can't satisfy cardinality %s for relation %s %s %s" |
|
460 |
% (card, subj, rschema, obj)) |
|
461 |
||
462 |
def choose_eid(values, avoid): |
|
463 |
values = tuple(values) |
|
464 |
if len(values) == 1 and values[0] == avoid: |
|
465 |
return None |
|
466 |
objeid = choice(values) |
|
467 |
while objeid == avoid: # avoid infinite recursion like in X comment X |
|
468 |
objeid = choice(values) |
|
469 |
return objeid |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
470 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
471 |
|
0 | 472 |
|
473 |
# UTILITIES FUNCS ############################################################## |
|
474 |
def make_tel(num_tel): |
|
475 |
"""takes an integer, converts is as a string and inserts |
|
476 |
white spaces each 2 chars (french notation) |
|
477 |
""" |
|
478 |
num_list = list(str(num_tel)) |
|
479 |
for index in (6, 4, 2): |
|
480 |
num_list.insert(index, ' ') |
|
481 |
||
482 |
return ''.join(num_list) |
|
483 |
||
484 |
||
485 |
def numlen(number): |
|
486 |
"""returns the number's length""" |
|
487 |
return len(str(number)) |