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