Skip to content

Fix docstrings, use duck typing #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions adafruit_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,18 @@
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):
"""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

Expand Down Expand Up @@ -143,8 +153,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(
Expand All @@ -154,6 +163,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()
Expand All @@ -164,10 +175,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
Expand All @@ -177,8 +190,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))

Expand All @@ -203,16 +215,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))

Expand Down Expand Up @@ -286,7 +296,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

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
# SPDX-License-Identifier: Unlicense

Adafruit-Blinka
typing-extensions
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
# Author details
author="Adafruit Industries",
author_email="[email protected]",
install_requires=[
"Adafruit-Blinka",
],
install_requires=["Adafruit-Blinka", "typing-extensions"],
# Choose your license
license="MIT",
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
Expand Down