comparison mercurial/templater.py @ 37157:888507ec655e

templateutil: move flatten() from templater It's the same kind of utility as stringify().
author Yuya Nishihara <yuya@tcha.org>
date Sat, 17 Mar 2018 20:00:54 +0900
parents 638a241202a3
children b56b79185aad
comparison
equal deleted inserted replaced
37155:fb7140f1d09d 37157:888507ec655e
525 aliasmap = _aliasrules.buildmap(aliases) 525 aliasmap = _aliasrules.buildmap(aliases)
526 return _aliasrules.expand(aliasmap, tree) 526 return _aliasrules.expand(aliasmap, tree)
527 527
528 # template engine 528 # template engine
529 529
530 def _flatten(thing):
531 '''yield a single stream from a possibly nested set of iterators'''
532 thing = templateutil.unwraphybrid(thing)
533 if isinstance(thing, bytes):
534 yield thing
535 elif isinstance(thing, str):
536 # We can only hit this on Python 3, and it's here to guard
537 # against infinite recursion.
538 raise error.ProgrammingError('Mercurial IO including templates is done'
539 ' with bytes, not strings, got %r' % thing)
540 elif thing is None:
541 pass
542 elif not util.safehasattr(thing, '__iter__'):
543 yield pycompat.bytestr(thing)
544 else:
545 for i in thing:
546 i = templateutil.unwraphybrid(i)
547 if isinstance(i, bytes):
548 yield i
549 elif i is None:
550 pass
551 elif not util.safehasattr(i, '__iter__'):
552 yield pycompat.bytestr(i)
553 else:
554 for j in _flatten(i):
555 yield j
556
557 def unquotestring(s): 530 def unquotestring(s):
558 '''unwrap quotes if any; otherwise returns unmodified string''' 531 '''unwrap quotes if any; otherwise returns unmodified string'''
559 if len(s) < 2 or s[0] not in "'\"" or s[0] != s[-1]: 532 if len(s) < 2 or s[0] not in "'\"" or s[0] != s[-1]:
560 return s 533 return s
561 return s[1:-1] 534 return s[1:-1]
704 # initial 'revcache' may contain pre-computed items. 677 # initial 'revcache' may contain pre-computed items.
705 extramapping = self._resources.populatemap(self, {}, mapping) 678 extramapping = self._resources.populatemap(self, {}, mapping)
706 if extramapping: 679 if extramapping:
707 extramapping.update(mapping) 680 extramapping.update(mapping)
708 mapping = extramapping 681 mapping = extramapping
709 return _flatten(func(self, mapping, data)) 682 return templateutil.flatten(func(self, mapping, data))
710 683
711 engines = {'default': engine} 684 engines = {'default': engine}
712 685
713 def stylelist(): 686 def stylelist():
714 paths = templatepaths() 687 paths = templatepaths()