changeset 36926:6ff6e1d6b5b8

templater: move stringify() to templateutil module As we have a util module, it doesn't make sense to import stringify() from templatefilters.py.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 08 Mar 2018 23:10:46 +0900
parents 8fba319750c2
children 32f9b7e3f056
files mercurial/formatter.py mercurial/hgweb/hgweb_mod.py mercurial/hgweb/hgwebdir_mod.py mercurial/logcmdutil.py mercurial/templatefilters.py mercurial/templater.py mercurial/templateutil.py tests/test-template-engine.t
diffstat 8 files changed, 39 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/formatter.py	Wed Feb 28 15:20:41 2018 -0500
+++ b/mercurial/formatter.py	Thu Mar 08 23:10:46 2018 +0900
@@ -124,6 +124,7 @@
     templatefilters,
     templatekw,
     templater,
+    templateutil,
     util,
 )
 from .utils import dateutil
@@ -403,7 +404,7 @@
             props['revcache'] = {}
         props = pycompat.strkwargs(props)
         g = self._t(ref, **props)
-        self._out.write(templater.stringify(g))
+        self._out.write(templateutil.stringify(g))
 
     def end(self):
         baseformatter.end(self)
--- a/mercurial/hgweb/hgweb_mod.py	Wed Feb 28 15:20:41 2018 -0500
+++ b/mercurial/hgweb/hgweb_mod.py	Thu Mar 08 23:10:46 2018 +0900
@@ -30,6 +30,7 @@
     repoview,
     templatefilters,
     templater,
+    templateutil,
     ui as uimod,
     util,
     wireprotoserver,
@@ -378,7 +379,7 @@
         try:
             rctx.tmpl = rctx.templater(req)
             ctype = rctx.tmpl('mimetype', encoding=encoding.encoding)
-            ctype = templater.stringify(ctype)
+            ctype = templateutil.stringify(ctype)
 
             # check read permissions non-static content
             if cmd != 'static':
--- a/mercurial/hgweb/hgwebdir_mod.py	Wed Feb 28 15:20:41 2018 -0500
+++ b/mercurial/hgweb/hgwebdir_mod.py	Thu Mar 08 23:10:46 2018 +0900
@@ -34,6 +34,7 @@
     pycompat,
     scmutil,
     templater,
+    templateutil,
     ui as uimod,
     util,
 )
@@ -370,7 +371,7 @@
             virtual = req.dispatchpath.strip('/')
             tmpl = self.templater(req, nonce)
             ctype = tmpl('mimetype', encoding=encoding.encoding)
-            ctype = templater.stringify(ctype)
+            ctype = templateutil.stringify(ctype)
 
             # Global defaults. These can be overridden by any handler.
             res.status = '200 Script output follows'
--- a/mercurial/logcmdutil.py	Wed Feb 28 15:20:41 2018 -0500
+++ b/mercurial/logcmdutil.py	Thu Mar 08 23:10:46 2018 +0900
@@ -33,6 +33,7 @@
     smartset,
     templatekw,
     templater,
+    templateutil,
     util,
 )
 from .utils import dateutil
@@ -449,13 +450,15 @@
             self._parts.update(m)
 
         if self._parts['docheader']:
-            self.ui.write(templater.stringify(self.t(self._parts['docheader'])))
+            self.ui.write(
+                templateutil.stringify(self.t(self._parts['docheader'])))
 
     def close(self):
         if self._parts['docfooter']:
             if not self.footer:
                 self.footer = ""
-            self.footer += templater.stringify(self.t(self._parts['docfooter']))
+            self.footer += templateutil.stringify(
+                self.t(self._parts['docfooter']))
         return super(changesettemplater, self).close()
 
     def _show(self, ctx, copies, props):
@@ -470,11 +473,12 @@
         # since there's inherently a conflict between header (across items) and
         # separator (per item)
         if self._parts['separator'] and index > 0:
-            self.ui.write(templater.stringify(self.t(self._parts['separator'])))
+            self.ui.write(
+                templateutil.stringify(self.t(self._parts['separator'])))
 
         # write header
         if self._parts['header']:
-            h = templater.stringify(self.t(self._parts['header'], **props))
+            h = templateutil.stringify(self.t(self._parts['header'], **props))
             if self.buffered:
                 self.header[ctx.rev()] = h
             else:
@@ -484,12 +488,12 @@
 
         # write changeset metadata, then patch if requested
         key = self._parts[self._tref]
