__init__.py 14.6 KB
Newer Older
1
# Copyright 2011-2014 GRNET S.A. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
33

34
from sys import stdout, stderr
35
from re import compile as regex_compile
36
from os import walk, path
37
from json import dumps
38
from locale import getpreferredencoding
39

40
from kamaki.cli.logger import get_logger
41
from kamaki.cli.errors import raiseCLIError
42
from kamaki.clients.utils import escape_ctrl_chars
43

44
INDENT_TAB = 4
45 46
log = get_logger(__name__)
pref_enc = getpreferredencoding()
Stavros Sachtouris's avatar
Stavros Sachtouris committed
47 48


49
suggest = dict(ansicolors=dict(
50 51 52
    active=False,
    url='#install-ansicolors',
    description='Add colors to console responses'))
53

54 55 56
try:
    from colors import magenta, red, yellow, bold
except ImportError:
57
    red = yellow = magenta = bold = lambda x: x
58 59 60
    suggest['ansicolors']['active'] = True


61 62 63 64 65 66 67 68
def remove_colors():
    global bold
    global red
    global yellow
    global magenta
    red = yellow = magenta = bold = lambda x: x


69
def suggest_missing(miss=None, exclude=[]):
70
    global suggest
71 72 73 74 75 76
    sgs = dict(suggest)
    for exc in exclude:
        try:
            sgs.pop(exc)
        except KeyError:
            pass
77
    kamaki_docs = 'http://www.synnefo.org/docs/kamaki/latest'
78

79
    for k, v in (miss, sgs[miss]) if miss else sgs.items():
80 81
        if v['active'] and stderr.isatty():
            stderr.write('Suggestion: you may like to install %s\n' % k)
82
            stderr.write(
83
                ('%s\n' % v['description']).encode(pref_enc, 'replace'))
84 85 86 87
            stderr.write('\tIt is easy, here are the instructions:\n')
            stderr.write('\t%s/installation.html%s\n' % (
                kamaki_docs, v['url']))
            stderr.flush()
88

89

90 91 92 93 94 95 96 97 98 99
def guess_mime_type(
        filename,
        default_content_type='application/octet-stream',
        default_encoding=None):
    assert filename, 'Cannot guess mimetype for empty filename'
    try:
        from mimetypes import guess_type
        ctype, cenc = guess_type(filename)
        return ctype or default_content_type, cenc or default_encoding
    except ImportError:
100 101
        stderr.write('WARNING: Cannot import mimetypes, using defaults\n')
        stderr.flush()
102 103 104
        return (default_content_type, default_encoding)


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

115

116
def print_json(data, out=stdout):
117 118 119 120
    """Print a list or dict as json in console

    :param data: json-dumpable data

121 122
    :param out: Input/Output stream to dump values into
    """
123
    out.write(dumps(data, indent=INDENT_TAB))
124
    out.write(u'\n')
125 126


127
def print_dict(
Stavros Sachtouris's avatar
Stavros Sachtouris committed
128 129
        d,
        exclude=(), indent=0,
130
        with_enumeration=False, recursive_enumeration=False, out=stdout):
Stavros Sachtouris's avatar
Stavros Sachtouris committed
131 132 133
    """Pretty-print a dictionary object
    <indent>key: <non iterable item>
    <indent>key:
134
    <indent + INDENT_TAB><pretty-print iterable>
135

Stavros Sachtouris's avatar
Stavros Sachtouris committed
136
    :param d: (dict)
137

Stavros Sachtouris's avatar
Stavros Sachtouris committed
138
    :param exclude: (iterable of strings) keys to exclude from printing
139

Stavros Sachtouris's avatar
Stavros Sachtouris committed
140
    :param indent: (int) initial indentation (recursive)
141

Stavros Sachtouris's avatar
Stavros Sachtouris committed
142
    :param with_enumeration: (bool) enumerate 1st-level keys
143

Stavros Sachtouris's avatar
Stavros Sachtouris committed
144 145
    :param recursive_enumeration: (bool) recursively enumerate iterables (does
        not enumerate 1st level keys)
146

147 148
    :param out: Input/Output stream to dump values into

Stavros Sachtouris's avatar
Stavros Sachtouris committed
149
    :raises CLIError: if preconditions fail
150
    """
Stavros Sachtouris's avatar
Stavros Sachtouris committed
151 152
    assert isinstance(d, dict), 'print_dict input must be a dict'
    assert indent >= 0, 'print_dict indent must be >= 0'
153

Stavros Sachtouris's avatar
Stavros Sachtouris committed
154 155 156
    for i, (k, v) in enumerate(d.items()):
        k = ('%s' % k).strip()
        if k in exclude:
157
            continue
158 159 160
        print_str = ' ' * indent
        print_str += '%s.' % (i + 1) if with_enumeration else ''
        print_str += '%s:' % k
Stavros Sachtouris's avatar
Stavros Sachtouris committed
161
        if isinstance(v, dict):
162
            out.write(escape_ctrl_chars(print_str) + u'\n')
163
            print_dict(
164
                v, exclude, indent + INDENT_TAB,
165
                recursive_enumeration, recursive_enumeration, out)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
166
        elif isinstance(v, list) or isinstance(v, tuple):
