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()
|
bm = BackupManager()
|
||||||
if is_temp:
|
if is_temp:
|
||||||
fn = Path.home().joinpath("config.json.kiauh.bak")
|
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:
|
else:
|
||||||
bm.backup_file([MAINSAIL_CONFIG_JSON])
|
bm.backup_file(MAINSAIL_CONFIG_JSON)
|
||||||
|
|
||||||
|
|
||||||
def restore_config_json() -> None:
|
def restore_config_json() -> None:
|
||||||
|
|||||||
@@ -42,27 +42,27 @@ class BackupManager:
|
|||||||
self._ignore_folders = value
|
self._ignore_folders = value
|
||||||
|
|
||||||
def backup_file(
|
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:
|
if not file:
|
||||||
raise ValueError("Parameter 'files' cannot be None or an empty List!")
|
raise ValueError("Parameter 'file' cannot be None!")
|
||||||
|
|
||||||
target = self.backup_root_dir if target is None else target
|
target = self.backup_root_dir if target is None else target
|
||||||
for file in files:
|
|
||||||
Logger.print_status(f"Creating backup of {file} ...")
|
Logger.print_status(f"Creating backup of {file} ...")
|
||||||
if Path(file).is_file():
|
if Path(file).is_file():
|
||||||
date = get_current_date().get("date")
|
date = get_current_date().get("date")
|
||||||
time = get_current_date().get("time")
|
time = get_current_date().get("time")
|
||||||
filename = f"{file.stem}-{date}-{time}{file.suffix}"
|
filename = f"{file.stem}-{date}-{time}{file.suffix}"
|
||||||
filename = custom_filename if custom_filename is not None else filename
|
filename = custom_filename if custom_filename is not None else filename
|
||||||
try:
|
try:
|
||||||
Path(target).mkdir(exist_ok=True)
|
Path(target).mkdir(exist_ok=True)
|
||||||
shutil.copyfile(file, target.joinpath(filename))
|
shutil.copyfile(file, target.joinpath(filename))
|
||||||
except OSError as e:
|
Logger.print_ok("Backup successfull!")
|
||||||
Logger.print_error(f"Unable to backup '{file}':\n{e}")
|
except OSError as e:
|
||||||
continue
|
Logger.print_error(f"Unable to backup '{file}':\n{e}")
|
||||||
else:
|
else:
|
||||||
Logger.print_info(f"File '{file}' not found ...")
|
Logger.print_info(f"File '{file}' not found ...")
|
||||||
|
|
||||||
def backup_directory(self, name: str, source: Path, target: Path = None) -> None:
|
def backup_directory(self, name: str, source: Path, target: Path = None) -> None:
|
||||||
if source is None or not Path(source).exists():
|
if source is None or not Path(source).exists():
|
||||||
@@ -79,11 +79,11 @@ class BackupManager:
|
|||||||
target.joinpath(f"{name.lower()}-{date}-{time}"),
|
target.joinpath(f"{name.lower()}-{date}-{time}"),
|
||||||
ignore=self.ignore_folders_func,
|
ignore=self.ignore_folders_func,
|
||||||
)
|
)
|
||||||
|
Logger.print_ok("Backup successfull!")
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
Logger.print_error(f"Unable to backup directory '{source}':\n{e}")
|
Logger.print_error(f"Unable to backup directory '{source}':\n{e}")
|
||||||
return
|
return
|
||||||
|
|
||||||
Logger.print_ok("Backup successfull!")
|
|
||||||
|
|
||||||
def ignore_folders_func(self, dirpath, filenames):
|
def ignore_folders_func(self, dirpath, filenames):
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ from kiauh.extensions.gcode_shell_cmd import (
|
|||||||
KLIPPER_DIR,
|
KLIPPER_DIR,
|
||||||
EXAMPLE_CFG_SRC,
|
EXAMPLE_CFG_SRC,
|
||||||
KLIPPER_EXTRAS,
|
KLIPPER_EXTRAS,
|
||||||
)
|
)
|
||||||
from kiauh.utils.filesystem_utils import check_file_exist
|
from kiauh.utils.filesystem_utils import check_file_exist
|
||||||
from kiauh.utils.input_utils import get_confirm
|
from kiauh.utils.input_utils import get_confirm
|
||||||
from kiauh.utils.logger import Logger
|
from kiauh.utils.logger import Logger
|
||||||
@@ -109,7 +109,7 @@ class GcodeShellCmdExtension(BaseExtension):
|
|||||||
bm = BackupManager()
|
bm = BackupManager()
|
||||||
for instance in instances:
|
for instance in instances:
|
||||||
bm.backup_file(
|
bm.backup_file(
|
||||||
[instance.cfg_file],
|
instance.cfg_file,
|
||||||
custom_filename=f"{instance.suffix}.printer.cfg",
|
custom_filename=f"{instance.suffix}.printer.cfg",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user