Mercurial > hg
comparison mercurial/templater.py @ 8476:d5dda64f5552
templater: keep a cache of Python functions for filter expressions
author | Dirkjan Ochtman <dirkjan@ochtman.nl> |
---|---|
date | Sun, 17 May 2009 16:08:47 +0200 |
parents | caaf1e779b37 |
children | d72d1a97408a |
comparison
equal
deleted
inserted
replaced
8475:caaf1e779b37 | 8476:d5dda64f5552 |
---|---|
46 | 46 |
47 def __init__(self, loader, filters={}, defaults={}): | 47 def __init__(self, loader, filters={}, defaults={}): |
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 |
54 added elements for use during expansion. Is a generator.''' | 55 added elements for use during expansion. Is a generator.''' |
55 tmpl = self.loader(t) | 56 tmpl = self.loader(t) |
78 for i in v: | 79 for i in v: |
79 lm.update(i) | 80 lm.update(i) |
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.''' |
91 | 98 |
92 def get(key): | 99 def get(key): |