diff --git a/MethodicConfigurator/ardupilot_methodic_configurator.py b/MethodicConfigurator/ardupilot_methodic_configurator.py index a82b4ac..03e4a0f 100755 --- a/MethodicConfigurator/ardupilot_methodic_configurator.py +++ b/MethodicConfigurator/ardupilot_methodic_configurator.py @@ -74,9 +74,10 @@ def argument_parser(): 'Defaults to the current working directory') parser.add_argument('--n', type=int, - default=0, + default=-1, help='Start directly on the nth intermediate parameter file (skips previous files). ' - 'Default is %(default)s') + 'Default is to start on the file next to the last that you wrote to the flight controller.' + 'If file does not exist, it will start on the first file.') parser.add_argument('--loglevel', type=str, default='INFO', @@ -126,17 +127,7 @@ def main(): vehicle_dir_window = VehicleDirectorySelectionWindow(local_filesystem) vehicle_dir_window.root.mainloop() - # Get the list of intermediate parameter files files that will be processed sequentially - files = list(local_filesystem.file_parameters.keys()) - - start_file = None # pylint: disable=invalid-name - if files: - # Determine the starting file based on the --n command line argument - start_file_index = min(args.n, len(files) - 1) # Ensure the index is within the range of available files - if start_file_index != args.n: - logging_warning("Starting file index %s is out of range. Starting with file %s instead.", - args.n, files[start_file_index]) - start_file = files[start_file_index] + start_file = local_filesystem.get_start_file(args.n) component_editor_window = JsonEditorApp(VERSION, local_filesystem) component_editor_window.root.mainloop() diff --git a/MethodicConfigurator/backend_filesystem.py b/MethodicConfigurator/backend_filesystem.py index e372ef7..f4f913a 100644 --- a/MethodicConfigurator/backend_filesystem.py +++ b/MethodicConfigurator/backend_filesystem.py @@ -475,3 +475,56 @@ def copy_fc_values_to_file(self, selected_file: str, params: Dict[str, float]): else: logging_warning("Parameter %s not found in the current parameter file", param) return ret + + def write_last_written_filename(self, current_file: str): + try: + with open(os_path.join(self.vehicle_dir, 'last_written_filename.txt'), 'w', encoding='utf-8') as file: + file.write(current_file) + except Exception as e: # pylint: disable=broad-except + logging_error("Error writing last written filename: %s", e) + + def __read_last_written_filename(self) -> str: + try: + with open(os_path.join(self.vehicle_dir, 'last_written_filename.txt'), 'r', encoding='utf-8') as file: + return file.read().strip() + except Exception as e: # pylint: disable=broad-except + logging_error("Error reading last written filename: %s", e) + return "" + + def get_start_file(self, explicit_index: int): + # Get the list of intermediate parameter files files that will be processed sequentially + files = list(self.file_parameters.keys()) + + if explicit_index >= 0: + if not files: + return "" + + # Determine the starting file based on the --n command line argument + start_file_index = explicit_index # Ensure the index is within the range of available files + if start_file_index >= len(files): + start_file_index = len(files) - 1 + logging_warning("Starting file index %s is out of range. Starting with file %s instead.", + explicit_index, files[start_file_index]) + return files[start_file_index] + + last_written_filename = self.__read_last_written_filename() + if last_written_filename: + logging_info("Last written file was %s.", last_written_filename) + else: + logging_info("No last written file found. Starting with the first file.") + return files[0] + + if last_written_filename not in files: + # Handle the case where last_written_filename is not found in the list + logging_warning("Last written file not found in the list of files. Starting with the first file.") + return files[0] + + # Find the index of last_written_filename in files + last_written_index = files.index(last_written_filename) + # Check if there is a file following last_written_filename + start_file_index = last_written_index + 1 + if start_file_index >= len(files): + # Handle the case where last_written_filename is the last file in the list + logging_warning("Last written file is the last file in the list. Starting from there.") + start_file_index = len(files) - 1 + return files[start_file_index] diff --git a/MethodicConfigurator/frontend_tkinter_directory_selection.py b/MethodicConfigurator/frontend_tkinter_directory_selection.py index a06dc64..0fe7015 100644 --- a/MethodicConfigurator/frontend_tkinter_directory_selection.py +++ b/MethodicConfigurator/frontend_tkinter_directory_selection.py @@ -337,19 +337,10 @@ def main(): # Get the list of intermediate parameter files files that will be processed sequentially files = list(local_filesystem.file_parameters.keys()) -# pylint: disable=duplicate-code - if files: - # Determine the starting file based on the --n command line argument - start_file_index = min(args.n, len(files) - 1) # Ensure the index is within the range of available files - if start_file_index != args.n: - logging_warning("Starting file index %s is out of range. Starting with file %s instead.", - args.n, files[start_file_index]) - else: + if not files: logging_error("No intermediate parameter files found in %s.", args.vehicle_dir) - # show_no_param_files_error(args.vehicle_dir) window = VehicleDirectorySelectionWindow(local_filesystem) window.root.mainloop() -# pylint: enable=duplicate-code if __name__ == "__main__": diff --git a/MethodicConfigurator/frontend_tkinter_parameter_editor.py b/MethodicConfigurator/frontend_tkinter_parameter_editor.py index 55c32f9..1481257 100644 --- a/MethodicConfigurator/frontend_tkinter_parameter_editor.py +++ b/MethodicConfigurator/frontend_tkinter_parameter_editor.py @@ -778,6 +778,7 @@ def write_selected_params(self, selected_params): self.write_selected_params(selected_params) else: logging_info("All parameters written to the flight controller successfully") + self.local_filesystem.write_last_written_filename(self.current_file) def on_skip_click(self, _event=None, force_focus_out_event=True): if force_focus_out_event: