Skip to content

Commit 63f11fb

Browse files
committed
FEATURE: validate the --reboot-time parameter range
1 parent a4728d1 commit 63f11fb

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# SPDX-FileCopyrightText: 2024 Dmitriy Kovalev
16+
17+
# SPDX-License-Identifier: Apache-2.0
18+
19+
# https://gist.github.com/dmitriykovalev/2ab1aa33a8099ef2d514925d84aa89e7
20+
21+
from argparse import Action
22+
from argparse import ArgumentError
23+
from operator import gt
24+
from operator import ge
25+
from operator import lt
26+
from operator import le
27+
28+
29+
class CheckRange(Action):
30+
ops = {'inf': gt,
31+
'min': ge,
32+
'sup': lt,
33+
'max': le}
34+
35+
def __init__(self, *args, **kwargs):
36+
if 'min' in kwargs and 'inf' in kwargs:
37+
raise ValueError('either min or inf, but not both')
38+
if 'max' in kwargs and 'sup' in kwargs:
39+
raise ValueError('either max or sup, but not both')
40+
41+
for name in self.ops:
42+
if name in kwargs:
43+
setattr(self, name, kwargs.pop(name))
44+
45+
super().__init__(*args, **kwargs)
46+
47+
def interval(self):
48+
if hasattr(self, 'min'):
49+
l = f'[{self.min}'
50+
elif hasattr(self, 'inf'):
51+
l = f'({self.inf}'
52+
else:
53+
l = '(-infinity'
54+
55+
if hasattr(self, 'max'):
56+
u = f'{self.max}]'
57+
elif hasattr(self, 'sup'):
58+
u = f'{self.sup})'
59+
else:
60+
u = '+infinity)'
61+
62+
return f'valid range: {l}, {u}'
63+
64+
def __call__(self, parser, namespace, values, option_string=None):
65+
for name, op in self.ops.items():
66+
if hasattr(self, name) and not op(values, getattr(self, name)):
67+
raise ArgumentError(self, self.interval())
68+
setattr(namespace, self.dest, values)

MethodicConfigurator/backend_flightcontroller.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
from MethodicConfigurator.backend_flightcontroller_info import BackendFlightcontrollerInfo
3131

32+
from MethodicConfigurator.argparse_check_range import CheckRange
33+
34+
3235
# adding all this allows pyinstaller to build a working windows executable
3336
# note that using --hidden-import does not work for these modules
3437
try:
@@ -431,6 +434,9 @@ def add_argparse_arguments(parser):
431434
)
432435
parser.add_argument('-r', '--reboot-time',
433436
type=int,
437+
min=5,
438+
max=50,
439+
action=CheckRange,
434440
default=7,
435441
help='Flight controller reboot time. '
436442
'Default is %(default)s')

0 commit comments

Comments
 (0)