From 14af5af7f6fba4a0062d40603408275658bc3225 Mon Sep 17 00:00:00 2001 From: Marko Semet Date: Thu, 16 Apr 2020 13:29:56 +0200 Subject: [PATCH] Support save and load user config. --- home_backup/user_service/config.py | 46 ++++++++++++++++++++++++++++-- setup.py | 2 +- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/home_backup/user_service/config.py b/home_backup/user_service/config.py index dd8fd7f..0e56fe7 100644 --- a/home_backup/user_service/config.py +++ b/home_backup/user_service/config.py @@ -1,5 +1,7 @@ +import aiofile import asyncio import configparser +import io import os import time from .. import utils @@ -23,12 +25,18 @@ class Backup(): # Check args if not isinstance(name, str): raise TypeError("Name has to be a string.") + for i in filter(lambda x: not("a" <= x <= "z" or "A" <= x <= "Z" or "0" <= x <= "9" or x in ("_", "-")), i): + raise ValueError("%s isn't a valid char in a backup name." % i) 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: if not isinstance(i, str): raise TypeError("Blocked have to be a list of strings.") + for j in filter(lambda x: not("a" <= x <= "z" or "A" <= x <= "Z" or "0" <= x <= "9" or x in ("_", "-")), i): + raise ValueError("%s isn't a valid char in a block name." % i) # Set values self.name = name @@ -47,12 +55,44 @@ backups = [] backups_lock = asyncio.Lock() if os.path.exists(CONFIG_FILE): def _parse_config(): - raise NotImplementedError() - config = _parse_config() + # Load config + config = configparser.ConfigParser() + config.read(CONFIG_FILE) + + # Create backups + backups = [] + for iID, i in filter(lambda x: x[0].startswith("BACKUP|"), config.items()): + iID = iID[len("BACKUP|"):] + periode = None + 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)) + return backups + backups = _parse_config() async def save_config(): - raise NotImplementedError() + # Write config + config = configparser.ConfigParser() + for i in backups: + if not isinstance(i, Backup): + raise ValueError("backups contains a non backup config entry.") + backup_data = { + "blocked": ",".join(i.blocked) + } + if i.periode is not None: + backup_data["periode"] = str(i.periode) + config["BACKUP|%s" % i.name] = backup_data + + # Write data + tmp = io.StringIO() + config.write(tmp) + async with aiofile.AIOFile(CONFIG_FILE, "w") as f: + await f.write(tmp.read()) + await f.fsync() # Timer support diff --git a/setup.py b/setup.py index 03a7599..4bec34f 100644 --- a/setup.py +++ b/setup.py @@ -13,5 +13,5 @@ setup(name="home-backup", author_email="marko@marko10-000.de", #url="", TODO: Make a project site packages=find_packages(), - install_requires=["systemd==0.16.1"] + install_requires=["systemd==0.16.1", "aiofile==1.5.2"] ) \ No newline at end of file