diff --git a/photograb/__main__.py b/photograb/__main__.py index 857d3e1..a29bf5e 100644 --- a/photograb/__main__.py +++ b/photograb/__main__.py @@ -28,21 +28,37 @@ __version__ = "0.1" import argparse from .main import main -parser = argparse.ArgumentParser() -parser.add_argument('-a', '--all', action="store_true") -parser.add_argument('-g', '--grab', action="store_true") -parser.add_argument('-s', '--sync', action="store_true") -parser.add_argument('-t', '--clean-trash', action="store_true") -parser.add_argument('-u', '--update-card', action="store_true") -parser.add_argument('--wipe-card', action="store_true") +parser = argparse.ArgumentParser(prog='Photograb') +parser.add_argument('-a', '--all', + action="store_true", + help="Perform all actions. Equivalent to -gst") +parser.add_argument('-g', '--grab', + action="store_true", + help="Grab files from camera") +parser.add_argument('-s', '--sync', + action="store_true", + help="Synchronize deleted files") +parser.add_argument('-t', '--clean-trash', + action="store_true", + help="Clean the bins") +parser.add_argument('-u', '--update-card', + action="store_true", + help="Use with sync, also update the camera card") +parser.add_argument('--wipe-card', + action="store_true", + help="Wipe the camera card") opts = parser.parse_args() -# --all, -a : Perform all actions (except --wipe-card !) -if opts.all: - opts.grab = True - opts.sync = True - opts.clean_trash = True -# Do not update card if we are going to wipe it anyway. -if opts.wipe_card: - opts.update_card = False -print(opts) -main(opts) + +# If no options are given, print the help message +if not any([is_set for is_set in vars(opts).values()]): + parser.print_help() +else: + # --all, -a : Perform all actions (except --wipe-card !) + if opts.all: + opts.grab = True + opts.sync = True + opts.clean_trash = True + # Do not update card if we are going to wipe it anyway. + if opts.wipe_card: + opts.update_card = False + main(opts) diff --git a/photograb/config.ini b/photograb/config.ini index a61a7bd..282d2ba 100644 --- a/photograb/config.ini +++ b/photograb/config.ini @@ -1,8 +1,8 @@ -[GLOBAL] -Import = /home/yunohost.app/lychee/import +[DEFAULT] Camera = /media/sdcard [jpg] +Import = /home/yunohost.app/lychee/import Backup = /media/multimedia/artus/Multimedia/Picture/Camera/ [mp4] diff --git a/photograb/config.py b/photograb/config.py index 3bf6c0d..e2bc2a5 100644 --- a/photograb/config.py +++ b/photograb/config.py @@ -1,10 +1,6 @@ # -*- coding:utf-8 -*- import configparser -from pathlib import Path CONFIG = configparser.ConfigParser() CONFIG.read("config.ini") - -IMPORT_PATH = Path(CONFIG["GLOBAL"]["Import"]) -CAMERA_PATH = Path(CONFIG["GLOBAL"]["Camera"]) diff --git a/photograb/main.py b/photograb/main.py index dd9a273..c357d3b 100644 --- a/photograb/main.py +++ b/photograb/main.py @@ -1,7 +1,43 @@ # -*- coding:utf-8 -*- +from configparser import ConfigParser +from pathlib import Path from .store import Content, Store, Backup -from .config import CONFIG, CAMERA_PATH + + +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(): @@ -9,11 +45,11 @@ def find_deleted(): def main(opts): - camera = Store(CAMERA_PATH) - backups = { - ext: Backup(CONFIG[ext]['Backup']) - for ext in CONFIG.sections() - } + try: + camera, backups, imports = load_config() + except RuntimeError as err: + print(f"\x1b[31m Error ! \x1b[0m\n{err}") + return None if opts.update_card or opts.wipe_card: # TODO: gracefully fail if camera is not writable. @@ -26,8 +62,9 @@ def main(opts): bkp_files = backup.read_content(exclude_trash=False) with_ext = camera_files.filter_by_ext(ext) new_files = with_ext.difference(bkp_files) - backup.copy_files(new_files) - # prepare_import(new_files) + backup.copy_content(new_files) + if ext in imports: + imports[ext].copy_content(new_files) if opts.sync: # Synchronize deleted files deleted = find_deleted()