moves load_config in its own module

This commit is contained in:
artus
2018-11-20 14:06:18 +01:00
parent 74dc2320a1
commit effbf6e41d
2 changed files with 47 additions and 42 deletions

View File

@@ -2,5 +2,40 @@
import configparser import configparser
from pathlib import Path
from .store import Store, Backup
def load_config():
CONFIG = configparser.ConfigParser() CONFIG = configparser.ConfigParser()
CONFIG.read("config.ini") 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

View File

@@ -1,43 +1,7 @@
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
from configparser import ConfigParser from .store import Content, Store
from pathlib import Path from .config import load_config
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
def find_deleted(): def find_deleted():
@@ -46,10 +10,16 @@ def find_deleted():
def main(opts): def main(opts):
try: try:
camera, backups, imports = load_config() camera_path, backups, imports = load_config()
except RuntimeError as err: except RuntimeError as err:
print(f"\x1b[31m Error ! \x1b[0m\n{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: if opts.update_card or opts.wipe_card:
# TODO: gracefully fail if camera is not writable. # TODO: gracefully fail if camera is not writable.