Skip to content

Commit c4f0789

Browse files
authored
Pass CLI extra_args to Logger initialisation (hardbyte#1366)
* pass CLI extra_args to Logger * pass extra_args to player
1 parent 3a7c80e commit c4f0789

11 files changed

Lines changed: 86 additions & 26 deletions

File tree

‎can/io/asc.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def __init__(
3939
file: Union[StringPathLike, TextIO],
4040
base: str = "hex",
4141
relative_timestamp: bool = True,
42+
*args: Any,
43+
**kwargs: Any,
4244
) -> None:
4345
"""
4446
:param file: a path-like object or as file-like object to read from
@@ -352,6 +354,7 @@ def __init__(
352354
self,
353355
file: Union[StringPathLike, TextIO],
354356
channel: int = 1,
357+
*args: Any,
355358
**kwargs: Any,
356359
) -> None:
357360
"""

‎can/io/blf.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,12 @@ class BLFReader(MessageReader):
143143

144144
file: BinaryIO
145145

146-
def __init__(self, file: Union[StringPathLike, BinaryIO]) -> None:
146+
def __init__(
147+
self,
148+
file: Union[StringPathLike, BinaryIO],
149+
*args: Any,
150+
**kwargs: Any,
151+
) -> None:
147152
"""
148153
:param file: a path-like object or as file-like object to read from
149154
If this is a file-like object, is has to opened in binary
@@ -371,7 +376,7 @@ def __init__(
371376
channel: int = 1,
372377
compression_level: int = -1,
373378
*args: Any,
374-
**kwargs: Any
379+
**kwargs: Any,
375380
) -> None:
376381
"""
377382
:param file: a path-like object or as file-like object to write to

‎can/io/canutils.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
"""
66

77
import logging
8-
from typing import Generator, TextIO, Union
8+
from typing import Generator, TextIO, Union, Any
99

1010
from can.message import Message
1111
from .generic import FileIOMessageWriter, MessageReader
12-
from ..typechecking import AcceptedIOType, StringPathLike
12+
from ..typechecking import StringPathLike
1313

1414
log = logging.getLogger("can.io.canutils")
1515

@@ -34,7 +34,12 @@ class CanutilsLogReader(MessageReader):
3434

3535
file: TextIO
3636

37-
def __init__(self, file: Union[StringPathLike, TextIO]) -> None:
37+
def __init__(
38+
self,
39+
file: Union[StringPathLike, TextIO],
40+
*args: Any,
41+
**kwargs: Any,
42+
) -> None:
3843
"""
3944
:param file: a path-like object or as file-like object to read from
4045
If this is a file-like object, is has to opened in text
@@ -132,6 +137,8 @@ def __init__(
132137
file: Union[StringPathLike, TextIO],
133138
channel: str = "vcan0",
134139
append: bool = False,
140+
*args: Any,
141+
**kwargs: Any,
135142
):
136143
"""
137144
:param file: a path-like object or as file-like object to write to

‎can/io/csv.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"""
1111

1212
from base64 import b64encode, b64decode
13-
from typing import TextIO, Generator, Union
13+
from typing import TextIO, Generator, Union, Any
1414

1515
from can.message import Message
1616
from .generic import FileIOMessageWriter, MessageReader
@@ -28,7 +28,12 @@ class CSVReader(MessageReader):
2828

2929
file: TextIO
3030

31-
def __init__(self, file: Union[StringPathLike, TextIO]) -> None:
31+
def __init__(
32+
self,
33+
file: Union[StringPathLike, TextIO],
34+
*args: Any,
35+
**kwargs: Any,
36+
) -> None:
3237
"""
3338
:param file: a path-like object or as file-like object to read from
3439
If this is a file-like object, is has to opened in text
@@ -87,7 +92,11 @@ class CSVWriter(FileIOMessageWriter):
8792
file: TextIO
8893

8994
def __init__(
90-
self, file: Union[StringPathLike, TextIO], append: bool = False
95+
self,
96+
file: Union[StringPathLike, TextIO],
97+
append: bool = False,
98+
*args: Any,
99+
**kwargs: Any,
91100
) -> None:
92101
"""
93102
:param file: a path-like object or a file-like object to write to.

‎can/io/generic.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Iterable,
88
Type,
99
ContextManager,
10+
Any,
1011
)
1112
from typing_extensions import Literal
1213
from types import TracebackType
@@ -28,7 +29,11 @@ class BaseIOHandler(ContextManager, metaclass=ABCMeta):
2829
file: Optional[can.typechecking.FileLike]
2930

3031
def __init__(
31-
self, file: Optional[can.typechecking.AcceptedIOType], mode: str = "rt"
32+
self,
33+
file: Optional[can.typechecking.AcceptedIOType],
34+
mode: str = "rt",
35+
*args: Any,
36+
**kwargs: Any
3237
) -> None:
3338
"""
3439
:param file: a path-like object to open a file, a file-like object
@@ -82,7 +87,13 @@ class FileIOMessageWriter(MessageWriter, metaclass=ABCMeta):
8287

8388
file: can.typechecking.FileLike
8489

85-
def __init__(self, file: can.typechecking.AcceptedIOType, mode: str = "wt") -> None:
90+
def __init__(
91+
self,
92+
file: can.typechecking.AcceptedIOType,
93+
mode: str = "wt",
94+
*args: Any,
95+
**kwargs: Any
96+
) -> None:
8697
# Not possible with the type signature, but be verbose for user-friendliness
8798
if file is None:
8899
raise ValueError("The given file cannot be None")

‎can/io/logger.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ class SizedRotatingLogger(BaseRotatingLogger):
303303
def __init__(
304304
self,
305305
base_filename: StringPathLike,
306-
*args: Any,
307306
max_bytes: int = 0,
307+
*args: Any,
308308
**kwargs: Any,
309309
) -> None:
310310
"""

