From 1de9e963aa23006f8c6f4407cb8fd1092456a57d Mon Sep 17 00:00:00 2001 From: Cedric Leporcq Date: Sun, 18 Jul 2021 08:14:01 +0200 Subject: [PATCH] Add --max-deep option --- elodie.py | 14 +++++++------- elodie/filesystem.py | 11 ++++++++--- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/elodie.py b/elodie.py index cd367d1..fa5c815 100755 --- a/elodie.py +++ b/elodie.py @@ -162,11 +162,6 @@ def _import(destination, source, file, album_from_folder, trash, 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.option('--debug', default=False, is_flag=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\'' ) @click.option('--keep-folders', '-k', default=None, 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, help='True to remove files that are exactly the same in name\ 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') @click.argument('paths', required=True, nargs=-1, type=click.Path()) 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 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: keep_folders = int(keep_folders) + if max_deep is not None: + max_deep = int(max_deep) + logger = logging.getLogger('elodie') logger.setLevel(constants.debug) @@ -248,7 +248,7 @@ def _sort(debug, dry_run, destination, copy, exclude_regex, filter_by_ext, ignor else: day_begins = 0 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, remove_duplicates, ignore_tags) diff --git a/elodie/filesystem.py b/elodie/filesystem.py index 01c18ce..17df748 100644 --- a/elodie/filesystem.py +++ b/elodie/filesystem.py @@ -29,7 +29,8 @@ class FileSystem(object): """A class for interacting with the file system.""" 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 self.default_file_name_definition = { 'date': '%Y-%m-%d_%H-%M-%S', @@ -59,6 +60,7 @@ class FileSystem(object): self.day_begins = day_begins self.filter_by_ext = filter_by_ext self.keep_folders = keep_folders + self.max_deep = max_deep # Instantiate a plugins object self.plugins = Plugins() @@ -109,7 +111,9 @@ class FileSystem(object): source: https://stackoverflow.com/questions/229186/os-walk-without-digging-into-directories-below """ 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) for root, dirs, files in os.walk(src_path): 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] 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'): continue if self.keep_folders is not None: