mercurial/templateutil.py
changeset 37157 888507ec655e
parent 37104 7db3c28dfdfd
child 37158 e09d2183e226
equal deleted inserted replaced
37155:fb7140f1d09d 37157:888507ec655e
   232         yield one(last, tag=lastname)
   232         yield one(last, tag=lastname)
   233     endname = 'end_' + plural
   233     endname = 'end_' + plural
   234     if context.preload(endname):
   234     if context.preload(endname):
   235         yield context.process(endname, mapping)
   235         yield context.process(endname, mapping)
   236 
   236 
       
   237 def flatten(thing):
       
   238     """Yield a single stream from a possibly nested set of iterators"""
       
   239     thing = unwraphybrid(thing)
       
   240     if isinstance(thing, bytes):
       
   241         yield thing
       
   242     elif isinstance(thing, str):
       
   243         # We can only hit this on Python 3, and it's here to guard
       
   244         # against infinite recursion.
       
   245         raise error.ProgrammingError('Mercurial IO including templates is done'
       
   246                                      ' with bytes, not strings, got %r' % thing)
       
   247     elif thing is None:
       
   248         pass
       
   249     elif not util.safehasattr(thing, '__iter__'):
       
   250         yield pycompat.bytestr(thing)
       
   251     else:
       
   252         for i in thing:
       
   253             i = unwraphybrid(i)
       
   254             if isinstance(i, bytes):
       
   255                 yield i
       
   256             elif i is None:
       
   257                 pass
       
   258             elif not util.safehasattr(i, '__iter__'):
       
   259                 yield pycompat.bytestr(i)
       
   260             else:
       
   261                 for j in flatten(i):
       
   262                     yield j
       
   263 
   237 def stringify(thing):
   264 def stringify(thing):
   238     """Turn values into bytes by converting into text and concatenating them"""
   265     """Turn values into bytes by converting into text and concatenating them"""
   239     thing = unwraphybrid(thing)
   266     thing = unwraphybrid(thing)
   240     if util.safehasattr(thing, '__iter__') and not isinstance(thing, bytes):
   267     if util.safehasattr(thing, '__iter__') and not isinstance(thing, bytes):
   241         if isinstance(thing, str):
   268         if isinstance(thing, str):