updates config and starts testing it

This commit is contained in:
artus
2018-11-20 14:22:20 +01:00
parent effbf6e41d
commit 1aa2b03315
4 changed files with 46 additions and 37 deletions

View File

@@ -6,36 +6,38 @@ from pathlib import Path
from .store import Store, Backup
class BadConfiguration(Exception):
pass
def load_config():
CONFIG = configparser.ConfigParser()
wd = Path(__file__).parent
CONFIG.read(wd / "config.ini")
try:
camera = CONFIG.defaults()['camera']
except KeyError:
raise RuntimeError(
f"Bad Configuration : No camera path set !")
raise BadConfiguration("No camera path set !")
backups = {}
imports = {}
for ext in CONFIG.sections():
backup_path = CONFIG.get(ext, 'Backup', fallback=None)
import_path = CONFIG.get(ext, 'Import', fallback=None)
if backup_path:
try:
backup_path = CONFIG.get(ext, 'Backup')
backups[ext] = Backup(backup_path)
except KeyError: # A section without backup is useless
raise BadConfiguration(f"{ext} has no backup path set !")
except Exception as err:
raise RuntimeError(
raise BadConfiguration(
f"Could not create a Backup at {backup_path}:\n{err}")
else:
raise RuntimeError(
f"Bad Configuration : {ext} has no backup path set !")
if import_path:
try:
import_path = CONFIG.get(ext, 'Import')
imports[ext] = Store(import_path)
except Exception as err:
raise RuntimeError(
f"Could not create import Store at {import_path}:\n{err}")
else:
except KeyError: # Could be wanted by user
print("Warning : {ext} will not be set to import.")
except Exception as err:
raise BadConfiguration(
f"Could not create import Store at {import_path}:\n{err}")
return camera, backups, imports

View File

@@ -1,7 +1,7 @@
# -*- coding:utf-8 -*-
from .store import Content, Store
from .config import load_config
from .config import load_config, BadConfiguration
def find_deleted():
@@ -11,8 +11,8 @@ def find_deleted():
def main(opts):
try:
camera_path, backups, imports = load_config()
except RuntimeError as err:
print(f"\x1b[31m Error ! \x1b[0m\n{err}")
except BadConfiguration as err:
print(f"\x1b[31mError! \x1b[0m\n{err}")
return False
try:

View File

@@ -1,14 +0,0 @@
# -*- coding:utf-8 -*-
import unittest
class TestBackup(unittest.TestCase):
def test_add(self):
pass
def test_list(self):
pass

View File

@@ -0,0 +1,21 @@
# -*- coding:utf-8 -*-
import unittest
class TestLoadConfig(unittest.TestCase):
def test_default_load_paths(self):
pass
def test_raises_exception_on_missing_backup_path(self):
pass
def test_warns_on_missing_import_path(self):
pass
def test_raises_exception_on_store_init_failure(self):
pass
def test_asserts_camera_path_is_present(self):
pass