root / kamaki / cli / utils.py @ f551841a
History | View | Annotate | Download (5 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 | f551841a | 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 | f551841a | Stavros Sachtouris | |
76 | f551841a | Stavros Sachtouris | margin = max(len(unicode(key).strip())\ |
77 | f551841a | Stavros Sachtouris | for key in d.keys() if key not in exclude) |
78 | 7493ccb6 | Stavros Sachtouris | |
79 | 7493ccb6 | Stavros Sachtouris | for key, val in sorted(d.items()): |
80 | 7493ccb6 | Stavros Sachtouris | if key in exclude: |
81 | 7493ccb6 | Stavros Sachtouris | continue
|
82 | f551841a | Stavros Sachtouris | print_str = ' ' * ident
|
83 | f551841a | Stavros Sachtouris | print_str += ('%s' % key).strip()
|
84 | f551841a | Stavros Sachtouris | print_str += ' ' * (margin - len(unicode(key).strip())) |
85 | f551841a | Stavros Sachtouris | print_str += ': '
|
86 | 7493ccb6 | Stavros Sachtouris | if isinstance(val, dict): |
87 | f551841a | Stavros Sachtouris | print(print_str) |
88 | f551841a | Stavros Sachtouris | print_dict(val, exclude=exclude, ident=margin + ident) |
89 | 7493ccb6 | Stavros Sachtouris | elif isinstance(val, list): |
90 | f551841a | Stavros Sachtouris | print(print_str) |
91 | f551841a | Stavros Sachtouris | print_list(val, exclude=exclude, ident=margin + ident) |
92 | 7493ccb6 | Stavros Sachtouris | else:
|
93 | 0d249b3e | Stavros Sachtouris | print print_str + ' ' + unicode(val).strip() |
94 | fd5db045 | Stavros Sachtouris | |
95 | 7493ccb6 | Stavros Sachtouris | |
96 | f551841a | Stavros Sachtouris | def print_list(l, exclude=(), ident=0): |
97 | 7493ccb6 | Stavros Sachtouris | if not isinstance(l, list): |
98 | 7493ccb6 | Stavros Sachtouris | raise CLIError(message='Cannot list_print a non-list object') |
99 | f551841a | Stavros Sachtouris | |
100 | f551841a | Stavros Sachtouris | margin = max(len(unicode(item).strip())\ |
101 | f551841a | Stavros Sachtouris | for item in l if item not in exclude) |
102 | 7493ccb6 | Stavros Sachtouris | |
103 | 7493ccb6 | Stavros Sachtouris | for item in sorted(l): |
104 | 7493ccb6 | Stavros Sachtouris | if item in exclude: |
105 | 7493ccb6 | Stavros Sachtouris | continue
|
106 | 7493ccb6 | Stavros Sachtouris | if isinstance(item, dict): |
107 | f551841a | Stavros Sachtouris | print_dict(item, exclude=exclude, ident=margin + ident) |
108 | 7493ccb6 | Stavros Sachtouris | elif isinstance(item, list): |
109 | f551841a | Stavros Sachtouris | print_list(item, exclude=exclude, ident=margin + ident) |
110 | 7493ccb6 | Stavros Sachtouris | else:
|
111 | f551841a | Stavros Sachtouris | print ' ' * ident + unicode(item) |
112 | 7493ccb6 | Stavros Sachtouris | |
113 | fd5db045 | Stavros Sachtouris | |
114 | 7493ccb6 | Stavros Sachtouris | def print_items(items, title=('id', 'name')): |
115 | 7493ccb6 | Stavros Sachtouris | for item in items: |
116 | 7493ccb6 | Stavros Sachtouris | if isinstance(item, dict) or isinstance(item, list): |
117 | fd5db045 | Stavros Sachtouris | print ' '.join(unicode(item.pop(key))\ |
118 | fd5db045 | Stavros Sachtouris | for key in title if key in item) |
119 | 7493ccb6 | Stavros Sachtouris | if isinstance(item, dict): |
120 | 7493ccb6 | Stavros Sachtouris | print_dict(item) |
121 | 7493ccb6 | Stavros Sachtouris | |
122 | fd5db045 | Stavros Sachtouris | |
123 | 7493ccb6 | Stavros Sachtouris | def format_size(size): |
124 | 7493ccb6 | Stavros Sachtouris | units = ('B', 'K', 'M', 'G', 'T') |
125 | 7493ccb6 | Stavros Sachtouris | try:
|
126 | 7493ccb6 | Stavros Sachtouris | size = float(size)
|
127 | 7493ccb6 | Stavros Sachtouris | except ValueError: |
128 | fd5db045 | Stavros Sachtouris | raise CLIError(message='Cannot format %s in bytes' % size) |
129 | 7493ccb6 | Stavros Sachtouris | for unit in units: |
130 | 7493ccb6 | Stavros Sachtouris | if size < 1024: |
131 | 7493ccb6 | Stavros Sachtouris | break
|
132 | 7493ccb6 | Stavros Sachtouris | size /= 1024
|
133 | 7493ccb6 | Stavros Sachtouris | s = ('%.1f' % size)
|
134 | 7493ccb6 | Stavros Sachtouris | if '.0' == s[-2:]: |
135 | 7493ccb6 | Stavros Sachtouris | s = s[:-2]
|
136 | 7493ccb6 | Stavros Sachtouris | return s + unit
|
137 | 7493ccb6 | Stavros Sachtouris | |
138 | fd5db045 | Stavros Sachtouris | |
139 | fd5db045 | Stavros Sachtouris | def dict2file(d, f, depth=0): |
140 | 7493ccb6 | Stavros Sachtouris | for k, v in d.items(): |
141 | fd5db045 | Stavros Sachtouris | f.write('%s%s: ' % ('\t' * depth, k)) |
142 | fd5db045 | Stavros Sachtouris | if isinstance(v, dict): |
143 | 7493ccb6 | Stavros Sachtouris | f.write('\n')
|
144 | fd5db045 | Stavros Sachtouris | dict2file(v, f, depth + 1)
|
145 | fd5db045 | Stavros Sachtouris | elif isinstance(v, list): |
146 | 7493ccb6 | Stavros Sachtouris | f.write('\n')
|
147 | fd5db045 | Stavros Sachtouris | list2file(v, f, depth + 1)
|
148 | 7493ccb6 | Stavros Sachtouris | else:
|
149 | fd5db045 | Stavros Sachtouris | f.write(' %s\n' % unicode(v)) |
150 | fd5db045 | Stavros Sachtouris | |
151 | 7493ccb6 | Stavros Sachtouris | |
152 | fd5db045 | Stavros Sachtouris | def list2file(l, f, depth=1): |
153 | 7493ccb6 | Stavros Sachtouris | for item in l: |
154 | fd5db045 | Stavros Sachtouris | if isinstance(item, dict): |
155 | fd5db045 | Stavros Sachtouris | dict2file(item, f, depth + 1)
|
156 | fd5db045 | Stavros Sachtouris | elif isinstance(item, list): |
157 | fd5db045 | Stavros Sachtouris | list2file(item, f, depth + 1)
|
158 | 7493ccb6 | Stavros Sachtouris | else:
|
159 | fd5db045 | Stavros Sachtouris | f.write('%s%s\n' % ('\t' * depth, unicode(item))) |