##// END OF EJS Templates
formatter: populate fctx from ctx and path value...
Yuya Nishihara -
r39622:b1239aee default
parent child Browse files
Show More
@@ -543,6 +543,10 def maketemplater(ui, tmpl, defaults=Non
543 543 t.cache[''] = tmpl
544 544 return t
545 545
546 # marker to denote a resource to be loaded on demand based on mapping values
547 # (e.g. (ctx, path) -> fctx)
548 _placeholder = object()
549
546 550 class templateresources(templater.resourcemapper):
547 551 """Resource mapper designed for the default templatekw and function"""
548 552
@@ -563,7 +567,10 class templateresources(templater.resour
563 567 def lookup(self, mapping, key):
564 568 if key not in self.knownkeys():
565 569 return None
566 return self._getsome(mapping, key)
570 v = self._getsome(mapping, key)
571 if v is _placeholder:
572 v = mapping[key] = self._loadermap[key](self, mapping)
573 return v
567 574
568 575 def populatemap(self, context, origmapping, newmapping):
569 576 mapping = {}
@@ -572,6 +579,10 class templateresources(templater.resour
572 579 if self._hasnodespec(origmapping) and self._hasnodespec(newmapping):
573 580 orignode = templateutil.runsymbol(context, origmapping, 'node')
574 581 mapping['originalnode'] = orignode
582 # put marker to override 'fctx' in mapping if any, and flag
583 # its existence to be reported by availablekeys()
584 if 'fctx' not in newmapping and self._hasliteral(newmapping, 'path'):
585 mapping['fctx'] = _placeholder
575 586 return mapping
576 587
577 588 def _getsome(self, mapping, key):
@@ -580,10 +591,35 class templateresources(templater.resour
580 591 return v
581 592 return self._resmap.get(key)
582 593
594 def _hasliteral(self, mapping, key):
595 """Test if a literal value is set or unset in the given mapping"""
596 return key in mapping and not callable(mapping[key])
597
598 def _getliteral(self, mapping, key):
599 """Return value of the given name if it is a literal"""
600 v = mapping.get(key)
601 if callable(v):
602 return None
603 return v
604
583 605 def _hasnodespec(self, mapping):
584 606 """Test if context revision is set or unset in the given mapping"""
585 607 return 'node' in mapping or 'ctx' in mapping
586 608
609 def _loadfctx(self, mapping):
610 ctx = self._getsome(mapping, 'ctx')
611 path = self._getliteral(mapping, 'path')
612 if ctx is None or path is None:
613 return None
614 try:
615 return ctx[path]
616 except error.LookupError:
617 return None # maybe removed file?
618
619 _loadermap = {
620 'fctx': _loadfctx,
621 }
622
587 623 def formatter(ui, out, topic, opts):
588 624 template = opts.get("template", "")
589 625 if template == "json":
General Comments 0
You need to be logged in to leave comments. Login now