Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / utils.py @ cdeadadc

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 suggest_missing(miss=None, exclude=[]):
61
    global suggest
62
    sgs = dict(suggest)
63
    for exc in exclude:
64
        try:
65
            sgs.pop(exc)
66
        except KeyError:
67
            pass
68
    kamaki_docs = 'http://www.synnefo.org/docs/kamaki/latest'
69
    for k, v in (miss, sgs[miss]) if miss else sgs.items():
70
        if v['active'] and stdout.isatty():
71
            print('Suggestion: for better user experience install %s' % k)
72
            print('\t%s' % v['description'])
73
            print('\tIt is easy, here are the instructions:')
74
            print('\t%s/installation.html%s' % (kamaki_docs, v['url']))
75
            print('')
76

    
77

    
78
def guess_mime_type(
79
        filename,
80
        default_content_type='application/octet-stream',
81
        default_encoding=None):
82
    assert filename, 'Cannot guess mimetype for empty filename'
83
    try:
84
        from mimetypes import guess_type
85
        ctype, cenc = guess_type(filename)
86
        return ctype or default_content_type, cenc or default_encoding
87
    except ImportError:
88
        print 'WARNING: Cannot import mimetypes, using defaults'
89
        return (default_content_type, default_encoding)
90

    
91

    
92
def remove_colors():
93
    global bold
94
    global red
95
    global yellow
96
    global magenta
97

    
98
    def dummy(val):
99
        return val
100
    red = yellow = magenta = bold = dummy
101

    
102

    
103
def pretty_keys(d, delim='_', recursive=False):
104
    """<term>delim<term> to <term> <term> transformation"""
105
    new_d = dict(d)
106
    for k, v in d.items():
107
        new_v = new_d.pop(k)
108
        new_d[k.replace(delim, ' ').strip()] = pretty_keys(
109
            new_v, delim, True) if (
110
                recursive and isinstance(v, dict)) else new_v
111
    return new_d
112

    
113

    
114
def print_json(data):
115
    """Print a list or dict as json in console
116

117
    :param data: json-dumpable data
118
    """
119
    print(dumps(data, indent=INDENT_TAB))
120

    
121

    
122
def pretty_dict(d, *args, **kwargs):
123
    print_dict(pretty_keys(d, *args, **kwargs))
124

    
125

    
126
def print_dict(
127
        d,
128
        exclude=(), indent=0,
129
        with_enumeration=False, recursive_enumeration=False):
130
    """Pretty-print a dictionary object
131
    <indent>key: <non iterable item>
132
    <indent>key:
133
    <indent + INDENT_TAB><pretty-print iterable>
134

135
    :param d: (dict)
136

137
    :param exclude: (iterable of strings) keys to exclude from printing
138

139
    :param indent: (int) initial indentation (recursive)
140

141
    :param with_enumeration: (bool) enumerate 1st-level keys
142

143
    :param recursive_enumeration: (bool) recursively enumerate iterables (does
144
        not enumerate 1st level keys)
145

146
    :raises CLIError: if preconditions fail
147
    """
148
    assert isinstance(d, dict), 'print_dict input must be a dict'
149
    assert indent >= 0, 'print_dict indent must be >= 0'
150

    
151
    for i, (k, v) in enumerate(d.items()):
152
        k = ('%s' % k).strip()
153
        if k in exclude:
154
            continue
155
        print_str = ' ' * indent
156
        print_str += '%s.' % (i + 1) if with_enumeration else ''
157
        print_str += '%s:' % k
158
        if isinstance(v, dict):
159
            print print_str
160
            print_dict(
161
                v, exclude, indent + INDENT_TAB,
162
                recursive_enumeration, recursive_enumeration)
163
        elif isinstance(v, list) or isinstance(v, tuple):
164
            print print_str
165
            print_list(
166
                v, exclude, indent + INDENT_TAB,
167
                recursive_enumeration, recursive_enumeration)
168
        else:
169
            print '%s %s' % (print_str, v)
170

    
171

    
172
def print_list(
173
        l,
174
        exclude=(), indent=0,
175
        with_enumeration=False, recursive_enumeration=False):
176
    """Pretty-print a list of items
177
    <indent>key: <non iterable item>
178
    <indent>key:
179
    <indent + INDENT_TAB><pretty-print iterable>
180

181
    :param l: (list)
182

183
    :param exclude: (iterable of strings) items to exclude from printing
184

185
    :param indent: (int) initial indentation (recursive)
186

187
    :param with_enumeration: (bool) enumerate 1st-level items
188

189
    :param recursive_enumeration: (bool) recursively enumerate iterables (does
190
        not enumerate 1st level keys)
191

192
    :raises CLIError: if preconditions fail
193
    """
194
    assert isinstance(l, list) or isinstance(l, tuple), (
195
        'print_list prinbts a list or tuple')
196
    assert indent >= 0, 'print_list indent must be >= 0'
197

    
198
    counter = 0
199
    for i, item in enumerate(l):
