-
Notifications
You must be signed in to change notification settings - Fork 78
/
rendering.py
29 lines (23 loc) · 890 Bytes
/
rendering.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
import re
from .parsing import get_parsing_context
from ..units import fold_constants
class TemplateEvaluator:
def __init__(self, pattern=r"\${(.*?)}", fmt="${%s}", globals_=None, post_procs=()):
self.mark = re.compile(pattern)
self.fmt = fmt
if globals_ is None:
globals_ = get_parsing_context()
self.globals_ = globals_
self._post_procs = post_procs
def _post_proc(self, arg):
for pp in self._post_procs:
arg = pp(arg)
return arg
def __call__(self, template, **kwargs):
for item in self.mark.findall(template):
ev = self._post_proc(eval(item, dict(self.globals_, **kwargs)))
template = template.replace(self.fmt % item, str(ev))
return template
eval_template = TemplateEvaluator(
post_procs=(fold_constants, lambda x: str(x).replace(" ", "*"))
)