From a8c32127df79a34814d1e88f53dffc4066f2e8dd Mon Sep 17 00:00:00 2001 From: "Dr.-Ing. Amilcar do Carmo Lucas" Date: Thu, 1 Aug 2024 18:15:18 +0200 Subject: [PATCH] BUGFIX: pylint argparse_check_range.py --- MethodicConfigurator/argparse_check_range.py | 101 +++++++++---------- credits/CREDITS.md | 3 +- 2 files changed, 50 insertions(+), 54 deletions(-) diff --git a/MethodicConfigurator/argparse_check_range.py b/MethodicConfigurator/argparse_check_range.py index 46120b6f..849e15d9 100644 --- a/MethodicConfigurator/argparse_check_range.py +++ b/MethodicConfigurator/argparse_check_range.py @@ -1,22 +1,14 @@ #!/usr/bin/env python3 -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +''' +This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator -# SPDX-FileCopyrightText: 2024 Dmitriy Kovalev +SPDX-FileCopyrightText: 2024 Dmitriy Kovalev -# SPDX-License-Identifier: Apache-2.0 +SPDX-License-Identifier: Apache-2.0 -# https://gist.github.com/dmitriykovalev/2ab1aa33a8099ef2d514925d84aa89e7 +https://gist.github.com/dmitriykovalev/2ab1aa33a8099ef2d514925d84aa89e7 +''' from argparse import Action from argparse import ArgumentError @@ -27,42 +19,45 @@ class CheckRange(Action): - ops = {'inf': gt, - 'min': ge, - 'sup': lt, - 'max': le} - - def __init__(self, *args, **kwargs): - if 'min' in kwargs and 'inf' in kwargs: - raise ValueError('either min or inf, but not both') - if 'max' in kwargs and 'sup' in kwargs: - raise ValueError('either max or sup, but not both') - - for name in self.ops: - if name in kwargs: - setattr(self, name, kwargs.pop(name)) - - super().__init__(*args, **kwargs) - - def interval(self): - if hasattr(self, 'min'): - l = f'[{self.min}' - elif hasattr(self, 'inf'): - l = f'({self.inf}' - else: - l = '(-infinity' - - if hasattr(self, 'max'): - u = f'{self.max}]' - elif hasattr(self, 'sup'): - u = f'{self.sup})' - else: - u = '+infinity)' - - return f'valid range: {l}, {u}' - - def __call__(self, parser, namespace, values, option_string=None): - for name, op in self.ops.items(): - if hasattr(self, name) and not op(values, getattr(self, name)): - raise ArgumentError(self, self.interval()) - setattr(namespace, self.dest, values) + ''' + Check if the Argparse argument value is within the specified range + ''' + ops = {"inf": gt, + "min": ge, + "sup": lt, + "max": le} + + def __init__(self, *args, **kwargs): + if "min" in kwargs and "inf" in kwargs: + raise ValueError("either min or inf, but not both") + if "max" in kwargs and "sup" in kwargs: + raise ValueError("either max or sup, but not both") + + for name in self.ops: + if name in kwargs: + setattr(self, name, kwargs.pop(name)) + + super().__init__(*args, **kwargs) + + def interval(self): + if hasattr(self, "min"): + lo = f"[{self.min}" + elif hasattr(self, "inf"): + lo = f"({self.inf}" + else: + lo = "(-infinity" + + if hasattr(self, "max"): + up = f"{self.max}]" + elif hasattr(self, "sup"): + up = f"{self.sup})" + else: + up = "+infinity)" + + return f"valid range: {lo}, {up}" + + def __call__(self, parser, namespace, values, option_string=None): + for name, op in self.ops.items(): + if hasattr(self, name) and not op(values, getattr(self, name)): + raise ArgumentError(self, self.interval()) + setattr(namespace, self.dest, values) diff --git a/credits/CREDITS.md b/credits/CREDITS.md index 85ded19b..ceca1aac 100644 --- a/credits/CREDITS.md +++ b/credits/CREDITS.md @@ -7,7 +7,7 @@ SPDX-License-Identifier: GPL-3.0-or-later # Licenses We use [REUSE software API](https://api.reuse.software/) in the form of SPDX tags in most of the files to explicitly declare Copyright and License. -However we do not do it for every single file and hence are [![REUSE status](https://api.reuse.software/badge/github.com/ArduPilot/MethodicConfigurator)](https://api.reuse.software/info/github.com/ArduPilot/MethodicConfigurator) +However, we do not do it for every single file and hence are [![REUSE status](https://api.reuse.software/badge/github.com/ArduPilot/MethodicConfigurator)](https://api.reuse.software/info/github.com/ArduPilot/MethodicConfigurator) This software is licensed under the [GNU General Public License v3.0](../LICENSE.md) and is built on top of (depends on) other open-source software. We are thankful to the developers of those software packages. @@ -30,6 +30,7 @@ It directly uses: | [pyserial](https://pyserial.readthedocs.io/en/latest/pyserial.html) | [BSD License](https://github.com/pyserial/pyserial/blob/master/LICENSE.txt) | | [Scrollable TK frame](https://gist.github.com/mp035/9f2027c3ef9172264532fcd6262f3b01) by Mark Pointing | [Mozilla Public License, v. 2.0](https://mozilla.org/MPL/2.0/) | | [Python Tkinter ComboBox](https://dev.to/geraldew/python-tkinter-an-exercise-in-wrapping-the-combobox-ndb) by geraldew | [Mozilla Public License, v. 2.0](https://mozilla.org/MPL/2.0/) | +| [Argparse check limits](https://gist.github.com/dmitriykovalev/2ab1aa33a8099ef2d514925d84aa89e7) by Dmitriy Kovalev | [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0) | It indirectly uses: