Skip to content

Commit 1430154

Browse files
authored
Enhance can.logger to consider the append option (#1327)
* Add passage of **options to the logger initialization * Add -a boolean option to the parser * Add manual arg parse for boolean variables This solution is only required until Python>=3.9. * Fix the index-error check * Bolster help documentation for new -a option * Clean up variable names, notes, etc. * Whitespace formatting (PEP-8 style) * Make formatting recomendations based on `pylint logger.py` * Format code with black logger.py to stop failing the `black` check * Reduce complexity of append arg parse incorporation * Fix format with `black can/logger.py` * Change append argparse access from `results.a` to `results.append` * Add `dest` option to append argument
1 parent 0c4dee0 commit 1430154

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

can/logger.py

+27-14
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ def _create_base_argument_parser(parser: argparse.ArgumentParser) -> None:
3131
parser.add_argument(
3232
"-c",
3333
"--channel",
34-
help='''Most backend interfaces require some sort of channel.
35-
For example with the serial interface the channel might be a rfcomm device: "/dev/rfcomm0"
36-
With the socketcan interfaces valid channel examples include: "can0", "vcan0"''',
34+
help=r"Most backend interfaces require some sort of channel. For "
35+
r"example with the serial interface the channel might be a rfcomm"
36+
r' device: "/dev/rfcomm0". With the socketcan interface valid '
37+
r'channel examples include: "can0", "vcan0".',
3738
)
3839

3940
parser.add_argument(
@@ -60,11 +61,10 @@ def _create_base_argument_parser(parser: argparse.ArgumentParser) -> None:
6061
parser.add_argument(
6162
"extra_args",
6263
nargs=argparse.REMAINDER,
63-
help="""\
64-
The remaining arguments will be used for the interface initialisation.
65-
For example, `-i vector -c 1 --app-name=MyCanApp` is the equivalent to
66-
opening the bus with `Bus('vector', channel=1, app_name='MyCanApp')`
67-
""",
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')",
6868
)
6969

7070

@@ -82,8 +82,10 @@ def _append_filter_argument(
8282
*args,
8383
"--filter",
8484
help="R|Space separated CAN filters for the given CAN interface:"
85-
"\n <can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)"
86-
"\n <can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)"
85+
"\n <can_id>:<can_mask> (matches when <received_can_id> & mask =="
86+
" can_id & mask)"
87+
"\n <can_id>~<can_mask> (matches when <received_can_id> & mask !="
88+
" can_id & mask)"
8789
"\nFx to show only frames with ID 0x100 to 0x103 and 0x200 to 0x20F:"
8890
"\n python -m can.viewer -f 100:7FC 200:7F0"
8991
"\nNote that the ID and mask are always interpreted as hex values",
@@ -141,7 +143,8 @@ def _parse_additonal_config(unknown_args):
141143

142144
def main() -> None:
143145
parser = argparse.ArgumentParser(
144-
description="Log CAN traffic, printing messages to stdout or to a given file.",
146+
description="Log CAN traffic, printing messages to stdout or to a "
147+
"given file.",
145148
)
146149

147150
_create_base_argument_parser(parser)
@@ -154,12 +157,21 @@ def main() -> None:
154157
default=None,
155158
)
156159

160+
parser.add_argument(
161+
"-a",
162+
"--append",
163+
dest="append",
164+
help="Append to the log file if it already exists.",
165+
action="store_true",
166+
)
167+
157168
parser.add_argument(
158169
"-s",
159170
"--file_size",
160171
dest="file_size",
161172
type=int,
162-
help="Maximum file size in bytes. Rotate log file when size threshold is reached.",
173+
help="Maximum file size in bytes. Rotate log file when size threshold "
174+
"is reached.",
163175
default=None,
164176
)
165177

@@ -201,12 +213,13 @@ def main() -> None:
201213
print(f"Connected to {bus.__class__.__name__}: {bus.channel_info}")
202214
print(f"Can Logger (Started on {datetime.now()})")
203215

216+
options = {"append": results.append}
204217
if results.file_size:
205218
logger = SizedRotatingLogger(
206-
base_filename=results.log_file, max_bytes=results.file_size
219+
base_filename=results.log_file, max_bytes=results.file_size, **options
207220
)
208221
else:
209-
logger = Logger(filename=results.log_file) # type: ignore
222+
logger = Logger(filename=results.log_file, **options) # type: ignore
210223

211224
try:
212225
while True:

0 commit comments

Comments
 (0)