##// END OF EJS Templates
templater: convert resources to a table of callables for future extension...
Yuya Nishihara -
r36997:255f635c default
parent child Browse files
Show More
@@ -501,14 +501,23 def maketemplater(ui, tmpl, defaults=Non
501 def templateresources(ui, repo=None):
501 def templateresources(ui, repo=None):
502 """Create a dict of template resources designed for the default templatekw
502 """Create a dict of template resources designed for the default templatekw
503 and function"""
503 and function"""
504 return {
504 resmap = {
505 'cache': {}, # for templatekw/funcs to store reusable data
505 'cache': {}, # for templatekw/funcs to store reusable data
506 'ctx': None,
507 'repo': repo,
506 'repo': repo,
508 'revcache': None, # per-ctx cache; set later
509 'ui': ui,
507 'ui': ui,
510 }
508 }
511
509
510 def getsome(context, mapping, key):
511 return resmap.get(key)
512
513 return {
514 'cache': getsome,
515 'ctx': getsome,
516 'repo': getsome,
517 'revcache': getsome, # per-ctx cache; set later
518 'ui': getsome,
519 }
520
512 def formatter(ui, out, topic, opts):
521 def formatter(ui, out, topic, opts):
513 template = opts.get("template", "")
522 template = opts.get("template", "")
514 if template == "json":
523 if template == "json":
@@ -423,7 +423,7 class changesettemplater(changesetprinte
423 resources=tres,
423 resources=tres,
424 cache=templatekw.defaulttempl)
424 cache=templatekw.defaulttempl)
425 self._counter = itertools.count()
425 self._counter = itertools.count()
426 self.cache = tres['cache'] # shared with _graphnodeformatter()
426 self._getcache = tres['cache'] # shared with _graphnodeformatter()
427
427
428 self._tref = tmplspec.ref
428 self._tref = tmplspec.ref
429 self._parts = {'header': '', 'footer': '',
429 self._parts = {'header': '', 'footer': '',
@@ -852,7 +852,8 def _graphnodeformatter(ui, displayer):
852 spec = templater.unquotestring(spec)
852 spec = templater.unquotestring(spec)
853 tres = formatter.templateresources(ui)
853 tres = formatter.templateresources(ui)
854 if isinstance(displayer, changesettemplater):
854 if isinstance(displayer, changesettemplater):
855 tres['cache'] = displayer.cache # reuse cache of slow templates
855 # reuse cache of slow templates
856 tres['cache'] = displayer._getcache
856 templ = formatter.maketemplater(ui, spec, defaults=templatekw.keywords,
857 templ = formatter.maketemplater(ui, spec, defaults=templatekw.keywords,
857 resources=tres)
858 resources=tres)
858 def formatnode(repo, ctx):
859 def formatnode(repo, ctx):
@@ -566,8 +566,8 class engine(object):
566 v = None
566 v = None
567 if key in self._resources:
567 if key in self._resources:
568 v = mapping.get(key)
568 v = mapping.get(key)
569 if v is None:
569 if v is None and key in self._resources:
570 v = self._resources.get(key)
570 v = self._resources[key](self, mapping, key)
571 if v is None:
571 if v is None:
572 raise templateutil.ResourceUnavailable(
572 raise templateutil.ResourceUnavailable(
573 _('template resource not available: %s') % key)
573 _('template resource not available: %s') % key)
@@ -670,8 +670,9 class templater(object):
670 - ``filters``: a dict of functions to transform a value into another.
670 - ``filters``: a dict of functions to transform a value into another.
671 - ``defaults``: a dict of symbol values/functions; may be overridden
671 - ``defaults``: a dict of symbol values/functions; may be overridden
672 by a ``mapping`` dict.
672 by a ``mapping`` dict.
673 - ``resources``: a dict of internal data (e.g. cache), inaccessible
673 - ``resources``: a dict of functions returning internal data
674 from user template; may be overridden by a ``mapping`` dict.
674 (e.g. cache), inaccessible from user template; may be overridden by
675 a ``mapping`` dict.
675 - ``cache``: a dict of preloaded template fragments.
676 - ``cache``: a dict of preloaded template fragments.
676 - ``aliases``: a list of alias (name, replacement) pairs.
677 - ``aliases``: a list of alias (name, replacement) pairs.
677
678
@@ -691,7 +692,7 class templater(object):
691 self.filters = templatefilters.filters.copy()
692 self.filters = templatefilters.filters.copy()
692 self.filters.update(filters)
693 self.filters.update(filters)
693 self.defaults = defaults
694 self.defaults = defaults
694 self._resources = {'templ': self}
695 self._resources = {'templ': lambda context, mapping, key: self}
695 self._resources.update(resources)
696 self._resources.update(resources)
696 self._aliases = aliases
697 self._aliases = aliases
697 self.minchunk, self.maxchunk = minchunk, maxchunk
698 self.minchunk, self.maxchunk = minchunk, maxchunk
@@ -350,7 +350,8 def runsymbol(context, mapping, key, def
350 v = default
350 v = default
351 if callable(v) and getattr(v, '_requires', None) is None:
351 if callable(v) and getattr(v, '_requires', None) is None:
352 # old templatekw: expand all keywords and resources
352 # old templatekw: expand all keywords and resources
353 props = context._resources.copy()
353 props = {k: f(context, mapping, k)
354 for k, f in context._resources.items()}
354 props.update(mapping)
355 props.update(mapping)
355 return v(**pycompat.strkwargs(props))
356 return v(**pycompat.strkwargs(props))
356 if callable(v):
357 if callable(v):
General Comments 0
You need to be logged in to leave comments. Login now