root / kamaki / cli / utils.py @ 545c6c29
History | View | Annotate | Download (15 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 sys import stdout, stdin |
35 |
from re import compile as regex_compile |
36 |
from time import sleep |
37 |
from os import walk, path |
38 |
from json import dumps |
39 |
|
40 |
from kamaki.cli.errors import raiseCLIError |
41 |
|
42 |
suggest = dict(
|
43 |
ansicolors=dict(
|
44 |
active=False,
|
45 |
url='#install-ansicolors-progress',
|
46 |
description='Add colors to console responses'),
|
47 |
progress=dict(
|
48 |
active=False,
|
49 |
url='#install-ansicolors-progress',
|
50 |
description='Add progress bars to some commands'))
|
51 |
|
52 |
try:
|
53 |
from colors import magenta, red, yellow, bold |
54 |
except ImportError: |
55 |
# No colours? No worries, use dummy foo instead
|
56 |
def dummy(val): |
57 |
return val
|
58 |
red = yellow = magenta = bold = dummy |
59 |
suggest['ansicolors']['active'] = True |
60 |
|
61 |
try:
|
62 |
from progress.bar import ShadyBar |
63 |
except ImportError: |
64 |
suggest['progress']['active'] = True |
65 |
|
66 |
|
67 |
def suggest_missing(miss=None): |
68 |
global suggest
|
69 |
kamaki_docs = 'http://www.synnefo.org/docs/kamaki/latest'
|
70 |
for k, v in (miss, suggest[miss]) if miss else suggest.items(): |
71 |
if v['active'] and stdout.isatty(): |
72 |
print('Suggestion: for better user experience install %s' % k)
|
73 |
print('\t%s' % v['description']) |
74 |
print('\tIt is easy, here are the instructions:')
|
75 |
print('\t%s/installation.html%s' % (kamaki_docs, v['url'])) |
76 |
print('')
|
77 |
|
78 |
|
79 |
def remove_colors(): |
80 |
global bold
|
81 |
global red
|
82 |
global yellow
|
83 |
global magenta
|
84 |
|
85 |
def dummy(val): |
86 |
return val
|
87 |
red = yellow = magenta = bold = dummy |
88 |
|
89 |
|
90 |
def pretty_keys(d, delim='_', recurcive=False): |
91 |
"""<term>delim<term> to <term> <term> transformation
|
92 |
"""
|
93 |
new_d = {} |
94 |
for key, val in d.items(): |
95 |
new_key = key.split(delim)[-1]
|
96 |
if recurcive and isinstance(val, dict): |
97 |
new_val = pretty_keys(val, delim, recurcive) |
98 |
else:
|
99 |
new_val = val |
100 |
new_d[new_key] = new_val |
101 |
return new_d
|
102 |
|
103 |
|
104 |
def print_json(data): |
105 |
"""Print a list or dict as json in console
|
106 |
|
107 |
:param data: json-dumpable data
|
108 |
"""
|
109 |
print(dumps(data, indent=2))
|
110 |
|
111 |
|
112 |
def pretty_dict(d, *args, **kwargs): |
113 |
print_dict(pretty_keys(d, *args, **kwargs)) |
114 |
|
115 |
|
116 |
def print_dict( |
117 |
d, exclude=(), ident=0,
|
118 |
with_enumeration=False, recursive_enumeration=False): |
119 |
"""
|
120 |
Pretty-print a dictionary object
|
121 |
|
122 |
:param d: (dict) the input
|
123 |
|
124 |
:param excelude: (set or list) keys to exclude from printing
|
125 |
|
126 |
:param ident: (int) initial indentation (recursive)
|
127 |
|
128 |
:param with_enumeration: (bool) enumerate each 1st level key if true
|
129 |
|
130 |
:recursive_enumeration: (bool) recursively enumerate dicts and lists of
|
131 |
2nd level or deeper
|
132 |
|
133 |
:raises CLIError: (TypeError wrapper) non-dict input
|
134 |
"""
|
135 |
if not isinstance(d, dict): |
136 |
raiseCLIError(TypeError('Cannot dict_print a non-dict object')) |
137 |
|
138 |
if d:
|
139 |
margin = max(len(('%s' % key).strip()) for key in d.keys() if ( |
140 |
key not in exclude)) |
141 |
|
142 |
counter = 1
|
143 |
for key, val in sorted(d.items()): |
144 |
key = '%s' % key
|
145 |
if key in exclude: |
146 |
continue
|
147 |
print_str = ''
|
148 |
if with_enumeration:
|
149 |
print_str = '%s. ' % counter
|
150 |
counter += 1
|
151 |
print_str = '%s%s' % (' ' * (ident - len(print_str)), print_str) |
152 |
print_str += key.strip() |
153 |
print_str += ' ' * (margin - len(key.strip())) |
154 |
print_str += ': '
|
155 |
if isinstance(val, dict): |
156 |
print(print_str) |
157 |
print_dict( |
158 |
val, |
159 |
exclude=exclude, |
160 |
ident=margin + ident, |
161 |
with_enumeration=recursive_enumeration, |
162 |
recursive_enumeration=recursive_enumeration) |
163 |
elif isinstance(val, list): |
164 |
print(print_str) |
165 |
print_list( |
166 |
val, |
167 |
exclude=exclude, |
168 |
ident=margin + ident, |
169 |
with_enumeration=recursive_enumeration, |
170 |
recursive_enumeration=recursive_enumeration) |
171 |
else:
|
172 |
print print_str + ' ' + ('%s' % val).strip() |
173 |
|
174 |
|
175 |
def print_list( |
176 |
l, exclude=(), ident=0,
|
177 |
with_enumeration=False, recursive_enumeration=False): |
178 |
"""
|
179 |
Pretty-print a list object
|
180 |
|
181 |
:param l: (list) the input
|
182 |
|
183 |
:param excelude: (object - anytype) values to exclude from printing
|
184 |
|
185 |
:param ident: (int) initial indentation (recursive)
|
186 |
|
187 |
:param with_enumeration: (bool) enumerate each 1st level value if true
|
188 |
|
189 |
:recursive_enumeration: (bool) recursively enumerate dicts and lists of
|
190 |
2nd level or deeper
|
191 |
|
192 |
:raises CLIError: (TypeError wrapper) non-list input
|
193 |
"""
|
194 |
if not isinstance(l, list): |
195 |
raiseCLIError(TypeError('Cannot list_print a non-list object')) |
196 |
|
197 |
if l:
|
198 |
try:
|
199 |
margin = max(len(('%s' % item).strip()) for item in l if not ( |
200 |
isinstance(item, dict) or |
201 |
isinstance(item, list) or |
202 |
item in exclude))
|
203 |
except ValueError: |
204 |
margin = (2 + len(('%s' % len(l)))) if enumerate else 1 |
205 |
|
206 |
counter = 1
|
207 |
prefix = ''
|
208 |
for item in sorted(l): |
209 |
if item in exclude: |
210 |
continue
|
211 |
elif with_enumeration:
|
212 |
prefix = '%s. ' % counter
|
213 |
counter += 1
|
214 |
prefix = '%s%s' % (' ' * (ident - len(prefix)), prefix) |
215 |
else:
|
216 |
prefix = ' ' * ident
|
217 |
if isinstance(item, dict): |
218 |
if with_enumeration:
|
219 |
print(prefix) |
220 |
print_dict( |
221 |
item, |
222 |
exclude=exclude, |
223 |
ident=margin + ident, |
224 |
with_enumeration=recursive_enumeration, |
225 |
recursive_enumeration=recursive_enumeration) |
226 |
elif isinstance(item, list): |
227 |
if with_enumeration:
|
228 |
print(prefix) |
229 |
print_list( |
230 |
item, |
231 |
exclude=exclude, |
232 |
ident=margin + ident, |
233 |
with_enumeration=recursive_enumeration, |
234 |
recursive_enumeration=recursive_enumeration) |
235 |
else:
|
236 |
print('%s%s' % (prefix, item))
|
237 |
|
238 |
|
239 |
def page_hold(index, limit, maxlen): |
240 |
"""Check if there are results to show, and hold the page when needed
|
241 |
:param index: (int) > 0
|
242 |
:param limit: (int) 0 < limit <= max, page hold if limit mod index == 0
|
243 |
:param maxlen: (int) Don't hold if index reaches maxlen
|
244 |
|
245 |
:returns: True if there are more to show, False if all results are shown
|
246 |
"""
|
247 |
if index >= limit and index % limit == 0: |
248 |
if index >= maxlen:
|
249 |
return False |
250 |
else:
|
251 |
print('(%s listed - %s more - "enter" to continue)' % (
|
252 |
index, |
253 |
maxlen - index)) |
254 |
c = ' '
|
255 |
while c != '\n': |
256 |
c = stdin.read(1)
|
257 |
return True |
258 |
|
259 |
|
260 |
def print_items( |
261 |
items, title=('id', 'name'), |
262 |
with_enumeration=False, with_redundancy=False, |
263 |
page_size=0):
|
264 |
"""print dict or list items in a list, using some values as title
|
265 |
Objects of next level don't inherit enumeration (default: off) or titles
|
266 |
|
267 |
:param items: (list) items are lists or dict
|
268 |
:param title: (tuple) keys to use their values as title
|
269 |
:param with_enumeration: (boolean) enumerate items (order id on title)
|
270 |
:param with_redundancy: (boolean) values in title also appear on body
|
271 |
:param page_size: (int) show results in pages of page_size items, enter to
|
272 |
continue
|
273 |
"""
|
274 |
if not items: |
275 |
return
|
276 |
try:
|
277 |
page_size = int(page_size) if int(page_size) > 0 else len(items) |
278 |
except:
|
279 |
page_size = len(items)
|
280 |
num_of_pages = len(items) // page_size
|
281 |
num_of_pages += 1 if len(items) % page_size else 0 |
282 |
for i, item in enumerate(items): |
283 |
if with_enumeration:
|
284 |
stdout.write('%s. ' % (i + 1)) |
285 |
if isinstance(item, dict): |
286 |
title = sorted(set(title).intersection(item.keys())) |
287 |
if with_redundancy:
|
288 |
header = ' '.join('%s' % item[key] for key in title) |
289 |
else:
|
290 |
header = ' '.join('%s' % item.pop(key) for key in title) |
291 |
print(bold(header)) |
292 |
if isinstance(item, dict): |
293 |
print_dict(item, ident=1)
|
294 |
elif isinstance(item, list): |
295 |
print_list(item, ident=1)
|
296 |
else:
|
297 |
print(' %s' % item)
|
298 |
page_hold(i + 1, page_size, len(items)) |
299 |
|
300 |
|
301 |
def format_size(size): |
302 |
units = ('B', 'KiB', 'MiB', 'GiB', 'TiB') |
303 |
try:
|
304 |
size = float(size)
|
305 |
except ValueError as err: |
306 |
raiseCLIError(err, 'Cannot format %s in bytes' % size)
|
307 |
for unit in units: |
308 |
if size < 1024: |
309 |
break
|
310 |
size /= 1024.0
|
311 |
s = ('%.2f' % size)
|
312 |
while '.' in s and s[-1] in ('0', '.'): |
313 |
s = s[:-1]
|
314 |
return s + unit
|
315 |
|
316 |
|
317 |
def to_bytes(size, format): |
318 |
"""
|
319 |
:param size: (float) the size in the given format
|
320 |
:param format: (case insensitive) KiB, KB, MiB, MB, GiB, GB, TiB, TB
|
321 |
|
322 |
:returns: (int) the size in bytes
|
323 |
"""
|
324 |
format = format.upper() |
325 |
if format == 'B': |
326 |
return int(size) |
327 |
size = float(size)
|
328 |
units_dc = ('KB', 'MB', 'GB', 'TB') |
329 |
units_bi = ('KIB', 'MIB', 'GIB', 'TIB') |
330 |
|
331 |
factor = 1024 if format in units_bi else 1000 if format in units_dc else 0 |
332 |
if not factor: |
333 |
raise ValueError('Invalid data size format %s' % format) |
334 |
for prefix in ('K', 'M', 'G', 'T'): |
335 |
size *= factor |
336 |
if format.startswith(prefix):
|
337 |
break
|
338 |
return int(size) |
339 |
|
340 |
|
341 |
def dict2file(d, f, depth=0): |
342 |
for k, v in d.items(): |
343 |
f.write('%s%s: ' % ('\t' * depth, k)) |
344 |
if isinstance(v, dict): |
345 |
f.write('\n')
|
346 |
dict2file(v, f, depth + 1)
|
347 |
elif isinstance(v, list): |
348 |
f.write('\n')
|
349 |
list2file(v, f, depth + 1)
|
350 |
else:
|
351 |
f.write(' %s\n' % v)
|
352 |
|
353 |
|
354 |
def list2file(l, f, depth=1): |
355 |
for item in l: |
356 |
if isinstance(item, dict): |
357 |
dict2file(item, f, depth + 1)
|
358 |
elif isinstance(item, list): |
359 |
list2file(item, f, depth + 1)
|
360 |
else:
|
361 |
f.write('%s%s\n' % ('\t' * depth, item)) |
362 |
|
363 |
# Split input auxiliary
|
364 |
|
365 |
|
366 |
def _parse_with_regex(line, regex): |
367 |
re_parser = regex_compile(regex) |
368 |
return (re_parser.split(line), re_parser.findall(line))
|
369 |
|
370 |
|
371 |
def _sub_split(line): |
372 |
terms = [] |
373 |
(sub_trivials, sub_interesting) = _parse_with_regex(line, ' ".*?" ')
|
374 |
for subi, subipart in enumerate(sub_interesting): |
375 |
terms += sub_trivials[subi].split() |
376 |
terms.append(subipart[2:-2]) |
377 |
terms += sub_trivials[-1].split()
|
378 |
return terms
|
379 |
|
380 |
|
381 |
def old_split_input(line): |
382 |
"""Use regular expressions to split a line correctly"""
|
383 |
line = ' %s ' % line
|
384 |
(trivial_parts, interesting_parts) = _parse_with_regex(line, ' \'.*?\' ')
|
385 |
terms = [] |
386 |
for i, ipart in enumerate(interesting_parts): |
387 |
terms += _sub_split(trivial_parts[i]) |
388 |
terms.append(ipart[2:-2]) |
389 |
terms += _sub_split(trivial_parts[-1])
|
390 |
return terms
|
391 |
|
392 |
|
393 |
def _get_from_parsed(parsed_str): |
394 |
try:
|
395 |
parsed_str = parsed_str.strip() |
396 |
except:
|
397 |
return None |
398 |
if parsed_str:
|
399 |
if parsed_str[0] == parsed_str[-1] and parsed_str[0] in ("'", '"'): |
400 |
return [parsed_str[1:-1]] |
401 |
return parsed_str.split(' ') |
402 |
return None |
403 |
|
404 |
|
405 |
def split_input(line): |
406 |
if not line: |
407 |
return []
|
408 |
reg_expr = '\'.*?\'|".*?"|^[\S]*$'
|
409 |
(trivial_parts, interesting_parts) = _parse_with_regex(line, reg_expr) |
410 |
assert(len(trivial_parts) == 1 + len(interesting_parts)) |
411 |
#print(' [split_input] trivial_parts %s are' % trivial_parts)
|
412 |
#print(' [split_input] interesting_parts %s are' % interesting_parts)
|
413 |
terms = [] |
414 |
for i, tpart in enumerate(trivial_parts): |
415 |
part = _get_from_parsed(tpart) |
416 |
if part:
|
417 |
terms += part |
418 |
try:
|
419 |
part = _get_from_parsed(interesting_parts[i]) |
420 |
except IndexError: |
421 |
break
|
422 |
if part:
|
423 |
terms += part |
424 |
return terms
|
425 |
|
426 |
|
427 |
def ask_user(msg, true_resp=['Y', 'y']): |
428 |
"""Print msg and read user response
|
429 |
|
430 |
:param true_resp: (tuple of chars)
|
431 |
|
432 |
:returns: (bool) True if reponse in true responses, False otherwise
|
433 |
"""
|
434 |
stdout.write('%s (%s or enter for yes):' % (msg, ', '.join(true_resp))) |
435 |
stdout.flush() |
436 |
user_response = stdin.readline() |
437 |
return user_response[0] in true_resp + ['\n'] |
438 |
|
439 |
|
440 |
def spiner(size=None): |
441 |
spins = ('/', '-', '\\', '|') |
442 |
stdout.write(' ')
|
443 |
size = size or -1 |
444 |
i = 0
|
445 |
while size - i:
|
446 |
stdout.write('\b%s' % spins[i % len(spins)]) |
447 |
stdout.flush() |
448 |
i += 1
|
449 |
sleep(0.1)
|
450 |
yield
|
451 |
yield
|
452 |
|
453 |
if __name__ == '__main__': |
454 |
examples = [ |
455 |
'la_la le_le li_li',
|
456 |
'\'la la\' \'le le\' \'li li\'',
|
457 |
'\'la la\' le_le \'li li\'',
|
458 |
'la_la \'le le\' li_li',
|
459 |
'la_la \'le le\' \'li li\'',
|
460 |
'"la la" "le le" "li li"',
|
461 |
'"la la" le_le "li li"',
|
462 |
'la_la "le le" li_li',
|
463 |
'"la_la" "le le" "li li"',
|
464 |
'\'la la\' "le le" \'li li\'',
|
465 |
'la_la \'le le\' "li li"',
|
466 |
'la_la \'le le\' li_li',
|
467 |
'\'la la\' le_le "li li"',
|
468 |
'"la la" le_le \'li li\'',
|
469 |
'"la la" \'le le\' li_li',
|
470 |
'la_la \'le\'le\' "li\'li"',
|
471 |
'"la \'le le\' la"',
|
472 |
'\'la "le le" la\'',
|
473 |
'\'la "la" la\' "le \'le\' le" li_"li"_li',
|
474 |
'\'\' \'L\' "" "A"']
|
475 |
|
476 |
for i, example in enumerate(examples): |
477 |
print('%s. Split this: (%s)' % (i + 1, example)) |
478 |
ret = old_split_input(example) |
479 |
print('\t(%s) of size %s' % (ret, len(ret))) |
480 |
|
481 |
|
482 |
def get_path_size(testpath): |
483 |
if path.isfile(testpath):
|
484 |
return path.getsize(testpath)
|
485 |
total_size = 0
|
486 |
for top, dirs, files in walk(path.abspath(testpath)): |
487 |
for f in files: |
488 |
f = path.join(top, f) |
489 |
if path.isfile(f):
|
490 |
total_size += path.getsize(f) |
491 |
return total_size
|