~openerp-dev/openobject-client/improve_attrs_for_oring_operation

« back to all changes in this revision

Viewing changes to bin/tools/__init__.py

  • Committer: HDA(OpenERP)
  • Author(s): ACH(OpenERP)
  • Date: 2010-05-11 12:31:13 UTC
  • Revision ID: hda@tinyerp.com-20100511123113-j9cmkz184b015lnb
added functionality to use '|' in attrs doamin

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
            result[attrs.item(i).localName] = eval(attrs.item(i).nodeValue)
69
69
    return result
70
70
 
71
 
#FIXME use spaces
72
 
def calc_condition(self,model,con):
73
 
    if model and (con[0] in model.mgroup.fields):
74
 
        val = model[con[0]].get(model)
75
 
        if con[1]=="=" or con[1]=="==":
76
 
            if val==con[2]:
77
 
                                return True
78
 
        elif con[1]=="!=" or con[1]=="<>":
79
 
                        if val!=con[2]:
80
 
                                return True
81
 
        elif con[1]=="<":
82
 
            if val<con[2]:
83
 
                                return True
84
 
        elif con[1]==">":
85
 
                        if val>con[2]:
86
 
                                return True
87
 
        elif con[1]=="<=":
88
 
                        if val<=con[2]:
89
 
                                return True
90
 
        elif con[1]==">=":
91
 
                        if val>=con[2]:
92
 
                                return True
93
 
        elif con[1].lower()=="in":
94
 
                        for cond in con[2]:
95
 
                                if val == cond:
96
 
                                        return True
97
 
        elif con[1].lower()=="not in":
98
 
                        for cond in con[2]:
99
 
                                if val == cond:
100
 
                                        return False
101
 
                        return True
102
 
        return False
103
 
    
 
71
def calc_condition(self, model, cond):
 
72
    cond_main = cond[:]
 
73
    try:
 
74
        return ConditionExpr(cond).eval(model)
 
75
    except:
 
76
        import common
 
77
        common.error('Wrong attrs Implementation!','You have wrongly specified conditions in attrs %s' %(cond_main,))
 
78
 
 
79
class ConditionExpr(object):
 
80
    OPERATORS = {'=': lambda x, y, model: model[x].get(model) == y,
 
81
                 '!=': lambda x, y, model: model[x].get(model) != y,
 
82
                 '<': lambda x, y, model: model[x].get(model) < y,
 
83
                 '>': lambda x, y, model: model[x].get(model) > y,
 
84
                 '<=': lambda x, y, model: model[x].get(model) <= y,
 
85
                 '>=': lambda x, y, model: model[x].get(model) >= y,
 
86
                 'in': lambda x, y, model: model[x].get(model) in y,
 
87
                 'not in': lambda x, y, model: model[x].get(model) not in y}
 
88
 
 
89
    OPERAND_MAPPER = {'<>': '!=', '==': '='}
 
90
 
 
91
    def __init__(self, condition):
 
92
        self.cond = condition
 
93
 
 
94
    def eval(self, context):
 
95
        def evaluate(cond):
 
96
            if isinstance(cond,bool):
 
97
                return cond
 
98
            left, operand, right = cond
 
99
            real_op = self.OPERAND_MAPPER.get(operand.lower(), operand)
 
100
            return self.OPERATORS[real_op](left, right, context)
 
101
 
 
102
        def find_index(con):
 
103
            index=-1
 
104
            for a in range(len(con)):
 
105
                if con[a] == '|':
 
106
                    index = a
 
107
            return index
 
108
        ind = find_index(self.cond)
 
109
        while(ind!= -1):
 
110
            result = any((evaluate(self.cond[ind+1]),evaluate(self.cond[ind+2])))
 
111
            self.cond.__delslice__(ind,ind+3)
 
112
            self.cond.__setslice__(ind,ind,[result])
 
113
            ind = find_index(self.cond)
 
114
        return all(evaluate(expr) for expr in self.cond)
 
115
 
104
116
def call_log(fun):
105
117
    """Debug decorator
106
118
       TODO: Add optionnal execution time