diff mercurial/templateutil.py @ 40525:8fa26f3baf30

templater: add wrapper for a single template mapping This can be used to nest template mappings without inserting a sequence-like layer. See the next patch for example.
author Yuya Nishihara <yuya@tcha.org>
date Fri, 19 Oct 2018 21:11:30 +0900
parents 28f974d83c0a
children 4591c9791a82
line wrap: on
line diff
--- a/mercurial/templateutil.py	Fri Oct 26 21:28:20 2018 +0900
+++ b/mercurial/templateutil.py	Fri Oct 19 21:11:30 2018 +0900
@@ -472,6 +472,29 @@
     def tobool(self, context, mapping):
         return bool(self._mappings)
 
+class mappingdict(mappable, _mappingsequence):
+    """Wrapper for a single template mapping
+
+    This isn't a sequence in a way that the underlying dict won't be iterated
+    as a dict, but shares most of the _mappingsequence functions.
+    """
+
+    def __init__(self, mapping, name=None, tmpl=None):
+        super(mappingdict, self).__init__(name, tmpl)
+        self._mapping = mapping
+
+    def tomap(self, context):
+        return self._mapping
+
+    def tobool(self, context, mapping):
+        # no idea when a template mapping should be considered an empty, but
+        # a mapping dict should have at least one item in practice, so always
+        # mark this as non-empty.
+        return True
+
+    def tovalue(self, context, mapping):
+        return super(mappingdict, self).tovalue(context, mapping)[0]
+
 class mappedgenerator(wrapped):
     """Wrapper for generator of strings which acts as a list