Skip to content

Commit

Permalink
BUGFIX: pylint argparse_check_range.py
Browse files Browse the repository at this point in the history
  • Loading branch information
amilcarlucas committed Aug 1, 2024
1 parent 8d8cca4 commit a8c3212
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 54 deletions.
101 changes: 48 additions & 53 deletions MethodicConfigurator/argparse_check_range.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
3 changes: 2 additions & 1 deletion credits/CREDITS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:

Expand Down

0 comments on commit a8c3212

Please sign in to comment.