From effbf6e41de6ccdfaef877eb50fd122ea9a0ba2f Mon Sep 17 00:00:00 2001 From: artus Date: Tue, 20 Nov 2018 14:06:18 +0100 Subject: [PATCH] moves load_config in its own module --- photograb/config.py | 39 +++++++++++++++++++++++++++++++++-- photograb/main.py | 50 +++++++++------------------------------------ 2 files changed, 47 insertions(+), 42 deletions(-) diff --git a/photograb/config.py b/photograb/config.py index e2bc2a5..ab6e35a 100644 --- a/photograb/config.py +++ b/photograb/config.py @@ -2,5 +2,40 @@ import configparser -CONFIG = configparser.ConfigParser() -CONFIG.read("config.ini") +from pathlib import Path +from .store import Store, Backup + + +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 !") + 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: + print("Warning : {ext} will not be set to import.") + + return camera, backups, imports diff --git a/photograb/main.py b/photograb/main.py index c357d3b..9085acb 100644 --- a/photograb/main.py +++ b/photograb/main.py @@ -1,43 +1,7 @@ # -*- coding:utf-8 -*- -from configparser import ConfigParser -from pathlib import Path -from .store import Content, Store, Backup - - -def load_config(): - CONFIG = 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 !") - 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: - print("Warning : {ext} will not be set to import.") - - return camera, backups, imports +from .store import Content, Store +from .config import load_config def find_deleted(): @@ -46,10 +10,16 @@ def find_deleted(): def main(opts): try: - camera, backups, imports = load_config() + camera_path, backups, imports = load_config() except RuntimeError as err: print(f"\x1b[31m Error ! \x1b[0m\n{err}") - return None + return False + + try: + camera = Store(camera_path) + except Exception: + print("Err: Failed to initialize a Store at {camera_path]") + return False if opts.update_card or opts.wipe_card: # TODO: gracefully fail if camera is not writable.