Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruff improvements #65

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ ignore =
E261


max-line-length = 127
max-line-length = 128
10 changes: 8 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.1
rev: v0.7.2
hooks:
- id: ruff
args: [ --fix ]
args: [ --select=F,E,W,I,PL,RUF,PERF --line-length=127 --ignore=E501,PLR0911,PLR0912,PLR0913,PLR0915,PLR2004,PLW0603,PERF401,PLW2901,PERF203,RUF005 --fix ]
language: system
types: [python]
stages: [pre-commit]
# Run the formatter.
- id: ruff-format
args: [ --line-length=127 ]
types_or: [ python ]
stages: [pre-commit]

- repo: local
hooks:
- id: pylint
Expand Down
16 changes: 15 additions & 1 deletion MethodicConfigurator/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
'''ArduPilot Methodic Configurator GUI application''' # pylint: skip-file
#!/usr/bin/env python3

"""
This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator

SPDX-FileCopyrightText: 2024 Amilcar do Carmo Lucas <[email protected]>

SPDX-License-Identifier: GPL-3.0-or-later
"""
# pylint: skip-file

from MethodicConfigurator.internationalization import load_translation


_ = load_translation()
251 changes: 140 additions & 111 deletions MethodicConfigurator/annotate_params.py

Large diffs are not rendered by default.

89 changes: 41 additions & 48 deletions MethodicConfigurator/ardupilot_methodic_configurator.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
#!/usr/bin/env python3

'''
"""
This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator

SPDX-FileCopyrightText: 2024 Amilcar do Carmo Lucas <[email protected]>

SPDX-License-Identifier: GPL-3.0-or-later
'''
"""

import argparse
from logging import basicConfig as logging_basicConfig
from logging import getLevelName as logging_getLevelName
from logging import debug as logging_debug
from logging import info as logging_info
#from logging import warning as logging_warning

# from logging import warning as logging_warning
from logging import error as logging_error
from logging import getLevelName as logging_getLevelName
from logging import info as logging_info
from sys import exit as sys_exit

from MethodicConfigurator.backend_filesystem import LocalFilesystem
from MethodicConfigurator.backend_flightcontroller import FlightController

from MethodicConfigurator.common_arguments import add_common_arguments_and_parse
from MethodicConfigurator.frontend_tkinter_base import show_error_message

from MethodicConfigurator.frontend_tkinter_component_editor import ComponentEditorWindow
from MethodicConfigurator.frontend_tkinter_connection_selection import ConnectionSelectionWindow

from MethodicConfigurator.frontend_tkinter_flightcontroller_info import FlightControllerInfoWindow

from MethodicConfigurator.frontend_tkinter_directory_selection import VehicleDirectorySelectionWindow

from MethodicConfigurator.frontend_tkinter_component_editor import ComponentEditorWindow

from MethodicConfigurator.frontend_tkinter_flightcontroller_info import FlightControllerInfoWindow
from MethodicConfigurator.frontend_tkinter_parameter_editor import ParameterEditorWindow

from MethodicConfigurator.common_arguments import add_common_arguments_and_parse

from MethodicConfigurator.internationalization import _, load_translation

from MethodicConfigurator import _
from MethodicConfigurator.version import VERSION


Expand All @@ -48,16 +40,20 @@ def argument_parser():
Returns:
argparse.Namespace: An object containing the parsed arguments.
"""
parser = argparse.ArgumentParser(description=_('ArduPilot methodic configurator is a simple GUI with a table that lists '
'parameters. The GUI reads intermediate parameter files from a directory and '
'displays their parameters in a table. Each row displays the parameter name, '
'its current value on the flight controller, its new value from the selected '
'intermediate parameter file, and an "Upload" checkbox. The GUI includes "Upload '
'selected params to FC" and "Skip" buttons at the bottom. '
'When "Upload Selected to FC" is clicked, it uploads the selected parameters to the '
'flight controller. '
'When "Skip" is pressed, it skips to the next intermediate parameter file. '
'The process gets repeated for each intermediate parameter file.'))
parser = argparse.ArgumentParser(
description=_(
"ArduPilot methodic configurator is a simple GUI with a table that lists "
"parameters. The GUI reads intermediate parameter files from a directory and "
"displays their parameters in a table. Each row displays the parameter name, "
"its current value on the flight controller, its new value from the selected "
'intermediate parameter file, and an "Upload" checkbox. The GUI includes "Upload '
'selected params to FC" and "Skip" buttons at the bottom. '
'When "Upload Selected to FC" is clicked, it uploads the selected parameters to the '
"flight controller. "
'When "Skip" is pressed, it skips to the next intermediate parameter file. '
"The process gets repeated for each intermediate parameter file."
)
)
parser = FlightController.add_argparse_arguments(parser)
parser = LocalFilesystem.add_argparse_arguments(parser)
parser = ComponentEditorWindow.add_argparse_arguments(parser)
Expand All @@ -83,59 +79,56 @@ def connect_to_fc_and_read_parameters(args):
else:
logging_info(_("Vehicle type explicitly set to %s."), vehicle_type)