167
            out.write(escape_ctrl_chars(print_str) + u'\n')
168
            print_list(
169
                v, exclude, indent + INDENT_TAB,
170
                recursive_enumeration, recursive_enumeration, out)
171
        else:
172 173
            out.write(escape_ctrl_chars(u'%s %s' % (print_str, v)))
            out.write(u'\n')
174

175

176
def print_list(
Stavros Sachtouris's avatar
Stavros Sachtouris committed
177 178
        l,
        exclude=(), indent=0,
179
        with_enumeration=False, recursive_enumeration=False, out=stdout):
Stavros Sachtouris's avatar
Stavros Sachtouris committed
180 181 182
    """Pretty-print a list of items
    <indent>key: <non iterable item>
    <indent>key:
183
    <indent + INDENT_TAB><pretty-print iterable>
184

Stavros Sachtouris's avatar
Stavros Sachtouris committed
185
    :param l: (list)
186

Stavros Sachtouris's avatar
Stavros Sachtouris committed
187
    :param exclude: (iterable of strings) items to exclude from printing
188

Stavros Sachtouris's avatar
Stavros Sachtouris committed
189
    :param indent: (int) initial indentation (recursive)
190

Stavros Sachtouris's avatar
Stavros Sachtouris committed
191
    :param with_enumeration: (bool) enumerate 1st-level items
192

Stavros Sachtouris's avatar
Stavros Sachtouris committed
193 194
    :param recursive_enumeration: (bool) recursively enumerate iterables (does
        not enumerate 1st level keys)
195

196 197
    :param out: Input/Output stream to dump values into

Stavros Sachtouris's avatar
Stavros Sachtouris committed
198
    :raises CLIError: if preconditions fail
199
    """
Stavros Sachtouris's avatar
Stavros Sachtouris committed
200 201 202
    assert isinstance(l, list) or isinstance(l, tuple), (
        'print_list prinbts a list or tuple')
    assert indent >= 0, 'print_list indent must be >= 0'
Stavros Sachtouris's avatar
Stavros Sachtouris committed
203

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


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

    :param items: (list) items are lists or dict
Stavros Sachtouris's avatar
Stavros Sachtouris committed
239

240
    :param title: (tuple) keys to use their values as title
Stavros Sachtouris's avatar
Stavros Sachtouris committed
241

242
    :param with_enumeration: (boolean) enumerate items (order id on title)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
243

244
    :param with_redundancy: (boolean) values in title also appear on body
Stavros Sachtouris's avatar
Stavros Sachtouris committed
245

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

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

272

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

293

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

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

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


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

333

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

# Split input auxiliary


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


def split_input(line):
    terms = []
358 359 360 361 362 363 364
    if line:
        rprs = regex_compile('\'.*?\'|".*?"|^[\S]*$')
        trivial_parts, interesting_parts = rprs.split(line), rprs.findall(line)
        assert(len(trivial_parts) == 1 + len(interesting_parts))
        for i, tpart in enumerate(trivial_parts):
            part = _get_from_parsed(tpart)
            if part:
Stavros Sachtouris's avatar
Stavros Sachtouris committed
365
                terms += part
366 367 368 369 370 371 372 373 374
            try:
                part = _get_from_parsed(interesting_parts[i])
            except IndexError:
                break
            if part:
                if tpart and not tpart[-1].endswith(' '):
                    terms[-1] += ' '.join(part)
                else:
                    terms += part
375 376 377
    return terms


378
def ask_user(msg, true_resp=('y', ), **kwargs):
379 380
    """Print msg and read user response

381
    :param true_resp: (tuple of chars)
382 383 384

    :returns: (bool) True if reponse in true responses, False otherwise
    """
385 386
    yep = u', '.join(true_resp)
    nope = u'<not %s>' % yep if 'n' in true_resp or 'N' in true_resp else 'N'
387 388 389
    msg = escape_ctrl_chars(msg).encode(pref_enc, 'replace')
    yep = yep.encode(pref_enc, 'replace')
    nope = nope.encode(pref_enc, 'replace')
390 391
    user_response = raw_input(
        '%s [%s/%s]: ' % (msg, yep, nope))
392
    return user_response[0].lower() in [s.lower() for s in true_resp]
393

394 395 396 397 398 399 400 401 402 403 404

def get_path_size(testpath):
    if path.isfile(testpath):
        return path.getsize(testpath)
    total_size = 0
    for top, dirs, files in walk(path.abspath(testpath)):
        for f in files:
            f = path.join(top, f)
            if path.isfile(f):
                total_size += path.getsize(f)
    return total_size
405 406 407 408 409 410


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


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

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

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

423
    :param case_sensitive: (bool) refers to values only (not keys)
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

    :returns: (list) only the dicts that match all filters
    """
    new_dicts = []
    for d in list_of_dicts:
        if set(filters).difference(d):
            continue
        match = True
        for k, v in filters.items():
            dv, v = ('%s' % d[k]), ('%s' % v)
            if not case_sensitive:
                dv, v = dv.lower(), v.lower()
            if not ((
                    exact_match and v == dv) or (
                    (not exact_match) and v in dv)):
                match = False
                break
        if match:
            new_dicts.append(d)
    return new_dicts