Revision 0d249b3e kamaki/cli/utils.py

b/kamaki/cli/utils.py
69 69
    return new_d
70 70

  
71 71

  
72
def print_dict(d, exclude=(), ident=0):
72
def print_dict(d, exclude=(), ident=0, rjust=True):
73 73
    if not isinstance(d, dict):
74 74
        raise CLIError(message='Cannot dict_print a non-dict object')
75
    try:
76
        margin = max(
77
            1 + max(len(unicode(key).strip()) for key in d.keys() \
78
                if not isinstance(key, dict) and not isinstance(key, list)),
79
            ident)
80
    except ValueError:
75
    if rjust:
76
        try:
77
            margin = max(
78
                1 + max(len(unicode(key).strip()) for key in d.keys() \
79
                    if not (isinstance(key, dict) or isinstance(key, list))),
80
                ident)
81
        except ValueError:
82
            margin = ident
83
    else:
81 84
        margin = ident
82 85

  
83 86
    for key, val in sorted(d.items()):
84 87
        if key in exclude:
85 88
            continue
86
        print_str = '%s:' % unicode(key).strip()
89
        print_str = '%s:\t' % unicode(key).strip()
90
        print_str = print_str.rjust(margin) if rjust\
91
        else '%s%s' % (' ' * margin, print_str)
87 92
        if isinstance(val, dict):
88
            print(print_str.rjust(margin) + ' {')
89
            print_dict(val, exclude=exclude, ident=margin + 6)
90
            print '}'.rjust(margin)
93
            print(print_str + ' {')
94
            print_dict(val, exclude=exclude, ident=margin + 6, rjust=rjust)
95
            if rjust:
96
                print '}'.rjust(margin)
97
            else:
98
                print '}'
91 99
        elif isinstance(val, list):
92
            print(print_str.rjust(margin) + ' [')
93
            print_list(val, exclude=exclude, ident=margin + 6)
94
            print ']'.rjust(margin)
100
            print(print_str + ' [')
101
            print_list(val, exclude=exclude, ident=margin + 6, rjust=rjust)
102
            if rjust:
103
                print ']'.rjust(margin)
104
            else:
105
                print']'
95 106
        else:
96
            print print_str.rjust(margin) + ' ' + unicode(val).strip()
107
            print print_str + ' ' + unicode(val).strip()
97 108

  
98 109

  
99 110
def print_list(l, exclude=(), ident=0):

Also available in: Unified diff