Skip to content

Commit 8dfa6e8

Browse files
committed
Added option group helpers
1 parent ec436be commit 8dfa6e8

File tree

9 files changed

+78
-92
lines changed

9 files changed

+78
-92
lines changed

src/instrumentman/geocomtest/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
from click_extra import extra_command
44

55
from ..utils import (
6-
com_baud_option,
7-
com_timeout_option,
6+
com_option_group,
87
com_port_argument
98
)
109

@@ -15,8 +14,7 @@
1514
context_settings={"auto_envvar_prefix": None}
1615
) # type: ignore[misc]
1716
@com_port_argument()
18-
@com_baud_option()
19-
@com_timeout_option()
17+
@com_option_group()
2018
def cli(**kwargs: Any) -> None:
2119
"""Test the availability of various GeoCom protocol functions on an
2220
instrument."""

src/instrumentman/geocomtest/app.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ def tests(tps: GeoCom) -> None:
5858
def main(
5959
port: str,
6060
baud: int = 9600,
61-
timeout: int = 15
61+
timeout: int = 15,
62+
retry: int = 1,
63+
sync_after_timeout: bool = False
6264
) -> None:
6365
try:
6466
with open_serial(
6567
port,
6668
speed=baud,
67-
timeout=timeout
69+
timeout=timeout,
70+
retry=retry,
71+
sync_after_timeout=sync_after_timeout
6872
) as com:
6973
tps = GeoCom(com)
7074
tests(tps)

src/instrumentman/inclination/__init__.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
extra_command,
55
argument,
66
option,
7-
option_group,
87
IntRange,
98
File,
109
file_path
1110
)
1211

1312
from ..utils import (
14-
com_baud_option,
15-
com_timeout_option,
13+
com_option_group,
1614
com_port_argument
1715
)
1816

