Mercurial > public > mercurial-scm > hg-stable
diff mercurial/templater.py @ 36475:e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
The current templatekw interface, f(repo, ctx, templ, **args), is horrible
because it's quite easy to encounter TypeError, ValueError, etc. It's also
bad for Python 3 porting due to the **kwargs issue.
This patch introduces a flag to switch to new f(context, mapping) API seen
in templater functions. The requirement spec isn't verified (yet) because
context.resource() can gracefully raise a ResourceUnavailable exception,
but it's planned to be used as a filter in the help, such as "Revision
Keywords" (if 'ctx' in requires), "File Keywords" (if 'fctx' in requires),
etc.
showauthor() is ported to the new API as an example. 20 more follows.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 25 Feb 2018 13:24:35 +0900 |
parents | 717a279c0c21 |
children | 43e108027b0d |
line wrap: on
line diff
--- a/mercurial/templater.py Sun Feb 25 12:50:30 2018 +0900 +++ b/mercurial/templater.py Sun Feb 25 13:24:35 2018 +0900 @@ -436,12 +436,18 @@ v = context.process(key, safemapping) except TemplateNotFound: v = default - if callable(v): - # TODO: templatekw functions will be updated to take (context, mapping) - # pair instead of **props + if callable(v) and getattr(v, '_requires', None) is None: + # old templatekw: expand all keywords and resources props = context._resources.copy() props.update(mapping) return v(**pycompat.strkwargs(props)) + if callable(v): + # new templatekw + try: + return v(context, mapping) + except ResourceUnavailable: + # unsupported keyword is mapped to empty just like unknown keyword + return None return v def buildtemplate(exp, context):