/usr/share/pyshared/tryton/common/domain_inversion.py is in tryton-client 2.2.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | #This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
import operator
import types
def in_(a, b):
if isinstance(a, (list, tuple)):
return any(operator.contains(b, x) for x in a)
else:
return operator.contains(b, a)
OPERATORS = {
'=': operator.eq,
'>': operator.gt,
'<': operator.lt,
'<=': operator.le,
'>=': operator.ge,
'!=': operator.ne,
'in': in_,
'not in': lambda a, b: not in_(b, a),
# Those operators are not supported (yet ?)
'like': lambda a, b: True,
'ilike': lambda a, b: True,
'not like': lambda a, b: True,
'not ilike': lambda a, b: True,
'child_of': lambda a, b: True,
'not child_of': lambda a, b: True,
}
def locale_part(expression, field_name):
if expression == field_name:
return 'id'
if '.' in expression:
fieldname, local = expression.split('.', 1)
return local
return expression
def is_leaf(expression):
return (isinstance(expression, (list, tuple))
and len(expression) > 2
and isinstance(expression[1], basestring)
and expression[1] in OPERATORS)
def eval_leaf(part, context, boolop=operator.and_):
field, operand, value = part[:3]
if '.' in field:
# In the case where the leaf concerns a m2o then having a value in the
# evaluation context is deemed suffisant
return bool(context.get(field.split('.')[0]))
if operand == '=' and not context[field] and boolop == operator.and_:
# We should consider that other domain inversion will set a correct
# value to this field
return True
return OPERATORS[operand](context[field], value)
def inverse_leaf(domain):
if domain in ('AND', 'OR'):
return domain
elif is_leaf(domain):
if 'child_of' in domain[1]:
if len(domain) == 3:
return domain
else:
return [domain[3]] + list(domain[1:])
return domain
else:
return map(inverse_leaf, domain)
def eval_domain(domain, context, boolop=operator.and_):
"compute domain boolean value according to the context"
if is_leaf(domain):
return eval_leaf(domain, context, boolop=boolop)
elif not domain and boolop is operator.and_:
return True
elif not domain and boolop is operator.or_:
return False
elif domain[0] == 'AND':
return eval_domain(domain[1:], context)
elif domain[0] == 'OR':
return eval_domain(domain[1:], context, operator.or_)
else:
return boolop(eval_domain(domain[0], context),
eval_domain(domain[1:], context, boolop))
def localize_domain(domain, field_name=None):
"returns only locale part of domain. eg: langage.code -> code"
if domain in ('AND', 'OR', True, False):
return domain
elif is_leaf(domain):
if 'child_of' in domain[1]:
if len(domain) == 3:
return domain
else:
return [domain[3]] + list(domain[1:-1])
return [locale_part(domain[0], field_name)] + list(domain[1:])
else:
return [localize_domain(part, field_name) for part in domain]
def unlocalize_domain(domain, fieldname):
if domain in ('AND', 'OR', True, False):
return domain
elif is_leaf(domain):
return ['%s.%s' % (fieldname, domain[0])] + list(domain[1:])
else:
return [unlocalize_domain(part, fieldname) for part in domain]
def simplify(domain):
"remove unused domain delimiter"
if is_leaf(domain):
return domain
elif domain in ('OR', 'AND'):
return domain
elif (isinstance(domain, list) and len(domain) == 1
and not is_leaf(domain[0])):
return simplify(domain[0])
elif (isinstance(domain, list) and len(domain) == 2
and domain[0] in ('AND', 'OR')):
return [simplify(domain[1])]
else:
return [simplify(branch) for branch in domain]
def merge(domain, domoperator=None):
if not domain or domain in ('AND', 'OR'):
return []
domain_type = 'OR' if domain[0] == 'OR' else 'AND'
if is_leaf(domain):
return [domain]
elif domoperator is None:
return [domain_type] + reduce(operator.add,
[merge(e, domain_type) for e in domain])
elif domain_type == domoperator:
return reduce(operator.add, [merge(e, domain_type) for e in domain])
else:
# without setting the domoperator
return [merge(domain)]
def parse(domain):
if is_leaf(domain):
return domain
elif not domain:
return And([])
elif domain[0] == 'OR':
return Or(domain[1:])
else:
return And(domain[1:] if domain[0] == 'AND' else domain)
def domain_inversion(domain, symbol, context=None):
"""compute an inversion of the domain eventually the context is used to
simplify the expression"""
if context is None:
context = {}
expression = parse(domain)
if symbol not in expression.variables:
return True
return expression.inverse(symbol, context)
class And(object):
def __init__(self, expressions):
self.branches = map(parse, expressions)
self.variables = set()
for expression in self.branches:
if is_leaf(expression):
self.variables.add(self.base(expression[0]))
elif isinstance(expression, And):
self.variables |= expression.variables
def base(self, expression):
return expression if '.' not in expression else expression.split('.')[0]
def inverse(self, symbol, context):
result = []
for part in self.branches:
if isinstance(part, And):
part_inversion = part.inverse(symbol, context)
evaluated = isinstance(part_inversion, types.BooleanType)
if not evaluated:
result.append(part_inversion)
elif part_inversion:
continue
else:
return False
elif is_leaf(part) and self.base(part[0]) == symbol:
result.append(part)
else:
field = part[0]
if (field not in context
or field in context
and eval_leaf(part, context, operator.and_)):
result.append(True)
else:
return False
result = filter(lambda e: e is not True, result)
if result == []:
return True
else:
return simplify(result)
class Or(And):
def inverse(self, symbol, context):
result = []
known_variables = set(context.keys())
if (symbol not in self.variables
and not known_variables >= self.variables):
# In this case we don't know anything about this OR part, we
# consider it to be True (because people will have the constraint on
# this part later).
return True
for part in self.branches:
if isinstance(part, And):
part_inversion = part.inverse(symbol, context)
evaluated = isinstance(part_inversion, types.BooleanType)
if symbol not in part.variables:
if evaluated and part_inversion:
return True
continue
if not evaluated:
result.append(part_inversion)
elif part_inversion:
return True
else:
continue
elif is_leaf(part) and self.base(part[0]) == symbol:
result.append(part)
else:
field = part[0]
field = self.base(field)
if (field in context
and eval_leaf(part, context, operator.or_)):
return True
elif (field in context
and not eval_leaf(part, context, operator.or_)):
result.append(False)
result = filter(lambda e: e is not False, result)
if result == []:
return False
else:
return simplify(['OR'] + result)
# Test stuffs
def test_simple_inversion():
domain = [['x', '=', 3]]
assert domain_inversion(domain, 'x') == [['x', '=', 3]]
domain = []
assert domain_inversion(domain, 'x') == True
assert domain_inversion(domain, 'y') == True
assert domain_inversion(domain, 'x', {'x': 5}) == True
assert domain_inversion(domain, 'z', {'x': 7}) == True
domain = [['x.id', '>', 5]]
assert domain_inversion(domain, 'x') == [['x.id', '>', 5]]
def test_and_inversion():
domain = [['x', '=', 3], ['y', '>', 5]]
assert domain_inversion(domain, 'x') == [['x', '=', 3]]
assert domain_inversion(domain, 'x', {'y': 4}) == False
assert domain_inversion(domain, 'x', {'y': 6}) == [['x', '=', 3]]
domain = [['x', '=', 3], ['y', '=', 5]]
assert domain_inversion(domain, 'z') == True
assert domain_inversion(domain, 'z', {'x': 2, 'y': 7}) == True
assert domain_inversion(domain, 'x', {'y': False}) == [['x', '=', 3]]
domain = [['x.id', '>', 5], ['y', '<', 3]]
assert domain_inversion(domain, 'y') == [['y', '<', 3]]
assert domain_inversion(domain, 'y', {'x': 3}) == [['y', '<', 3]]
assert domain_inversion(domain, 'x') == [['x.id', '>', 5]]
def test_or_inversion():
domain = ['OR', ['x', '=', 3], ['y', '>', 5], ['z', '=', 'abc']]
assert domain_inversion(domain, 'x') == [['x', '=', 3]]
assert domain_inversion(domain, 'x', {'y': 4}) == [['x', '=', 3]]
assert domain_inversion(domain, 'x', {'y': 4, 'z': 'ab'}) == [['x', '=', 3]]
assert domain_inversion(domain, 'x', {'y': 7}) == True
assert domain_inversion(domain, 'x', {'y': 7, 'z': 'b'}) == True
assert domain_inversion(domain, 'x', {'z': 'abc'}) == True
assert domain_inversion(domain, 'x', {'y': 4, 'z': 'abc'}) == True
domain = ['OR', ['x', '=', 3], ['y', '=', 5]]
assert domain_inversion(domain, 'x', {'y': False}) == [['x', '=', 3]]
domain = ['OR', ['x', '=', 3], ['y', '>', 5]]
assert domain_inversion(domain, 'z') == True
domain = ['OR', ['x.id', '>', 5], ['y', '<', 3]]
assert domain_inversion(domain, 'y') == [['y', '<', 3]]
assert domain_inversion(domain, 'y', {'z': 4}) == [['y', '<', 3]]
assert domain_inversion(domain, 'y', {'x': 3}) == True
domain = [u'OR', [u'length', u'>', 5], [u'language.code', u'=', u'de_DE']]
assert domain_inversion(domain, 'length', {'length': 0, 'name': 'n'}) ==\
[['length', '>', 5]]
def test_orand_inversion():
domain = ['OR', [['x', '=', 3], ['y', '>', 5], ['z', '=', 'abc']],
[['x', '=', 4]], [['y', '>', 6]]]
assert domain_inversion(domain, 'x') == True
assert domain_inversion(domain, 'x', {'y': 4}) == [[['x', '=', 4]]]
assert domain_inversion(domain, 'x', {'z': 'abc', 'y': 7}) == True
assert domain_inversion(domain, 'x', {'y': 7}) == True
assert domain_inversion(domain, 'x', {'z': 'ab'}) == True
def test_andor_inversion():
domain = [['OR', ['x', '=', 4], ['y', '>', 6]], ['z', '=', 3]]
assert domain_inversion(domain, 'z') == [['z', '=', 3]]
assert domain_inversion(domain, 'z', {'x': 5}) == [['z', '=', 3]]
assert domain_inversion(domain, 'z', {'x': 5, 'y': 5}) == False
assert domain_inversion(domain, 'z', {'x': 5, 'y': 7}) == [['z', '=', 3]]
def test_andand_inversion():
domain = [[['x', '=', 4], ['y', '>', 6]], ['z', '=', 3]]
assert domain_inversion(domain, 'z') == [['z', '=', 3]]
assert domain_inversion(domain, 'z', {'x': 5}) == False
assert domain_inversion(domain, 'z', {'y': 5}) == False
assert domain_inversion(domain, 'z', {'x': 4, 'y': 7}) == [['z', '=', 3]]
domain = [[['x', '=', 4], ['y', '>', 6], ['z', '=', 2]], [['w', '=', 2]]]
assert domain_inversion(domain, 'z', {'x': 4}) == [['z', '=', 2]]
def test_oror_inversion():
domain = ['OR', ['OR', ['x', '=', 3], ['y', '>', 5]],
['OR', ['x', '=', 2], ['z', '=', 'abc']],
['OR', ['y', '=', 8], ['z', '=', 'y']]]
assert domain_inversion(domain, 'x') == True
assert domain_inversion(domain, 'x', {'y': 4}) == True
assert domain_inversion(domain, 'x', {'z': 'ab'}) == True
assert domain_inversion(domain, 'x', {'y': 7}) == True
assert domain_inversion(domain, 'x', {'z': 'abc'}) == True
assert domain_inversion(domain, 'x', {'z': 'y'}) == True
assert domain_inversion(domain, 'x', {'y': 8}) == True
assert domain_inversion(domain, 'x', {'y': 8, 'z': 'b'}) == True
assert domain_inversion(domain, 'x', {'y': 4, 'z': 'y'}) == True
assert domain_inversion(domain, 'x', {'y': 7, 'z': 'abc'}) == True
assert domain_inversion(domain, 'x', {'y': 4, 'z': 'b'}) == \
['OR', [['x', '=', 3]], [['x', '=', 2]]]
def test_parse():
domain = parse([['x', '=', 5]])
assert domain.variables == set('x')
domain = parse(['OR', ['x', '=', 4], ['y', '>', 6]])
assert domain.variables == set('xy')
domain = parse([['OR', ['x', '=', 4], ['y', '>', 6]], ['z', '=', 3]])
assert domain.variables == set('xyz')
domain = parse([[['x', '=', 4], ['y', '>', 6]], ['z', '=', 3]])
assert domain.variables == set('xyz')
def test_simplify():
domain = [['x', '=', 3]]
assert simplify(domain) == [['x', '=', 3]]
domain = [[['x', '=', 3]]]
assert simplify(domain) == [['x', '=', 3]]
domain = ['OR', ['x', '=', 3]]
assert simplify(domain) == [['x', '=', 3]]
domain = ['OR', [['x', '=', 3]], [['y', '=', 5]]]
assert simplify(domain) == ['OR', [['x', '=', 3]], [['y', '=', 5]]]
domain = ['OR', ['x', '=', 3], ['AND', ['y', '=', 5]]]
assert simplify(domain) == ['OR', ['x', '=', 3], [['y', '=', 5]]]
def test_merge():
domain = [['x', '=', 6], ['y', '=', 7]]
assert merge(domain) == ['AND', ['x', '=', 6], ['y', '=', 7]]
domain = ['AND', ['x', '=', 6], ['y', '=', 7]]
assert merge(domain) == ['AND', ['x', '=', 6], ['y', '=', 7]]
domain = [['z', '=', 8], ['AND', ['x', '=', 6], ['y', '=', 7]]]
assert merge(domain) == ['AND', ['z', '=', 8], ['x', '=', 6],
['y', '=', 7]]
domain = ['OR', ['x', '=', 1], ['y', '=', 2], ['z', '=', 3]]
assert merge(domain) == ['OR', ['x', '=', 1], ['y', '=', 2],
['z', '=', 3]]
domain = ['OR', ['x', '=', 1], ['OR', ['y', '=', 2], ['z', '=', 3]]]
assert merge(domain) == ['OR', ['x', '=', 1], ['y', '=', 2],
['z', '=', 3]]
domain = ['OR', ['x', '=', 1], ['AND', ['y', '=', 2], ['z', '=', 3]]]
assert merge(domain) == ['OR', ['x', '=', 1], ['AND', ['y', '=', 2],
['z', '=', 3]]]
domain = [['z', '=', 8], ['OR', ['x', '=', 6], ['y', '=', 7]]]
assert merge(domain) == ['AND', ['z', '=', 8], ['OR', ['x', '=', 6],
['y', '=', 7]]]
domain = ['AND', ['OR', ['a', '=', 1], ['b', '=', 2]],
['OR', ['c', '=', 3], ['AND', ['d', '=', 4], ['d2', '=', 6]]],
['AND', ['d', '=', 5], ['e', '=', 6]], ['f', '=', 7]]
assert merge(domain) == ['AND', ['OR', ['a', '=', 1], ['b', '=', 2]],
['OR', ['c', '=', 3], ['AND', ['d', '=', 4], ['d2', '=', 6]]],
['d', '=', 5], ['e', '=', 6], ['f', '=', 7]]
def test_evaldomain():
domain = [['x', '>', 5]]
assert eval_domain(domain, {'x': 6})
assert not eval_domain(domain, {'x': 4})
domain = [['x', 'in', [3, 5]]]
assert eval_domain(domain, {'x': 3})
assert not eval_domain(domain, {'x': 4})
assert eval_domain(domain, {'x': [3]})
assert eval_domain(domain, {'x': [3, 4]})
assert not eval_domain(domain, {'x': [1, 2]})
domain = ['OR', ['x', '>', 10], ['x', '<', 0]]
assert eval_domain(domain, {'x': 11})
assert eval_domain(domain, {'x': -4})
assert not eval_domain(domain, {'x': 5})
domain = [['x', '>', 0], ['OR', ['x', '=', 3], ['x', '=', 2]]]
assert not eval_domain(domain, {'x': 1})
assert eval_domain(domain, {'x': 3})
assert eval_domain(domain, {'x': 2})
assert not eval_domain(domain, {'x': 4})
assert not eval_domain(domain, {'x': 5})
assert not eval_domain(domain, {'x': 6})
domain = ['OR', ['x', '=', 4], [['x', '>', 6], ['x', '<', 10]]]
assert eval_domain(domain, {'x': 4})
assert eval_domain(domain, {'x': 7})
assert not eval_domain(domain, {'x': 3})
assert not eval_domain(domain, {'x': 5})
assert not eval_domain(domain, {'x': 11})
def test_localize():
domain = [['x', '=', 5]]
assert localize_domain(domain) == [['x', '=', 5]]
domain = [['x', '=', 5], ['x.code', '=', 7]]
assert localize_domain(domain, 'x') == [['id', '=', 5], ['code', '=', 7]]
domain = ['OR', ['AND', ['x', '>', 7], ['x', '<', 15]], ['x.code', '=', 8]]
assert localize_domain(domain, 'x') == \
['OR', ['AND', ['id', '>', 7], ['id', '<', 15]], ['code', '=', 8]]
domain = [['x', 'child_of', [1]]]
assert localize_domain(domain, 'x') == [['x', 'child_of', [1]]]
domain = [['x', 'child_of', [1], 'y']]
assert localize_domain(domain, 'x') == [['y', 'child_of', [1]]]
if __name__ == '__main__':
test_simple_inversion()
test_and_inversion()
test_or_inversion()
test_orand_inversion()
test_andor_inversion()
test_andand_inversion()
test_oror_inversion()
test_parse()
test_simplify()
test_evaldomain()
test_localize()
|