-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbabi_eval.py
92 lines (76 loc) · 1.96 KB
/
babi_eval.py
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
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from decimal import Decimal
import datetime
import math
from dateutil.relativedelta import relativedelta
from trytond.pool import Pool
from trytond.transaction import Transaction
def year(text):
if not text:
return None
text = str(text)
return text[0:4]
def year_month(text):
if not text:
return None
text = str(text)
return text[0:4] + '-' + text[5:7]
def year_month_day(text):
if not text:
return None
text = str(text)
return text[0:10]
def month(text):
if not text:
return None
text = str(text)
return text[5:7]
def day(text):
if not text:
return None
text = str(text)
return text[8:10]
def week(text):
if not text:
return None
return datetime.datetime.strptime(year_month_day(text),
'%Y-%m-%d').strftime('%W')
def date(text):
if not text:
return None
return datetime.datetime.strptime(year_month_day(text), '%Y-%m-%d').date()
def babi_eval(expression, obj, convert_none='empty'):
objects = {
'o': obj,
'Pool': Pool,
'Transaction': Transaction,
'y': year,
'm': month,
'd': day,
'w': week,
'ym': year_month,
'ymd': year_month_day,
'date': date,
'int': int,
'float': float,
'sum': sum,
'min': min,
'max': max,
'now': datetime.datetime.now,
'today': datetime.date.today,
'relativedelta': relativedelta,
'math': math,
'Decimal': Decimal,
'str': str,
}
value = eval(expression, objects)
if (value is False or value is None):
if convert_none == 'empty':
# TODO: Make translatable
value = '(empty)'
elif convert_none == 'zero':
value = '0'
else:
value = convert_none
return value