##// END OF EJS Templates
templater: keep a cache of Python functions for filter expressions
Dirkjan Ochtman -
r8476:d5dda64f default
parent child Browse files
Show More
@@ -48,6 +48,7 b' class engine(object):'
48 self.loader = loader
48 self.loader = loader
49 self.filters = filters
49 self.filters = filters
50 self.defaults = defaults
50 self.defaults = defaults
51 self.cache = {}
51
52
52 def process(self, t, map):
53 def process(self, t, map):
53 '''Perform expansion. t is name of map element to expand. map contains
54 '''Perform expansion. t is name of map element to expand. map contains
@@ -80,11 +81,17 b' class engine(object):'
80 yield self.process(format, lm)
81 yield self.process(format, lm)
81
82
82 def _filter(self, expr, get, map):
83 def _filter(self, expr, get, map):
83 parts = expr.split('|')
84 if expr not in self.cache:
84 v = get(parts[0])
85 parts = expr.split('|')
85 for f in parts[1:]:
86 filters = parts[1:]
86 v = self.filters[f](v)
87 for f in filters:
87 return v
88 if f not in self.filters:
89 raise SyntaxError(_("unknown filter '%s'") % expr)
90 calls = '('.join(i for i in reversed(filters))
91 end = ')' * len(filters)
92 code = "lambda _get: %s(_get('%s')%s" % (calls, parts[0], end)
93 self.cache[expr] = eval(code, self.filters)
94 return self.cache[expr](get)
88
95
89 def _process(self, tmpl, map):
96 def _process(self, tmpl, map):
90 '''Render a template. Returns a generator.'''
97 '''Render a template. Returns a generator.'''
General Comments 0
You need to be logged in to leave comments. Login now