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

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

Comments

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

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -321,8 +321,8 @@  class _templateconverter(object):
         data = util.sortdict(_iteritems(data))
         def f():
             yield _plainconverter.formatdict(data, key, value, fmt, sep)
-        return templatekw._hybrid(f(), data, lambda k: {key: k, value: data[k]},
-                                  lambda d: fmt % (d[key], d[value]))
+        return templatekw.hybriddict(data, key=key, value=value, fmt=fmt,
+                                     gen=f())
     @staticmethod
     def formatlist(data, name, fmt, sep):
         '''build object that can be evaluated as either plain string or list'''
diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -62,6 +62,11 @@  class _hybrid(object):
             raise AttributeError(name)
         return getattr(self._values, name)
 
+def hybriddict(data, key='key', value='value', fmt='%s=%s', gen=None):
+    """Wrap data to support both dict-like and string-like operations"""
+    return _hybrid(gen, data, lambda k: {key: k, value: data[k]},
+                   lambda d: fmt % (d[key], d[value]))
+
 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])