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