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

1
photograb/__init__.py Normal file
View File

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

20
photograb/__main__.py Normal file
View File

@@ -0,0 +1,20 @@
#!/bin/python3
# -*- coding:utf-8 -*-
"""
Photograb
"""
__version__ = "0.1"
from .grab import grab
from .sync import sync
# Grab the files
grab()
# Synchronize deleted files, pass --update-card, -u to update the card too.
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,
)

9
photograb/config.py Normal file
View File

@@ -0,0 +1,9 @@
# -*- coding:utf-8 -*-
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