-        self.ui.write(templater.stringify(self.t(key, **props)))
+        self.ui.write(templateutil.stringify(self.t(key, **props)))
         self._showpatch(ctx)
 
         if self._parts['footer']:
             if not self.footer:
-                self.footer = templater.stringify(
+                self.footer = templateutil.stringify(
                     self.t(self._parts['footer'], **props))
 
 def templatespec(tmpl, mapfile):
--- a/mercurial/templatefilters.py	Wed Feb 28 15:20:41 2018 -0500
+++ b/mercurial/templatefilters.py	Thu Mar 08 23:10:46 2018 +0900
@@ -18,6 +18,7 @@
     pycompat,
     registrar,
     templatekw,
+    templateutil,
     url,
     util,
 )
@@ -376,18 +377,7 @@
     """Any type. Turns the value into text by converting values into
     text and concatenating them.
     """
-    thing = templatekw.unwraphybrid(thing)
-    if util.safehasattr(thing, '__iter__') and not isinstance(thing, bytes):
-        if isinstance(thing, str):
-            # This is only reachable on Python 3 (otherwise
-            # isinstance(thing, bytes) would have been true), and is
-            # here to prevent infinite recursion bugs on Python 3.
-            raise error.ProgrammingError(
-                'stringify got unexpected unicode string: %r' % thing)
-        return "".join([stringify(t) for t in thing if t is not None])
-    if thing is None:
-        return ""
-    return pycompat.bytestr(thing)
+    return templateutil.stringify(thing)
 
 @templatefilter('stripdir')
 def stripdir(text):
--- a/mercurial/templater.py	Wed Feb 28 15:20:41 2018 -0500
+++ b/mercurial/templater.py	Thu Mar 08 23:10:46 2018 +0900
@@ -1118,8 +1118,6 @@
 
 # template engine
 
-stringify = templatefilters.stringify
-
 def _flatten(thing):
     '''yield a single stream from a possibly nested set of iterators'''
     thing = templatekw.unwraphybrid(thing)
@@ -1366,7 +1364,7 @@
     def render(self, mapping):
         """Render the default unnamed template and return result as string"""
         mapping = pycompat.strkwargs(mapping)
-        return stringify(self('', **mapping))
+        return templateutil.stringify(self('', **mapping))
 
     def __call__(self, t, **mapping):
         mapping = pycompat.byteskwargs(mapping)
--- a/mercurial/templateutil.py	Wed Feb 28 15:20:41 2018 -0500
+++ b/mercurial/templateutil.py	Thu Mar 08 23:10:46 2018 +0900
@@ -13,7 +13,6 @@
 from . import (
     error,
     pycompat,
-    templatefilters,
     templatekw,
     util,
 )
@@ -24,6 +23,21 @@
 class TemplateNotFound(error.Abort):
     pass
 
+def stringify(thing):
+    """Turn values into bytes by converting into text and concatenating them"""
+    thing = templatekw.unwraphybrid(thing)
+    if util.safehasattr(thing, '__iter__') and not isinstance(thing, bytes):
+        if isinstance(thing, str):
+            # This is only reachable on Python 3 (otherwise
+            # isinstance(thing, bytes) would have been true), and is
+            # here to prevent infinite recursion bugs on Python 3.
+            raise error.ProgrammingError(
+                'stringify got unexpected unicode string: %r' % thing)
+        return "".join([stringify(t) for t in thing if t is not None])
+    if thing is None:
+        return ""
+    return pycompat.bytestr(thing)
+
 def findsymbolicname(arg):
     """Find symbolic name for the given compiled expression; returns None
     if nothing found reliably"""
@@ -223,5 +237,3 @@
     if val is None:
         return
     return templatekw.wraphybridvalue(dictarg, key, val)
-
-stringify = templatefilters.stringify
--- a/tests/test-template-engine.t	Wed Feb 28 15:20:41 2018 -0500
+++ b/tests/test-template-engine.t	Thu Mar 08 23:10:46 2018 +0900
@@ -1,7 +1,10 @@
 
   $ cat > engine.py << EOF
   > 
-  > from mercurial import templater
+  > from mercurial import (
+  >     templater,
+  >     templateutil,
+  > )
   > 
   > class mytemplater(object):
   >     def __init__(self, loader, filters, defaults, resources, aliases):
@@ -31,7 +34,7 @@
   >                 v = v(**props)
   >             elif callable(v):
   >                 v = v(self, props)
-  >             v = templater.stringify(v)
+  >             v = templateutil.stringify(v)
   >             tmpl = tmpl.replace('{{%s}}' % k, v)
   >         yield tmpl
   >