Mercurial > public > mercurial-scm > hg
comparison mercurial/formatter.py @ 36445: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 | b44fac3a49fb |
children | c6061cadb400 |
comparison
equal
deleted
inserted
replaced
36444:717a279c0c21 | 36445:e8d37838f5df |
---|---|
381 def _renderitem(self, part, item): | 381 def _renderitem(self, part, item): |
382 if part not in self._parts: | 382 if part not in self._parts: |
383 return | 383 return |
384 ref = self._parts[part] | 384 ref = self._parts[part] |
385 | 385 |
386 # TODO: add support for filectx. probably each template keyword or | 386 # TODO: add support for filectx |
387 # function will have to declare dependent resources. e.g. | |
388 # @templatekeyword(..., requires=('ctx',)) | |
389 props = {} | 387 props = {} |
390 # explicitly-defined fields precede templatekw | 388 # explicitly-defined fields precede templatekw |
391 props.update(item) | 389 props.update(item) |
392 if 'ctx' in item: | 390 if 'ctx' in item: |
393 # but template resources must be always available | 391 # but template resources must be always available |