From 18c6d8e44b15fe1152108bf084c4f0431636b954 Mon Sep 17 00:00:00 2001 From: "Antonio J. Delgado" Date: Tue, 17 May 2022 09:04:37 +0300 Subject: [PATCH] remove extra join --- image_classifier/image_classifier.py | 38 +++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/image_classifier/image_classifier.py b/image_classifier/image_classifier.py index 1e2a365..2bb0e00 100755 --- a/image_classifier/image_classifier.py +++ b/image_classifier/image_classifier.py @@ -49,7 +49,7 @@ class CustomFormatter(logging.Formatter): class image_classifier: def __init__(self, debug_level, log_file, faces_directory, directory, no_move, - people_folder): + people_folder, recursive): ''' Initial function called when object is created ''' self.debug_level = debug_level if log_file is None: @@ -62,12 +62,34 @@ class image_classifier: self.known_people = self.load_known_people() self.no_move = no_move self.people_folder = people_folder + self.recursive = recursive if os.access(directory, os.R_OK): - with os.scandir(directory) as directory_item: - for entry in directory_item: - if not entry.name.startswith('.') and entry.is_file(): - self.process_file(os.path.join(directory, entry.name)) + if self.recursive: + entries = self.recursive_scandir(directory) + else: + entries = os.scandir(directory) + for entry in entries: + if not entry.name.startswith('.') and entry.is_file(): + self.process_file(entry.path) + + def recursive_scandir(path, ignore_hidden_files=True): + files = [] + try: + for file in os.scandir(path): + if not file.name.startswith('.'): + if file.is_file(): + files.append(file) + elif file.is_dir(follow_symlinks=False): + more_files = recursive_scandir(file.path, + ignore_hidden_files=ignore_hidden_files + ) + if more_files: + files = files + more_files + except PermissionError as error: + self._log.warning(f"Permission denied accessing folder '{path}'") + return files + def process_metadata(self, file): self.metadata = pyexiv2.ImageMetadata(file) @@ -277,10 +299,12 @@ identify people. Filename would be used as the name for the person. Just one per @click.option('--people-folder', '-p', help="Define a folder for people's folders and copy \ pictures to each person's folder. Be sure to have deduplication in the filesystem to avoid using \ too much storage.") +@click.option('--recursive', '-r', is_flag=True, help='Recursively search for files in the provided --directory') @click_config_file.configuration_option() -def __main__(debug_level, log_file, faces_directory, directory, no_move, people_folder): +def __main__(debug_level, log_file, faces_directory, directory, no_move, + people_folder, recursive): return image_classifier(debug_level, log_file, faces_directory, directory, no_move, - people_folder) + people_folder, recursive) if __name__ == "__main__":