@@ -23,12 +21,7 @@
2321
context_settings={"auto_envvar_prefix": None}
2422
) # type: ignore[misc]
2523
@com_port_argument()
26-
@option_group(
27-
"Connection options",
28-
"",
29-
com_baud_option(),
30-
com_timeout_option()
31-
)
24+
@com_option_group()
3225
@option(
3326
"-o",
3427
"--output",

src/instrumentman/inclination/app.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,17 @@ def main_measure(
5656
port: str,
5757
baud: int = 9600,
5858
timeout: int = 15,
59+
retry: int = 1,
60+
sync_after_timeout: bool = False,
5961
output: TextIOWrapper | None = None,
6062
positions: int = 1,
6163
zero: bool = False,
6264
cycles: int = 1
6365
) -> None:
64-
6566
with open_serial(
6667
port,
67-
retry=2,
68-
sync_after_timeout=True,
68+
retry=retry,
69+
sync_after_timeout=sync_after_timeout,
6970
speed=baud,
7071
timeout=timeout
7172
) as com:

src/instrumentman/morse/__init__.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
from click_extra import (
44
extra_command,
55
option,
6-
option_group,
76
argument,
87
Choice,
98
IntRange
109
)
1110

1211
from ..utils import (
13-
com_baud_option,
14-
com_timeout_option,
12+
com_option_group,
1513
com_port_argument
1614
)
1715

@@ -53,12 +51,7 @@
5351
help="suppress encoding errors and skip non-ASCII characters",
5452
is_flag=True
5553
)
56-
@option_group(
57-
"Connection options",
58-
"Options related to the serial connection",
59-
com_baud_option(),
60-
com_timeout_option()
61-
)
54+
@com_option_group()
6255
def cli(**kwargs: Any) -> None:
6356
"""Play a Morse encoded ASCII message through the beep signals
6457
of a GeoCom capable total station.

src/instrumentman/morse/app.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ def main(
124124
ignore_non_ascii: bool = False,
125125
baud: int = 9600,
126126
timeout: int = 15,
127+
retry: int = 1,
128+
sync_after_timeout: bool = False,
127129
unittime: int = 50,
128130
compatibility: str = "none",
129131
) -> None:
@@ -134,7 +136,13 @@ def main(
134136
echo_red("The message contains non-ASCII characters.")
135137
exit(1)
136138

137-
with open_serial(port, speed=baud, timeout=timeout) as com:
139+
with open_serial(
140+
port,
141+
speed=baud,
142+
timeout=timeout,
143+
retry=retry,
144+
sync_after_timeout=sync_after_timeout
145+
) as com:
138146
tps = GeoCom(com)
139147
beepstart = tps.bmm.beep_start
140148
beepstop = tps.bmm.beep_stop

src/instrumentman/setmeasurement/__init__.py

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
extra_command,
55
argument,
66
option,
7-
option_group,
87
Choice,
98
IntRange,
109
file_path,
1110
dir_path
1211
)
13-
from cloup.constraints import mutually_exclusive
1412

1513
from ..utils import (
16-
com_baud_option,
17-
com_timeout_option,
14+
com_option_group,
15+
logging_option_group,
1816
com_port_argument
1917
)
2018

@@ -35,24 +33,7 @@
3533
type=dir_path(),
3634
help="directory to save measurement output to"
3735
)
38-
@option_group(
39-
"Connection options",
40-
"Options related to the serial connection",
41-
com_baud_option(),
42-
com_timeout_option(),
43-
option(
44-
"-r",
45-
"--retry",
46-
help="number of connection retry attempts",
47-
type=IntRange(min=0, max=10),
48-
default=1
49-
),
50-
option(
51-
"--sync-after-timeout",
52-
help="attempt to synchronize message que after a connection timeout",
53-
is_flag=True
54-
)
55-
)
36+
@com_option_group()
5637
@option(
5738
"-f",
5839
"--format",
@@ -93,27 +74,7 @@
9374
),
9475
default=""
9576
)
96-
@option_group(
97-
"Logging options",
98-
"Options related to the logging functionalities.",
99-
option(
100-
"--debug",
101-
is_flag=True
102-
),
103-
option(
104-
"--info",
105-
is_flag=True
106-
),
107-
option(
108-
"--warning",
109-
is_flag=True
110-
),
111-
option(
112-
"--error",
113-
is_flag=True
114-
),
115-
constraint=mutually_exclusive
116-
)
77+
@logging_option_group()
11778
def cli_measure(**kwargs: Any) -> None:
11879
"""Run sets of measurements to predefined targets."""
11980
from .measure import main

src/instrumentman/setup/__init__.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
extra_command,
55
argument,
66
option,
7-
option_group,
87
IntRange,
98
Choice
109
)
1110

1211
from ..utils import (
13-
com_baud_option,
14-
com_timeout_option,
12+
com_option_group,
1513
com_port_argument
1614
)
1715

@@ -30,24 +28,7 @@
3028
),
3129
type=str
3230
)
33-
@option_group(
34-
"Connection options",
35-
"Options related to the serial connection",
36-
com_baud_option(),
37-
com_timeout_option(),
38-
option(
39-
"-r",
40-
"--retry",
41-
help="number of connection retry attempts",
42-
type=IntRange(min=0, max=10),
43-
default=1
44-
),
45-
option(
46-
"--sync-after-timeout",
47-
help="attempt to synchronize message que after a connection timeout",
48-
is_flag=True
49-
)
50-
)
31+
@com_option_group()
5132
def cli_measure(**kwargs: Any) -> None:
5233
"""Measure target points.
5334

src/instrumentman/utils.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
echo,
88
style,
99
option,
10+
option_group,
1011
argument,
1112
Choice,
1213
IntRange
1314
)
15+
from cloup.constraints import mutually_exclusive
1416

1517

1618
F = TypeVar('F', bound=Callable[..., Any])
@@ -75,6 +77,51 @@ def com_baud_option() -> Callable[[F], F]:
7577
)
7678

7779

80+
def com_option_group() -> Callable[[F], F]:
81+
return option_group(
82+
"Connection options",
83+
"Options related to the serial connection",
84+
com_baud_option(),
85+
com_timeout_option(),
86+
option(
87+
"-r",
88+
"--retry",
89+
help="number of connection retry attempts",
90+
type=IntRange(min=0, max=10),
91+
default=1
92+
),
93+
option(
94+
"--sync-after-timeout",
95+
help="attempt to synchronize message que after a timeout",
96+
is_flag=True
97+
)
98+
)
99+
100+
101+
def logging_option_group() -> Callable[[F], F]:
102+
return option_group(
103+
"Logging options",
104+
"Options related to the logging functionalities.",
105+
option(
106+
"--debug",
107+
is_flag=True
108+
),
109+
option(
110+
"--info",
111+
is_flag=True
112+
),
113+
option(
114+
"--warning",
115+
is_flag=True
116+
),
117+
option(
118+
"--error",
119+
is_flag=True
120+
),
121+
constraint=mutually_exclusive
122+
)
123+
124+
78125
def echo_color(
79126
message: Any,
80127
color: str,

0 commit comments

Comments
 (0)