diff --git a/can/io/asc.py b/can/io/asc.py index 2826a22a6..b0c7777d7 100644 --- a/can/io/asc.py +++ b/can/io/asc.py @@ -39,6 +39,8 @@ def __init__( file: Union[StringPathLike, TextIO], base: str = "hex", relative_timestamp: bool = True, + *args: Any, + **kwargs: Any, ) -> None: """ :param file: a path-like object or as file-like object to read from @@ -352,6 +354,7 @@ def __init__( self, file: Union[StringPathLike, TextIO], channel: int = 1, + *args: Any, **kwargs: Any, ) -> None: """ diff --git a/can/io/blf.py b/can/io/blf.py index 911177d20..93fa54ca2 100644 --- a/can/io/blf.py +++ b/can/io/blf.py @@ -143,7 +143,12 @@ class BLFReader(MessageReader): file: BinaryIO - def __init__(self, file: Union[StringPathLike, BinaryIO]) -> None: + def __init__( + self, + file: Union[StringPathLike, BinaryIO], + *args: Any, + **kwargs: Any, + ) -> None: """ :param file: a path-like object or as file-like object to read from If this is a file-like object, is has to opened in binary @@ -371,7 +376,7 @@ def __init__( channel: int = 1, compression_level: int = -1, *args: Any, - **kwargs: Any + **kwargs: Any, ) -> None: """ :param file: a path-like object or as file-like object to write to diff --git a/can/io/canutils.py b/can/io/canutils.py index 0cca82eb8..f63df3f6b 100644 --- a/can/io/canutils.py +++ b/can/io/canutils.py @@ -5,11 +5,11 @@ """ import logging -from typing import Generator, TextIO, Union +from typing import Generator, TextIO, Union, Any from can.message import Message from .generic import FileIOMessageWriter, MessageReader -from ..typechecking import AcceptedIOType, StringPathLike +from ..typechecking import StringPathLike log = logging.getLogger("can.io.canutils") @@ -34,7 +34,12 @@ class CanutilsLogReader(MessageReader): file: TextIO - def __init__(self, file: Union[StringPathLike, TextIO]) -> None: + def __init__( + self, + file: Union[StringPathLike, TextIO], + *args: Any, + **kwargs: Any, + ) -> None: """ :param file: a path-like object or as file-like object to read from If this is a file-like object, is has to opened in text @@ -132,6 +137,8 @@ def __init__( file: Union[StringPathLike, TextIO], channel: str = "vcan0", append: bool = False, + *args: Any, + **kwargs: Any, ): """ :param file: a path-like object or as file-like object to write to diff --git a/can/io/csv.py b/can/io/csv.py index 0161b4f55..2e2f46699 100644 --- a/can/io/csv.py +++ b/can/io/csv.py @@ -10,7 +10,7 @@ """ from base64 import b64encode, b64decode -from typing import TextIO, Generator, Union +from typing import TextIO, Generator, Union, Any from can.message import Message from .generic import FileIOMessageWriter, MessageReader @@ -28,7 +28,12 @@ class CSVReader(MessageReader): file: TextIO - def __init__(self, file: Union[StringPathLike, TextIO]) -> None: + def __init__( + self, + file: Union[StringPathLike, TextIO], + *args: Any, + **kwargs: Any, + ) -> None: """ :param file: a path-like object or as file-like object to read from If this is a file-like object, is has to opened in text @@ -87,7 +92,11 @@ class CSVWriter(FileIOMessageWriter): file: TextIO def __init__( - self, file: Union[StringPathLike, TextIO], append: bool = False + self, + file: Union[StringPathLike, TextIO], + append: bool = False, + *args: Any, + **kwargs: Any, ) -> None: """ :param file: a path-like object or a file-like object to write to. diff --git a/can/io/generic.py b/can/io/generic.py index acdf367d6..d5c7a2057 100644 --- a/can/io/generic.py +++ b/can/io/generic.py @@ -7,6 +7,7 @@ Iterable, Type, ContextManager, + Any, ) from typing_extensions import Literal from types import TracebackType @@ -28,7 +29,11 @@ class BaseIOHandler(ContextManager, metaclass=ABCMeta): file: Optional[can.typechecking.FileLike] def __init__( - self, file: Optional[can.typechecking.AcceptedIOType], mode: str = "rt" + self, + file: Optional[can.typechecking.AcceptedIOType], + mode: str = "rt", + *args: Any, + **kwargs: Any ) -> None: """ :param file: a path-like object to open a file, a file-like object @@ -82,7 +87,13 @@ class FileIOMessageWriter(MessageWriter, metaclass=ABCMeta): file: can.typechecking.FileLike - def __init__(self, file: can.typechecking.AcceptedIOType, mode: str = "wt") -> None: + def __init__( + self, + file: can.typechecking.AcceptedIOType, + mode: str = "wt", + *args: Any, + **kwargs: Any + ) -> None: # Not possible with the type signature, but be verbose for user-friendliness if file is None: raise ValueError("The given file cannot be None") diff --git a/can/io/logger.py b/can/io/logger.py index 071eeb300..b50825d3f 100644 --- a/can/io/logger.py +++ b/can/io/logger.py @@ -303,8 +303,8 @@ class SizedRotatingLogger(BaseRotatingLogger): def __init__( self, base_filename: StringPathLike, - *args: Any, max_bytes: int = 0, + *args: Any, **kwargs: Any, ) -> None: """ diff --git a/can/io/printer.py b/can/io/printer.py index cafab3815..61871e8ad 100644 --- a/can/io/printer.py +++ b/can/io/printer.py @@ -4,7 +4,7 @@ import logging -from typing import Optional, TextIO, Union +from typing import Optional, TextIO, Union, Any from ..message import Message from .generic import MessageWriter @@ -26,7 +26,11 @@ class Printer(MessageWriter): file: Optional[TextIO] def __init__( - self, file: Optional[Union[StringPathLike, TextIO]] = None, append: bool = False + self, + file: Optional[Union[StringPathLike, TextIO]] = None, + append: bool = False, + *args: Any, + **kwargs: Any ) -> None: """ :param file: An optional path-like object or a file-like object to "print" diff --git a/can/io/sqlite.py b/can/io/sqlite.py index 98e870a84..b9cbf9f93 100644 --- a/can/io/sqlite.py +++ b/can/io/sqlite.py @@ -32,7 +32,13 @@ class SqliteReader(MessageReader): .. note:: The database schema is given in the documentation of the loggers. """ - def __init__(self, file: StringPathLike, table_name: str = "messages") -> None: + def __init__( + self, + file: StringPathLike, + table_name: str = "messages", + *args: Any, + **kwargs: Any, + ) -> None: """ :param file: a `str` path like object that points to the database file to use @@ -129,7 +135,11 @@ class SqliteWriter(MessageWriter, BufferedReader): """Maximum number of messages to buffer before writing to the database""" def __init__( - self, file: StringPathLike, table_name: str = "messages", **kwargs: Any + self, + file: StringPathLike, + table_name: str = "messages", + *args: Any, + **kwargs: Any, ) -> None: """ :param file: a `str` or path like object that points diff --git a/can/logger.py b/can/logger.py index dbf78e408..0b73dd785 100644 --- a/can/logger.py +++ b/can/logger.py @@ -21,6 +21,8 @@ from typing import Any, Dict, List, Union, Sequence, Tuple import can +from can.io import BaseRotatingLogger +from can.io.generic import MessageWriter from . import Bus, BusState, Logger, SizedRotatingLogger from .typechecking import CanFilter, CanFilters @@ -61,10 +63,10 @@ def _create_base_argument_parser(parser: argparse.ArgumentParser) -> None: parser.add_argument( "extra_args", nargs=argparse.REMAINDER, - help=r"The remaining arguments will be used for the interface " - r"initialisation. For example, `-i vector -c 1 --app-name=" - r"MyCanApp` is the equivalent to opening the bus with `Bus(" - r"'vector', channel=1, app_name='MyCanApp')", + help="The remaining arguments will be used for the interface and " + "logger/player initialisation. " + "For example, `-i vector -c 1 --app-name=MyCanApp` is the equivalent " + "to opening the bus with `Bus('vector', channel=1, app_name='MyCanApp')", ) @@ -224,7 +226,7 @@ def main() -> None: raise SystemExit(errno.EINVAL) results, unknown_args = parser.parse_known_args() - additional_config = _parse_additional_config(unknown_args) + additional_config = _parse_additional_config([*results.extra_args, *unknown_args]) bus = _create_bus(results, can_filters=_parse_filters(results), **additional_config) if results.active: @@ -235,13 +237,20 @@ def main() -> None: print(f"Connected to {bus.__class__.__name__}: {bus.channel_info}") print(f"Can Logger (Started on {datetime.now()})") - options = {"append": results.append} + logger: Union[MessageWriter, BaseRotatingLogger] if results.file_size: logger = SizedRotatingLogger( - base_filename=results.log_file, max_bytes=results.file_size, **options + base_filename=results.log_file, + max_bytes=results.file_size, + append=results.append, + **additional_config, ) else: - logger = Logger(filename=results.log_file, **options) # type: ignore + logger = Logger( + filename=results.log_file, + append=results.append, + **additional_config, + ) try: while True: diff --git a/can/player.py b/can/player.py index 72faa892a..c029981be 100644 --- a/can/player.py +++ b/can/player.py @@ -79,14 +79,14 @@ def main() -> None: raise SystemExit(errno.EINVAL) results, unknown_args = parser.parse_known_args() - additional_config = _parse_additional_config(unknown_args) + additional_config = _parse_additional_config([*results.extra_args, *unknown_args]) verbosity = results.verbosity error_frames = results.error_frames with _create_bus(results, **additional_config) as bus: - with LogReader(results.infile) as reader: + with LogReader(results.infile, **additional_config) as reader: in_sync = MessageSync( cast(Iterable[Message], reader), diff --git a/can/viewer.py b/can/viewer.py index 7be04949d..6773a0acd 100644 --- a/can/viewer.py +++ b/can/viewer.py @@ -540,7 +540,9 @@ def parse_args(args): else: data_structs[key] = struct.Struct(fmt) - additional_config = _parse_additional_config(unknown_args) + additional_config = _parse_additional_config( + [*parsed_args.extra_args, *unknown_args] + ) return parsed_args, can_filters, data_structs, additional_config