Support save and load user config.
parent
04d4787c0d
commit
14af5af7f6
|
@ -1,5 +1,7 @@
|
||||||
|
import aiofile
|
||||||
import asyncio
|
import asyncio
|
||||||
import configparser
|
import configparser
|
||||||
|
import io
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
from .. import utils
|
from .. import utils
|
||||||
|
@ -23,12 +25,18 @@ class Backup():
|
||||||
# Check args
|
# Check args
|
||||||
if not isinstance(name, str):
|
if not isinstance(name, str):
|
||||||
raise TypeError("Name has to be a string.")
|
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):
|
if periode is not None and not isinstance(periode, int):
|
||||||
raise TypeError("Periode have to be an integer or null.")
|
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)
|
blocked = set(blocked)
|
||||||
for i in blocked:
|
for i in blocked:
|
||||||
if not isinstance(i, str):
|
if not isinstance(i, str):
|
||||||
raise TypeError("Blocked have to be a list of strings.")
|
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
|
# Set values
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -47,12 +55,44 @@ backups = []
|
||||||
backups_lock = asyncio.Lock()
|
backups_lock = asyncio.Lock()
|
||||||
if os.path.exists(CONFIG_FILE):
|
if os.path.exists(CONFIG_FILE):
|
||||||
def _parse_config():
|
def _parse_config():
|
||||||
raise NotImplementedError()
|
# Load config
|
||||||
config = _parse_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():
|
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
|
# Timer support
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -13,5 +13,5 @@ setup(name="home-backup",
|
||||||
author_email="marko@marko10-000.de",
|
author_email="marko@marko10-000.de",
|
||||||
#url="", TODO: Make a project site
|
#url="", TODO: Make a project site
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
install_requires=["systemd==0.16.1"]
|
install_requires=["systemd==0.16.1", "aiofile==1.5.2"]
|
||||||
)
|
)
|
Loading…
Reference in New Issue