rewrites config, adds error handling
This commit is contained in:
@@ -28,14 +28,31 @@ __version__ = "0.1"
|
|||||||
import argparse
|
import argparse
|
||||||
from .main import main
|
from .main import main
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser(prog='Photograb')
|
||||||
parser.add_argument('-a', '--all', action="store_true")
|
parser.add_argument('-a', '--all',
|
||||||
parser.add_argument('-g', '--grab', action="store_true")
|
action="store_true",
|
||||||
parser.add_argument('-s', '--sync', action="store_true")
|
help="Perform all actions. Equivalent to -gst")
|
||||||
parser.add_argument('-t', '--clean-trash', action="store_true")
|
parser.add_argument('-g', '--grab',
|
||||||
parser.add_argument('-u', '--update-card', action="store_true")
|
action="store_true",
|
||||||
parser.add_argument('--wipe-card', 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()
|
opts = parser.parse_args()
|
||||||
|
|
||||||
|
# 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 !)
|
# --all, -a : Perform all actions (except --wipe-card !)
|
||||||
if opts.all:
|
if opts.all:
|
||||||
opts.grab = True
|
opts.grab = True
|
||||||
@@ -44,5 +61,4 @@ if opts.all:
|
|||||||
# Do not update card if we are going to wipe it anyway.
|
# Do not update card if we are going to wipe it anyway.
|
||||||
if opts.wipe_card:
|
if opts.wipe_card:
|
||||||
opts.update_card = False
|
opts.update_card = False
|
||||||
print(opts)
|
|
||||||
main(opts)
|
main(opts)
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
[GLOBAL]
|
[DEFAULT]
|
||||||
Import = /home/yunohost.app/lychee/import
|
|
||||||
Camera = /media/sdcard
|
Camera = /media/sdcard
|
||||||
|
|
||||||
[jpg]
|
[jpg]
|
||||||
|
Import = /home/yunohost.app/lychee/import
|
||||||
Backup = /media/multimedia/artus/Multimedia/Picture/Camera/
|
Backup = /media/multimedia/artus/Multimedia/Picture/Camera/
|
||||||
|
|
||||||
[mp4]
|
[mp4]
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
# -*- coding:utf-8 -*-
|
# -*- coding:utf-8 -*-
|
||||||
|
|
||||||
import configparser
|
import configparser
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
CONFIG = configparser.ConfigParser()
|
CONFIG = configparser.ConfigParser()
|
||||||
CONFIG.read("config.ini")
|
CONFIG.read("config.ini")
|
||||||
|
|
||||||
IMPORT_PATH = Path(CONFIG["GLOBAL"]["Import"])
|
|
||||||
CAMERA_PATH = Path(CONFIG["GLOBAL"]["Camera"])
|
|
||||||
|
|||||||
@@ -1,7 +1,43 @@
|
|||||||
# -*- coding:utf-8 -*-
|
# -*- coding:utf-8 -*-
|
||||||
|
|
||||||
|
from configparser import ConfigParser
|
||||||
|
from pathlib import Path
|
||||||
from .store import Content, Store, Backup
|
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():
|
def find_deleted():
|
||||||
@@ -9,11 +45,11 @@ def find_deleted():
|
|||||||
|
|
||||||
|
|
||||||
def main(opts):
|
def main(opts):
|
||||||
camera = Store(CAMERA_PATH)
|
try:
|
||||||
backups = {
|
camera, backups, imports = load_config()
|
||||||
ext: Backup(CONFIG[ext]['Backup'])
|
except RuntimeError as err:
|
||||||
for ext in CONFIG.sections()
|
print(f"\x1b[31m Error ! \x1b[0m\n{err}")
|
||||||
}
|
return None
|
||||||
|
|
||||||
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.
|
||||||
@@ -26,8 +62,9 @@ def main(opts):
|
|||||||
bkp_files = backup.read_content(exclude_trash=False)
|
bkp_files = backup.read_content(exclude_trash=False)
|
||||||
with_ext = camera_files.filter_by_ext(ext)
|
with_ext = camera_files.filter_by_ext(ext)
|
||||||
new_files = with_ext.difference(bkp_files)
|
new_files = with_ext.difference(bkp_files)
|
||||||
backup.copy_files(new_files)
|
backup.copy_content(new_files)
|
||||||
# prepare_import(new_files)
|
if ext in imports:
|
||||||
|
imports[ext].copy_content(new_files)
|
||||||
|
|
||||||
if opts.sync: # Synchronize deleted files
|
if opts.sync: # Synchronize deleted files
|
||||||
deleted = find_deleted()
|
deleted = find_deleted()
|
||||||
|
|||||||
Reference in New Issue
Block a user