root / kamaki / cli / utils.py @ f23a5cdb
History | View | Annotate | Download (6 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 | efbcdc41 | Stavros Sachtouris | from re import compile as regex_compile |
35 | 3dabe5d2 | Stavros Sachtouris | from .errors import CLIError |
36 | 3dabe5d2 | Stavros Sachtouris | |
37 | dfee2caf | Stavros Sachtouris | try:
|
38 | dfee2caf | Stavros Sachtouris | from colors import magenta, red, yellow, bold |
39 | dfee2caf | Stavros Sachtouris | except ImportError: |
40 | 3dabe5d2 | Stavros Sachtouris | # No colours? No worries, use dummy foo instead
|
41 | f93854ae | Stavros Sachtouris | def dummy(val): |
42 | dfee2caf | Stavros Sachtouris | return val
|
43 | f93854ae | Stavros Sachtouris | red = yellow = magenta = bold = dummy |
44 | f997679d | Stavros Sachtouris | |
45 | fd5db045 | Stavros Sachtouris | |
46 | 67cea04c | Stavros Sachtouris | def remove_colors(): |
47 | 67cea04c | Stavros Sachtouris | global bold
|
48 | 3dabe5d2 | Stavros Sachtouris | global red
|
49 | 3dabe5d2 | Stavros Sachtouris | global yellow
|
50 | 3dabe5d2 | Stavros Sachtouris | global magenta
|
51 | fd5db045 | Stavros Sachtouris | |
52 | 67cea04c | Stavros Sachtouris | def dummy(val): |
53 | 67cea04c | Stavros Sachtouris | return val
|
54 | 3dabe5d2 | Stavros Sachtouris | red = yellow = magenta = bold = dummy |
55 | 7493ccb6 | Stavros Sachtouris | |
56 | fd5db045 | Stavros Sachtouris | |
57 | 7493ccb6 | Stavros Sachtouris | def pretty_keys(d, delim='_', recurcive=False): |
58 | f23a5cdb | Stavros Sachtouris | """<term>delim<term> to <term> <term> transformation
|
59 | 7493ccb6 | Stavros Sachtouris | """
|
60 | 7493ccb6 | Stavros Sachtouris | new_d = {} |
61 | 7493ccb6 | Stavros Sachtouris | for key, val in d.items(): |
62 | 7493ccb6 | Stavros Sachtouris | new_key = key.split(delim)[-1]
|
63 | 7493ccb6 | Stavros Sachtouris | if recurcive and isinstance(val, dict): |
64 | fd5db045 | Stavros Sachtouris | new_val = pretty_keys(val, delim, recurcive) |
65 | 7493ccb6 | Stavros Sachtouris | else:
|
66 | 7493ccb6 | Stavros Sachtouris | new_val = val |
67 | 7493ccb6 | Stavros Sachtouris | new_d[new_key] = new_val |
68 | 7493ccb6 | Stavros Sachtouris | return new_d
|
69 | 7493ccb6 | Stavros Sachtouris | |
70 | fd5db045 | Stavros Sachtouris | |
71 | f551841a | Stavros Sachtouris | def print_dict(d, exclude=(), ident=0): |
72 | 7493ccb6 | Stavros Sachtouris | if not isinstance(d, dict): |
73 | 7493ccb6 | Stavros Sachtouris | raise CLIError(message='Cannot dict_print a non-dict object') |
74 | f551841a | Stavros Sachtouris | |
75 | db950b10 | Stavros Sachtouris | if d:
|
76 | db950b10 | Stavros Sachtouris | margin = max(len(unicode(key).strip())\ |
77 | db950b10 | 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 | db950b10 | Stavros Sachtouris | if l:
|
101 | db950b10 | Stavros Sachtouris | margin = max(len(unicode(item).strip())\ |
102 | db950b10 | Stavros Sachtouris | for item in l if item not in exclude) |
103 | 7493ccb6 | Stavros Sachtouris | |
104 | 7493ccb6 | Stavros Sachtouris | for item in sorted(l): |
105 | 7493ccb6 | Stavros Sachtouris | if item in exclude: |
106 | 7493ccb6 | Stavros Sachtouris | continue
|
107 | 7493ccb6 | Stavros Sachtouris | if isinstance(item, dict): |
108 | f551841a | Stavros Sachtouris | print_dict(item, exclude=exclude, ident=margin + ident) |
109 | 7493ccb6 | Stavros Sachtouris | elif isinstance(item, list): |
110 | f551841a | Stavros Sachtouris | print_list(item, exclude=exclude, ident=margin + ident) |
111 | 7493ccb6 | Stavros Sachtouris | else:
|
112 | f551841a | Stavros Sachtouris | print ' ' * ident + unicode(item) |
113 | 7493ccb6 | Stavros Sachtouris | |
114 | fd5db045 | Stavros Sachtouris | |
115 | 7493ccb6 | Stavros Sachtouris | def print_items(items, title=('id', 'name')): |
116 | 7493ccb6 | Stavros Sachtouris | for item in items: |
117 | 7493ccb6 | Stavros Sachtouris | if isinstance(item, dict) or isinstance(item, list): |
118 | aa25346e | Stavros Sachtouris | header = ' '.join(unicode(item.pop(key))\ |
119 | fd5db045 | Stavros Sachtouris | for key in title if key in item) |
120 | aa25346e | Stavros Sachtouris | print(' ')
|
121 | aa25346e | Stavros Sachtouris | print(bold(header)) |
122 | 7493ccb6 | Stavros Sachtouris | if isinstance(item, dict): |
123 | aa25346e | Stavros Sachtouris | print_dict(item, ident=2)
|
124 | aa25346e | Stavros Sachtouris | elif isinstance(item, list): |
125 | aa25346e | Stavros Sachtouris | print_list(item, ident=2)
|
126 | 7493ccb6 | Stavros Sachtouris | |
127 | fd5db045 | Stavros Sachtouris | |
128 | 7493ccb6 | Stavros Sachtouris | def format_size(size): |
129 | 7493ccb6 | Stavros Sachtouris | units = ('B', 'K', 'M', 'G', 'T') |
130 | 7493ccb6 | Stavros Sachtouris | try:
|
131 | 7493ccb6 | Stavros Sachtouris | size = float(size)
|
132 | 7493ccb6 | Stavros Sachtouris | except ValueError: |
133 | fd5db045 | Stavros Sachtouris | raise CLIError(message='Cannot format %s in bytes' % size) |
134 | 7493ccb6 | Stavros Sachtouris | for unit in units: |
135 | 7493ccb6 | Stavros Sachtouris | if size < 1024: |
136 | 7493ccb6 | Stavros Sachtouris | break
|
137 | 7493ccb6 | Stavros Sachtouris | size /= 1024
|
138 | 7493ccb6 | Stavros Sachtouris | s = ('%.1f' % size)
|
139 | 7493ccb6 | Stavros Sachtouris | if '.0' == s[-2:]: |
140 | 7493ccb6 | Stavros Sachtouris | s = s[:-2]
|
141 | 7493ccb6 | Stavros Sachtouris | return s + unit
|
142 | 7493ccb6 | Stavros Sachtouris | |
143 | fd5db045 | Stavros Sachtouris | |
144 | fd5db045 | Stavros Sachtouris | def dict2file(d, f, depth=0): |
145 | 7493ccb6 | Stavros Sachtouris | for k, v in d.items(): |
146 | fd5db045 | Stavros Sachtouris | f.write('%s%s: ' % ('\t' * depth, k)) |
147 | fd5db045 | Stavros Sachtouris | if isinstance(v, dict): |
148 | 7493ccb6 | Stavros Sachtouris | f.write('\n')
|
149 | fd5db045 | Stavros Sachtouris | dict2file(v, f, depth + 1)
|
150 | fd5db045 | Stavros Sachtouris | elif isinstance(v, list): |
151 | 7493ccb6 | Stavros Sachtouris | f.write('\n')
|
152 | fd5db045 | Stavros Sachtouris | list2file(v, f, depth + 1)
|
153 | 7493ccb6 | Stavros Sachtouris | else:
|
154 | fd5db045 | Stavros Sachtouris | f.write(' %s\n' % unicode(v)) |
155 | fd5db045 | Stavros Sachtouris | |
156 | 7493ccb6 | Stavros Sachtouris | |
157 | fd5db045 | Stavros Sachtouris | def list2file(l, f, depth=1): |
158 | 7493ccb6 | Stavros Sachtouris | for item in l: |
159 | fd5db045 | Stavros Sachtouris | if isinstance(item, dict): |
160 | fd5db045 | Stavros Sachtouris | dict2file(item, f, depth + 1)
|
161 | fd5db045 | Stavros Sachtouris | elif isinstance(item, list): |
162 | fd5db045 | Stavros Sachtouris | list2file(item, f, depth + 1)
|
163 | 7493ccb6 | Stavros Sachtouris | else:
|
164 | fd5db045 | Stavros Sachtouris | f.write('%s%s\n' % ('\t' * depth, unicode(item))) |
165 | efbcdc41 | Stavros Sachtouris | |
166 | efbcdc41 | Stavros Sachtouris | # Split input auxiliary
|
167 | efbcdc41 | Stavros Sachtouris | |
168 | efbcdc41 | Stavros Sachtouris | |
169 | efbcdc41 | Stavros Sachtouris | def _parse_with_regex(line, regex): |
170 | efbcdc41 | Stavros Sachtouris | re_parser = regex_compile(regex) |
171 | efbcdc41 | Stavros Sachtouris | return (re_parser.split(line), re_parser.findall(line))
|
172 | efbcdc41 | Stavros Sachtouris | |
173 | efbcdc41 | Stavros Sachtouris | |
174 | efbcdc41 | Stavros Sachtouris | def _sub_split(line): |
175 | efbcdc41 | Stavros Sachtouris | terms = [] |
176 | efbcdc41 | Stavros Sachtouris | (sub_trivials, sub_interesting) = _parse_with_regex(line, ' ".*?" ')
|
177 | efbcdc41 | Stavros Sachtouris | for subi, subipart in enumerate(sub_interesting): |
178 | efbcdc41 | Stavros Sachtouris | terms += sub_trivials[subi].split() |
179 | efbcdc41 | Stavros Sachtouris | terms.append(subipart[2:-2]) |
180 | efbcdc41 | Stavros Sachtouris | terms += sub_trivials[-1].split()
|
181 | efbcdc41 | Stavros Sachtouris | return terms
|
182 | efbcdc41 | Stavros Sachtouris | |
183 | efbcdc41 | Stavros Sachtouris | |
184 | efbcdc41 | Stavros Sachtouris | def split_input(line): |
185 | efbcdc41 | Stavros Sachtouris | """Use regular expressions to split a line correctly
|
186 | efbcdc41 | Stavros Sachtouris | """
|
187 | efbcdc41 | Stavros Sachtouris | line = ' %s ' % line
|
188 | efbcdc41 | Stavros Sachtouris | (trivial_parts, interesting_parts) = _parse_with_regex(line, ' \'.*?\' ')
|
189 | efbcdc41 | Stavros Sachtouris | terms = [] |
190 | efbcdc41 | Stavros Sachtouris | for i, ipart in enumerate(interesting_parts): |
191 | efbcdc41 | Stavros Sachtouris | terms += _sub_split(trivial_parts[i]) |
192 | efbcdc41 | Stavros Sachtouris | terms.append(ipart[2:-2]) |
193 | efbcdc41 | Stavros Sachtouris | terms += _sub_split(trivial_parts[-1])
|
194 | efbcdc41 | Stavros Sachtouris | return terms |