root / kamaki / cli / utils.py @ fd5db045
History | View | Annotate | Download (5.3 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
try:
|
34 |
from colors import magenta, red, yellow, bold |
35 |
except ImportError: |
36 |
#No colours? No worries, use dummy foo instead
|
37 |
def bold(val): |
38 |
return val
|
39 |
red = yellow = magenta = bold |
40 |
|
41 |
|
42 |
def remove_colors(): |
43 |
global bold
|
44 |
|
45 |
def dummy(val): |
46 |
return val
|
47 |
bold = dummy |
48 |
|
49 |
|
50 |
def pretty_keys(d, delim='_', recurcive=False): |
51 |
"""Transform keys of a dict from the form
|
52 |
str1_str2_..._strN to the form strN
|
53 |
where _ is the delimeter
|
54 |
"""
|
55 |
new_d = {} |
56 |
for key, val in d.items(): |
57 |
new_key = key.split(delim)[-1]
|
58 |
if recurcive and isinstance(val, dict): |
59 |
new_val = pretty_keys(val, delim, recurcive) |
60 |
else:
|
61 |
new_val = val |
62 |
new_d[new_key] = new_val |
63 |
return new_d
|
64 |
|
65 |
|
66 |
def print_dict(d, exclude=(), ident=0): |
67 |
if not isinstance(d, dict): |
68 |
raise CLIError(message='Cannot dict_print a non-dict object') |
69 |
try:
|
70 |
margin = max(
|
71 |
1 + max(len(unicode(key).strip()) for key in d.keys() \ |
72 |
if not isinstance(key, dict) and not isinstance(key, list)), |
73 |
ident) |
74 |
except ValueError: |
75 |
margin = ident |
76 |
|
77 |
for key, val in sorted(d.items()): |
78 |
if key in exclude: |
79 |
continue
|
80 |
print_str = '%s:' % unicode(key).strip() |
81 |
if isinstance(val, dict): |
82 |
print(print_str.rjust(margin) + ' {')
|
83 |
print_dict(val, exclude=exclude, ident=margin + 6)
|
84 |
print '}'.rjust(margin) |
85 |
elif isinstance(val, list): |
86 |
print(print_str.rjust(margin) + ' [')
|
87 |
print_list(val, exclude=exclude, ident=margin + 6)
|
88 |
print ']'.rjust(margin) |
89 |
else:
|
90 |
print print_str.rjust(margin) + ' ' + unicode(val).strip() |
91 |
|
92 |
|
93 |
def print_list(l, exclude=(), ident=0): |
94 |
if not isinstance(l, list): |
95 |
raise CLIError(message='Cannot list_print a non-list object') |
96 |
try:
|
97 |
margin = max(
|
98 |
1 + max(len(unicode(item).strip()) for item in l \ |
99 |
if not isinstance(item, dict) and not isinstance(item, list)), |
100 |
ident) |
101 |
except ValueError: |
102 |
margin = ident |
103 |
|
104 |
for item in sorted(l): |
105 |
if item in exclude: |
106 |
continue
|
107 |
if isinstance(item, dict): |
108 |
print('{'.rjust(margin))
|
109 |
print_dict(item, exclude=exclude, ident=margin + 6)
|
110 |
print '}'.rjust(margin) |
111 |
elif isinstance(item, list): |
112 |
print '['.rjust(margin) |
113 |
print_list(item, exclude=exclude, ident=margin + 6)
|
114 |
print ']'.rjust(margin) |
115 |
else:
|
116 |
print unicode(item).rjust(margin) |
117 |
|
118 |
|
119 |
def print_items(items, title=('id', 'name')): |
120 |
for item in items: |
121 |
if isinstance(item, dict) or isinstance(item, list): |
122 |
print ' '.join(unicode(item.pop(key))\ |
123 |
for key in title if key in item) |
124 |
if isinstance(item, dict): |
125 |
print_dict(item) |
126 |
|
127 |
|
128 |
def format_size(size): |
129 |
units = ('B', 'K', 'M', 'G', 'T') |
130 |
try:
|
131 |
size = float(size)
|
132 |
except ValueError: |
133 |
raise CLIError(message='Cannot format %s in bytes' % size) |
134 |
for unit in units: |
135 |
if size < 1024: |
136 |
break
|
137 |
size /= 1024
|
138 |
s = ('%.1f' % size)
|
139 |
if '.0' == s[-2:]: |
140 |
s = s[:-2]
|
141 |
return s + unit
|
142 |
|
143 |
|
144 |
def dict2file(d, f, depth=0): |
145 |
for k, v in d.items(): |
146 |
f.write('%s%s: ' % ('\t' * depth, k)) |
147 |
if isinstance(v, dict): |
148 |
f.write('\n')
|
149 |
dict2file(v, f, depth + 1)
|
150 |
elif isinstance(v, list): |
151 |
f.write('\n')
|
152 |
list2file(v, f, depth + 1)
|
153 |
else:
|
154 |
f.write(' %s\n' % unicode(v)) |
155 |
|
156 |
|
157 |
def list2file(l, f, depth=1): |
158 |
for item in l: |
159 |
if isinstance(item, dict): |
160 |
dict2file(item, f, depth + 1)
|
161 |
elif isinstance(item, list): |
162 |
list2file(item, f, depth + 1)
|
163 |
else:
|
164 |
f.write('%s%s\n' % ('\t' * depth, unicode(item))) |