Patchwork [1,of,7] templateutil: move flatten() from templater

login
register
mail settings
Submitter Yuya Nishihara
Date March 25, 2018, 5:15 a.m.
Message ID <e9ae0d2c60b7a4c623f4.1521954948@mimosa>
Download mbox | patch
Permalink /patch/29838/
State Accepted
Headers show

Comments

Yuya Nishihara - March 25, 2018, 5:15 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521284454 -32400
#      Sat Mar 17 20:00:54 2018 +0900
# Node ID e9ae0d2c60b7a4c623f4065559f155733a290096
# Parent  65d54e54ddbe7617f5434d9bf0add18318b4fa3d
templateutil: move flatten() from templater

It's the same kind of utility as stringify().

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -527,33 +527,6 @@  def expandaliases(tree, aliases):
 
 # template engine
 
-def _flatten(thing):
-    '''yield a single stream from a possibly nested set of iterators'''
-    thing = templateutil.unwraphybrid(thing)
-    if isinstance(thing, bytes):
-        yield thing
-    elif isinstance(thing, str):
-        # We can only hit this on Python 3, and it's here to guard
-        # against infinite recursion.
-        raise error.ProgrammingError('Mercurial IO including templates is done'
-                                     ' with bytes, not strings, got %r' % thing)
-    elif thing is None:
-        pass
-    elif not util.safehasattr(thing, '__iter__'):
-        yield pycompat.bytestr(thing)
-    else:
-        for i in thing:
-            i = templateutil.unwraphybrid(i)
-            if isinstance(i, bytes):
-                yield i
-            elif i is None:
-                pass
-            elif not util.safehasattr(i, '__iter__'):
-                yield pycompat.bytestr(i)
-            else:
-                for j in _flatten(i):
-                    yield j
-
 def unquotestring(s):
     '''unwrap quotes if any; otherwise returns unmodified string'''
     if len(s) < 2 or s[0] not in "'\"" or s[0] != s[-1]:
@@ -706,7 +679,7 @@  class engine(object):
         if extramapping:
             extramapping.update(mapping)
             mapping = extramapping
-        return _flatten(func(self, mapping, data))
+        return templateutil.flatten(func(self, mapping, data))
 
 engines = {'default': engine}
 
diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -234,6 +234,33 @@  def _showcompatlist(context, mapping, na
     if context.preload(endname):
         yield context.process(endname, mapping)
 
+def flatten(thing):
+    """Yield a single stream from a possibly nested set of iterators"""
+    thing = unwraphybrid(thing)
+    if isinstance(thing, bytes):
+        yield thing
+    elif isinstance(thing, str):
+        # We can only hit this on Python 3, and it's here to guard
+        # against infinite recursion.
+        raise error.ProgrammingError('Mercurial IO including templates is done'
+                                     ' with bytes, not strings, got %r' % thing)
+    elif thing is None:
+        pass
+    elif not util.safehasattr(thing, '__iter__'):
+        yield pycompat.bytestr(thing)
+    else:
+        for i in thing:
+            i = unwraphybrid(i)
+            if isinstance(i, bytes):
+                yield i
+            elif i is None:
+                pass
+            elif not util.safehasattr(i, '__iter__'):
+                yield pycompat.bytestr(i)
+            else:
+                for j in flatten(i):
+                    yield j
+
 def stringify(thing):
     """Turn values into bytes by converting into text and concatenating them"""
     thing = unwraphybrid(thing)