Support save and load user config.

master
Marko Semet 2020-04-16 13:29:56 +02:00
parent 04d4787c0d
commit 14af5af7f6
2 changed files with 44 additions and 4 deletions

View File

@ -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

View File

@ -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"]
)