[PATCH 2 of 4] templater: add wrapper for a single template mapping
Yuya Nishihara
yuya at tcha.org
Fri Nov 2 09:18:37 EDT 2018
# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1539951090 -32400
# Fri Oct 19 21:11:30 2018 +0900
# Node ID 49a6ece003a203da3e4a74fb60256f811fd2f32b
# Parent 276558285cd47fc388aed7861eaf7237c6d184e3
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.
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -49,6 +49,10 @@ mappinggenerator, mappinglist
represents mappings (i.e. a list of dicts), which may have default
output format.
+mappingdict
+ represents a single mapping (i.e. a dict), which may have default output
+ format.
+
mappedgenerator
a lazily-evaluated list of byte strings, which is e.g. a result of %
operation.
diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -472,6 +472,29 @@ class mappinglist(_mappingsequence):
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
More information about the Mercurial-devel
mailing list