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