template: fold template() into __call__, minor optimizations

- use non-grouping operator to avoid some extra processing
- avoid copying and updating defaults
- unnest main template body
- avoid returning extra empty string if format
This commit is contained in:
Matt Mackall 2006-11-13 13:26:57 -06:00
parent 6fe92f16bb
commit c517977e84

View File

@ -44,6 +44,9 @@ class templater(object):
filter uses function to transform value. syntax is
{key|filter1|filter2|...}.'''
template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))"
r"(\w+)(?:(?:%(\w+))|((?:\|\w+)*))[#}]")
def __init__(self, mapfile, filters={}, defaults={}, cache={}):
'''set up template engine.
mapfile is name of file to read map definitions from.
@ -84,35 +87,33 @@ class templater(object):
'''perform expansion.
t is name of map element to expand.
map is added elements to use during expansion.'''
m = self.defaults.copy()
m.update(map)
if not self.cache.has_key(t):
try:
self.cache[t] = file(self.map[t]).read()
except IOError, inst:
raise IOError(inst.args[0], _('template file %s: %s') %
(self.map[t], inst.args[1]))
return self.template(self.cache[t], **m)
tmpl = self.cache[t]
template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))"
r"(\w+)((%\w+)*)((\|\w+)*)[#}]")
def template(self, tmpl, **map):
while tmpl:
m = self.template_re.search(tmpl)
if m:
if not m:
yield tmpl
break
start, end = m.span(0)
key = m.group(1)
format = m.group(2)
fl = m.group(4)
key, format, fl = m.groups()
if start:
yield tmpl[:start]
tmpl = tmpl[end:]
v = map.get(key, "")
if key in map:
v = map[key]
else:
v = self.defaults.get(key, "")
if callable(v):
v = v(**map)
if format:
if not hasattr(v, '__iter__'):
raise SyntaxError(_("Error expanding '%s%s'")
@ -120,19 +121,12 @@ class templater(object):
lm = map.copy()
for i in v:
lm.update(i)
yield self(format[1:], **lm)
v = ""
elif fl:
yield self(format, **lm)
else:
if fl:
for f in fl.split("|")[1:]:
v = self.filters[f](v)
yield v
tmpl = tmpl[end:]
else:
yield tmpl
break
agescales = [("second", 1),
("minute", 60),