Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 25 additions & 9 deletions can/io/generic.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
"""
Contains a generic class for file IO.
"""
"""Contains generic base classes for file IO."""

from abc import ABCMeta
from typing import Any, Optional, cast, Union, TextIO, BinaryIO, Type
from typing import (
Optional,
cast,
Iterable,
Union,
TextIO,
BinaryIO,
Type,
ContextManager,
)
from typing_extensions import Literal
from types import TracebackType

import can
import can.typechecking


class BaseIOHandler(metaclass=ABCMeta):
class BaseIOHandler(ContextManager, metaclass=ABCMeta):
"""A generic file handler that can be used for reading and writing.

Can be used as a context manager.

:attr Optional[FileLike] file:
the file-like object that is kept internally, or None if none
:attr file:
the file-like object that is kept internally, or `None` if none
was opened
"""

file: Optional[can.typechecking.FileLike]

def __init__(self, file: can.typechecking.AcceptedIOType, mode: str = "rt") -> None:
"""
:param file: a path-like object to open a file, a file-like object
Expand All @@ -39,7 +50,12 @@ def __init__(self, file: can.typechecking.AcceptedIOType, mode: str = "rt") -> N
def __enter__(self) -> "BaseIOHandler":
return self

def __exit__(self, exc_type: Type, exc_val: Any, exc_tb: Any) -> Any:
def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> Literal[False]:
self.stop()
return False

Expand All @@ -63,5 +79,5 @@ class FileIOMessageWriter(MessageWriter, metaclass=ABCMeta):


# pylint: disable=too-few-public-methods
class MessageReader(BaseIOHandler, metaclass=ABCMeta):
class MessageReader(BaseIOHandler, Iterable, metaclass=ABCMeta):
"""The base class for all readers."""
8 changes: 4 additions & 4 deletions can/typechecking.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
if typing.TYPE_CHECKING:
import os

import mypy_extensions
import typing_extensions

CanFilter = mypy_extensions.TypedDict("CanFilter", {"can_id": int, "can_mask": int})
CanFilterExtended = mypy_extensions.TypedDict(
CanFilter = typing_extensions.TypedDict("CanFilter", {"can_id": int, "can_mask": int})
CanFilterExtended = typing_extensions.TypedDict(
"CanFilterExtended", {"can_id": int, "can_mask": int, "extended": bool}
)
CanFilters = typing.Sequence[typing.Union[CanFilter, CanFilterExtended]]
Expand All @@ -33,7 +33,7 @@

BusConfig = typing.NewType("BusConfig", typing.Dict[str, typing.Any])

AutoDetectedConfig = mypy_extensions.TypedDict(
AutoDetectedConfig = typing_extensions.TypedDict(
"AutoDetectedConfig", {"interface": str, "channel": Channel}
)

Expand Down
2 changes: 1 addition & 1 deletion examples/simple_log_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def main():
with can.LogReader(sys.argv[1]) as reader:
with can.Logger(sys.argv[2]) as writer:

for msg in reader:
for msg in reader: # pylint: disable=not-an-iterable
writer.on_message_received(msg)


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
# "setuptools",
"wrapt~=1.10",
'windows-curses;platform_system=="Windows" and platform_python_implementation=="CPython"',
"mypy_extensions>=0.4.0,<0.5.0",
"typing_extensions>=3.10.0.0",
'pywin32;platform_system=="Windows" and platform_python_implementation=="CPython"',
'msgpack~=1.0.0;platform_system!="Windows"',
],
Expand Down