1 # Copyright 2011 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
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.
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.
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.
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
40 from kamaki.cli.errors import raiseCLIError
42 suggest = dict(ansicolors=dict(
44 url='#install-ansicolors-progress',
45 description='Add colors to console responses'))
48 from colors import magenta, red, yellow, bold
50 # No colours? No worries, use dummy foo instead
53 red = yellow = magenta = bold = dummy
54 #from kamaki.cli import _colors
55 #if _colors.lower() == 'on':
56 suggest['ansicolors']['active'] = True
59 from progress.bar import ShadyBar
61 suggest['progress']['active'] = True
64 def suggest_missing(miss=None, exclude=[]):
72 kamaki_docs = 'http://www.synnefo.org/docs/kamaki/latest'
73 for k, v in (miss, sgs[miss]) if miss else sgs.items():
74 if v['active'] and stdout.isatty():
75 print('Suggestion: for better user experience install %s' % k)
76 print('\t%s' % v['description'])
77 print('\tIt is easy, here are the instructions:')
78 print('\t%s/installation.html%s' % (kamaki_docs, v['url']))
90 red = yellow = magenta = bold = dummy
93 def pretty_keys(d, delim='_', recurcive=False):
94 """<term>delim<term> to <term> <term> transformation
97 for key, val in d.items():
98 new_key = key.split(delim)[-1]
99 if recurcive and isinstance(val, dict):
100 new_val = pretty_keys(val, delim, recurcive)
103 new_d[new_key] = new_val
107 def print_json(data):
108 """Print a list or dict as json in console
110 :param data: json-dumpable data
112 print(dumps(data, indent=2))
115 def pretty_dict(d, *args, **kwargs):
116 print_dict(pretty_keys(d, *args, **kwargs))
120 d, exclude=(), ident=0,
121 with_enumeration=False, recursive_enumeration=False):
123 Pretty-print a dictionary object
125 :param d: (dict) the input
127 :param excelude: (set or list) keys to exclude from printing
129 :param ident: (int) initial indentation (recursive)
131 :param with_enumeration: (bool) enumerate each 1st level key if true
133 :recursive_enumeration: (bool) recursively enumerate dicts and lists of
136 :raises CLIError: (TypeError wrapper) non-dict input
138 if not isinstance(d, dict):
139 raiseCLIError(TypeError('Cannot dict_print a non-dict object'))
142 margin = max(len(('%s' % key).strip()) for key in d.keys() if (
146 for key, val in sorted(d.items()):
152 print_str = '%s. ' % counter
154 print_str = '%s%s' % (' ' * (ident - len(print_str)), print_str)
155 print_str += key.strip()
157 print_str += ' ' * (margin - len(key.strip()))
159 if isinstance(val, dict):
164 ident=margin + ident,
165 with_enumeration=recursive_enumeration,
166 recursive_enumeration=recursive_enumeration)
167 elif isinstance(val, list):
172 ident=margin + ident,
173 with_enumeration=recursive_enumeration,
174 recursive_enumeration=recursive_enumeration)
176 print print_str + ' ' + ('%s' % val).strip()
180 l, exclude=(), ident=0,
181 with_enumeration=False, recursive_enumeration=False):
183 Pretty-print a list object
185 :param l: (list) the input
187 :param excelude: (object - anytype) values to exclude from printing
189 :param ident: (int) initial indentation (recursive)
191 :param with_enumeration: (bool) enumerate each 1st level value if true
193 :recursive_enumeration: (bool) recursively enumerate dicts and lists of
196 :raises CLIError: (TypeError wrapper) non-list input
198 if not isinstance(l, list):
199 raiseCLIError(TypeError('Cannot list_print a non-list object'))
203 margin = max(len(('%s' % item).strip()) for item in l if not (
204 isinstance(item, dict) or
205 isinstance(item, list) or
208 margin = (2 + len(('%s' % len(l)))) if enumerate else 1
212 for item in sorted(l):
215 elif with_enumeration:
216 prefix = '%s. ' % counter
218 prefix = '%s%s' % (' ' * (ident - len(prefix)), prefix)
221 if isinstance(item, dict):
227 ident=margin + ident,
228 with_enumeration=recursive_enumeration,
229 recursive_enumeration=recursive_enumeration)
230 elif isinstance(item, list):
236 ident=margin + ident,
237 with_enumeration=recursive_enumeration,
238 recursive_enumeration=recursive_enumeration)
240 print('%s%s' % (prefix, item))
243 def page_hold(index, limit, maxlen):
244 """Check if there are results to show, and hold the page when needed
245 :param index: (int) > 0
246 :param limit: (int) 0 < limit <= max, page hold if limit mod index == 0
247 :param maxlen: (int) Don't hold if index reaches maxlen
249 :returns: True if there are more to show, False if all results are shown
251 if index >= limit and index % limit == 0:
255 print('(%s listed - %s more - "enter" to continue)' % (
265 items, title=('id', 'name'),
266 with_enumeration=False, with_redundancy=False,
268 """print dict or list items in a list, using some values as title
269 Objects of next level don't inherit enumeration (default: off) or titles
271 :param items: (list) items are lists or dict
272 :param title: (tuple) keys to use their values as title
273 :param with_enumeration: (boolean) enumerate items (order id on title)
274 :param with_redundancy: (boolean) values in title also appear on body
275 :param page_size: (int) show results in pages of page_size items, enter to
281 page_size = int(page_size) if int(page_size) > 0 else len(items)
283 page_size = len(items)
284 num_of_pages = len(items) // page_size
285 num_of_pages += 1 if len(items) % page_size else 0
286 for i, item in enumerate(items):
288 stdout.write('%s. ' % (i + 1))
289 if isinstance(item, dict):
290 title = sorted(set(title).intersection(item.keys()))
292 header = ' '.join('%s' % item[key] for key in title)
294 header = ' '.join('%s' % item.pop(key) for key in title)
296 if isinstance(item, dict):
297 print_dict(item, ident=1)
298 elif isinstance(item, list):
299 print_list(item, ident=1)
302 page_hold(i + 1, page_size, len(items))
305 def format_size(size):
306 units = ('B', 'KiB', 'MiB', 'GiB', 'TiB')
309 except ValueError as err:
310 raiseCLIError(err, 'Cannot format %s in bytes' % size)
316 while '.' in s and s[-1] in ('0', '.'):
321 def to_bytes(size, format):
323 :param size: (float) the size in the given format
324 :param format: (case insensitive) KiB, KB, MiB, MB, GiB, GB, TiB, TB
326 :returns: (int) the size in bytes
328 format = format.upper()
332 units_dc = ('KB', 'MB', 'GB', 'TB')
333 units_bi = ('KIB', 'MIB', 'GIB', 'TIB')
335 factor = 1024 if format in units_bi else 1000 if format in units_dc else 0
337 raise ValueError('Invalid data size format %s' % format)
338 for prefix in ('K', 'M', 'G', 'T'):
340 if format.startswith(prefix):
345 def dict2file(d, f, depth=0):
346 for k, v in d.items():
347 f.write('%s%s: ' % ('\t' * depth, k))
348 if isinstance(v, dict):
350 dict2file(v, f, depth + 1)
351 elif isinstance(v, list):
353 list2file(v, f, depth + 1)
358 def list2file(l, f, depth=1):
360 if isinstance(item, dict):
361 dict2file(item, f, depth + 1)
362 elif isinstance(item, list):
363 list2file(item, f, depth + 1)
365 f.write('%s%s\n' % ('\t' * depth, item))
367 # Split input auxiliary
370 def _parse_with_regex(line, regex):
371 re_parser = regex_compile(regex)
372 return (re_parser.split(line), re_parser.findall(line))
375 def _sub_split(line):
377 (sub_trivials, sub_interesting) = _parse_with_regex(line, ' ".*?" ')
378 for subi, subipart in enumerate(sub_interesting):
379 terms += sub_trivials[subi].split()
380 terms.append(subipart[2:-2])
381 terms += sub_trivials[-1].split()
385 def old_split_input(line):
386 """Use regular expressions to split a line correctly"""
388 (trivial_parts, interesting_parts) = _parse_with_regex(line, ' \'.*?\' ')
390 for i, ipart in enumerate(interesting_parts):
391 terms += _sub_split(trivial_parts[i])
392 terms.append(ipart[2:-2])
393 terms += _sub_split(trivial_parts[-1])
397 def _get_from_parsed(parsed_str):
399 parsed_str = parsed_str.strip()
403 if parsed_str[0] == parsed_str[-1] and parsed_str[0] in ("'", '"'):
404 return [parsed_str[1:-1]]
405 return parsed_str.split(' ')
409 def split_input(line):
412 reg_expr = '\'.*?\'|".*?"|^[\S]*$'
413 (trivial_parts, interesting_parts) = _parse_with_regex(line, reg_expr)
414 assert(len(trivial_parts) == 1 + len(interesting_parts))
415 #print(' [split_input] trivial_parts %s are' % trivial_parts)
416 #print(' [split_input] interesting_parts %s are' % interesting_parts)
418 for i, tpart in enumerate(trivial_parts):
419 part = _get_from_parsed(tpart)
423 part = _get_from_parsed(interesting_parts[i])
431 def ask_user(msg, true_resp=['Y', 'y']):
432 """Print msg and read user response
434 :param true_resp: (tuple of chars)
436 :returns: (bool) True if reponse in true responses, False otherwise
438 stdout.write('%s (%s or enter for yes):' % (msg, ', '.join(true_resp)))
440 user_response = stdin.readline()
441 return user_response[0] in true_resp + ['\n']
444 def spiner(size=None):
445 spins = ('/', '-', '\\', '|')
450 stdout.write('\b%s' % spins[i % len(spins)])
457 if __name__ == '__main__':
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 la\' le_le "li li"',
472 '"la la" le_le \'li li\'',
473 '"la la" \'le le\' li_li',
474 'la_la \'le\'le\' "li\'li"',
477 '\'la "la" la\' "le \'le\' le" li_"li"_li',
480 for i, example in enumerate(examples):
481 print('%s. Split this: (%s)' % (i + 1, example))
482 ret = old_split_input(example)
483 print('\t(%s) of size %s' % (ret, len(ret)))
486 def get_path_size(testpath):
487 if path.isfile(testpath):
488 return path.getsize(testpath)
490 for top, dirs, files in walk(path.abspath(testpath)):
492 f = path.join(top, f)
494 total_size += path.getsize(f)