From 3a1751015308e22b12d5fb790eed7d28ee2cd6dc Mon Sep 17 00:00:00 2001 From: artus Date: Fri, 16 Nov 2018 14:54:19 +0100 Subject: [PATCH] building base functionnality --- .gitignore | 2 ++ grab.py | 41 --------------------------- photograb/__init__.py | 1 + __main__.py => photograb/__main__.py | 1 - photograb/backup.py | 17 +++++++++++ config.py => photograb/config.py | 2 ++ photograb/grab.py | 21 ++++++++++++++ photograb/sync.py | 37 ++++++++++++++++++++++++ photograb/trash.py | 25 +++++++++++++++++ photograb/utils.py | 42 ++++++++++++++++++++++++++++ sync.py | 23 --------------- trash.py | 17 ----------- 12 files changed, 147 insertions(+), 82 deletions(-) create mode 100644 .gitignore delete mode 100644 grab.py create mode 100644 photograb/__init__.py rename __main__.py => photograb/__main__.py (99%) create mode 100644 photograb/backup.py rename config.py => photograb/config.py (88%) create mode 100644 photograb/grab.py create mode 100644 photograb/sync.py create mode 100644 photograb/trash.py create mode 100644 photograb/utils.py delete mode 100644 sync.py delete mode 100644 trash.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a60b85 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +*.pyc diff --git a/grab.py b/grab.py deleted file mode 100644 index f06b235..0000000 --- a/grab.py +++ /dev/null @@ -1,41 +0,0 @@ -# -*- coding:utf-8 -*- - -""" - Grab photos from camera and store them. -""" -import os -import shutil -import itertools -from .config import CAMERA_PATH, BACKUP_PATH, IMPORT_PATH - - -def _copy_files(files, target_dir): - print("copy {files} to {target_dir}") - # for f in files: - # shutil.copy(f, target_dir) - - -def _get_filenames(folder): - # TODO: exact location of files is lost, but will be used ! - # Could return a dict{ filename: full_path } - return set( - itertools.chain.from_iterable( - filenames for _, _, filenames in os.walk(folder) - )) - - -def backup(files): - _copy_files(files, BACKUP_PATH) - - -def prepare_import(files): - _copy_files(files, IMPORT_PATH) - - -def grab(): - # Get new files from camera - camera_files = _get_filenames(CAMERA_PATH) - backup_files = _get_filenames(BACKUP_PATH) - new_files = camera_files.difference(backup_files) - backup(new_files) - prepare_import(new_files) diff --git a/photograb/__init__.py b/photograb/__init__.py new file mode 100644 index 0000000..380474e --- /dev/null +++ b/photograb/__init__.py @@ -0,0 +1 @@ +# -*- coding:utf-8 -*- diff --git a/__main__.py b/photograb/__main__.py similarity index 99% rename from __main__.py rename to photograb/__main__.py index c210efa..b314575 100644 --- a/__main__.py +++ b/photograb/__main__.py @@ -18,4 +18,3 @@ sync(update_card=False) # If you want to wipe the card, pass --wipe. # TODO # Clean the trash, default behaviour. If you wish to skip it, --no-clean, -n. - diff --git a/photograb/backup.py b/photograb/backup.py new file mode 100644 index 0000000..7b952ef --- /dev/null +++ b/photograb/backup.py @@ -0,0 +1,17 @@ +# -*- coding:utf-8 -*- + +from .config import BACKUP_PATH, TRASH_NAME +from .utils import copy_files, get_content + + +def add(files): + """ Add a set of files to backup folder """ + copy_files(files, BACKUP_PATH) + + +def list(exclude_trash=True): + """ List files in backup folders """ + return get_content( + BACKUP_PATH, + exclude=TRASH_NAME if exclude_trash else None, + ) diff --git a/config.py b/photograb/config.py similarity index 88% rename from config.py rename to photograb/config.py index 662fd67..f274f86 100644 --- a/config.py +++ b/photograb/config.py @@ -4,4 +4,6 @@ IMPORT_PATH = "/home/yunohost.app/lychee/import/" BACKUP_PATH = "/media/multimedia/artus/Picture/Camera/original/" CAMERA_PATH = "/media/sdcard/" +TRASH_NAME = ".trash" + diff --git a/photograb/grab.py b/photograb/grab.py new file mode 100644 index 0000000..99b4880 --- /dev/null +++ b/photograb/grab.py @@ -0,0 +1,21 @@ +# -*- coding:utf-8 -*- + +""" + Grab photos from camera and store them. +""" +from .utils import copy_files, get_filenames +from .config import CAMERA_PATH, IMPORT_PATH +from . import backup + + +def prepare_import(content): + copy_files(content, IMPORT_PATH) + + +def grab(): + # Get new files from camera + camera_files = get_filenames(CAMERA_PATH) + backup_files = backup.list(exclude_trash=False) + new_files = camera_files.difference(backup_files) + backup.add(new_files) + prepare_import(new_files) diff --git a/photograb/sync.py b/photograb/sync.py new file mode 100644 index 0000000..cf8fb66 --- /dev/null +++ b/photograb/sync.py @@ -0,0 +1,37 @@ +# -*- coding:utf-8 -*- + +""" + Synchronize files from gallery. + + Mark deleted files from gallery, to be deleted at later time. +""" +import shutil +import time +from . import trash +from .utils import get_content +from .config import CAMERA_PATH + + +def find_deleted(): + pass + + +def remove_trashed_from_card(): + print("Removing trashed files from camera : ", end="") + trashed = trash.list() + on_card = get_content(CAMERA_PATH) + del_count = 0 + start_time = time.process_time() + for name, path in on_card.items(): + if name in trashed: + shutil.rm(path) + del_count += 1 + elapsed = time.process_time() - start_time + print(f"{del_count} in {elapsed}s.") + + +def sync(update_card=False): + deleted = find_deleted() + trash.add(deleted) + if update_card: + remove_trashed_from_card() diff --git a/photograb/trash.py b/photograb/trash.py new file mode 100644 index 0000000..e467a80 --- /dev/null +++ b/photograb/trash.py @@ -0,0 +1,25 @@ +# -*- coding:utf-8 -*- + +""" + Trash container +""" + +from .utils import move_files, get_content +from .config import BACKUP_PATH, TRASH_NAME + +TRASH_PATH = BACKUP_PATH + "/" + TRASH_NAME + +def add(files): + move_files(files, TRASH_PATH) + + +def list(): + return get_content(TRASH_PATH) + + +def manage_trash(): + """ Remove files that have been sitting there for too long """ + pass + + + diff --git a/photograb/utils.py b/photograb/utils.py new file mode 100644 index 0000000..432fb03 --- /dev/null +++ b/photograb/utils.py @@ -0,0 +1,42 @@ +# -*- coding:utf-8 -*- +import itertools +import os +# import shutil + + +def copy_files(content, target_dir): + print(f"copy {content} to {target_dir}") + # for name,path in content.items(): + # shutil.copy(path, target_dir) + + +def move_files(content, target_dir): + print(f"move {content} to {target_dir}") + # for name, path in content.items(): + # shutil.mv(path, target_dir) + + +class Content(dict): + + def difference(self, other): + """ Returns a new dict that only contains items from + this instance that are not present in the other + """ + my_keys = set(self.keys()) + your_keys = set(other.keys()) + new_keys = my_keys.difference(your_keys) + return dict(filter(lambda i: i[0] in new_keys, self.items())) + + +def get_content(folder, exclude=None): + # TODO: exact location of files is lost, but will be used ! + # Could return a dict{ filename: full_path } + result = Content() + for path, _, filenames in os.walk(folder): + # Ignore excluded folders + if path.split("/")[-1] == exclude: + continue + + for name in filenames: + result[name] = path + "/" + name + return result diff --git a/sync.py b/sync.py deleted file mode 100644 index a974949..0000000 --- a/sync.py +++ /dev/null @@ -1,23 +0,0 @@ -# -*- coding:utf-8 -*- - -""" - Synchronize files from gallery. - - Mark deleted files from gallery, to be deleted at later time. -""" -from .trash import move_to_trash - - -def find_deleted(): - pass - - -def delete_from_card(files): - pass - - -def sync(update_card=False): - deleted = find_deleted() - move_to_trash(deleted) - if update_card: - delete_from_card(deleted) diff --git a/trash.py b/trash.py deleted file mode 100644 index 5bd7d41..0000000 --- a/trash.py +++ /dev/null @@ -1,17 +0,0 @@ -# -*- coding:utf-8 -*- - -""" - Trash container -""" - - -def move_to_trash(files): - pass - - -def manage_trash(): - """ Remove files that have been sitting there for too long """ - pass - - -