‎can/io/printer.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import logging
66

7-
from typing import Optional, TextIO, Union
7+
from typing import Optional, TextIO, Union, Any
88

99
from ..message import Message
1010
from .generic import MessageWriter
@@ -26,7 +26,11 @@ class Printer(MessageWriter):
2626
file: Optional[TextIO]
2727

2828
def __init__(
29-
self, file: Optional[Union[StringPathLike, TextIO]] = None, append: bool = False
29+
self,
30+
file: Optional[Union[StringPathLike, TextIO]] = None,
31+
append: bool = False,
32+
*args: Any,
33+
**kwargs: Any
3034
) -> None:
3135
"""
3236
:param file: An optional path-like object or a file-like object to "print"

‎can/io/sqlite.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ class SqliteReader(MessageReader):
3232
.. note:: The database schema is given in the documentation of the loggers.
3333
"""
3434

35-
def __init__(self, file: StringPathLike, table_name: str = "messages") -> None:
35+
def __init__(
36+
self,
37+
file: StringPathLike,
38+
table_name: str = "messages",
39+
*args: Any,
40+
**kwargs: Any,
41+
) -> None:
3642
"""
3743
:param file: a `str` path like object that points
3844
to the database file to use
@@ -129,7 +135,11 @@ class SqliteWriter(MessageWriter, BufferedReader):
129135
"""Maximum number of messages to buffer before writing to the database"""
130136

131137
def __init__(
132-
self, file: StringPathLike, table_name: str = "messages", **kwargs: Any
138+
self,
139+
file: StringPathLike,
140+
table_name: str = "messages",
141+
*args: Any,
142+
**kwargs: Any,
133143
) -> None:
134144
"""
135145
:param file: a `str` or path like object that points

‎can/logger.py‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from typing import Any, Dict, List, Union, Sequence, Tuple
2222

2323
import can
24+
from can.io import BaseRotatingLogger
25+
from can.io.generic import MessageWriter
2426
from . import Bus, BusState, Logger, SizedRotatingLogger
2527
from .typechecking import CanFilter, CanFilters
2628

@@ -61,10 +63,10 @@ def _create_base_argument_parser(parser: argparse.ArgumentParser) -> None:
6163
parser.add_argument(
6264
"extra_args",
6365
nargs=argparse.REMAINDER,
64-
help=r"The remaining arguments will be used for the interface "
65-
r"initialisation. For example, `-i vector -c 1 --app-name="
66-
r"MyCanApp` is the equivalent to opening the bus with `Bus("
67-
r"'vector', channel=1, app_name='MyCanApp')",
66+
help="The remaining arguments will be used for the interface and "
67+
"logger/player initialisation. "
68+
"For example, `-i vector -c 1 --app-name=MyCanApp` is the equivalent "
69+
"to opening the bus with `Bus('vector', channel=1, app_name='MyCanApp')",
6870
)
6971

7072

@@ -224,7 +226,7 @@ def main() -> None:
224226
raise SystemExit(errno.EINVAL)
225227

226228
results, unknown_args = parser.parse_known_args()
227-
additional_config = _parse_additional_config(unknown_args)
229+
additional_config = _parse_additional_config([*results.extra_args, *unknown_args])
228230
bus = _create_bus(results, can_filters=_parse_filters(results), **additional_config)
229231

230232
if results.active:
@@ -235,13 +237,20 @@ def main() -> None:
235237
print(f"Connected to {bus.__class__.__name__}: {bus.channel_info}")
236238
print(f"Can Logger (Started on {datetime.now()})")
237239

238-
options = {"append": results.append}
240+
logger: Union[MessageWriter, BaseRotatingLogger]
239241
if results.file_size:
240242
logger = SizedRotatingLogger(
241-
base_filename=results.log_file, max_bytes=results.file_size, **options
243+
base_filename=results.log_file,
244+
max_bytes=results.file_size,
245+
append=results.append,
246+
**additional_config,
242247
)
243248
else:
244-
logger = Logger(filename=results.log_file, **options) # type: ignore
249+
logger = Logger(
250+
filename=results.log_file,
251+
append=results.append,
252+
**additional_config,
253+
)
245254

246255
try:
247256
while True:

‎can/player.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ def main() -> None:
7979
raise SystemExit(errno.EINVAL)
8080

8181
results, unknown_args = parser.parse_known_args()
82-
additional_config = _parse_additional_config(unknown_args)
82+
additional_config = _parse_additional_config([*results.extra_args, *unknown_args])
8383

8484
verbosity = results.verbosity
8585

8686
error_frames = results.error_frames
8787

8888
with _create_bus(results, **additional_config) as bus:
89-
with LogReader(results.infile) as reader:
89+
with LogReader(results.infile, **additional_config) as reader:
9090

9191
in_sync = MessageSync(
9292
cast(Iterable[Message], reader),

0 commit comments

Comments
 (0)