From bf3b698b340af8777f0dd2f9071f9eee57cc5f72 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 8 Feb 2023 14:57:34 +0000 Subject: [PATCH] refactor(backup): add a backup function to Backups singleton class --- selfprivacy_api/backup/__init__.py | 38 ++++++++++++++++---- selfprivacy_api/backup/backuper.py | 10 ++++++ selfprivacy_api/backup/providers/provider.py | 2 +- selfprivacy_api/backup/restic_backuper.py | 2 +- 4 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 selfprivacy_api/backup/backuper.py diff --git a/selfprivacy_api/backup/__init__.py b/selfprivacy_api/backup/__init__.py index 3054f2e..79125ef 100644 --- a/selfprivacy_api/backup/__init__.py +++ b/selfprivacy_api/backup/__init__.py @@ -1,14 +1,40 @@ -from abc import ABC, abstractmethod +from selfprivacy_api.utils.singleton_metaclass import SingletonMetaclass + +from selfprivacy_api.services.service import Service +from selfprivacy_api.backup.providers.provider import AbstractBackupProvider +from selfprivacy_api.backup.providers import get_provider +from selfprivacy_api.graphql.queries.providers import BackupProvider -class Backups: +class Backups(metaclass=SingletonMetaclass): """A singleton controller for backups""" + provider: AbstractBackupProvider -class AbstractBackuper(ABC): def __init__(self): + self.lookup_provider() + + def lookup_provider(self): + redis_provider = Backups.load_provider_redis() + if redis_provider is not None: + self.provider = redis_provider + + json_provider = Backups.load_provider_json() + if json_provider is not None: + self.provider = json_provider + + provider_class = get_provider(BackupProvider.MEMORY) + self.provider = provider_class(login="", key="") + + @staticmethod + def load_provider_redis() -> AbstractBackupProvider: pass - @abstractmethod - def start_backup(self, folder: str): - raise NotImplementedError + @staticmethod + def load_provider_json() -> AbstractBackupProvider: + pass + + def back_up(self, service: Service): + folder = service.get_location() + repo_name = service.get_id() + self.provider.backuper.start_backup(folder, repo_name) diff --git a/selfprivacy_api/backup/backuper.py b/selfprivacy_api/backup/backuper.py new file mode 100644 index 0000000..f428145 --- /dev/null +++ b/selfprivacy_api/backup/backuper.py @@ -0,0 +1,10 @@ +from abc import ABC, abstractmethod + + +class AbstractBackuper(ABC): + def __init__(self): + pass + + @abstractmethod + def start_backup(self, folder: str, repo_name: str): + raise NotImplementedError diff --git a/selfprivacy_api/backup/providers/provider.py b/selfprivacy_api/backup/providers/provider.py index dd41e9a..0b57528 100644 --- a/selfprivacy_api/backup/providers/provider.py +++ b/selfprivacy_api/backup/providers/provider.py @@ -4,7 +4,7 @@ It assumes that while some providers are supported via restic/rclone, others may require different backends """ from abc import ABC -from selfprivacy_api.backup import AbstractBackuper +from selfprivacy_api.backup.backuper import AbstractBackuper class AbstractBackupProvider(ABC): diff --git a/selfprivacy_api/backup/restic_backuper.py b/selfprivacy_api/backup/restic_backuper.py index 7c379a7..5098e97 100644 --- a/selfprivacy_api/backup/restic_backuper.py +++ b/selfprivacy_api/backup/restic_backuper.py @@ -1,6 +1,6 @@ import subprocess -from selfprivacy_api.backup import AbstractBackuper +from selfprivacy_api.backup.backuper import AbstractBackuper class ResticBackuper(AbstractBackuper):