comparison mercurial/templater.py @ 19770:0361163efbaf

templater: support using templates with non-standard names from map file Allow to add arbitrarily-named entries to a template map file and then reference them, to make it possible to deduplicate and simplify templates code.
author Alexander Plavin <alexander@plav.in>
date Sun, 22 Sep 2013 13:52:18 +0400
parents 3af3a165db18
children 64b4f0cd7336
comparison
equal deleted inserted replaced
19768:186f54d40fdd 19770:0361163efbaf
137 return data 137 return data
138 138
139 def runsymbol(context, mapping, key): 139 def runsymbol(context, mapping, key):
140 v = mapping.get(key) 140 v = mapping.get(key)
141 if v is None: 141 if v is None:
142 v = context._defaults.get(key, '') 142 v = context._defaults.get(key)
143 if v is None:
144 try:
145 v = context.process(key, mapping)
146 except TemplateNotFound:
147 v = ''
143 if util.safehasattr(v, '__call__'): 148 if util.safehasattr(v, '__call__'):
144 return v(**mapping) 149 return v(**mapping)
145 if isinstance(v, types.GeneratorType): 150 if isinstance(v, types.GeneratorType):
146 v = list(v) 151 v = list(v)
147 mapping[key] = v 152 mapping[key] = v
447 split = file.split(".") 452 split = file.split(".")
448 if split[0] == "map-cmdline": 453 if split[0] == "map-cmdline":
449 stylelist.append(split[1]) 454 stylelist.append(split[1])
450 return ", ".join(sorted(stylelist)) 455 return ", ".join(sorted(stylelist))
451 456
457 class TemplateNotFound(util.Abort):
458 pass
459
452 class templater(object): 460 class templater(object):
453 461
454 def __init__(self, mapfile, filters={}, defaults={}, cache={}, 462 def __init__(self, mapfile, filters={}, defaults={}, cache={},
455 minchunk=1024, maxchunk=65536): 463 minchunk=1024, maxchunk=65536):
456 '''set up template engine. 464 '''set up template engine.
498 '''Get the template for the given template name. Use a local cache.''' 506 '''Get the template for the given template name. Use a local cache.'''
499 if t not in self.cache: 507 if t not in self.cache:
500 try: 508 try:
501 self.cache[t] = util.readfile(self.map[t][1]) 509 self.cache[t] = util.readfile(self.map[t][1])
502 except KeyError, inst: 510 except KeyError, inst:
503 raise util.Abort(_('"%s" not in template map') % inst.args[0]) 511 raise TemplateNotFound(_('"%s" not in template map') %
512 inst.args[0])
504 except IOError, inst: 513 except IOError, inst:
505 raise IOError(inst.args[0], _('template file %s: %s') % 514 raise IOError(inst.args[0], _('template file %s: %s') %
506 (self.map[t][1], inst.args[1])) 515 (self.map[t][1], inst.args[1]))
507 return self.cache[t] 516 return self.cache[t]
508 517