Revision 90c22848

b/kamaki/cli/commands/__init__.py
116 116

  
117 117
    @DontRaiseUnicodeError
118 118
    def write(self, s):
119
        self._out.write('%s' % s)
119
        self._out.write(s)
120 120
        self._out.flush()
121 121

  
122 122
    @DontRaiseUnicodeError
......
133 133
        return print_list(*args, **kwargs)
134 134

  
135 135
    def print_dict(self, *args, **kwargs):
136
        kwargs.setdefault('out', self._out)
136
        kwargs.setdefault('out', self)
137 137
        return print_dict(*args, **kwargs)
138 138

  
139 139
    def print_json(self, *args, **kwargs):
140
        kwargs.setdefault('out', self._out)
140
        kwargs.setdefault('out', self)
141 141
        return print_json(*args, **kwargs)
142 142

  
143 143
    def print_items(self, *args, **kwargs):
144
        kwargs.setdefault('out', self._out)
144
        kwargs.setdefault('out', self)
145 145
        return print_items(*args, **kwargs)
146 146

  
147 147
    def ask_user(self, *args, **kwargs):
148 148
        kwargs.setdefault('user_in', self._in)
149
        kwargs.setdefault('out', self._out)
149
        kwargs.setdefault('out', self)
150 150
        return ask_user(*args, **kwargs)
151 151

  
152 152
    @DontRaiseKeyError
......
298 298

  
299 299
    def _optional_output(self, r):
300 300
        if self['json_output']:
301
            print_json(r, out=self._out)
301
            print_json(r, out=self)
302 302
        elif self['with_output']:
303
            print_items([r] if isinstance(r, dict) else r, out=self._out)
303
            print_items([r] if isinstance(r, dict) else r, out=self)
304 304

  
305 305

  
306 306
class _optional_json(object):
......
317 317

  
318 318
    def _print(self, output, print_method=print_items, **print_method_kwargs):
319 319
        if self['json_output'] or self['output_format']:
320
            print_json(output, out=self._out)
320
            print_json(output, out=self)
321 321
        else:
322
            print_method_kwargs.setdefault('out', self._out)
322
            print_method_kwargs.setdefault('out', self)
323 323
            print_method(output, **print_method_kwargs)
324 324

  
325 325

  
b/kamaki/cli/utils/__init__.py
69 69
    newstr, err_counter = '', 0
70 70
    for c in somestr:
71 71
        try:
72
            newc = c.encode(encoding)
72
            newc = c.decode('utf-8').encode(encoding)
73 73
            newstr = '%s%s' % (newstr, newc)
74 74
        except UnicodeError:
75 75
            newstr = '%s%s' % (newstr, replacement)
76 76
            err_counter += 1
77 77
    if err_counter:
78
        log.debug('\t%s character%s failed to be encoded as %s' % (
78
        log.warning('\t%s character%s failed to be encoded as %s' % (
79 79
            err_counter, 's' if err_counter > 1 else '', encoding))
80 80
    return newstr
81 81

  
82 82

  
83 83
def DontRaiseUnicodeError(foo):
84
    if pref_enc.lower() == 'utf-8':
85
        return foo
86

  
84 87
    def wrap(self, *args, **kwargs):
85 88
        try:
86 89
            s = kwargs.pop('s')
......
91 94
                return foo(self, *args, **kwargs)
92 95
            args = args[1:]
93 96
        try:
94
            s = s.encode(pref_enc)
97
            s = (u'%s' % s).decode('utf-8').encode(pref_enc)
95 98
        except UnicodeError as ue:
96 99
            log.debug('Encoding(%s): %s' % (pref_enc, ue))
97
            s = _encode_nicely(s, pref_enc, replacement='?')
100
            s = _encode_nicely(u'%s' % s, pref_enc, replacement='?')
98 101
        return foo(self, s, *args, **kwargs)
99 102
    return wrap
100 103

  
......
103 106
    if encoding.lower() == 'utf-8':
104 107
        return s
105 108
    try:
106
        return s.encode(encoding)
109
        return s.decode('utf-8').encode(encoding)
107 110
    except UnicodeError as ue:
108 111
        log.debug('Encoding(%s): %s' % (encoding, ue))
109 112
        return _encode_nicely(s, encoding, replacement)
......
175 178
    """
176 179
    out.write(dumps(data, indent=INDENT_TAB))
177 180
    out.write('\n')
178
    out.flush()
179 181

  
180 182

  
181 183
def print_dict(
......
224 226
                recursive_enumeration, recursive_enumeration, out)
225 227
        else:
226 228
            out.write('%s %s\n' % (print_str, v))
227
        out.flush()
228 229

  
229 230

  
230 231
def print_list(
......
280 281
            if item in exclude:
281 282
                continue
282 283
            out.write('%s%s\n' % (print_str, item))
283
        out.flush()
284
    out.flush()
285 284

  
286 285

  
287 286
def print_items(
......
305 304
    if not (isinstance(items, dict) or isinstance(items, list) or isinstance(
306 305
                items, tuple)):
307 306
        out.write('%s\n' % items)
308
        out.flush()
309 307
        return
310 308

  
311 309
    for i, item in enumerate(items):
......
322 320
            print_list(item, indent=INDENT_TAB, out=out)
323 321
        else:
324 322
            out.write(' %s\n' % item)
325
        out.flush()
326
    out.flush()
327 323

  
328 324

  
329 325
def format_size(size, decimal_factors=False):
......
447 443
    yep = ', '.join(true_resp)
448 444
    nope = '<not %s>' % yep if 'n' in true_resp or 'N' in true_resp else 'N'
449 445
    out.write('%s [%s/%s]: ' % (msg, yep, nope))
450
    out.flush()
451 446
    user_response = user_in.readline()
452 447
    return user_response[0].lower() in [s.lower() for s in true_resp]
453 448

  

Also available in: Unified diff