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