Revision fd5db045 kamaki/cli/utils.py
b/kamaki/cli/utils.py | ||
---|---|---|
38 | 38 |
return val |
39 | 39 |
red = yellow = magenta = bold |
40 | 40 |
|
41 |
|
|
41 | 42 |
def remove_colors(): |
42 | 43 |
global bold |
44 |
|
|
43 | 45 |
def dummy(val): |
44 | 46 |
return val |
45 | 47 |
bold = dummy |
46 | 48 |
|
49 |
|
|
47 | 50 |
def pretty_keys(d, delim='_', recurcive=False): |
48 | 51 |
"""Transform keys of a dict from the form |
49 | 52 |
str1_str2_..._strN to the form strN |
... | ... | |
53 | 56 |
for key, val in d.items(): |
54 | 57 |
new_key = key.split(delim)[-1] |
55 | 58 |
if recurcive and isinstance(val, dict): |
56 |
new_val = pretty_keys(val, delim, recurcive)
|
|
59 |
new_val = pretty_keys(val, delim, recurcive) |
|
57 | 60 |
else: |
58 | 61 |
new_val = val |
59 | 62 |
new_d[new_key] = new_val |
60 | 63 |
return new_d |
61 | 64 |
|
62 |
def print_dict(d, exclude=(), ident= 0): |
|
65 |
|
|
66 |
def print_dict(d, exclude=(), ident=0): |
|
63 | 67 |
if not isinstance(d, dict): |
64 | 68 |
raise CLIError(message='Cannot dict_print a non-dict object') |
65 | 69 |
try: |
... | ... | |
75 | 79 |
continue |
76 | 80 |
print_str = '%s:' % unicode(key).strip() |
77 | 81 |
if isinstance(val, dict): |
78 |
print(print_str.rjust(margin)+' {')
|
|
79 |
print_dict(val, exclude = exclude, ident = margin + 6)
|
|
82 |
print(print_str.rjust(margin) + ' {')
|
|
83 |
print_dict(val, exclude=exclude, ident=margin + 6)
|
|
80 | 84 |
print '}'.rjust(margin) |
81 | 85 |
elif isinstance(val, list): |
82 |
print(print_str.rjust(margin)+' [')
|
|
83 |
print_list(val, exclude = exclude, ident = margin + 6)
|
|
86 |
print(print_str.rjust(margin) + ' [')
|
|
87 |
print_list(val, exclude=exclude, ident=margin + 6)
|
|
84 | 88 |
print ']'.rjust(margin) |
85 | 89 |
else: |
86 |
print print_str.rjust(margin)+' '+unicode(val).strip() |
|
90 |
print print_str.rjust(margin) + ' ' + unicode(val).strip() |
|
91 |
|
|
87 | 92 |
|
88 |
def print_list(l, exclude=(), ident = 0):
|
|
93 |
def print_list(l, exclude=(), ident=0):
|
|
89 | 94 |
if not isinstance(l, list): |
90 | 95 |
raise CLIError(message='Cannot list_print a non-list object') |
91 | 96 |
try: |
... | ... | |
101 | 106 |
continue |
102 | 107 |
if isinstance(item, dict): |
103 | 108 |
print('{'.rjust(margin)) |
104 |
print_dict(item, exclude = exclude, ident = margin + 6)
|
|
109 |
print_dict(item, exclude=exclude, ident=margin + 6)
|
|
105 | 110 |
print '}'.rjust(margin) |
106 | 111 |
elif isinstance(item, list): |
107 | 112 |
print '['.rjust(margin) |
108 |
print_list(item, exclude = exclude, ident = margin + 6)
|
|
113 |
print_list(item, exclude=exclude, ident=margin + 6)
|
|
109 | 114 |
print ']'.rjust(margin) |
110 | 115 |
else: |
111 | 116 |
print unicode(item).rjust(margin) |
112 | 117 |
|
118 |
|
|
113 | 119 |
def print_items(items, title=('id', 'name')): |
114 | 120 |
for item in items: |
115 | 121 |
if isinstance(item, dict) or isinstance(item, list): |
116 |
print ' '.join(unicode(item.pop(key)) for key in title if key in item) |
|
122 |
print ' '.join(unicode(item.pop(key))\ |
|
123 |
for key in title if key in item) |
|
117 | 124 |
if isinstance(item, dict): |
118 | 125 |
print_dict(item) |
119 | 126 |
|
127 |
|
|
120 | 128 |
def format_size(size): |
121 | 129 |
units = ('B', 'K', 'M', 'G', 'T') |
122 | 130 |
try: |
123 | 131 |
size = float(size) |
124 | 132 |
except ValueError: |
125 |
raise CLIError(message='Cannot format %s in bytes'%size)
|
|
133 |
raise CLIError(message='Cannot format %s in bytes' % size)
|
|
126 | 134 |
for unit in units: |
127 | 135 |
if size < 1024: |
128 | 136 |
break |
... | ... | |
132 | 140 |
s = s[:-2] |
133 | 141 |
return s + unit |
134 | 142 |
|
135 |
def dict2file(d, f, depth = 0): |
|
143 |
|
|
144 |
def dict2file(d, f, depth=0): |
|
136 | 145 |
for k, v in d.items(): |
137 |
f.write('%s%s: '%('\t'*depth, k))
|
|
138 |
if isinstance(v,dict): |
|
146 |
f.write('%s%s: ' % ('\t' * depth, k))
|
|
147 |
if isinstance(v, dict):
|
|
139 | 148 |
f.write('\n') |
140 |
dict2file(v, f, depth+1)
|
|
141 |
elif isinstance(v,list): |
|
149 |
dict2file(v, f, depth + 1)
|
|
150 |
elif isinstance(v, list):
|
|
142 | 151 |
f.write('\n') |
143 |
list2file(v, f, depth+1)
|
|
152 |
list2file(v, f, depth + 1)
|
|
144 | 153 |
else: |
145 |
f.write(' %s\n'%unicode(v)) |
|
154 |
f.write(' %s\n' % unicode(v)) |
|
155 |
|
|
146 | 156 |
|
147 |
def list2file(l, f, depth = 1):
|
|
157 |
def list2file(l, f, depth=1):
|
|
148 | 158 |
for item in l: |
149 |
if isinstance(item,dict): |
|
150 |
dict2file(item, f, depth+1)
|
|
151 |
elif isinstance(item,list): |
|
152 |
list2file(item, f, depth+1)
|
|
159 |
if isinstance(item, dict):
|
|
160 |
dict2file(item, f, depth + 1)
|
|
161 |
elif isinstance(item, list):
|
|
162 |
list2file(item, f, depth + 1)
|
|
153 | 163 |
else: |
154 |
f.write('%s%s\n'%('\t'*depth, unicode(item))) |
|
164 |
f.write('%s%s\n' % ('\t' * depth, unicode(item))) |
Also available in: Unified diff