Revision c2332605

b/kamaki/cli/commands/__init__.py
39 39
from kamaki.cli.errors import CLIInvalidArgument
40 40
from sys import stdin, stdout, stderr
41 41
import codecs
42
import locale
42 43

  
43 44

  
44 45
log = get_logger(__name__)
46
pref_enc = locale.getpreferredencoding()
47

  
48

  
49
def _encode_nicely(somestr, encoding=pref_enc, replacement='?'):
50
    """Encode somestr as 'encoding', but don't raise errors (replace with ?)
51
        :param encoding: (str) encode every character in this encoding
52
        :param replacement: (char) replace each char raising encode-decode errs
53
    """
54
    newstr = ''
55
    for c in somestr:
56
        try:
57
            newc = c.encode(encoding)
58
            newstr = '%s%s' % (newstr, newc)
59
        except (UnicodeDecodeError, UnicodeEncodeError) as e:
60
            log.debug('Encoding(%s): %s' % (encoding, e))
61
            newstr = '%s%s' % (newstr, replacement)
62
    return newstr
45 63

  
46 64

  
47 65
def DontRaiseKeyError(func):
......
89 107
        self._in, self._out, self._err = (
90 108
            _in or stdin, _out or stdout, _err or stderr)
91 109
        self._in = codecs.getreader('utf-8')(_in or stdin)
92
        self._out = codecs.getreader('utf-8')(_out or stdout)
93
        self._err = codecs.getreader('utf-8')(_err or stderr)
110
        self._out = codecs.getwriter(pref_enc)(_out or stdout)
111
        self._err = codecs.getwriter(pref_enc)(_err or stderr)
94 112
        self.required = getattr(self, 'required', None)
95 113
        if hasattr(self, 'arguments'):
96 114
            arguments.update(self.arguments)

Also available in: Unified diff