-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemailtemplate.py
47 lines (37 loc) · 1.22 KB
/
emailtemplate.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
import re
def format_price(p):
return ('%.2f' % p).replace('.', ',')
def format_price_set(ps):
return '/'.join(map(format_price, sorted(ps)))
def format_count(c):
return ('%.2f' % c).rstrip('0').rstrip('.').replace('.', ',')
def format(template, context):
r'''
>>> format('Hello #TARGET#!', dict(TARGET='world'))
'Hello world!'
>>> format('Hello.#SKJULNUL:# Value is #V#',
... dict(V='42'))
'Hello. Value is 42'
>>> format('Hello.#SKJULNUL:# Value is #V#\nHello again.',
... dict(V='0'))
'Hello.\nHello again.'
>>> format('#SKJULNUL:#Values #X# #Y#',
... dict(X='1', Y='2'))
'Values 1 2'
>>> format('#SKJULNUL:#Values #X# #Y#',
... dict(X='1', Y='0'))
''
'''
pattern = r'#([A-Z][^#]*)#'
res = re.sub(r'\r\n|\n|\r', '\n', template)
def hide_zero(s):
if any(context[m.group(1)].strip('0,.') == ''
for m in re.finditer(pattern, s)):
return ''
else:
return s
res = re.sub(r'#SKJULNUL:#(.*\n?)',
lambda mo: hide_zero(mo.group(1)), res)
res = re.sub(pattern, lambda mo: context[mo.group(1)], res)
res = re.sub(r'\n\n+', '\n\n', res)
return res