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