Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / utils / __init__.py @ 6acfcec4

History | View | Annotate | Download (14.2 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 os import walk, path
37
from json import dumps
38

    
39
from kamaki.cli.errors import raiseCLIError
40

    
41

    
42
INDENT_TAB = 4
43

    
44

    
45
suggest = dict(ansicolors=dict(
46
        active=False,
47
        url='#install-ansicolors-progress',
48
        description='Add colors to console responses'))
49

    
50
try:
51
    from colors import magenta, red, yellow, bold
52
except ImportError:
53
    def dummy(val):
54
        return val
55
    red = yellow = magenta = bold = dummy
56
    suggest['ansicolors']['active'] = True
57

    
58

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

    
76

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

    
90

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

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

    
101

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

    
112

    
113
def print_json(data, out=stdout):
114
    """Print a list or dict as json in console
115

116
    :param data: json-dumpable data
117

118
    :param out: Input/Output stream to dump values into
119
    """
120
    out.write(unicode(dumps(data, indent=INDENT_TAB) + '\n'))
121
    out.flush()
122

    
123

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

133
    :param d: (dict)
134

135
    :param exclude: (iterable of strings) keys to exclude from printing
136

137
    :param indent: (int) initial indentation (recursive)
138

139
    :param with_enumeration: (bool) enumerate 1st-level keys
140

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

144
    :param out: Input/Output stream to dump values into
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 = u' ' * indent
156
        print_str += u'%s.' % (i + 1) if with_enumeration else u''
157
        print_str += u'%s:' % k
158
        if isinstance(v, dict):
159
            out.write(print_str + '\n')
160
            print_dict(
161
                v, exclude, indent + INDENT_TAB,
162
                recursive_enumeration, recursive_enumeration, out)
163
        elif isinstance(v, list) or isinstance(v, tuple):
164
            out.write(print_str + '\n')
165
            print_list(
166
                v, exclude, indent + INDENT_TAB,
167
                recursive_enumeration, recursive_enumeration, out)
168
        else:
169
            out.write(u'%s %s\n' % (print_str, v))
170
        out.flush()
171

    
172

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

182
    :param l: (list)
183

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

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

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

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

193
    :param out: Input/Output stream to dump values into
194

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

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

    
229

    
230
def print_items(
231
        items, title=('id', 'name'),
232
        with_enumeration=False, with_redundancy=False, out=stdout):
233
    """print dict or list items in a list, using some values as title
234
    Objects of next level don't inherit enumeration (default: off) or titles
235

236
    :param items: (list) items are lists or dict
237

238
    :param title: (tuple) keys to use their values as title
239

240
    :param with_enumeration: (boolean) enumerate items (order id on title)
241

242
    :param with_redundancy: (boolean) values in title also appear on body
243

244
    :param out: Input/Output stream to dump values into
245
    """
246
    if not items:
247
        return
248
    if not (isinstance(items, dict) or isinstance(items, list) or isinstance(
249
                items, tuple)):
250
        out.write(u'%s\n' % items)
251
        out.flush()
252
        return
253

    
254
    for i, item in enumerate(items):
255
        if with_enumeration:
256
            out.write(u'%s. ' % (i + 1))
257
        if isinstance(item, dict):
258
            item = dict(item)
259
            title = sorted(set(title).intersection(item))
260
            pick = item.get if with_redundancy else item.pop
261
            header = u' '.join(u'%s' % pick(key) for key in title)
262
            out.write((unicode(bold(header) if header else '') + '\n'))
263
            print_dict(item, indent=INDENT_TAB, out=out)
264
        elif isinstance(item, list) or isinstance(item, tuple):
265
            print_list(item, indent=INDENT_TAB, out=out)
266
        else:
267
            out.write(u' %s\n' % item)
268
        out.flush()
269
    out.flush()
270

    
271

    
272
def format_size(size, decimal_factors=False):
273
    units = ('B', 'KB', 'MB', 'GB', 'TB') if decimal_factors else (
274
        'B', 'KiB', 'MiB', 'GiB', 'TiB')
275
    step = 1000 if decimal_factors else 1024
276
    fstep = float(step)
277
    try:
278
        size = float(size)
279
    except (ValueError, TypeError) as err:
280
        raiseCLIError(err, 'Cannot format %s in bytes' % (
281
            ','.join(size) if isinstance(size, tuple) else size))
282
    for i, unit in enumerate(units):
283
        if size < step or i + 1 == len(units):
284
            break
285
        size /= fstep
286
    s = ('%.2f' % size)
287
    s = s.replace('%s' % step, '%s.99' % (step - 1)) if size <= fstep else s
288
    while '.' in s and s[-1] in ('0', '.'):
289
        s = s[:-1]
290
    return s + unit
291

    
292

    
293
def to_bytes(size, format):
294
    """
295
    :param size: (float) the size in the given format
296
    :param format: (case insensitive) KiB, KB, MiB, MB, GiB, GB, TiB, TB
297

298
    :returns: (int) the size in bytes
299
    :raises ValueError: if invalid size or format
300
    :raises AttributeError: if format is not str
301
    :raises TypeError: if size is not arithmetic or convertible to arithmetic
302
    """
303
    format = format.upper()
304
    if format == 'B':
305
        return int(size)
306
    size = float(size)
307
    units_dc = ('KB', 'MB', 'GB', 'TB')
308
    units_bi = ('KIB', 'MIB', 'GIB', 'TIB')
309

    
310
    factor = 1024 if format in units_bi else 1000 if format in units_dc else 0
311
    if not factor:
312
        raise ValueError('Invalid data size format %s' % format)
313
    for prefix in ('K', 'M', 'G', 'T'):
314
        size *= factor
315
        if format.startswith(prefix):
316
            break
317
    return int(size)
318

    
319

    
320
def dict2file(d, f, depth=0):
321
    for k, v in d.items():
322
        f.write('%s%s: ' % (' ' * INDENT_TAB * depth, k))
323
        if isinstance(v, dict):
324
            f.write('\n')
325
            dict2file(v, f, depth + 1)
326
        elif isinstance(v, list) or isinstance(v, tuple):
327
            f.write('\n')
328
            list2file(v, f, depth + 1)
329
        else:
330
            f.write('%s\n' % v)
331

    
332

    
333
def list2file(l, f, depth=1):
334
    for item in l:
335
        if isinstance(item, dict):
336
            dict2file(item, f, depth + 1)
337
        elif isinstance(item, list) or isinstance(item, tuple):
338
            list2file(item, f, depth + 1)
339
        else:
340
            f.write('%s%s\n' % (' ' * INDENT_TAB * depth, item))
341

    
342
# Split input auxiliary
343

    
344

    
345
def _parse_with_regex(line, regex):
346
    re_parser = regex_compile(regex)
347
    return (re_parser.split(line), re_parser.findall(line))
348

    
349

    
350
def _get_from_parsed(parsed_str):
351
    try:
352
        parsed_str = parsed_str.strip()
353
    except:
354
        return None
355
    return ([parsed_str[1:-1]] if (
356
        parsed_str[0] == parsed_str[-1] and parsed_str[0] in ("'", '"')) else (
357
            parsed_str.split(' '))) if parsed_str else None
358

    
359

    
360
def split_input(line):
361
    if not line:
362
        return []
363
    reg_expr = '\'.*?\'|".*?"|^[\S]*$'
364
    (trivial_parts, interesting_parts) = _parse_with_regex(line, reg_expr)
365
    assert(len(trivial_parts) == 1 + len(interesting_parts))
366
    terms = []
367
    for i, tpart in enumerate(trivial_parts):
368
        part = _get_from_parsed(tpart)
369
        if part:
370
            terms += part
371
        try:
372
            part = _get_from_parsed(interesting_parts[i])
373
        except IndexError:
374
            break
375
        if part:
376
            if tpart and not tpart[-1].endswith(' '):
377
                terms[-1] += ' '.join(part)
378
            else:
379
                terms += part
380
    return terms
381

    
382

    
383
def ask_user(msg, true_resp=('y', ), out=stdout, user_in=stdin):
384
    """Print msg and read user response
385

386
    :param true_resp: (tuple of chars)
387

388
    :returns: (bool) True if reponse in true responses, False otherwise
389
    """
390
    yep = ', '.join(true_resp)
391
    nope = '<not %s>' % yep if 'n' in true_resp or 'N' in true_resp else 'N'
392
    out.write(u'%s [%s/%s]: ' % (msg, yep, nope))
393
    out.flush()
394
    user_response = user_in.readline()
395
    return user_response[0].lower() in [s.lower() for s in true_resp]
396

    
397

    
398
def get_path_size(testpath):
399
    if path.isfile(testpath):
400
        return path.getsize(testpath)
401
    total_size = 0
402
    for top, dirs, files in walk(path.abspath(testpath)):
403
        for f in files:
404
            f = path.join(top, f)
405
            if path.isfile(f):
406
                total_size += path.getsize(f)
407
    return total_size
408

    
409

    
410
def remove_from_items(list_of_dicts, key_to_remove):
411
    for item in list_of_dicts:
412
        assert isinstance(item, dict), 'Item %s not a dict' % item
413
        item.pop(key_to_remove, None)
414

    
415

    
416
def filter_dicts_by_dict(
417
    list_of_dicts, filters,
418
    exact_match=True, case_sensitive=False):
419
    """
420
    :param list_of_dicts: (list) each dict contains "raw" key-value pairs
421

422
    :param filters: (dict) filters in key-value form
423

424
    :param exact_match: (bool) if false, check if the filter value is part of
425
        the actual value
426

427
    :param case_sensitive: (bool) revers to values only (not keys)
428

429
    :returns: (list) only the dicts that match all filters
430
    """
431
    new_dicts = []
432
    for d in list_of_dicts:
433
        if set(filters).difference(d):
434
            continue
435
        match = True
436
        for k, v in filters.items():
437
            dv, v = ('%s' % d[k]), ('%s' % v)
438
            if not case_sensitive:
439
                dv, v = dv.lower(), v.lower()
440
            if not ((
441
                    exact_match and v == dv) or (
442
                    (not exact_match) and v in dv)):
443
                match = False
444
                break
445
        if match:
446
            new_dicts.append(d)
447
    return new_dicts