Mercurial > hg
comparison mercurial/templatekw.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 | e46b24582fa0 |
children | 9ff5cbfbc26a |
comparison
equal
deleted
inserted
replaced
36444:717a279c0c21 | 36445:e8d37838f5df |
---|---|
339 'extra': '{key}={value|stringescape}' | 339 'extra': '{key}={value|stringescape}' |
340 } | 340 } |
341 # filecopy is preserved for compatibility reasons | 341 # filecopy is preserved for compatibility reasons |
342 defaulttempl['filecopy'] = defaulttempl['file_copy'] | 342 defaulttempl['filecopy'] = defaulttempl['file_copy'] |
343 | 343 |
344 # keywords are callables like: | 344 # keywords are callables (see registrar.templatekeyword for details) |
345 # fn(repo, ctx, templ, cache, revcache, **args) | |
346 # with: | |
347 # repo - current repository instance | |
348 # ctx - the changectx being displayed | |
349 # templ - the templater instance | |
350 # cache - a cache dictionary for the whole templater run | |
351 # revcache - a cache dictionary for the current revision | |
352 keywords = {} | 345 keywords = {} |
353 | |
354 templatekeyword = registrar.templatekeyword(keywords) | 346 templatekeyword = registrar.templatekeyword(keywords) |
355 | 347 |
356 @templatekeyword('author') | 348 @templatekeyword('author', requires={'ctx'}) |
357 def showauthor(repo, ctx, templ, **args): | 349 def showauthor(context, mapping): |
358 """String. The unmodified author of the changeset.""" | 350 """String. The unmodified author of the changeset.""" |
351 ctx = context.resource(mapping, 'ctx') | |
359 return ctx.user() | 352 return ctx.user() |
360 | 353 |
361 @templatekeyword('bisect') | 354 @templatekeyword('bisect') |
362 def showbisect(repo, ctx, templ, **args): | 355 def showbisect(repo, ctx, templ, **args): |
363 """String. The changeset bisection status.""" | 356 """String. The changeset bisection status.""" |