200
        print_str = ' ' * indent
201
        print_str += '%s.' % (i + 1) if with_enumeration else ''
202
        if isinstance(item, dict):
203
            if with_enumeration:
204
                print print_str
205
            elif counter and counter < len(l):
206
                print
207
            print_dict(
208
                item, exclude,
209
                indent + (INDENT_TAB if with_enumeration else 0),
210
                recursive_enumeration, recursive_enumeration)
211
        elif isinstance(item, list) or isinstance(item, tuple):
212
            if with_enumeration:
213
                print print_str
214
            elif counter and counter < len(l):
215
                print
216
            print_list(
217
                item, exclude, indent + INDENT_TAB,
218
                recursive_enumeration, recursive_enumeration)
219
        else:
220
            item = ('%s' % item).strip()
221
            if item in exclude:
222
                continue
223
            print '%s%s' % (print_str, item)
224
        counter += 1
225

    
226

    
227
def page_hold(index, limit, maxlen):
228
    """Check if there are results to show, and hold the page when needed
229
    :param index: (int) > 0
230
    :param limit: (int) 0 < limit <= max, page hold if limit mod index == 0
231
    :param maxlen: (int) Don't hold if index reaches maxlen
232

233
    :returns: True if there are more to show, False if all results are shown
234
    """
235
    if index >= limit and index % limit == 0:
236
        if index >= maxlen:
237
            return False
238
        else:
239
            print('(%s listed - %s more - "enter" to continue)' % (
240
                index,
241
                maxlen - index))
242
            c = ' '
243
            while c != '\n':
244
                c = stdin.read(1)
245
    return True
246

    
247

    
248
def print_items(
249
        items, title=('id', 'name'),
250
        with_enumeration=False, with_redundancy=False,
251
        page_size=0):
252
    """print dict or list items in a list, using some values as title
253
    Objects of next level don't inherit enumeration (default: off) or titles
254

255
    :param items: (list) items are lists or dict
256

257
    :param title: (tuple) keys to use their values as title
258

259
    :param with_enumeration: (boolean) enumerate items (order id on title)
260

261
    :param with_redundancy: (boolean) values in title also appear on body
262

263
    :param page_size: (int) show results in pages of page_size items, enter to
264
        continue
265
    """
266
    if not items:
267
        return
268
    elif not (
269
            isinstance(items, dict) or isinstance(
270
                items, list) or isinstance(items, dict)):
271
        print '%s' % items
272
        return
273

    
274
    try:
275
        page_size = int(page_size) if int(page_size) > 0 else len(items)
276
    except:
277
        page_size = len(items)
278
    num_of_pages = len(items) // page_size
279
    num_of_pages += 1 if len(items) % page_size else 0
280
    for i, item in enumerate(items):
281
        if with_enumeration:
282
            stdout.write('%s. ' % (i + 1))
283
        if isinstance(item, dict):
284
            title = sorted(set(title).intersection(item.keys()))
285
            if with_redundancy:
286
                header = ' '.join('%s' % item[key] for key in title)
287
            else:
288
                header = ' '.join('%s' % item.pop(key) for key in title)
289
            print(bold(header))
290
        if isinstance(item, dict):
291
            print_dict(item, indent=INDENT_TAB)
292
        elif isinstance(item, list):
293
            print_list(item, indent=INDENT_TAB)
294
        else:
295
            print(' %s' % item)
296
        page_hold(i + 1, page_size, len(items))
297

    
298

    
299
def format_size(size):
300
    units = ('B', 'KiB', 'MiB', 'GiB', 'TiB')
301
    try:
302
        size = float(size)
303
    except ValueError as err:
304
        raiseCLIError(err, 'Cannot format %s in bytes' % size)
305
    for unit in units:
306
        if size < 1024:
307
            break
308
        size /= 1024.0
309
    s = ('%.2f' % size)
310
    while '.' in s and s[-1] in ('0', '.'):
311
        s = s[:-1]
312
    return s + unit
313

    
314

    
315
def to_bytes(size, format):
316
    """
317
    :param size: (float) the size in the given format
318
    :param format: (case insensitive) KiB, KB, MiB, MB, GiB, GB, TiB, TB
319

320
    :returns: (int) the size in bytes
321
    """
322
    format = format.upper()
323
    if format == 'B':
324
        return int(size)
325
    size = float(size)
326
    units_dc = ('KB', 'MB', 'GB', 'TB')
327
    units_bi = ('KIB', 'MIB', 'GIB', 'TIB')
328

    
329
    factor = 1024 if format in units_bi else 1000 if format in units_dc else 0
330
    if not factor:
331
        raise ValueError('Invalid data size format %s' % format)
332
    for prefix in ('K', 'M', 'G', 'T'):
333
        size *= factor
334
        if format.startswith(prefix):
335
            break
336
    return int(size)
337

    
338

    
339
def dict2file(d, f, depth=0):
340
    for k, v in d.items():
