Skip to content

Commit 30202b7

Browse files
committed
factor out some common parts
1 parent 47dee13 commit 30202b7

2 files changed

Lines changed: 48 additions & 66 deletions

File tree

can/logger.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,52 @@
2424
from can import Bus, BusState, Logger, SizedRotatingLogger
2525

2626

27-
def main():
27+
def _create_base_argument_parser(parser: argparse.ArgumentParser):
28+
"""Adds common options to an argument parser."""
29+
30+
parser.add_argument(
31+
"-c",
32+
"--channel",
33+
help='''Most backend interfaces require some sort of channel.
34+
For example with the serial interface the channel might be a rfcomm device: "/dev/rfcomm0"
35+
With the socketcan interfaces valid channel examples include: "can0", "vcan0"''',
36+
)
37+
38+
parser.add_argument(
39+
"-i",
40+
"--interface",
41+
dest="interface",
42+
help="""Specify the backend CAN interface to use. If left blank,
43+
fall back to reading from configuration files.""",
44+
choices=can.VALID_INTERFACES,
45+
)
46+
47+
parser.add_argument(
48+
"-b", "--bitrate", type=int, help="Bitrate to use for the CAN bus."
49+
)
50+
51+
parser.add_argument("--fd", help="Activate CAN-FD support", action="store_true")
52+
53+
parser.add_argument(
54+
"--data_bitrate",
55+
type=int,
56+
help="Bitrate to use for the data phase in case of CAN-FD.",
57+
)
58+
59+
60+
def main() -> None:
2861
parser = argparse.ArgumentParser(
2962
"python -m can.logger",
3063
description="Log CAN traffic, printing messages to stdout or to a given file.",
3164
)
3265

66+
_create_base_argument_parser(parser)
67+
3368
parser.add_argument(
3469
"-f",
3570
"--file_name",
3671
dest="log_file",
37-
help="""Path and base log filename, for supported types see can.Logger.""",
72+
help="Path and base log filename, for supported types see can.Logger.",
3873
default=None,
3974
)
4075

@@ -43,7 +78,7 @@ def main():
4378
"--file_size",
4479
dest="file_size",
4580
type=int,
46-
help="""Maximum file size in bytes. Rotate log file when size threshold is reached.""",
81+
help="Maximum file size in bytes. Rotate log file when size threshold is reached.",
4782
default=None,
4883
)
4984

@@ -56,23 +91,6 @@ def main():
5691
default=2,
5792
)
5893

59-
parser.add_argument(
60-
"-c",
61-
"--channel",
62-
help='''Most backend interfaces require some sort of channel.
63-
For example with the serial interface the channel might be a rfcomm device: "/dev/rfcomm0"
64-
With the socketcan interfaces valid channel examples include: "can0", "vcan0"''',
65-
)
66-
67-
parser.add_argument(
68-
"-i",
69-
"--interface",
70-
dest="interface",
71-
help="""Specify the backend CAN interface to use. If left blank,
72-
fall back to reading from configuration files.""",
73-
choices=can.VALID_INTERFACES,
74-
)
75-
7694
parser.add_argument(
7795
"--filter",
7896
help="""Comma separated filters can be specified for the given CAN interface:
@@ -83,18 +101,6 @@ def main():
83101
default="",
84102
)
85103

86-
parser.add_argument(
87-
"-b", "--bitrate", type=int, help="""Bitrate to use for the CAN bus."""
88-
)
89-
90-
parser.add_argument("--fd", help="Activate CAN-FD support", action="store_true")
91-
92-
parser.add_argument(
93-
"--data_bitrate",
94-
type=int,
95-
help="""Bitrate to use for the data phase in case of CAN-FD.""",
96-
)
97-
98104
state_group = parser.add_mutually_exclusive_group(required=False)
99105
state_group.add_argument(
100106
"--active",
@@ -105,7 +111,7 @@ def main():
105111
"--passive", help="Start the bus as passive.", action="store_true"
106112
)
107113

108-
# print help message when no arguments wre given
114+
# print help message when no arguments were given
109115
if len(sys.argv) < 2:
110116
parser.print_help(sys.stderr)
111117
raise SystemExit(errno.EINVAL)

can/player.py

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@
1414
from can import Bus, LogReader, MessageSync
1515

1616

17-
def main():
17+
from .logger import _create_base_argument_parser
18+
19+
20+
def main() -> None:
1821
parser = argparse.ArgumentParser(
1922
"python -m can.player", description="Replay CAN traffic."
2023
)
2124

25+
_create_base_argument_parser(parser)
26+
2227
parser.add_argument(
2328
"-f",
2429
"--file_name",
2530
dest="log_file",
26-
help="""Path and base log filename, for supported types see can.LogReader.""",
31+
help="Path and base log filename, for supported types see can.LogReader.",
2732
default=None,
2833
)
2934

@@ -36,35 +41,6 @@ def main():
3641
default=2,
3742
)
3843

39-
parser.add_argument(
40-
"-c",
41-
"--channel",
42-
help='''Most backend interfaces require some sort of channel.
43-
For example with the serial interface the channel might be a rfcomm device: "/dev/rfcomm0"
44-
With the socketcan interfaces valid channel examples include: "can0", "vcan0"''',
45-
)
46-
47-
parser.add_argument(
48-
"-i",
49-
"--interface",
50-
dest="interface",
51-
help="""Specify the backend CAN interface to use. If left blank,
52-
fall back to reading from configuration files.""",
53-
choices=can.VALID_INTERFACES,
54-
)
55-
56-
parser.add_argument(
57-
"-b", "--bitrate", type=int, help="""Bitrate to use for the CAN bus."""
58-
)
59-
60-
parser.add_argument("--fd", help="Activate CAN-FD support", action="store_true")
61-
62-
parser.add_argument(
63-
"--data_bitrate",
64-
type=int,
65-
help="""Bitrate to use for the data phase in case of CAN-FD.""",
66-
)
67-
6844
parser.add_argument(
6945
"--ignore-timestamps",
7046
dest="timestamps",
@@ -82,15 +58,15 @@ def main():
8258
"-g",
8359
"--gap",
8460
type=float,
85-
help="""<s> minimum time between replayed frames""",
61+
help="<s> minimum time between replayed frames",
8662
default=0.0001,
8763
)
8864
parser.add_argument(
8965
"-s",
9066
"--skip",
9167
type=float,
9268
default=60 * 60 * 24,
93-
help="""<s> skip gaps greater than 's' seconds""",
69+
help="<s> skip gaps greater than 's' seconds",
9470
)
9571

9672
parser.add_argument(

0 commit comments

Comments
 (0)