Patchwork [5,of,9] templatekw: add public function to wrap a list by _hybrid object

login
register
mail settings
Submitter Yuya Nishihara
Date April 12, 2017, 3:53 p.m.
Message ID <7200e004748addb15ae9.1492012401@mimosa>
Download mbox | patch
Permalink /patch/20136/
State Accepted
Headers show

Comments

Yuya Nishihara - April 12, 2017, 3:53 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1491398736 -32400
#      Wed Apr 05 22:25:36 2017 +0900
# Node ID 7200e004748addb15ae973b21f54ddef11b39c8a
# Parent  e7257a6a6fd5cc5565fb7e4bfe0b0a8a780c1285
templatekw: add public function to wrap a list by _hybrid object

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -329,8 +329,7 @@  class _templateconverter(object):
         data = list(data)
         def f():
             yield _plainconverter.formatlist(data, name, fmt, sep)
-        return templatekw._hybrid(f(), data, lambda x: {name: x},
-                                  lambda d: fmt % d[name])
+        return templatekw.hybridlist(data, name=name, fmt=fmt, gen=f())
 
 class templateformatter(baseformatter):
     def __init__(self, ui, out, topic, opts):
diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -62,6 +62,10 @@  class _hybrid(object):
             raise AttributeError(name)
         return getattr(self._values, name)
 
+def hybridlist(data, name, fmt='%s', gen=None):
+    """Wrap data to support both list-like and string-like operations"""
+    return _hybrid(gen, data, lambda x: {name: x}, lambda d: fmt % d[name])
+
 def unwraphybrid(thing):
     """Return an object which can be stringified possibly by using a legacy
     template"""
@@ -73,7 +77,7 @@  def showlist(name, values, plural=None, 
     if not element:
         element = name
     f = _showlist(name, values, plural, separator, **args)
-    return _hybrid(f, values, lambda x: {element: x}, lambda d: d[element])
+    return hybridlist(values, name=element, gen=f)
 
 def _showlist(name, values, plural=None, separator=' ', **args):
     '''expand set of values.