73 lines
2.4 KiB
Python
73 lines
2.4 KiB
Python
# -*- coding:utf-8 -*-
|
|
|
|
from .store import Content, Store
|
|
from .config import load_config, BadConfiguration
|
|
from .utils import print_error, print_success
|
|
|
|
|
|
def find_imported():
|
|
""" Finds files imported into the gallery """
|
|
return Content()
|
|
|
|
|
|
def main(opts):
|
|
try:
|
|
camera_path, backups, imports = load_config()
|
|
except BadConfiguration as err:
|
|
print_error(err)
|
|
return False
|
|
|
|
try:
|
|
camera = Store(camera_path)
|
|
except Exception:
|
|
print_error(f"Failed to read camera card at {camera_path}")
|
|
return False
|
|
|
|
if opts.update_card or opts.wipe_card:
|
|
# TODO: gracefully fail if camera is not writable.
|
|
pass
|
|
|
|
# Store some shared context
|
|
camera_files = camera.read_content()
|
|
new_files = {}
|
|
if opts.grab: # Grab new files from camera
|
|
print_success("Grabbing...")
|
|
for ext, backup in backups.items():
|
|
bkp_files = backup.read_content(exclude_trash=False)
|
|
with_ext = camera_files.filter_by_ext(ext)
|
|
new_files[ext] = with_ext.difference(bkp_files)
|
|
backup.copy_content(new_files[ext])
|
|
if ext in imports:
|
|
imports[ext].copy_content(new_files[ext])
|
|
|
|
if opts.sync: # Synchronize deleted files
|
|
print_success("Syncing...")
|
|
imported = find_imported()
|
|
for ext, backup in backups.items():
|
|
with_ext = imported.filter_by_ext(ext)
|
|
deleted = backup.read_content().difference(with_ext)
|
|
# Prevent from trashing new (thus not yet imported) files
|
|
deleted.difference(new_files[ext])
|
|
backup.trash_content(deleted)
|
|
if opts.update_card:
|
|
deleted = camera_files.difference(with_ext)
|
|
# Do not delete new files while syncing.
|
|
# TODO:
|
|
# /!\ Unwanted behaviour (remove new files)
|
|
# if --grab option is absent
|
|
deleted.difference(new_files[ext])
|
|
camera.remove_content(deleted)
|
|
|
|
if opts.wipe_card: # Remove all files from camera
|
|
for ext in backups:
|
|
to_delete = camera_files.filter_by_ext(ext)
|
|
camera.remove_content(to_delete)
|
|
|
|
if opts.clean_trash: # Clean the bins
|
|
print_success("Cleaning...")
|
|
for _, backup in backups.items():
|
|
backup.clean_trash()
|
|
|
|
print_success("Done !")
|
|
return True
|