refactor(BackupManager): backup_file method only takes in single files now
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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 (
|
||||
|
||||
@@ -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",
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user