diff --git a/proteus/pyson.py b/proteus/pyson.py index f17191db78..b89741bda2 100644 --- a/proteus/pyson.py +++ b/proteus/pyson.py @@ -1,13 +1,13 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. __all__ = ['PYSONEncoder', 'PYSONDecoder', 'Eval', 'Not', 'Bool', 'And', 'Or', - 'Equal', 'Greater', 'Less', 'If', 'Get', 'In', 'Date', 'DateTime'] -import sys -if sys.version_info < (2, 6): + 'Equal', 'Greater', 'Less', 'If', 'Get', 'In', 'Date', 'DateTime'] +try: import simplejson as json -else: +except ImportError: import json import datetime +from functools import reduce class PYSON(object): @@ -22,6 +22,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): @@ -133,7 +191,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 { @@ -408,7 +466,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']: diff --git a/setup.py b/setup.py index 4d1e957540..1683241e56 100644 --- a/setup.py +++ b/setup.py @@ -39,6 +39,7 @@ setup(name='proteus', extras_require={ 'trytond': ['trytond >= %s.%s, < %s.%s' % (major_version, minor_version, major_version, minor_version + 1)], + 'simplejson': ['simplejson'], }, test_suite='proteus.tests', tests_require=('trytond >= %s.%s, < %s.%s' %