comparison mercurial/formatter.py @ 37073:44757e6dad93

templater: introduce resourcemapper class A couple more functions will be added later to work around nested mapping bugs such as the issue 5612.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 15 Mar 2018 20:43:39 +0900
parents de117f579431
children 46859b437697
comparison
equal deleted inserted replaced
37072:d64ae4fef471 37073:44757e6dad93
492 for k, v in ui.configitems('templates')) 492 for k, v in ui.configitems('templates'))
493 if tmpl: 493 if tmpl:
494 t.cache[''] = tmpl 494 t.cache[''] = tmpl
495 return t 495 return t
496 496
497 def templateresources(ui, repo=None): 497 class templateresources(templater.resourcemapper):
498 """Create a dict of template resources designed for the default templatekw 498 """Resource mapper designed for the default templatekw and function"""
499 and function""" 499
500 resmap = { 500 def __init__(self, ui, repo=None):
501 'cache': {}, # for templatekw/funcs to store reusable data 501 self._resmap = {
502 'repo': repo, 502 'cache': {}, # for templatekw/funcs to store reusable data
503 'ui': ui, 503 'repo': repo,
504 } 504 'ui': ui,
505 505 }
506 def getsome(context, mapping, key): 506
507 def knownkeys(self):
508 return self._knownkeys
509
510 def lookup(self, context, mapping, key):
511 get = self._gettermap.get(key)
512 if not get:
513 return None
514 return get(self, context, mapping, key)
515
516 def _getsome(self, context, mapping, key):
507 v = mapping.get(key) 517 v = mapping.get(key)
508 if v is not None: 518 if v is not None:
509 return v 519 return v
510 return resmap.get(key) 520 return self._resmap.get(key)
511 521
512 def getctx(context, mapping, key): 522 def _getctx(self, context, mapping, key):
513 ctx = mapping.get('ctx') 523 ctx = mapping.get('ctx')
514 if ctx is not None: 524 if ctx is not None:
515 return ctx 525 return ctx
516 fctx = mapping.get('fctx') 526 fctx = mapping.get('fctx')
517 if fctx is not None: 527 if fctx is not None:
518 return fctx.changectx() 528 return fctx.changectx()
519 529
520 def getrepo(context, mapping, key): 530 def _getrepo(self, context, mapping, key):
521 ctx = getctx(context, mapping, 'ctx') 531 ctx = self._getctx(context, mapping, 'ctx')
522 if ctx is not None: 532 if ctx is not None:
523 return ctx.repo() 533 return ctx.repo()
524 return getsome(context, mapping, key) 534 return self._getsome(context, mapping, key)
525 535
526 return { 536 _gettermap = {
527 'cache': getsome, 537 'cache': _getsome,
528 'ctx': getctx, 538 'ctx': _getctx,
529 'fctx': getsome, 539 'fctx': _getsome,
530 'repo': getrepo, 540 'repo': _getrepo,
531 'revcache': getsome, # per-ctx cache; set later 541 'revcache': _getsome, # per-ctx cache; set later
532 'ui': getsome, 542 'ui': _getsome,
533 } 543 }
544 _knownkeys = set(_gettermap.keys())
534 545
535 def formatter(ui, out, topic, opts): 546 def formatter(ui, out, topic, opts):
536 template = opts.get("template", "") 547 template = opts.get("template", "")
537 if template == "json": 548 if template == "json":
538 return jsonformatter(ui, out, topic, opts) 549 return jsonformatter(ui, out, topic, opts)