return flight_controller,vehicle_type
return flight_controller, vehicle_type


def component_editor(args, flight_controller, vehicle_type, local_filesystem, vehicle_dir_window):
component_editor_window = ComponentEditorWindow(VERSION, local_filesystem)
if vehicle_dir_window and \
vehicle_dir_window.configuration_template is not None and \
vehicle_dir_window.use_fc_params.get() and \
flight_controller.fc_parameters:
if (
vehicle_dir_window
and vehicle_dir_window.configuration_template is not None
and vehicle_dir_window.use_fc_params.get()
and flight_controller.fc_parameters
):
# copy vehicle parameters to component editor values
component_editor_window.set_values_from_fc_parameters(flight_controller.fc_parameters, local_filesystem.doc_dict)
component_editor_window.populate_frames()
component_editor_window.set_vehicle_type_and_version(vehicle_type, flight_controller.info.flight_sw_version_and_type)
component_editor_window.set_fc_manufacturer(flight_controller.info.vendor)
component_editor_window.set_fc_model(flight_controller.info.product)
if vehicle_dir_window and \
vehicle_dir_window.configuration_template is not None:
if vehicle_dir_window and vehicle_dir_window.configuration_template is not None:
component_editor_window.set_vehicle_configuration_template(vehicle_dir_window.configuration_template)
if args.skip_component_editor:
component_editor_window.root.after(10, component_editor_window.root.destroy)
component_editor_window.root.mainloop()

if vehicle_dir_window and \
vehicle_dir_window.configuration_template is not None and \
vehicle_dir_window.use_fc_params.get():
if vehicle_dir_window and vehicle_dir_window.configuration_template is not None and vehicle_dir_window.use_fc_params.get():
error_message = local_filesystem.copy_fc_params_values_to_template_created_vehicle_files(
flight_controller.fc_parameters)
flight_controller.fc_parameters
)
if error_message:
logging_error(error_message)
show_error_message(_("Error in derived parameters"), error_message)
sys_exit(1)


def main():
# modify the global _() function
global _ # pylint: disable=global-statement

_ = load_translation() # done as soon as possible so that the correct language is used
args = argument_parser()

logging_basicConfig(level=logging_getLevelName(args.loglevel), format='%(asctime)s - %(levelname)s - %(message)s')
logging_basicConfig(level=logging_getLevelName(args.loglevel), format="%(asctime)s - %(levelname)s - %(message)s")

# Connect to the flight controller and read the parameters
flight_controller, vehicle_type = connect_to_fc_and_read_parameters(args)

param_default_values = {}
if flight_controller.master is not None or args.device == 'test':
if flight_controller.master is not None or args.device == "test":
fciw = FlightControllerInfoWindow(flight_controller)
param_default_values = fciw.get_param_default_values()

try:
local_filesystem = LocalFilesystem(args.vehicle_dir, vehicle_type, flight_controller.info.flight_sw_version,
args.allow_editing_template_files)
local_filesystem = LocalFilesystem(
args.vehicle_dir, vehicle_type, flight_controller.info.flight_sw_version, args.allow_editing_template_files
)
except SystemExit as exp:
show_error_message(_("Fatal error reading parameter files"), f"{exp}")
raise
Expand All @@ -158,7 +151,7 @@ def main():
if param_default_values_dirty:
local_filesystem.write_param_default_values_to_file(param_default_values)

imu_tcal_available = 'INS_TCAL1_ENABLE' in flight_controller.fc_parameters or not flight_controller.fc_parameters
imu_tcal_available = "INS_TCAL1_ENABLE" in flight_controller.fc_parameters or not flight_controller.fc_parameters
start_file = local_filesystem.get_start_file(args.n, imu_tcal_available)

# Call the GUI function with the starting intermediate parameter file
Expand Down
26 changes: 11 additions & 15 deletions MethodicConfigurator/argparse_check_range.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
#!/usr/bin/env python3

'''
"""
This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator

SPDX-FileCopyrightText: 2024 Dmitriy Kovalev

SPDX-License-Identifier: Apache-2.0

https://gist.github.com/dmitriykovalev/2ab1aa33a8099ef2d514925d84aa89e7
'''
"""

from argparse import Action, ArgumentError
from operator import ge, gt, le, lt

from MethodicConfigurator import _

from argparse import Action
from argparse import ArgumentError
from operator import gt
from operator import ge
from operator import lt
from operator import le
from MethodicConfigurator.internationalization import _

class CheckRange(Action):
'''
"""
Check if the Argparse argument value is within the specified range
'''
ops = {"inf": gt,
"min": ge,
"sup": lt,
"max": le}
"""

ops = {"inf": gt, "min": ge, "sup": lt, "max": le}

def __init__(self, *args, **kwargs):
if "min" in kwargs and "inf" in kwargs:
Expand Down
Loading