Revision f8681ec8 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, |
|
73 |
exclude=(), |
|
74 |
ident=0, |
|
75 |
with_enumeration=False, |
|
76 |
recursive_enumeration=False): |
|
77 |
""" |
|
78 |
Pretty-print a dictionary object |
|
79 |
|
|
80 |
:param d: (dict) the input |
|
81 |
|
|
82 |
:param excelude: (set or list) keys to exclude from printing |
|
83 |
|
|
84 |
:param ident: (int) initial indentation (recursive) |
|
85 |
|
|
86 |
:param with_enumeration: (bool) enumerate each 1st level key if true |
|
87 |
|
|
88 |
:recursive_enumeration: (bool) recursively enumerate dicts and lists of |
|
89 |
2nd level or deeper |
|
90 |
|
|
91 |
:raises CLIError: (TypeError wrapper) non-dict input |
|
92 |
""" |
|
73 | 93 |
if not isinstance(d, dict): |
74 | 94 |
raiseCLIError(TypeError('Cannot dict_print a non-dict object')) |
75 | 95 |
|
... | ... | |
77 | 97 |
margin = max(len(unicode(key).strip())\ |
78 | 98 |
for key in d.keys() if key not in exclude) |
79 | 99 |
|
100 |
counter = 1 |
|
80 | 101 |
for key, val in sorted(d.items()): |
81 | 102 |
if key in exclude: |
82 | 103 |
continue |
83 |
print_str = ' ' * ident |
|
104 |
print_str = '' |
|
105 |
if with_enumeration: |
|
106 |
print_str = '%s. ' % counter |
|
107 |
counter += 1 |
|
108 |
print_str = '%s%s' % (' ' * (ident - len(print_str)), print_str) |
|
84 | 109 |
print_str += ('%s' % key).strip() |
85 | 110 |
print_str += ' ' * (margin - len(unicode(key).strip())) |
86 | 111 |
print_str += ': ' |
87 | 112 |
if isinstance(val, dict): |
88 | 113 |
print(print_str) |
89 |
print_dict(val, exclude=exclude, ident=margin + ident) |
|
114 |
print_dict(val, |
|
115 |
exclude=exclude, |
|
116 |
ident=margin + ident, |
|
117 |
with_enumeration=recursive_enumeration, |
|
118 |
recursive_enumeration=recursive_enumeration) |
|
90 | 119 |
elif isinstance(val, list): |
91 | 120 |
print(print_str) |
92 |
print_list(val, exclude=exclude, ident=margin + ident) |
|
121 |
print_list(val, |
|
122 |
exclude=exclude, |
|
123 |
ident=margin + ident, |
|
124 |
with_enumeration=recursive_enumeration, |
|
125 |
recursive_enumeration=recursive_enumeration) |
|
93 | 126 |
else: |
94 | 127 |
print print_str + ' ' + unicode(val).strip() |
95 | 128 |
|
96 | 129 |
|
97 |
def print_list(l, exclude=(), ident=0, enumerate=False): |
|
130 |
def print_list(l, |
|
131 |
exclude=(), |
|
132 |
ident=0, |
|
133 |
with_enumeration=False, |
|
134 |
recursive_enumeration=False): |
|
135 |
""" |
|
136 |
Pretty-print a list object |
|
137 |
|
|
138 |
:param l: (list) the input |
|
139 |
|
|
140 |
:param excelude: (object - anytype) values to exclude from printing |
|
141 |
|
|
142 |
:param ident: (int) initial indentation (recursive) |
|
143 |
|
|
144 |
:param with_enumeration: (bool) enumerate each 1st level value if true |
|
145 |
|
|
146 |
:recursive_enumeration: (bool) recursively enumerate dicts and lists of |
|
147 |
2nd level or deeper |
|
148 |
|
|
149 |
:raises CLIError: (TypeError wrapper) non-list input |
|
150 |
""" |
|
98 | 151 |
if not isinstance(l, list): |
99 | 152 |
raiseCLIError(TypeError('Cannot list_print a non-list object')) |
100 | 153 |
|
... | ... | |
112 | 165 |
for item in sorted(l): |
113 | 166 |
if item in exclude: |
114 | 167 |
continue |
115 |
elif enumerate:
|
|
168 |
elif with_enumeration:
|
|
116 | 169 |
prefix = '%s. ' % counter |
117 | 170 |
counter += 1 |
118 | 171 |
prefix = '%s%s' % (' ' * (ident - len(prefix)), prefix) |
... | ... | |
120 | 173 |
prefix = ' ' * ident |
121 | 174 |
if isinstance(item, dict): |
122 | 175 |
print('%s' % prefix) |
123 |
print_dict(item, exclude=exclude, ident=margin + ident) |
|
176 |
print_dict(item, |
|
177 |
exclude=exclude, |
|
178 |
ident=margin + ident, |
|
179 |
with_enumeration=recursive_enumeration, |
|
180 |
recursive_enumeration=recursive_enumeration) |
|
124 | 181 |
elif isinstance(item, list): |
125 | 182 |
print('%s' % prefix) |
126 |
print_list(item, exclude=exclude, ident=margin + ident) |
|
183 |
print_list(item, |
|
184 |
exclude=exclude, |
|
185 |
ident=margin + ident, |
|
186 |
with_enumeration=recursive_enumeration, |
|
187 |
recursive_enumeration=recursive_enumeration) |
|
127 | 188 |
else: |
128 | 189 |
print('%s%s' % (prefix, item)) |
129 | 190 |
|
Also available in: Unified diff