From 98f494791784ae8c298835baf703b6a4c74a5148 Mon Sep 17 00:00:00 2001 From: Cedric Leporcq Date: Sat, 17 Jul 2021 14:09:47 +0200 Subject: [PATCH] Add --filter-by-ext option --- elodie.py | 6 +++--- elodie/filesystem.py | 20 ++++++++++++-------- tests/helper.py | 3 +-- tests/test_filesystem.py | 6 ++++++ 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/elodie.py b/elodie.py index c3fd29f..d7fbb88 100755 --- a/elodie.py +++ b/elodie.py @@ -179,8 +179,8 @@ def _import(destination, source, file, album_from_folder, trash, dest_dir rather than moved') @click.option('--exclude-regex', '-e', default=set(), multiple=True, help='Regular expression for directories or files to exclude.') -@click.option('--filter-by-ext', '-f', default=False, help='''Use filename - extension to filter files for sorting. If used without argument, use +@click.option('--filter-by-ext', '-f', default=set(), multiple=True, help='''Use filename + extension to filter files for sorting. If value is '*', use common media file extension for filtering. Ignored files remain in the same directory structure''' ) @click.option('--ignore-tags', '-i', default=set(), multiple=True, @@ -243,7 +243,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) + day_begins, filter_by_ext) summary, has_errors = filesystem.sort_files(paths, destination, db, remove_duplicates) diff --git a/elodie/filesystem.py b/elodie/filesystem.py index 0ad61f5..b05becc 100644 --- a/elodie/filesystem.py +++ b/elodie/filesystem.py @@ -20,7 +20,7 @@ from elodie.config import load_config from elodie import constants from elodie.localstorage import Db -from elodie.media.media import get_media_class +from elodie.media.media import get_media_class, get_all_subclasses from elodie.plugins.plugins import Plugins from elodie.summary import Summary @@ -29,7 +29,7 @@ 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): + logger=logging.getLogger(), day_begins=0, filter_by_ext=()): # 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', @@ -57,6 +57,7 @@ class FileSystem(object): self.logger = logger self.summary = Summary() self.day_begins = day_begins + self.filter_by_ext = filter_by_ext # Instantiate a plugins object self.plugins = Plugins() @@ -107,12 +108,15 @@ class FileSystem(object): :param tuple(str) extensions: File extensions to include (whitelist) :returns: generator """ - # If extensions is None then we get all files - # if not extensions: - # extensions = set() - # subclasses = media.get_all_subclasses() - # for cls in subclasses: - # extensions.update(cls.extensions) + if self.filter_by_ext != () and not extensions: + # Filtering files by extensions. + if '%media' in self.filter_by_ext: + extensions = set() + subclasses = get_all_subclasses() + for cls in subclasses: + extensions.update(cls.extensions) + else: + extensions = self.filter_by_ext # Create a list of compiled regular expressions to match against the file path compiled_regex_list = [re.compile(regex) for regex in exclude_regex_list] diff --git a/tests/helper.py b/tests/helper.py index ac01303..334df5c 100644 --- a/tests/helper.py +++ b/tests/helper.py @@ -56,8 +56,7 @@ def get_test_location(): return (61.013710, 99.196656, 'Siberia') def populate_folder(number_of_files, include_invalid=False): - folder = '%s/%s' % (tempfile.gettempdir(), random_string(10)) - os.makedirs(folder) + temporary_folder, folder = create_working_folder() for x in range(0, number_of_files): ext = 'jpg' if x % 2 == 0 else 'txt' diff --git a/tests/test_filesystem.py b/tests/test_filesystem.py index 289a14f..1df2cd6 100644 --- a/tests/test_filesystem.py +++ b/tests/test_filesystem.py @@ -133,6 +133,12 @@ def test_get_all_files_by_extension(): length = len(files) assert length == 5, length + files = set() + filesystem = FileSystem(filter_by_ext=('%media',)) + files.update(filesystem.get_all_files(folder)) + length = len(files) + assert length == 3, length + files = set() files.update(filesystem.get_all_files(folder, 'jpg')) length = len(files)