building base functionnality

This commit is contained in:
artus
2018-11-16 14:54:19 +01:00
parent 51294ccec4
commit 3a17510153
12 changed files with 147 additions and 82 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
__pycache__/
*.pyc

41
grab.py
View File

@@ -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)

1
photograb/__init__.py Normal file
View File

@@ -0,0 +1 @@
# -*- coding:utf-8 -*-

View File

@@ -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.

17
photograb/backup.py Normal file
View File

@@ -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,
)

View File

@@ -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"

21
photograb/grab.py Normal file
View File

@@ -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)

37
photograb/sync.py Normal file
View File

@@ -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()

25
photograb/trash.py Normal file
View File

@@ -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

42
photograb/utils.py Normal file
View File

@@ -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

23
sync.py
View File

@@ -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)

View File

@@ -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