diff --git a/photograb/config.py b/photograb/config.py index ab6e35a..e4bdf22 100644 --- a/photograb/config.py +++ b/photograb/config.py @@ -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: - backups[ext] = Backup(backup_path) - except Exception as err: - raise RuntimeError( - 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: - imports[ext] = Store(import_path) - except Exception as err: - raise RuntimeError( - f"Could not create import Store at {import_path}:\n{err}") - else: + 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 BadConfiguration( + f"Could not create a Backup at {backup_path}:\n{err}") + try: + import_path = CONFIG.get(ext, 'Import') + imports[ext] = Store(import_path) + 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 diff --git a/photograb/main.py b/photograb/main.py index 9085acb..640d7ee 100644 --- a/photograb/main.py +++ b/photograb/main.py @@ -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: diff --git a/photograb/test/test_backup.py b/photograb/test/test_backup.py deleted file mode 100644 index 26fd77f..0000000 --- a/photograb/test/test_backup.py +++ /dev/null @@ -1,14 +0,0 @@ -# -*- coding:utf-8 -*- - -import unittest - - -class TestBackup(unittest.TestCase): - - def test_add(self): - pass - - def test_list(self): - pass - - diff --git a/photograb/test/test_config.py b/photograb/test/test_config.py new file mode 100644 index 0000000..c23fe14 --- /dev/null +++ b/photograb/test/test_config.py @@ -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