Centralize encoding guesses, default to UTF-8
This might not be enough for print() and could need a locale.setlocale() even though it is generally discouraged. closes #1352
This commit is contained in:
parent
429ad91a78
commit
19a95dc0d6
4 changed files with 96 additions and 90 deletions
|
|
@ -76,11 +76,22 @@ class ConsoleApplication(BaseApplication):
|
|||
|
||||
stdin = sys.stdin
|
||||
stdout = sys.stdout
|
||||
stderr = sys.stderr
|
||||
|
||||
def __init__(self, option_parser=None):
|
||||
BaseApplication.__init__(self, option_parser)
|
||||
self.weboob.callbacks['login'] = self.login_cb
|
||||
self.enabled_backends = set()
|
||||
self.encoding = self.guess_encoding()
|
||||
|
||||
def guess_encoding(self, stdio=None):
|
||||
if stdio is None:
|
||||
stdio = self.stdout
|
||||
encoding = stdio.encoding or locale.getpreferredencoding()
|
||||
# ASCII or ANSII is most likely a user mistake
|
||||
if not encoding or encoding.lower() == 'ascii' or encoding.lower().startswith('ansi'):
|
||||
encoding = 'UTF-8'
|
||||
return encoding
|
||||
|
||||
def login_cb(self, backend_name, value):
|
||||
return self.ask('[%s] %s' % (backend_name,
|
||||
|
|
@ -109,7 +120,7 @@ class ConsoleApplication(BaseApplication):
|
|||
ret = super(ConsoleApplication, self).load_backends(*args, **kwargs)
|
||||
|
||||
for err in errors:
|
||||
print('Error(%s): %s' % (err.backend_name, err), file=sys.stderr)
|
||||
print('Error(%s): %s' % (err.backend_name, err), file=self.stderr)
|
||||
if self.ask('Do you want to reconfigure this backend?', default=True):
|
||||
self.edit_backend(err.backend_name)
|
||||
self.load_backends(names=[err.backend_name])
|
||||
|
|
@ -124,7 +135,7 @@ class ConsoleApplication(BaseApplication):
|
|||
def check_loaded_backends(self, default_config=None):
|
||||
while len(self.enabled_backends) == 0:
|
||||
print('Warning: there is currently no configured backend for %s' % self.APPNAME)
|
||||
if not os.isatty(sys.stdout.fileno()) or not self.ask('Do you want to configure backends?', default=True):
|
||||
if not os.isatty(self.stdout.fileno()) or not self.ask('Do you want to configure backends?', default=True):
|
||||
return False
|
||||
|
||||
self.prompt_create_backends(default_config)
|
||||
|
|
@ -158,7 +169,7 @@ class ConsoleApplication(BaseApplication):
|
|||
if str(r).isdigit():
|
||||
i = int(r) - 1
|
||||
if i < 0 or i >= len(modules):
|
||||
print('Error: %s is not a valid choice' % r, file=sys.stderr)
|
||||
print('Error: %s is not a valid choice' % r, file=self.stderr)
|
||||
continue
|
||||
name = modules[i]
|
||||
try:
|
||||
|
|
@ -245,11 +256,11 @@ class ConsoleApplication(BaseApplication):
|
|||
backend = None
|
||||
|
||||
if not backend:
|
||||
print('Backend "%s" does not exist.' % name, file=sys.stderr)
|
||||
print('Backend "%s" does not exist.' % name, file=self.stderr)
|
||||
return 1
|
||||
|
||||
if not backend.has_caps(CapAccount) or backend.klass.ACCOUNT_REGISTER_PROPERTIES is None:
|
||||
print('You can\'t register a new account with %s' % name, file=sys.stderr)
|
||||
print('You can\'t register a new account with %s' % name, file=self.stderr)
|
||||
return 1
|
||||
|
||||
account = Account()
|
||||
|
|
@ -294,7 +305,7 @@ class ConsoleApplication(BaseApplication):
|
|||
try:
|
||||
self.weboob.repositories.install(name)
|
||||
except ModuleInstallError as e:
|
||||
print('Unable to install module "%s": %s' % (name, e), file=sys.stderr)
|
||||
print('Unable to install module "%s": %s' % (name, e), file=self.stderr)
|
||||
return False
|
||||
|
||||
print('')
|
||||
|
|
@ -326,7 +337,7 @@ class ConsoleApplication(BaseApplication):
|
|||
params = items
|
||||
config = module.config.load(self.weboob, bname, name, params, nofail=True)
|
||||
except ModuleLoadError as e:
|
||||
print('Unable to load module "%s": %s' % (name, e), file=sys.stderr)
|
||||
print('Unable to load module "%s": %s' % (name, e), file=self.stderr)
|
||||
return 1
|
||||
|
||||
# ask for params non-specified on command-line arguments
|
||||
|
|
@ -345,7 +356,7 @@ class ConsoleApplication(BaseApplication):
|
|||
print('-------------------------%s' % ('-' * len(module.name)))
|
||||
|
||||
while not edit and self.weboob.backends_config.backend_exists(name):
|
||||
print('Backend instance "%s" already exists in "%s"' % (name, self.weboob.backends_config.confpath), file=sys.stderr)
|
||||
print('Backend instance "%s" already exists in "%s"' % (name, self.weboob.backends_config.confpath), file=self.stderr)
|
||||
if not self.ask('Add new backend for module "%s"?' % module.name, default=False):
|
||||
return 1
|
||||
|
||||
|
|
@ -361,7 +372,7 @@ class ConsoleApplication(BaseApplication):
|
|||
print('Backend "%s" successfully %s.' % (name, 'edited' if edit else 'added'))
|
||||
return name
|
||||
except BackendAlreadyExists:
|
||||
print('Backend "%s" already exists.' % name, file=sys.stderr)
|
||||
print('Backend "%s" already exists.' % name, file=self.stderr)
|
||||
return 1
|
||||
|
||||
def ask(self, question, default=None, masked=None, regexp=None, choices=None, tiny=None):
|
||||
|
|
@ -406,7 +417,7 @@ class ConsoleApplication(BaseApplication):
|
|||
question = u'[%s] %s' % (v.id, question)
|
||||
|
||||
if isinstance(v, ValueBackendPassword):
|
||||
print(question.encode(sys.stdout.encoding or locale.getpreferredencoding()) + ':')
|
||||
print(question.encode(self.encoding) + ':')
|
||||
question = v.label
|
||||
choices = OrderedDict()
|
||||
choices['c'] = 'Run an external tool during backend load'
|
||||
|
|
@ -476,9 +487,9 @@ class ConsoleApplication(BaseApplication):
|
|||
if sys.platform == 'win32':
|
||||
line = getpass.getpass(str(question))
|
||||
else:
|
||||
line = getpass.getpass(question.encode(sys.stdout.encoding or locale.getpreferredencoding()))
|
||||
line = getpass.getpass(self.encoding)
|
||||
else:
|
||||
self.stdout.write(question.encode(sys.stdout.encoding or locale.getpreferredencoding()))
|
||||
self.stdout.write(question.encode(self.encoding))
|
||||
self.stdout.flush()
|
||||
line = self.stdin.readline()
|
||||
if len(line) == 0:
|
||||
|
|
@ -497,7 +508,7 @@ class ConsoleApplication(BaseApplication):
|
|||
try:
|
||||
v.set(line)
|
||||
except ValueError as e:
|
||||
print(u'Error: %s' % e, file=sys.stderr)
|
||||
print(u'Error: %s' % e, file=self.stderr)
|
||||
else:
|
||||
break
|
||||
|
||||
|
|
@ -512,7 +523,7 @@ class ConsoleApplication(BaseApplication):
|
|||
filename = f.name
|
||||
if content is not None:
|
||||
if isinstance(content, unicode):
|
||||
content = content.encode(sys.stdin.encoding or locale.getpreferredencoding())
|
||||
content = content.encode(self.encoding)
|
||||
f.write(content)
|
||||
f.flush()
|
||||
try:
|
||||
|
|
@ -527,7 +538,7 @@ class ConsoleApplication(BaseApplication):
|
|||
print('Reading content from stdin... Type ctrl-D ' \
|
||||
'from an empty line to stop.')
|
||||
text = sys.stdin.read()
|
||||
return text.decode(sys.stdin.encoding or locale.getpreferredencoding())
|
||||
return text.decode(self.encoding)
|
||||
|
||||
def bcall_error_handler(self, backend, error, backtrace):
|
||||
"""
|
||||
|
|
@ -539,7 +550,7 @@ class ConsoleApplication(BaseApplication):
|
|||
msg = unicode(error)
|
||||
if not msg:
|
||||
msg = 'invalid login/password.'
|
||||
print('Error(%s): %s' % (backend.name, msg), file=sys.stderr)
|
||||
print('Error(%s): %s' % (backend.name, msg), file=self.stderr)
|
||||
if self.ask('Do you want to reconfigure this backend?', default=True):
|
||||
self.unload_backends(names=[backend.name])
|
||||
self.edit_backend(backend.name)
|
||||
|
|
@ -548,21 +559,21 @@ class ConsoleApplication(BaseApplication):
|
|||
msg = unicode(error)
|
||||
if not msg:
|
||||
msg = 'website is unavailable.'
|
||||
print(u'Error(%s): %s' % (backend.name, msg), file=sys.stderr)
|
||||
print(u'Error(%s): %s' % (backend.name, msg), file=self.stderr)
|
||||
elif isinstance(error, BrowserForbidden):
|
||||
print(u'Error(%s): %s' % (backend.name, msg or 'Forbidden'), file=sys.stderr)
|
||||
print(u'Error(%s): %s' % (backend.name, msg or 'Forbidden'), file=self.stderr)
|
||||
elif isinstance(error, NotImplementedError):
|
||||
print(u'Error(%s): this feature is not supported yet by this backend.' % backend.name, file=sys.stderr)
|
||||
print(u' %s To help the maintainer of this backend implement this feature,' % (' ' * len(backend.name)), file=sys.stderr)
|
||||
print(u' %s please contact: %s <%s@issues.weboob.org>' % (' ' * len(backend.name), backend.MAINTAINER, backend.NAME), file=sys.stderr)
|
||||
print(u'Error(%s): this feature is not supported yet by this backend.' % backend.name, file=self.stderr)
|
||||
print(u' %s To help the maintainer of this backend implement this feature,' % (' ' * len(backend.name)), file=self.stderr)
|
||||
print(u' %s please contact: %s <%s@issues.weboob.org>' % (' ' * len(backend.name), backend.MAINTAINER, backend.NAME), file=self.stderr)
|
||||
elif isinstance(error, UserError):
|
||||
print(u'Error(%s): %s' % (backend.name, to_unicode(error)), file=sys.stderr)
|
||||
print(u'Error(%s): %s' % (backend.name, to_unicode(error)), file=self.stderr)
|
||||
elif isinstance(error, MoreResultsAvailable):
|
||||
print(u'Hint: There are more results for backend %s' % (backend.name), file=sys.stderr)
|
||||
print(u'Hint: There are more results for backend %s' % (backend.name), file=self.stderr)
|
||||
elif isinstance(error, BrowserSSLError):
|
||||
print(u'FATAL(%s): ' % backend.name + self.BOLD + '/!\ SERVER CERTIFICATE IS INVALID /!\\' + self.NC, file=sys.stderr)
|
||||
print(u'FATAL(%s): ' % backend.name + self.BOLD + '/!\ SERVER CERTIFICATE IS INVALID /!\\' + self.NC, file=self.stderr)
|
||||
else:
|
||||
print(u'Bug(%s): %s' % (backend.name, to_unicode(error)), file=sys.stderr)
|
||||
print(u'Bug(%s): %s' % (backend.name, to_unicode(error)), file=self.stderr)
|
||||
|
||||
minfo = self.weboob.repositories.get_module_info(backend.NAME)
|
||||
if minfo and not minfo.is_local():
|
||||
|
|
@ -577,7 +588,7 @@ class ConsoleApplication(BaseApplication):
|
|||
return
|
||||
|
||||
if logging.root.level == logging.DEBUG:
|
||||
print(backtrace, file=sys.stderr)
|
||||
print(backtrace, file=self.stderr)
|
||||
else:
|
||||
return True
|
||||
|
||||
|
|
@ -596,6 +607,6 @@ class ConsoleApplication(BaseApplication):
|
|||
ask_debug_mode = True
|
||||
|
||||
if ask_debug_mode:
|
||||
print(debugmsg, file=sys.stderr)
|
||||
print(debugmsg, file=self.stderr)
|
||||
elif len(more_results) > 0:
|
||||
print('Hint: There are more results available for %s (use option -n or count command)' % (', '.join(more_results)), file=sys.stderr)
|
||||
print('Hint: There are more results available for %s (use option -n or count command)' % (', '.join(more_results)), file=self.stderr)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue