From 34ebe5d15e6dbac169f8e3f53a2ef518be3d4ff9 Mon Sep 17 00:00:00 2001 From: dw-0 Date: Sat, 10 Feb 2024 11:38:23 +0100 Subject: [PATCH] refactor(BackupManager): backup_file method only takes in single files now Signed-off-by: Dominik Willner --- kiauh/components/mainsail/mainsail_utils.py | 4 +- kiauh/core/backup_manager/backup_manager.py | 38 +++++++++---------- .../gcode_shell_cmd_extension.py | 4 +- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/kiauh/components/mainsail/mainsail_utils.py b/kiauh/components/mainsail/mainsail_utils.py index 5f08765..de54941 100644 --- a/kiauh/components/mainsail/mainsail_utils.py +++ b/kiauh/components/mainsail/mainsail_utils.py @@ -37,9 +37,9 @@ def backup_config_json(is_temp=False) -> None: bm = BackupManager() if is_temp: fn = Path.home().joinpath("config.json.kiauh.bak") - bm.backup_file([MAINSAIL_CONFIG_JSON], custom_filename=fn) + bm.backup_file(MAINSAIL_CONFIG_JSON, custom_filename=fn) else: - bm.backup_file([MAINSAIL_CONFIG_JSON]) + bm.backup_file(MAINSAIL_CONFIG_JSON) def restore_config_json() -> None: diff --git a/kiauh/core/backup_manager/backup_manager.py b/kiauh/core/backup_manager/backup_manager.py index d5cfca5..7f5a887 100644 --- a/kiauh/core/backup_manager/backup_manager.py +++ b/kiauh/core/backup_manager/backup_manager.py @@ -42,27 +42,27 @@ class BackupManager: self._ignore_folders = value def backup_file( - self, files: List[Path] = None, target: Path = None, custom_filename=None + self, file: Path = None, target: Path = None, custom_filename=None ): - if not files: - raise ValueError("Parameter 'files' cannot be None or an empty List!") + if not file: + raise ValueError("Parameter 'file' cannot be None!") target = self.backup_root_dir if target is None else target - for file in files: - Logger.print_status(f"Creating backup of {file} ...") - if Path(file).is_file(): - date = get_current_date().get("date") - time = get_current_date().get("time") - filename = f"{file.stem}-{date}-{time}{file.suffix}" - filename = custom_filename if custom_filename is not None else filename - try: - Path(target).mkdir(exist_ok=True) - shutil.copyfile(file, target.joinpath(filename)) - except OSError as e: - Logger.print_error(f"Unable to backup '{file}':\n{e}") - continue - else: - Logger.print_info(f"File '{file}' not found ...") + + Logger.print_status(f"Creating backup of {file} ...") + if Path(file).is_file(): + date = get_current_date().get("date") + time = get_current_date().get("time") + filename = f"{file.stem}-{date}-{time}{file.suffix}" + filename = custom_filename if custom_filename is not None else filename + try: + Path(target).mkdir(exist_ok=True) + shutil.copyfile(file, target.joinpath(filename)) + Logger.print_ok("Backup successfull!") + except OSError as e: + Logger.print_error(f"Unable to backup '{file}':\n{e}") + else: + Logger.print_info(f"File '{file}' not found ...") def backup_directory(self, name: str, source: Path, target: Path = None) -> None: if source is None or not Path(source).exists(): @@ -79,11 +79,11 @@ class BackupManager: target.joinpath(f"{name.lower()}-{date}-{time}"), ignore=self.ignore_folders_func, ) + Logger.print_ok("Backup successfull!") except OSError as e: Logger.print_error(f"Unable to backup directory '{source}':\n{e}") return - Logger.print_ok("Backup successfull!") def ignore_folders_func(self, dirpath, filenames): return ( diff --git a/kiauh/extensions/gcode_shell_cmd/gcode_shell_cmd_extension.py b/kiauh/extensions/gcode_shell_cmd/gcode_shell_cmd_extension.py index d8ed49b..c0c28c1 100644 --- a/kiauh/extensions/gcode_shell_cmd/gcode_shell_cmd_extension.py +++ b/kiauh/extensions/gcode_shell_cmd/gcode_shell_cmd_extension.py @@ -24,7 +24,7 @@ from kiauh.extensions.gcode_shell_cmd import ( KLIPPER_DIR, EXAMPLE_CFG_SRC, KLIPPER_EXTRAS, -) + ) from kiauh.utils.filesystem_utils import check_file_exist from kiauh.utils.input_utils import get_confirm from kiauh.utils.logger import Logger @@ -109,7 +109,7 @@ class GcodeShellCmdExtension(BaseExtension): bm = BackupManager() for instance in instances: bm.backup_file( - [instance.cfg_file], + instance.cfg_file, custom_filename=f"{instance.suffix}.printer.cfg", )