On Tue, Feb 18, 2014 at 3:39 PM, Hrvoje Ribicic <[email protected]> wrote:
> Aligning dictionary entries makes no difference to the YAML parser, but > Just nitpicking: I'd say "a YAML parser" > makes the output much easier to read and compare. This patch adds the > possibility of specifying alignment groups to ordered dictionary > entries. > > Signed-off-by: Hrvoje Ribicic <[email protected]> > --- > lib/cli.py | 34 +++++++++++++++++++++++++++++++++- > 1 file changed, 33 insertions(+), 1 deletion(-) > > diff --git a/lib/cli.py b/lib/cli.py > index 97c16ba..6dc415f 100644 > --- a/lib/cli.py > +++ b/lib/cli.py > @@ -4182,6 +4182,30 @@ def _NotAContainer(data): > isinstance(data, tuple)) > > > +def _GetAlignmentMapping(data): > + """ Returns info about alignment if present in an encoded ordered > dictionary. > + > + @type data: list of tuple > + @param data: The encoded ordered dictionary, as defined in > + L{_SerializeGenericInfo}. > + @rtype: dict of any to int > + @return: The dictionary mapping alignment groups to the maximum length > of the > + dictionary key found in the group. > + > + """ > + alignment_map = {} > + for entry in data: > + if len(entry) > 2: > + group_key = entry[2] > + key_length = len(entry[0]) > + if group_key in alignment_map: > + alignment_map[group_key] = max(alignment_map[group_key], > key_length) > + else: > + alignment_map[group_key] = key_length > + > + return alignment_map > + > + > def _SerializeGenericInfo(buf, data, level, afterkey=False): > """Formatting core of L{PrintGenericInfo}. > > @@ -4214,18 +4238,26 @@ def _SerializeGenericInfo(buf, data, level, > afterkey=False): > _SerializeGenericInfo(buf, data[key], level + 1, afterkey=True) > elif isinstance(data, list) and len(data) > 0 and isinstance(data[0], > tuple): > # list of tuples (an ordered dictionary) > + # the tuples may have two or three members - key, value, and > alignment group > + # if the alignment group is present, align all values sharing the > same group > if afterkey: > buf.write("\n") > doindent = True > else: > doindent = False > - for (key, val) in data: > + > + alignment_mapping = _GetAlignmentMapping(data) > + for entry in data: > + key, val = entry[0:2] > if doindent: > buf.write(baseind * level) > else: > doindent = True > buf.write(key) > buf.write(": ") > + if len(entry) > 2: > + max_key_length = alignment_mapping[entry[2]] > + buf.write(" " * (max_key_length - len(key))) > _SerializeGenericInfo(buf, val, level + 1, afterkey=True) > elif isinstance(data, tuple) and all(map(_NotAContainer, data)): > # tuples with simple content are serialized as inline lists > -- > 1.9.0.rc1.175.g0b1dcb5 > > Also please include the new behavior in the docs for PrintGenericInfo. Otherwise LGTM, thanks.
