From ea60a8bdf3eb23c598315c91a8408363f0ff207a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 21 Aug 2011 11:22:23 +0200 Subject: [PATCH] Update pyson with trytond one --- tryton/pyson.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/tryton/pyson.py b/tryton/pyson.py index 2c639052e2..16c9c364d6 100644 --- a/tryton/pyson.py +++ b/tryton/pyson.py @@ -18,6 +18,64 @@ class PYSON(object): def eval(dct, context): raise NotImplementedError + def __invert__(self): + if self.types()!= set([bool]): + return Not(Bool(self)) + else: + return Not(self) + + def __and__(self, other): + if (isinstance(self, And) + and not isinstance(self, Or)): + self._statements.append(other) + return self + if (isinstance(other, PYSON) + and other.types() != set([bool])): + other = Bool(other) + if self.types() != set([bool]): + return And(Bool(self), other) + else: + return And(self, other) + + def __or__(self, other): + if isinstance(self, Or): + self._statements.append(other) + return self + if (isinstance(other, PYSON) + and other.types() != set([bool])): + other = Bool(other) + if self.types() != set([bool]): + return Or(Bool(self), other) + else: + return Or(self, other) + + def __eq__(self, other): + return Equal(self, other) + + def __ne__(self, other): + return Not(Equal(self, other)) + + def __gt__(self, other): + return Greater(self, other) + + def __ge__(self, other): + return Greater(self, other, True) + + def __lt__(self, other): + return Less(self, other) + + def __le__(self, other): + return Less(self, other, True) + + def get(self, k, d=''): + return Get(self, k, d) + + def in_(self, obj): + return In(self, obj) + + def contains(self, k): + return In(k, self) + class PYSONEncoder(json.JSONEncoder): @@ -129,7 +187,7 @@ class And(PYSON): assert isinstance(statement, bool), \ 'statement must be boolean' assert len(statements) >= 2, 'must have at least 2 statements' - self._statements = statements + self._statements = list(statements) def pyson(self): return { @@ -403,7 +461,7 @@ class Date(PYSON): date = date.replace(year=year) if dct['dM']: month = date.month + dct['dM'] - year = date.year + month / 12 + year = date.year + month // 12 month = month % 12 date = date.replace(year=year, month=month) if dct['dd']: