From 63c54eae539d8b754d3f0545f1eb133c20c90dcb Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sat, 18 Jun 2022 10:18:57 -0400 Subject: [PATCH 1/8] Update docstrings --- adafruit_logging.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/adafruit_logging.py b/adafruit_logging.py index f90ec5b..dcf7b7d 100644 --- a/adafruit_logging.py +++ b/adafruit_logging.py @@ -143,8 +143,7 @@ class Handler: def format(self, record: LogRecord) -> str: """Generate a timestamped message. - :param int log_level: the logging level - :param str message: the message to log + :param record: The record (message object) to be logged """ return "{0:<0.3f}: {1} - {2}".format( @@ -154,6 +153,8 @@ def format(self, record: LogRecord) -> str: def emit(self, record: LogRecord) -> None: """Send a message where it should go. Placeholder for subclass implementations. + + :param record: The record (message object) to be logged """ raise NotImplementedError() @@ -177,8 +178,7 @@ def __init__(self, stream: Optional[Union[TextIOWrapper, StringIO]] = None) -> N def emit(self, record: LogRecord) -> None: """Send a message to the console. - :param int log_level: the logging level - :param str message: the message to log + :param record: The record (message object) to be logged """ self.stream.write(self.format(record)) @@ -203,16 +203,14 @@ def close(self) -> None: def format(self, record: LogRecord) -> str: """Generate a string to log - :param level: The level of the message - :param msg: The message to format + :param record: The record (message object) to be logged """ return super().format(record) + "\r\n" def emit(self, record: LogRecord) -> None: """Generate the message and write it to the UART. - :param level: The level of the message - :param msg: The message to log + :param record: The record (message object) to be logged """ self.stream.write(self.format(record)) @@ -286,7 +284,7 @@ def addHandler(self, hdlr: Handler) -> None: *NOTE* This is slightly different from the CPython equivalent which adds the handler rather than replacing it. - :param Handler hdlr: the handler + :param Handler hdlr: The handler to add """ self._handler = hdlr From 3f4c59d8d57cb12e53d1d45489d64029ed19063a Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sat, 18 Jun 2022 10:20:14 -0400 Subject: [PATCH 2/8] Use Protocol for StreamHandler.__init__() --- adafruit_logging.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/adafruit_logging.py b/adafruit_logging.py index dcf7b7d..b1f7dfd 100644 --- a/adafruit_logging.py +++ b/adafruit_logging.py @@ -62,8 +62,14 @@ from collections import namedtuple try: - from typing import Optional, Union - from io import TextIOWrapper, StringIO + from typing import Optional + from typing_extensions import Protocol + + class WriteableStream(Protocol): + + def write(self, buf: str) -> int: + ... + except ImportError: pass @@ -165,10 +171,12 @@ class StreamHandler(Handler): """Send logging messages to a stream, `sys.stderr` (typically the serial console) by default. - :param stream: The stream to log to, default is `sys.stderr` + :param stream: The stream to log to, default is `sys.stderr`; + can accept any stream that implements ``stream.write()`` + with string inputs """ - def __init__(self, stream: Optional[Union[TextIOWrapper, StringIO]] = None) -> None: + def __init__(self, stream: Optional[WriteableStream] = None) -> None: super().__init__() if stream is None: stream = sys.stderr From acb6e04f1d0b96e5df7fdfc4590b94464934523a Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sat, 18 Jun 2022 10:26:18 -0400 Subject: [PATCH 3/8] Linted and reformatted per pre-commit --- adafruit_logging.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/adafruit_logging.py b/adafruit_logging.py index b1f7dfd..805af5c 100644 --- a/adafruit_logging.py +++ b/adafruit_logging.py @@ -66,9 +66,13 @@ from typing_extensions import Protocol class WriteableStream(Protocol): + """Any stream that can ``write`` strings""" def write(self, buf: str) -> int: - ... + """Write to the stream + + :param str buf: The string data to write to the stream + """ except ImportError: pass From 5c706853a0272c9a7c40fdd11c4f576a9195d417 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sat, 18 Jun 2022 11:40:09 -0400 Subject: [PATCH 4/8] Add typing_extensions as dependency --- requirements.txt | 1 + setup.py | 1 + 2 files changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index 5f97a2d..3396d72 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ # SPDX-License-Identifier: Unlicense Adafruit-Blinka +typing-extensions diff --git a/setup.py b/setup.py index 428e4c2..e95da64 100644 --- a/setup.py +++ b/setup.py @@ -34,6 +34,7 @@ author_email="circuitpython@adafruit.com", install_requires=[ "Adafruit-Blinka", + "typing-extensions" ], # Choose your license license="MIT", From 295f0dc0a8e6112434f61d04d57a5c06ba0fc2d0 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sat, 18 Jun 2022 11:47:03 -0400 Subject: [PATCH 5/8] Reformatted setup.py --- setup.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/setup.py b/setup.py index e95da64..86b612c 100644 --- a/setup.py +++ b/setup.py @@ -32,10 +32,7 @@ # Author details author="Adafruit Industries", author_email="circuitpython@adafruit.com", - install_requires=[ - "Adafruit-Blinka", - "typing-extensions" - ], + install_requires=["Adafruit-Blinka", "typing-extensions"], # Choose your license license="MIT", # See https://pypi.python.org/pypi?%3Aaction=list_classifiers From 8fa157182e9c3efaa0ca972ff1e1886744edd345 Mon Sep 17 00:00:00 2001 From: Alec Delaney <89490472+tekktrik@users.noreply.github.com> Date: Tue, 21 Jun 2022 10:09:34 -0400 Subject: [PATCH 6/8] Update adafruit_logging.py Use try/except for Protocol import Co-authored-by: Dan Halbert --- adafruit_logging.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/adafruit_logging.py b/adafruit_logging.py index 805af5c..5f01061 100644 --- a/adafruit_logging.py +++ b/adafruit_logging.py @@ -63,7 +63,10 @@ try: from typing import Optional - from typing_extensions import Protocol + try: + from typing import Protocol + except ImportError: + from typing_extensions import Protocol class WriteableStream(Protocol): """Any stream that can ``write`` strings""" From 368ebf59e71e49a7169768bdc691c235ba171596 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 21 Jun 2022 10:18:42 -0400 Subject: [PATCH 7/8] fix indentation --- adafruit_logging.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_logging.py b/adafruit_logging.py index 5f01061..651c8a2 100644 --- a/adafruit_logging.py +++ b/adafruit_logging.py @@ -63,10 +63,10 @@ try: from typing import Optional - try: - from typing import Protocol - except ImportError: - from typing_extensions import Protocol + try: + from typing import Protocol + except ImportError: + from typing_extensions import Protocol class WriteableStream(Protocol): """Any stream that can ``write`` strings""" From 1cc39f5e8802000bcfebb48bfcff8c3020aca5fb Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 21 Jun 2022 10:23:15 -0400 Subject: [PATCH 8/8] Add newline per pre-commit reformatting --- adafruit_logging.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_logging.py b/adafruit_logging.py index 651c8a2..5c5d6d6 100644 --- a/adafruit_logging.py +++ b/adafruit_logging.py @@ -63,6 +63,7 @@ try: from typing import Optional + try: from typing import Protocol except ImportError: