Add --max-deep option

This commit is contained in:
Cédric Leporcq 2021-07-18 08:14:01 +02:00
parent 562ac26345
commit 1de9e963aa
2 changed files with 15 additions and 10 deletions

View File

@ -162,11 +162,6 @@ def _import(destination, source, file, album_from_folder, trash,
sys.exit(1) sys.exit(1)
# TODO
# recursive : bool
# True if you want src_dir to be searched recursively for files (False to search only in top-level of src_dir)
@click.command('sort') @click.command('sort')
@click.option('--debug', default=False, is_flag=True, @click.option('--debug', default=False, is_flag=True,
help='Override the value in constants.py with True.') help='Override the value in constants.py with True.')
@ -188,6 +183,8 @@ def _import(destination, source, file, album_from_folder, trash,
searching for file data. Example \'File:FileModifyDate\' or \'Filename\'' ) searching for file data. Example \'File:FileModifyDate\' or \'Filename\'' )
@click.option('--keep-folders', '-k', default=None, @click.option('--keep-folders', '-k', default=None,
help='Folder from given level are keep back') help='Folder from given level are keep back')
@click.option('--max-deep', '-m', default=None,
help='Maximum level to proceed. Number from 0 to desired level.')
@click.option('--remove-duplicates', '-r', default=False, is_flag=True, @click.option('--remove-duplicates', '-r', default=False, is_flag=True,
help='True to remove files that are exactly the same in name\ help='True to remove files that are exactly the same in name\
and a file hash') and a file hash')
@ -195,7 +192,7 @@ def _import(destination, source, file, album_from_folder, trash,
help='True if you want to see details of file processing') help='True if you want to see details of file processing')
@click.argument('paths', required=True, nargs=-1, type=click.Path()) @click.argument('paths', required=True, nargs=-1, type=click.Path())
def _sort(debug, dry_run, destination, copy, exclude_regex, filter_by_ext, ignore_tags, def _sort(debug, dry_run, destination, copy, exclude_regex, filter_by_ext, ignore_tags,
keep_folders, remove_duplicates, verbose, paths): keep_folders, max_deep, remove_duplicates, verbose, paths):
"""Sort files or directories by reading their EXIF and organizing them """Sort files or directories by reading their EXIF and organizing them
according to config.ini preferences. according to config.ini preferences.
""" """
@ -215,6 +212,9 @@ def _sort(debug, dry_run, destination, copy, exclude_regex, filter_by_ext, ignor
if keep_folders is not None: if keep_folders is not None:
keep_folders = int(keep_folders) keep_folders = int(keep_folders)
if max_deep is not None:
max_deep = int(max_deep)
logger = logging.getLogger('elodie') logger = logging.getLogger('elodie')
logger.setLevel(constants.debug) logger.setLevel(constants.debug)
@ -248,7 +248,7 @@ def _sort(debug, dry_run, destination, copy, exclude_regex, filter_by_ext, ignor
else: else:
day_begins = 0 day_begins = 0
filesystem = FileSystem(mode, dry_run, exclude_regex_list, logger, filesystem = FileSystem(mode, dry_run, exclude_regex_list, logger,
day_begins, filter_by_ext, keep_folders) day_begins, filter_by_ext, keep_folders, max_deep)
summary, has_errors = filesystem.sort_files(paths, destination, db, summary, has_errors = filesystem.sort_files(paths, destination, db,
remove_duplicates, ignore_tags) remove_duplicates, ignore_tags)

View File

@ -29,7 +29,8 @@ class FileSystem(object):
"""A class for interacting with the file system.""" """A class for interacting with the file system."""
def __init__(self, mode='copy', dry_run=False, exclude_regex_list=set(), def __init__(self, mode='copy', dry_run=False, exclude_regex_list=set(),
logger=logging.getLogger(), day_begins=0, filter_by_ext=(), keep_folders=None): logger=logging.getLogger(), day_begins=0, filter_by_ext=(),
keep_folders=None, max_deep=None):
# The default folder path is along the lines of 2017-06-17_01-04-14-dsc_1234-some-title.jpg # The default folder path is along the lines of 2017-06-17_01-04-14-dsc_1234-some-title.jpg
self.default_file_name_definition = { self.default_file_name_definition = {
'date': '%Y-%m-%d_%H-%M-%S', 'date': '%Y-%m-%d_%H-%M-%S',
@ -59,6 +60,7 @@ class FileSystem(object):
self.day_begins = day_begins self.day_begins = day_begins
self.filter_by_ext = filter_by_ext self.filter_by_ext = filter_by_ext
self.keep_folders = keep_folders self.keep_folders = keep_folders
self.max_deep = max_deep
# Instantiate a plugins object # Instantiate a plugins object
self.plugins = Plugins() self.plugins = Plugins()
@ -109,7 +111,9 @@ class FileSystem(object):
source: https://stackoverflow.com/questions/229186/os-walk-without-digging-into-directories-below source: https://stackoverflow.com/questions/229186/os-walk-without-digging-into-directories-below
""" """
src_path = src_path.rstrip(os.path.sep) src_path = src_path.rstrip(os.path.sep)
assert os.path.isdir(src_path) if not os.path.isdir(src_path):
return None
num_sep = src_path.count(os.path.sep) num_sep = src_path.count(os.path.sep)
for root, dirs, files in os.walk(src_path): for root, dirs, files in os.walk(src_path):
level = root.count(os.path.sep) - num_sep level = root.count(os.path.sep) - num_sep
@ -802,7 +806,8 @@ class FileSystem(object):
compiled_regex_list = [re.compile(regex) for regex in self.exclude_regex_list] compiled_regex_list = [re.compile(regex) for regex in self.exclude_regex_list]
subdirs = '' subdirs = ''
for dirname, dirnames, filenames, level in self.walklevel(path): for dirname, dirnames, filenames, level in self.walklevel(path,
self.max_deep):
if dirname == os.path.join(path, '.elodie'): if dirname == os.path.join(path, '.elodie'):
continue continue
if self.keep_folders is not None: if self.keep_folders is not None: