Mercurial > public > mercurial-scm > hg
diff mercurial/templatekw.py @ 34534:b3073e175c17
templater: wrap get/min/max result so map operation can apply to element
See the test for usage example.
wraphybridvalue() takes a key/value pair because a hybrid dict passes a key
to its makemap() function. Since makemap() of showmanifest() doesn't need
a key, it's set to None.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 09 Sep 2017 19:13:25 +0900 |
parents | 12bfecd0ffe6 |
children | 78590585c0db |
line wrap: on
line diff
--- a/mercurial/templatekw.py Mon Oct 02 07:18:24 2017 +0100 +++ b/mercurial/templatekw.py Sat Sep 09 19:13:25 2017 +0900 @@ -78,13 +78,18 @@ value. Use unwrapvalue() or unwraphybrid() to obtain the inner object. """ - def __init__(self, gen, value, makemap): - self.gen = gen + def __init__(self, gen, key, value, makemap): + if gen is not None: + self.gen = gen # generator or function returning generator + self._key = key self._value = value # may be generator of strings self._makemap = makemap + def gen(self): + yield pycompat.bytestr(self._value) + def tomap(self): - return self._makemap() + return self._makemap(self._key) def itermaps(self): yield self.tomap() @@ -114,6 +119,20 @@ return thing return thing._value +def wraphybridvalue(container, key, value): + """Wrap an element of hybrid container to be mappable + + The key is passed to the makemap function of the given container, which + should be an item generated by iter(container). + """ + makemap = getattr(container, '_makemap', None) + if makemap is None: + return value + if util.safehasattr(value, '_makemap'): + # a nested hybrid list/dict, which has its own way of map operation + return value + return _mappable(None, key, value, makemap) + def showdict(name, data, mapping, plural=None, key='key', value='value', fmt='%s=%s', separator=' '): c = [{key: k, value: v} for k, v in data.iteritems()] @@ -578,7 +597,7 @@ f = templ('manifest', **args) # TODO: perhaps 'ctx' should be dropped from mapping because manifest # rev and node are completely different from changeset's. - return _mappable(f, f, lambda: {'rev': mrev, 'node': mhex}) + return _mappable(f, None, f, lambda x: {'rev': mrev, 'node': mhex}) def shownames(namespace, **args): """helper method to generate a template keyword for a namespace"""