Completely refactored gresource to correspond SOLID principles

- Create logger interface and updated its usage by console.py
This commit is contained in:
Vladyslav Hroshev
2025-04-06 17:02:49 +03:00
parent 8da9b564be
commit 9bb229df7d
17 changed files with 316 additions and 182 deletions

View File

@@ -0,0 +1,52 @@
import os
import shutil
import subprocess
from scripts.utils.gresource import GresourceBackupNotFoundError
from scripts.utils.logger.logger import LoggerFactory
class GresourceBackuperManager:
def __init__(self, destination_file: str, logger_factory: LoggerFactory):
self.destination_file = destination_file
self._backup_file = f"{destination_file}.backup"
self._backuper = GresourceBackuper(destination_file, self._backup_file, logger_factory)
def backup(self):
self._backuper.backup()
def restore(self):
self._backuper.restore()
def get_backup(self) -> str:
return self._backuper.get_backup()
class GresourceBackuper:
def __init__(self, destination_file: str, backup_file, logger_factory: LoggerFactory):
self.destination_file = destination_file
self.backup_file = backup_file
self.logger_factory = logger_factory
def get_backup(self) -> str:
if not os.path.exists(self.backup_file):
raise GresourceBackupNotFoundError(self.backup_file)
return self.backup_file
def backup(self):
backup_line = self.logger_factory.create_logger()
backup_line.update("Backing up gresource files...")
if os.path.exists(self.backup_file):
os.remove(self.backup_file)
shutil.copy2(self.destination_file, self.backup_file)
# subprocess.run(["cp", "-aT", self.destination_file, self.backup_file], check=True)
backup_line.success("Backed up gresource files.")
def restore(self):
if not os.path.exists(self.backup_file):
raise GresourceBackupNotFoundError(self.backup_file)
subprocess.run(["mv", "-f", self.backup_file, self.destination_file], check=True)