| 
1 | 1 | #!/usr/bin/env python3  | 
2 | 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.  | 
 | 3 | +'''  | 
 | 4 | +This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator  | 
14 | 5 | 
  | 
15 |  | -# SPDX-FileCopyrightText: 2024 Dmitriy Kovalev  | 
 | 6 | +SPDX-FileCopyrightText: 2024 Dmitriy Kovalev  | 
16 | 7 | 
  | 
17 |  | -# SPDX-License-Identifier: Apache-2.0  | 
 | 8 | +SPDX-License-Identifier: Apache-2.0  | 
18 | 9 | 
  | 
19 |  | -# https://gist.github.com/dmitriykovalev/2ab1aa33a8099ef2d514925d84aa89e7  | 
 | 10 | +https://gist.github.com/dmitriykovalev/2ab1aa33a8099ef2d514925d84aa89e7  | 
 | 11 | +'''  | 
20 | 12 | 
 
  | 
21 | 13 | from argparse import Action  | 
22 | 14 | from argparse import ArgumentError  | 
 | 
27 | 19 | 
 
  | 
28 | 20 | 
 
  | 
29 | 21 | 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)  | 
 | 22 | +    '''  | 
 | 23 | +    Check if the Argparse argument value is within the specified range  | 
 | 24 | +    '''  | 
 | 25 | +    ops = {"inf": gt,  | 
 | 26 | +           "min": ge,  | 
 | 27 | +           "sup": lt,  | 
 | 28 | +           "max": le}  | 
 | 29 | + | 
 | 30 | +    def __init__(self, *args, **kwargs):  | 
 | 31 | +        if "min" in kwargs and "inf" in kwargs:  | 
 | 32 | +            raise ValueError("either min or inf, but not both")  | 
 | 33 | +        if "max" in kwargs and "sup" in kwargs:  | 
 | 34 | +            raise ValueError("either max or sup, but not both")  | 
 | 35 | + | 
 | 36 | +        for name in self.ops:  | 
 | 37 | +            if name in kwargs:  | 
 | 38 | +                setattr(self, name, kwargs.pop(name))  | 
 | 39 | + | 
 | 40 | +        super().__init__(*args, **kwargs)  | 
 | 41 | + | 
 | 42 | +    def interval(self):  | 
 | 43 | +        if hasattr(self, "min"):  | 
 | 44 | +            lo = f"[{self.min}"  | 
 | 45 | +        elif hasattr(self, "inf"):  | 
 | 46 | +            lo = f"({self.inf}"  | 
 | 47 | +        else:  | 
 | 48 | +            lo = "(-infinity"  | 
 | 49 | + | 
 | 50 | +        if hasattr(self, "max"):  | 
 | 51 | +            up = f"{self.max}]"  | 
 | 52 | +        elif hasattr(self, "sup"):  | 
 | 53 | +            up = f"{self.sup})"  | 
 | 54 | +        else:  | 
 | 55 | +            up = "+infinity)"  | 
 | 56 | + | 
 | 57 | +        return f"valid range: {lo}, {up}"  | 
 | 58 | + | 
 | 59 | +    def __call__(self, parser, namespace, values, option_string=None):  | 
 | 60 | +        for name, op in self.ops.items():  | 
 | 61 | +            if hasattr(self, name) and not op(values, getattr(self, name)):  | 
 | 62 | +                raise ArgumentError(self, self.interval())  | 
 | 63 | +        setattr(namespace, self.dest, values)  | 
0 commit comments