Patchwork [1,of,3] templater: allow dynamically switching the default dict/list formatting

login
register
mail settings
Submitter Yuya Nishihara
Date March 2, 2018, 10:25 p.m.
Message ID <09b7cc4c0d6e56e77553.1520029531@mimosa>
Download mbox | patch
Permalink /patch/28737/
State Accepted
Headers show

Comments

Yuya Nishihara - March 2, 2018, 10:25 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1519909642 18000
#      Thu Mar 01 08:07:22 2018 -0500
# Node ID 09b7cc4c0d6e56e77553789225a2039f3de427fd
# Parent  c9dcc379e2dcc8d7718bdae3aa9df23ed54ff358
templater: allow dynamically switching the default dict/list formatting

'%s' doesn't work nicely on Python 3 because many Python types don't implement
__bytes__().

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -175,10 +175,10 @@  class baseformatter(object):
     def formatdate(self, date, fmt='%a %b %d %H:%M:%S %Y %1%2'):
         '''convert date tuple to appropriate format'''
         return self._converter.formatdate(date, fmt)
-    def formatdict(self, data, key='key', value='value', fmt='%s=%s', sep=' '):
+    def formatdict(self, data, key='key', value='value', fmt=None, sep=' '):
         '''convert dict or key-value pairs to appropriate dict format'''
         return self._converter.formatdict(data, key, value, fmt, sep)
-    def formatlist(self, data, name, fmt='%s', sep=' '):
+    def formatlist(self, data, name, fmt=None, sep=' '):
         '''convert iterable to appropriate list format'''
         # name is mandatory argument for now, but it could be optional if
         # we have default template keyword, e.g. {item}
@@ -247,10 +247,14 @@  class _plainconverter(object):
     @staticmethod
     def formatdict(data, key, value, fmt, sep):
         '''stringify key-value pairs separated by sep'''
+        if fmt is None:
+            fmt = '%s=%s'
         return sep.join(fmt % (k, v) for k, v in _iteritems(data))
     @staticmethod
     def formatlist(data, name, fmt, sep):
         '''stringify iterable separated by sep'''
+        if fmt is None:
+            fmt = '%s'
         return sep.join(fmt % e for e in data)
 
 class plainformatter(baseformatter):
diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -97,13 +97,17 @@  class _mappable(object):
     def itermaps(self):
         yield self.tomap()
 
-def hybriddict(data, key='key', value='value', fmt='%s=%s', gen=None):
+def hybriddict(data, key='key', value='value', fmt=None, gen=None):
     """Wrap data to support both dict-like and string-like operations"""
+    if fmt is None:
+        fmt = '%s=%s'
     return _hybrid(gen, data, lambda k: {key: k, value: data[k]},
                    lambda k: fmt % (k, data[k]))
 
-def hybridlist(data, name, fmt='%s', gen=None):
+def hybridlist(data, name, fmt=None, gen=None):
     """Wrap data to support both list-like and string-like operations"""
+    if fmt is None:
+        fmt = '%s'
     return _hybrid(gen, data, lambda x: {name: x}, lambda x: fmt % x)
 
 def unwraphybrid(thing):
@@ -137,7 +141,7 @@  def wraphybridvalue(container, key, valu
     return _mappable(None, key, value, makemap)
 
 def compatdict(context, mapping, name, data, key='key', value='value',
-               fmt='%s=%s', plural=None, separator=' '):
+               fmt=None, plural=None, separator=' '):
     """Wrap data like hybriddict(), but also supports old-style list template
 
     This exists for backward compatibility with the old-style template. Use
@@ -148,7 +152,7 @@  def compatdict(context, mapping, name, d
     f = _showlist(name, c, t, mapping, plural, separator)
     return hybriddict(data, key=key, value=value, fmt=fmt, gen=f)
 
-def compatlist(context, mapping, name, data, element=None, fmt='%s',
+def compatlist(context, mapping, name, data, element=None, fmt=None,
                plural=None, separator=' '):
     """Wrap data like hybridlist(), but also supports old-style list template
 
@@ -160,7 +164,7 @@  def compatlist(context, mapping, name, d
     return hybridlist(data, name=element or name, fmt=fmt, gen=f)
 
 def showdict(name, data, mapping, plural=None, key='key', value='value',
-             fmt='%s=%s', separator=' '):
+             fmt=None, separator=' '):
     ui = mapping.get('ui')
     if ui:
         ui.deprecwarn("templatekw.showdict() is deprecated, use compatdict()",