Split backups from config
parent
9278606870
commit
b3bbf8fcb3
|
@ -0,0 +1,58 @@
|
||||||
|
from .. import utils
|
||||||
|
|
||||||
|
|
||||||
|
class Backup():
|
||||||
|
name:str
|
||||||
|
periode:int
|
||||||
|
blocked:set
|
||||||
|
|
||||||
|
def __init__(self, name:str, periode:int=None, blocked:list=[]):
|
||||||
|
# Check args
|
||||||
|
utils.valid_name_check(name)
|
||||||
|
if periode is not None and not isinstance(periode, int):
|
||||||
|
raise TypeError("Periode have to be an integer or null.")
|
||||||
|
if periode is not None and periode < 0:
|
||||||
|
raise ValueError("periode can't be negetive.")
|
||||||
|
blocked = set(blocked)
|
||||||
|
for i in blocked:
|
||||||
|
utils.valid_name_check(i)
|
||||||
|
|
||||||
|
# Set values
|
||||||
|
self.name = name
|
||||||
|
self.periode = periode
|
||||||
|
self.blocks = blocked
|
||||||
|
|
||||||
|
def get_next_scedule(self, latest, zero):
|
||||||
|
if self.periode is not None:
|
||||||
|
tmp = (latest - zero) // self.periode
|
||||||
|
return zero + self.periode * (tmp + 1)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError("No implemented types.")
|
||||||
|
|
||||||
|
def dump_config(self):
|
||||||
|
result = {}
|
||||||
|
if self.blocked:
|
||||||
|
result["blocked"] = ",".join(self.blocked)
|
||||||
|
if self.periode is not None:
|
||||||
|
result["periode"] = str(self.periode)
|
||||||
|
return result
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def load_backup(name:str, config):
|
||||||
|
# Load informations
|
||||||
|
config = dict(config.items())
|
||||||
|
|
||||||
|
periode = None
|
||||||
|
if "periode" in config:
|
||||||
|
periode = int(config["periode"])
|
||||||
|
del config["periode"]
|
||||||
|
|
||||||
|
blocked = []
|
||||||
|
if "blocked" in config:
|
||||||
|
blocked = config["blocked"].split(",")
|
||||||
|
del config["blocked"]
|
||||||
|
|
||||||
|
# Generate backup
|
||||||
|
utils.check_empty_data_dict(config)
|
||||||
|
|
||||||
|
return Backup(name=name, periode=periode, blocked=blocked)
|
|
@ -4,8 +4,8 @@ import configparser
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
from .backups import Backup
|
||||||
from .remotes import Remote
|
from .remotes import Remote
|
||||||
from .. import utils
|
|
||||||
|
|
||||||
|
|
||||||
# Config
|
# Config
|
||||||
|
@ -17,35 +17,6 @@ else:
|
||||||
CONFIG_FILE = os.path.join(CONFIG_FILE, "home-backup.conf") # TODO: Set own config dir
|
CONFIG_FILE = os.path.join(CONFIG_FILE, "home-backup.conf") # TODO: Set own config dir
|
||||||
|
|
||||||
|
|
||||||
class Backup():
|
|
||||||
name:str
|
|
||||||
periode:int
|
|
||||||
blocked:set
|
|
||||||
|
|
||||||
def __init__(self, name:str, periode:int=None, blocked:list=[]):
|
|
||||||
# Check args
|
|
||||||
utils.valid_name_check(name)
|
|
||||||
if periode is not None and not isinstance(periode, int):
|
|
||||||
raise TypeError("Periode have to be an integer or null.")
|
|
||||||
if periode is not None and periode < 0:
|
|
||||||
raise ValueError("periode can't be negetive.")
|
|
||||||
blocked = set(blocked)
|
|
||||||
for i in blocked:
|
|
||||||
utils.valid_name_check(i)
|
|
||||||
|
|
||||||
# Set values
|
|
||||||
self.name = name
|
|
||||||
self.periode = periode
|
|
||||||
self.blocks = blocked
|
|
||||||
|
|
||||||
def get_next_scedule(self, latest, zero):
|
|
||||||
if self.periode is not None:
|
|
||||||
tmp = (latest - zero) // self.periode
|
|
||||||
return zero + self.periode * (tmp + 1)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError("No implemented types.")
|
|
||||||
|
|
||||||
|
|
||||||
backups = []
|
backups = []
|
||||||
remotes = {}
|
remotes = {}
|
||||||
config_lock = asyncio.Lock()
|
config_lock = asyncio.Lock()
|
||||||
|
@ -60,13 +31,7 @@ if os.path.exists(CONFIG_FILE):
|
||||||
for iID, i in filter(lambda x: x[0] != "DEFAULT", config.items()):
|
for iID, i in filter(lambda x: x[0] != "DEFAULT", config.items()):
|
||||||
if iID.startswith("BACKUP|"): # Parse backup config
|
if iID.startswith("BACKUP|"): # Parse backup config
|
||||||
iID = iID[len("BACKUP|"):]
|
iID = iID[len("BACKUP|"):]
|
||||||
periode = None
|
backups.append(Backup.load_backup(iID, i))
|
||||||
if "periode" in i:
|
|
||||||
periode = int(i["periode"])
|
|
||||||
blocked = []
|
|
||||||
if "blocked" in i:
|
|
||||||
blocked = i["blocked"].split(",")
|
|
||||||
backups.append(Backup(iID, periode=periode, blocked=blocked))
|
|
||||||
elif iID.startswith("REMOTE|"): # Parse remote config
|
elif iID.startswith("REMOTE|"): # Parse remote config
|
||||||
iID = iID[len("REMOTE|"):]
|
iID = iID[len("REMOTE|"):]
|
||||||
tmp = Remote.load_remote(iID, i)
|
tmp = Remote.load_remote(iID, i)
|
||||||
|
@ -85,12 +50,7 @@ async def save_config():
|
||||||
for i in backups:
|
for i in backups:
|
||||||
if not isinstance(i, Backup):
|
if not isinstance(i, Backup):
|
||||||
raise ValueError("backups contains a non backup config entry.")
|
raise ValueError("backups contains a non backup config entry.")
|
||||||
backup_data = {
|
config["BACKUP|%s" % i.name] = i.dump_config()
|
||||||
"blocked": ",".join(i.blocked)
|
|
||||||
}
|
|
||||||
if i.periode is not None:
|
|
||||||
backup_data["periode"] = str(i.periode)
|
|
||||||
config["BACKUP|%s" % i.name] = backup_data
|
|
||||||
|
|
||||||
# Add remotes
|
# Add remotes
|
||||||
for i in remotes.values():
|
for i in remotes.values():
|
||||||
|
|
Loading…
Reference in New Issue