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