Revision f551841a 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, rjust=True):
|
|
72 |
def print_dict(d, exclude=(), ident=0): |
|
73 | 73 |
if not isinstance(d, dict): |
74 | 74 |
raise CLIError(message='Cannot dict_print a non-dict object') |
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: |
|
84 |
margin = ident |
|
75 |
|
|
76 |
margin = max(len(unicode(key).strip())\ |
|
77 |
for key in d.keys() if key not in exclude) |
|
85 | 78 |
|
86 | 79 |
for key, val in sorted(d.items()): |
87 | 80 |
if key in exclude: |
88 | 81 |
continue |
89 |
print_str = '%s:\t' % unicode(key).strip() |
|
90 |
print_str = print_str.rjust(margin) if rjust\ |
|
91 |
else '%s%s' % (' ' * margin, print_str) |
|
82 |
print_str = ' ' * ident |
|
83 |
print_str += ('%s' % key).strip() |
|
84 |
print_str += ' ' * (margin - len(unicode(key).strip())) |
|
85 |
print_str += ': ' |
|
92 | 86 |
if isinstance(val, dict): |
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 '}' |
|
87 |
print(print_str) |
|
88 |
print_dict(val, exclude=exclude, ident=margin + ident) |
|
99 | 89 |
elif isinstance(val, list): |
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']' |
|
90 |
print(print_str) |
|
91 |
print_list(val, exclude=exclude, ident=margin + ident) |
|
106 | 92 |
else: |
107 | 93 |
print print_str + ' ' + unicode(val).strip() |
108 | 94 |
|
109 | 95 |
|
110 |
def print_list(l, exclude=(), ident=0, rjust=True):
|
|
96 |
def print_list(l, exclude=(), ident=0): |
|
111 | 97 |
if not isinstance(l, list): |
112 | 98 |
raise CLIError(message='Cannot list_print a non-list object') |
113 |
try: |
|
114 |
margin = max( |
|
115 |
1 + max(len(unicode(item).strip()) for item in l \ |
|
116 |
if not isinstance(item, dict) and not isinstance(item, list)), |
|
117 |
ident) |
|
118 |
except ValueError: |
|
119 |
margin = ident |
|
99 |
|
|
100 |
margin = max(len(unicode(item).strip())\ |
|
101 |
for item in l if item not in exclude) |
|
120 | 102 |
|
121 | 103 |
for item in sorted(l): |
122 | 104 |
if item in exclude: |
123 | 105 |
continue |
124 | 106 |
if isinstance(item, dict): |
125 |
print '{'.rjust(margin) if rjust else '{' |
|
126 |
print_dict(item, exclude=exclude, ident=margin + 6) |
|
127 |
print '}'.rjust(margin) if rjust else '}' |
|
107 |
print_dict(item, exclude=exclude, ident=margin + ident) |
|
128 | 108 |
elif isinstance(item, list): |
129 |
print '['.rjust(margin) if rjust else ']' |
|
130 |
print_list(item, exclude=exclude, ident=margin + 6) |
|
131 |
print ']'.rjust(margin) if rjust else ']' |
|
109 |
print_list(item, exclude=exclude, ident=margin + ident) |
|
132 | 110 |
else: |
133 |
print unicode(item).rjust(margin) if rjust else unicode(item)
|
|
111 |
print ' ' * ident + unicode(item)
|
|
134 | 112 |
|
135 | 113 |
|
136 | 114 |
def print_items(items, title=('id', 'name')): |
Also available in: Unified diff