Add path selection in interactive mode using inquierer

This commit is contained in:
Cédric Leporcq 2021-09-19 12:52:26 +02:00
parent 63b154b8f3
commit 6203498f20
3 changed files with 72 additions and 14 deletions

View File

@ -6,6 +6,7 @@ from builtins import object
import filecmp import filecmp
from fnmatch import fnmatch from fnmatch import fnmatch
import hashlib import hashlib
import inquirer
import logging import logging
import os import os
from pathlib import Path, PurePath from pathlib import Path, PurePath
@ -18,6 +19,7 @@ from ordigi import media
from ordigi.database import Sqlite from ordigi.database import Sqlite
from ordigi.media import Media, get_all_subclasses from ordigi.media import Media, get_all_subclasses
from ordigi.images import Image, Images from ordigi.images import Image, Images
from ordigi import request
from ordigi.summary import Summary from ordigi.summary import Summary
from ordigi.utils import get_date_regex, camel2snake from ordigi.utils import get_date_regex, camel2snake
@ -62,6 +64,9 @@ class Collection(object):
self.summary = Summary() self.summary = Summary()
self.whitespace_regex = '[ \t\n\r\f\v]+' self.whitespace_regex = '[ \t\n\r\f\v]+'
# Constants
self.theme = request.load_theme()
def get_items(self): def get_items(self):
return { return {
'album': '{album}', 'album': '{album}',
@ -477,8 +482,7 @@ class Collection(object):
:returns: Path file_path, Path subdirs :returns: Path file_path, Path subdirs
""" """
for path0 in path.glob(glob): for path0 in path.glob(glob):
if path0.is_dir(): if path0.is_dir(): continue
continue
else: else:
file_path = path0 file_path = path0
parts = file_path.parts parts = file_path.parts
@ -488,16 +492,18 @@ class Collection(object):
else: else:
level = len(subdirs.parts) level = len(subdirs.parts)
if file_path.parts[0] == '.ordigi': if file_path.parts[0] == '.ordigi': continue
continue
if maxlevel is not None: if maxlevel is not None:
if level > maxlevel: if level > maxlevel: continue
continue
matched = False
for exclude in self.exclude: for exclude in self.exclude:
if fnmatch(file_path, exclude): if fnmatch(file_path, exclude):
continue matched = True
break
if matched: continue
if ( if (
extensions == set() extensions == set()
@ -599,7 +605,8 @@ class Collection(object):
] ]
conflict_file_list = [] conflict_file_list = []
for src_path in self._get_files_in_path(path, glob=self.glob): file_list = [x for x in self._get_files_in_path(path, glob=self.glob)]
for src_path in file_list:
src_checksum = self.checksum(src_path) src_checksum = self.checksum(src_path)
path_parts = src_path.relative_to(self.root).parts path_parts = src_path.relative_to(self.root).parts
dedup_path = [] dedup_path = []
@ -638,6 +645,21 @@ class Collection(object):
return self.summary, has_errors return self.summary, has_errors
def _modify_selection(self, file_list):
"""
:params: list
:return: list
"""
message="Bellow the file selection list, modify selection if needed"
questions = [
inquirer.Checkbox('selection',
message=message,
choices=file_list,
default=file_list,
),
]
return inquirer.prompt(questions, theme=self.theme)['selection']
def sort_files(self, paths, loc, remove_duplicates=False, def sort_files(self, paths, loc, remove_duplicates=False,
ignore_tags=set()): ignore_tags=set()):
""" """
@ -647,8 +669,12 @@ class Collection(object):
for path in paths: for path in paths:
path = self._check_path(path) path = self._check_path(path)
conflict_file_list = [] conflict_file_list = []
for src_path in self._get_files_in_path(path, glob=self.glob, file_list = [x for x in self._get_files_in_path(path,
extensions=self.filter_by_ext): glob=self.glob, extensions=self.filter_by_ext)]
if self.interactive:
file_list = self._modify_selection(file_list)
print('Processing...')
for src_path in file_list:
subdirs = src_path.relative_to(path).parent subdirs = src_path.relative_to(path).parent
# Process files # Process files
src_checksum = self.checksum(src_path) src_checksum = self.checksum(src_path)

View File

@ -2,15 +2,19 @@
Media :class:`Media` class to get file metadata Media :class:`Media` class to get file metadata
""" """
import inquirer
import logging import logging
import mimetypes import mimetypes
import os import os
# import pprint
# load modules # load modules
from dateutil.parser import parse from dateutil.parser import parse
import re import re
from ordigi.exiftool import ExifTool, ExifToolCaching from ordigi.exiftool import ExifTool, ExifToolCaching
from ordigi.utils import get_date_from_string from ordigi.utils import get_date_from_string
from ordigi import request
class Media(): class Media():
@ -49,6 +53,8 @@ class Media():
self.metadata = None self.metadata = None
self.logger = logger self.logger = logger
self.theme = request.load_theme()
def get_tags(self): def get_tags(self):
tags_keys = {} tags_keys = {}
tags_keys['date_original'] = [ tags_keys['date_original'] = [
@ -235,6 +241,30 @@ class Media():
# Get metadata from exiftool. # Get metadata from exiftool.
self.exif_metadata = ExifToolCaching(self.file_path, logger=self.logger).asdict() self.exif_metadata = ExifToolCaching(self.file_path, logger=self.logger).asdict()
def _set_album(self, album, folder):
print(f"Conflict for file: {self.file_path}")
choices_list = [
inquirer.List('album',
message=f"Exif album is already set to {album}, choices",
choices=[
(f"album:'{album}'", f'{album}'),
(f"folder:'{folder}'", f'{folder}'),
("custom", None),
],
default=f'{album}'
),
]
prompt = [
inquirer.Text('custom', message="album"),
]
answers = inquirer.prompt(choices_list, theme=self.theme)
if not answers['album']:
answers = inquirer.prompt(prompt, theme=self.theme)
return answers['custom']
else:
return answers['album']
def get_metadata(self, loc=None, db=None, cache=False): def get_metadata(self, loc=None, db=None, cache=False):
"""Get a dictionary of metadata from exif. """Get a dictionary of metadata from exif.
All keys will be present and have a value of None if not obtained. All keys will be present and have a value of None if not obtained.
@ -276,10 +306,11 @@ class Media():
folder = self.folder folder = self.folder
if album and album != '': if album and album != '':
if self.interactive: if self.interactive:
print(f"Conflict for file: {self.file_path}") answer = self._set_album(album, folder)
print(f"Exif album is already set to '{album}'', folder='{folder}'") # print(f"Conflict for file: {self.file_path}")
i = f"Choice for 'album': (a) '{album}', (f) '{folder}', (c) custom ?\n" # print(f"Exif album is already set to '{album}'', folder='{folder}'")
answer = input(i) # i = f"Choice for 'album': (a) '{album}', (f) '{folder}', (c) custom ?\n"
# answer = input(i)
if answer == 'c': if answer == 'c':
self.metadata['album'] = input('album=') self.metadata['album'] = input('album=')
self.set_value('album', folder) self.set_value('album', folder)

View File

@ -1,5 +1,6 @@
click==6.6 click==6.6
imagehash==4.2.1 imagehash==4.2.1
inquirer
requests==2.20.0 requests==2.20.0
Send2Trash==1.3.0 Send2Trash==1.3.0
configparser==3.5.0 configparser==3.5.0