☔ more type annotations

This commit is contained in:
Gina Häußge committed 2019-04-08 13:05:27 +02:00
1 parent 43e2f79575
commit 55e8cc1118
2 files changed
+13 -8

No files matched your search

+10 -5
View File
@@ -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:]))
+3 -3
View File
@@ -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)