refactor(Mainsail): rework config.json backup

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2023-12-24 13:53:55 +01:00
parent 2f0feb317e
commit 7a6590e86a
3 changed files with 18 additions and 12 deletions

View File

@@ -31,22 +31,28 @@ class BackupManager:
def backup_root_dir(self, value: Path):
self._backup_root_dir = value
def backup_file(self, files: List[Path] = None, target: Path = None):
def backup_file(
self, files: List[Path] = None, target: Path = None, custom_filename=None
):
if not files:
raise ValueError("Parameter 'files' cannot be None or an empty List!")
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, Path(target, 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 ...")
def backup_directory(self, name: str, source: Path, target: Path = None) -> None:
if source is None or not Path(source).exists():