1 |
|
# Copyright 2011-2013 GRNET S.A. All rights reserved.
|
|
1 |
# Copyright 2011-2014 GRNET S.A. All rights reserved.
|
2 |
2 |
#
|
3 |
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
4 |
# without modification, are permitted provided that the following
|
... | ... | |
35 |
35 |
from re import compile as regex_compile
|
36 |
36 |
from os import walk, path
|
37 |
37 |
from json import dumps
|
|
38 |
from kamaki.cli.logger import get_logger
|
|
39 |
from locale import getpreferredencoding
|
38 |
40 |
|
39 |
41 |
from kamaki.cli.errors import raiseCLIError
|
40 |
42 |
|
41 |
43 |
|
42 |
44 |
INDENT_TAB = 4
|
|
45 |
log = get_logger(__name__)
|
|
46 |
pref_enc = getpreferredencoding()
|
43 |
47 |
|
44 |
48 |
|
45 |
49 |
suggest = dict(ansicolors=dict(
|
... | ... | |
56 |
60 |
suggest['ansicolors']['active'] = True
|
57 |
61 |
|
58 |
62 |
|
|
63 |
def _encode_nicely(somestr, encoding, replacement='?'):
|
|
64 |
"""Encode somestr as 'encoding', but don't raise errors (replace with ?)
|
|
65 |
This method is slow. Us it only for grace.
|
|
66 |
:param encoding: (str) encode every character in this encoding
|
|
67 |
:param replacement: (char) replace each char raising encode-decode errs
|
|
68 |
"""
|
|
69 |
newstr, err_counter = '', 0
|
|
70 |
for c in somestr:
|
|
71 |
try:
|
|
72 |
newc = c.encode(encoding)
|
|
73 |
newstr = '%s%s' % (newstr, newc)
|
|
74 |
except UnicodeError:
|
|
75 |
newstr = '%s%s' % (newstr, replacement)
|
|
76 |
err_counter += 1
|
|
77 |
if err_counter:
|
|
78 |
log.debug('\t%s character%s failed to be encoded as %s' % (
|
|
79 |
err_counter, 's' if err_counter > 1 else '', encoding))
|
|
80 |
return newstr
|
|
81 |
|
|
82 |
|
|
83 |
def DontRaiseUnicodeError(foo):
|
|
84 |
def wrap(self, *args, **kwargs):
|
|
85 |
try:
|
|
86 |
s = kwargs.pop('s')
|
|
87 |
except KeyError:
|
|
88 |
try:
|
|
89 |
s = args[0]
|
|
90 |
except IndexError:
|
|
91 |
return foo(self, *args, **kwargs)
|
|
92 |
args = args[1:]
|
|
93 |
try:
|
|
94 |
s = s.encode(pref_enc)
|
|
95 |
except UnicodeError as ue:
|
|
96 |
log.debug('Encoding(%s): %s' % (pref_enc, ue))
|
|
97 |
s = _encode_nicely(s, pref_enc, replacement='?')
|
|
98 |
return foo(self, s, *args, **kwargs)
|
|
99 |
return wrap
|
|
100 |
|
|
101 |
|
59 |
102 |
def suggest_missing(miss=None, exclude=[]):
|
60 |
103 |
global suggest
|
61 |
104 |
sgs = dict(suggest)
|
... | ... | |
424 |
467 |
:param exact_match: (bool) if false, check if the filter value is part of
|
425 |
468 |
the actual value
|
426 |
469 |
|
427 |
|
:param case_sensitive: (bool) revers to values only (not keys)
|
|
470 |
:param case_sensitive: (bool) refers to values only (not keys)
|
428 |
471 |
|
429 |
472 |
:returns: (list) only the dicts that match all filters
|
430 |
473 |
"""
|