diff --git a/src/octoprint/logging/__init__.py b/src/octoprint/logging/__init__.py index 1021b169a..d9237cfee 100644 --- a/src/octoprint/logging/__init__.py +++ b/src/octoprint/logging/__init__.py @@ -3,6 +3,8 @@ from __future__ import absolute_import from octoprint.logging import handlers +from typing import Union + def log_to_handler(logger, handler, level, msg, exc_info=None, extra=None, *args): """ Logs to the provided handler only. @@ -137,14 +139,17 @@ def get_divider_line(c, message=None, length=78, indent=3): return c * indent + " " + message -def prefix_multilines(text, prefix=": "): +def prefix_multilines(text, prefix=u": "): + # type: (Union[unicode, bytes], unicode) -> unicode + from octoprint.util import to_unicode + lines = text.splitlines() if not lines: - return "" + return u"" if len(lines) == 1: - return lines[0] + return to_unicode(lines[0]) - return lines[0] + "\n" + "\n".join(map(lambda line: prefix + line, - lines[1:])) + return to_unicode(lines[0]) + u"\n" + u"\n".join(map(lambda line: prefix + to_unicode(line), + lines[1:])) diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index a3298d22b..5c02e76af 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -37,7 +37,7 @@ logger = logging.getLogger(__name__) def to_bytes(s_or_u, encoding="utf-8", errors="strict"): - # type: (Union[str, bytes], str, str) -> bytes + # type: (Union[unicode, bytes], str, str) -> bytes """Make sure ``s_or_u`` is a bytestring.""" if isinstance(s_or_u, unicode): return s_or_u.encode(encoding, errors=errors) @@ -46,7 +46,7 @@ def to_bytes(s_or_u, encoding="utf-8", errors="strict"): def to_unicode(s_or_u, encoding="utf-8", errors="strict"): - # type: (Union[str, bytes], str, str) -> str + # type: (Union[unicode, bytes], str, str) -> unicode """Make sure ``s_or_u`` is a unicode string.""" if isinstance(s_or_u, bytes): return s_or_u.decode(encoding, errors=errors) @@ -55,7 +55,7 @@ def to_unicode(s_or_u, encoding="utf-8", errors="strict"): def to_native_str(s_or_u): - # type: (Union[str, bytes]) -> Union[str, bytes] + # type: (Union[unicode, bytes]) -> str """Make sure ``s_or_u`` is a 'str'.""" if sys.version_info[0] == 2: return to_bytes(s_or_u)