341
        f.write('%s%s: ' % ('\t' * depth, k))
342
        if isinstance(v, dict):
343
            f.write('\n')
344
            dict2file(v, f, depth + 1)
345
        elif isinstance(v, list):
346
            f.write('\n')
347
            list2file(v, f, depth + 1)
348
        else:
349
            f.write(' %s\n' % v)
350

    
351

    
352
def list2file(l, f, depth=1):
353
    for item in l:
354
        if isinstance(item, dict):
355
            dict2file(item, f, depth + 1)
356
        elif isinstance(item, list):
357
            list2file(item, f, depth + 1)
358
        else:
359
            f.write('%s%s\n' % ('\t' * depth, item))
360

    
361
# Split input auxiliary
362

    
363

    
364
def _parse_with_regex(line, regex):
365
    re_parser = regex_compile(regex)
366
    return (re_parser.split(line), re_parser.findall(line))
367

    
368

    
369
def _sub_split(line):
370
    terms = []
371
    (sub_trivials, sub_interesting) = _parse_with_regex(line, ' ".*?" ')
372
    for subi, subipart in enumerate(sub_interesting):
373
        terms += sub_trivials[subi].split()
374
        terms.append(subipart[2:-2])
375
    terms += sub_trivials[-1].split()
376
    return terms
377

    
378

    
379
def old_split_input(line):
380
    """Use regular expressions to split a line correctly"""
381
    line = ' %s ' % line
382
    (trivial_parts, interesting_parts) = _parse_with_regex(line, ' \'.*?\' ')
383
    terms = []
384
    for i, ipart in enumerate(interesting_parts):
385
        terms += _sub_split(trivial_parts[i])
386
        terms.append(ipart[2:-2])
387
    terms += _sub_split(trivial_parts[-1])
388
    return terms
389

    
390

    
391
def _get_from_parsed(parsed_str):
392
    try:
393
        parsed_str = parsed_str.strip()
394
    except:
395
        return None
396
    if parsed_str:
397
        if parsed_str[0] == parsed_str[-1] and parsed_str[0] in ("'", '"'):
398
            return [parsed_str[1:-1]]
399
        return parsed_str.split(' ')
400
    return None
401

    
402

    
403
def split_input(line):
404
    if not line:
405
        return []
406
    reg_expr = '\'.*?\'|".*?"|^[\S]*$'
407
    (trivial_parts, interesting_parts) = _parse_with_regex(line, reg_expr)
408
    assert(len(trivial_parts) == 1 + len(interesting_parts))
409
    #print('  [split_input] trivial_parts %s are' % trivial_parts)
410
    #print('  [split_input] interesting_parts %s are' % interesting_parts)
411
    terms = []
412
    for i, tpart in enumerate(trivial_parts):
413
        part = _get_from_parsed(tpart)
414
        if part:
415
            terms += part
416
        try:
417
            part = _get_from_parsed(interesting_parts[i])
418
        except IndexError:
419
            break
420
        if part:
421
            terms += part
422
    return terms
423

    
424

    
425
def ask_user(msg, true_resp=('y', )):
426
    """Print msg and read user response
427

428
    :param true_resp: (tuple of chars)
429

430
    :returns: (bool) True if reponse in true responses, False otherwise
431
    """
432
    stdout.write('%s [%s/N]: ' % (msg, ', '.join(true_resp)))
433
    stdout.flush()
434
    user_response = stdin.readline()
435
    return user_response[0].lower() in true_resp
436

    
437

    
438
def spiner(size=None):
439
    spins = ('/', '-', '\\', '|')
440
    stdout.write(' ')
441
    size = size or -1
442
    i = 0
443
    while size - i:
444
        stdout.write('\b%s' % spins[i % len(spins)])
445
        stdout.flush()
446
        i += 1
447
        sleep(0.1)
448
        yield
449
    yield
450

    
451
if __name__ == '__main__':
452
    examples = [
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_la \'le\'le\' "li\'li"',
469
        '"la \'le le\' la"',
470
        '\'la "le le" la\'',
471
        '\'la "la" la\' "le \'le\' le" li_"li"_li',
472
        '\'\' \'L\' "" "A"']
473

    
474
    for i, example in enumerate(examples):
475
        print('%s. Split this: (%s)' % (i + 1, example))
476
        ret = old_split_input(example)
477
        print('\t(%s) of size %s' % (ret, len(ret)))
478

    
479

    
480
def get_path_size(testpath):
481
    if path.isfile(testpath):
482
        return path.getsize(testpath)
483
    total_size = 0
484
    for top, dirs, files in walk(path.abspath(testpath)):
485
        for f in files:
486
            f = path.join(top, f)
487
            if path.isfile(f):
488
                total_size += path.getsize(f)
489
    return total_size
490

    
491

    
492
def remove_from_items(list_of_dicts, key_to_remove):
493
    for item in list_of_dicts:
494
        assert isinstance(item, dict), 'Item %s not a dict' % item
495
        item.pop(key_to_remove, None)