Mercurial > hg
changeset 40276:be57c7019c70
stringutil: allow to specify initial indent level of pprint()
I want to pprint() an inner object, which starts with level=1 indent.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 14 Oct 2018 07:23:02 +0200 |
parents | f2fbd32c7664 |
children | 1159031ada1e |
files | mercurial/utils/stringutil.py |
diffstat | 1 files changed, 36 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/utils/stringutil.py Sun Oct 14 07:18:19 2018 +0200 +++ b/mercurial/utils/stringutil.py Sun Oct 14 07:23:02 2018 +0200 @@ -43,11 +43,11 @@ return pat return pat.encode('latin1') -def pprint(o, bprefix=False, indent=0): +def pprint(o, bprefix=False, indent=0, level=0): """Pretty print an object.""" - return b''.join(pprintgen(o, bprefix=bprefix, indent=indent)) + return b''.join(pprintgen(o, bprefix=bprefix, indent=indent, level=level)) -def pprintgen(o, bprefix=False, indent=0, _level=0): +def pprintgen(o, bprefix=False, indent=0, level=0): """Pretty print an object to a generator of atoms. ``bprefix`` is a flag influencing whether bytestrings are preferred with @@ -56,6 +56,8 @@ ``indent`` controls whether collections and nested data structures span multiple lines via the indentation amount in spaces. By default, no newlines are emitted. + + ``level`` specifies the initial indent level. Used if ``indent > 0``. """ if isinstance(o, bytes): @@ -75,26 +77,26 @@ yield '[' if indent: - _level += 1 + level += 1 yield '\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) for i, a in enumerate(o): for chunk in pprintgen(a, bprefix=bprefix, indent=indent, - _level=_level): + level=level): yield chunk if i + 1 < len(o): if indent: yield ',\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) else: yield ', ' if indent: - _level -= 1 + level -= 1 yield '\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) yield ']' elif isinstance(o, dict): @@ -105,32 +107,32 @@ yield '{' if indent: - _level += 1 + level += 1 yield '\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) for i, (k, v) in enumerate(sorted(o.items())): for chunk in pprintgen(k, bprefix=bprefix, indent=indent, - _level=_level): + level=level): yield chunk yield ': ' for chunk in pprintgen(v, bprefix=bprefix, indent=indent, - _level=_level): + level=level): yield chunk if i + 1 < len(o): if indent: yield ',\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) else: yield ', ' if indent: - _level -= 1 + level -= 1 yield '\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) yield '}' elif isinstance(o, set): @@ -141,26 +143,26 @@ yield 'set([' if indent: - _level += 1 + level += 1 yield '\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) for i, k in enumerate(sorted(o)): for chunk in pprintgen(k, bprefix=bprefix, indent=indent, - _level=_level): + level=level): yield chunk if i + 1 < len(o): if indent: yield ',\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) else: yield ', ' if indent: - _level -= 1 + level -= 1 yield '\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) yield '])' elif isinstance(o, tuple): @@ -171,26 +173,26 @@ yield '(' if indent: - _level += 1 + level += 1 yield '\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) for i, a in enumerate(o): for chunk in pprintgen(a, bprefix=bprefix, indent=indent, - _level=_level): + level=level): yield chunk if i + 1 < len(o): if indent: yield ',\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) else: yield ', ' if indent: - _level -= 1 + level -= 1 yield '\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) yield ')' elif isinstance(o, types.GeneratorType): @@ -204,9 +206,9 @@ yield 'gen[' if indent: - _level += 1 + level += 1 yield '\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) last = False @@ -219,20 +221,20 @@ last = True for chunk in pprintgen(current, bprefix=bprefix, indent=indent, - _level=_level): + level=level): yield chunk if not last: if indent: yield ',\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) else: yield ', ' if indent: - _level -= 1 + level -= 1 yield '\n' - yield ' ' * (_level * indent) + yield ' ' * (level * indent) yield ']' else: