changeset 36633:034a07e60e98

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__().
author Yuya Nishihara <yuya@tcha.org>
date Thu, 01 Mar 2018 08:07:22 -0500
parents 9e2866065982
children cafd0586876b
files mercurial/formatter.py mercurial/templatekw.py
diffstat 2 files changed, 15 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/formatter.py	Sat Mar 03 21:01:07 2018 +0530
+++ b/mercurial/formatter.py	Thu Mar 01 08:07:22 2018 -0500
@@ -176,10 +176,10 @@
     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}
@@ -248,10 +248,14 @@
     @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):
--- a/mercurial/templatekw.py	Sat Mar 03 21:01:07 2018 +0530
+++ b/mercurial/templatekw.py	Thu Mar 01 08:07:22 2018 -0500
@@ -97,13 +97,17 @@
     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 @@
     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 @@
     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 @@
     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()",