Print errors to stderr
This commit is contained in:
parent
d9642b2047
commit
16054cc3bc
29
speedtest.py
29
speedtest.py
|
@ -149,24 +149,25 @@ except ImportError:
|
||||||
import builtins
|
import builtins
|
||||||
from io import TextIOWrapper, FileIO
|
from io import TextIOWrapper, FileIO
|
||||||
|
|
||||||
class _Py3Utf8Stdout(TextIOWrapper):
|
class _Py3Utf8Output(TextIOWrapper):
|
||||||
"""UTF-8 encoded wrapper around stdout for py3, to override
|
"""UTF-8 encoded wrapper around stdout for py3, to override
|
||||||
ASCII stdout
|
ASCII stdout
|
||||||
"""
|
"""
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, f, **kwargs):
|
||||||
buf = FileIO(sys.stdout.fileno(), 'w')
|
buf = FileIO(f.fileno(), 'w')
|
||||||
super(_Py3Utf8Stdout, self).__init__(
|
super(_Py3Utf8Output, self).__init__(
|
||||||
buf,
|
buf,
|
||||||
encoding='utf8',
|
encoding='utf8',
|
||||||
errors='strict'
|
errors='strict'
|
||||||
)
|
)
|
||||||
|
|
||||||
def write(self, s):
|
def write(self, s):
|
||||||
super(_Py3Utf8Stdout, self).write(s)
|
super(_Py3Utf8Output, self).write(s)
|
||||||
self.flush()
|
self.flush()
|
||||||
|
|
||||||
_py3_print = getattr(builtins, 'print')
|
_py3_print = getattr(builtins, 'print')
|
||||||
_py3_utf8_stdout = _Py3Utf8Stdout()
|
_py3_utf8_stdout = _Py3Utf8Output(sys.stdout)
|
||||||
|
_py3_utf8_stderr = _Py3Utf8Output(sys.stderr)
|
||||||
|
|
||||||
def to_utf8(v):
|
def to_utf8(v):
|
||||||
"""No-op encode to utf-8 for py3"""
|
"""No-op encode to utf-8 for py3"""
|
||||||
|
@ -174,7 +175,10 @@ except ImportError:
|
||||||
|
|
||||||
def print_(*args, **kwargs):
|
def print_(*args, **kwargs):
|
||||||
"""Wrapper function for py3 to print, with a utf-8 encoded stdout"""
|
"""Wrapper function for py3 to print, with a utf-8 encoded stdout"""
|
||||||
kwargs['file'] = _py3_utf8_stdout
|
if kwargs.get('file') == sys.stderr:
|
||||||
|
kwargs['file'] = _py3_utf8_stderr
|
||||||
|
else:
|
||||||
|
kwargs['file'] = kwargs.get('file', _py3_utf8_stdout)
|
||||||
_py3_print(*args, **kwargs)
|
_py3_print(*args, **kwargs)
|
||||||
else:
|
else:
|
||||||
del __builtin__
|
del __builtin__
|
||||||
|
@ -1577,7 +1581,7 @@ def validate_optional_args(args):
|
||||||
'unavailable' % (info[0], arg))
|
'unavailable' % (info[0], arg))
|
||||||
|
|
||||||
|
|
||||||
def printer(string, quiet=False, debug=False, **kwargs):
|
def printer(string, quiet=False, debug=False, error=False, **kwargs):
|
||||||
"""Helper function to print a string only when not quiet"""
|
"""Helper function to print a string only when not quiet"""
|
||||||
|
|
||||||
if debug and not DEBUG:
|
if debug and not DEBUG:
|
||||||
|
@ -1588,6 +1592,9 @@ def printer(string, quiet=False, debug=False, **kwargs):
|
||||||
else:
|
else:
|
||||||
out = string
|
out = string
|
||||||
|
|
||||||
|
if error:
|
||||||
|
kwargs['file'] = sys.stderr
|
||||||
|
|
||||||
if not quiet:
|
if not quiet:
|
||||||
print_(out, **kwargs)
|
print_(out, **kwargs)
|
||||||
|
|
||||||
|
@ -1648,14 +1655,14 @@ def shell():
|
||||||
secure=args.secure
|
secure=args.secure
|
||||||
)
|
)
|
||||||
except (ConfigRetrievalError,) + HTTP_ERRORS:
|
except (ConfigRetrievalError,) + HTTP_ERRORS:
|
||||||
printer('Cannot retrieve speedtest configuration')
|
printer('Cannot retrieve speedtest configuration', error=True)
|
||||||
raise SpeedtestCLIError(get_exception())
|
raise SpeedtestCLIError(get_exception())
|
||||||
|
|
||||||
if args.list:
|
if args.list:
|
||||||
try:
|
try:
|
||||||
speedtest.get_servers()
|
speedtest.get_servers()
|
||||||
except (ServersRetrievalError,) + HTTP_ERRORS:
|
except (ServersRetrievalError,) + HTTP_ERRORS:
|
||||||
print_('Cannot retrieve speedtest server list')
|
printer('Cannot retrieve speedtest server list', error=True)
|
||||||
raise SpeedtestCLIError(get_exception())
|
raise SpeedtestCLIError(get_exception())
|
||||||
|
|
||||||
for _, servers in sorted(speedtest.servers.items()):
|
for _, servers in sorted(speedtest.servers.items()):
|
||||||
|
@ -1683,7 +1690,7 @@ def shell():
|
||||||
', '.join('%s' % s for s in args.server)
|
', '.join('%s' % s for s in args.server)
|
||||||
)
|
)
|
||||||
except (ServersRetrievalError,) + HTTP_ERRORS:
|
except (ServersRetrievalError,) + HTTP_ERRORS:
|
||||||
print_('Cannot retrieve speedtest server list')
|
printer('Cannot retrieve speedtest server list', error=True)
|
||||||
raise SpeedtestCLIError(get_exception())
|
raise SpeedtestCLIError(get_exception())
|
||||||
except InvalidServerIDType:
|
except InvalidServerIDType:
|
||||||
raise SpeedtestCLIError(
|
raise SpeedtestCLIError(
|
||||||
|
|
Loading…
Reference in New Issue