Add --filter-by-ext option

This commit is contained in:
Cédric Leporcq 2021-07-17 14:09:47 +02:00
parent 2e84888c59
commit 98f4947917
4 changed files with 22 additions and 13 deletions

View File

@ -179,8 +179,8 @@ def _import(destination, source, file, album_from_folder, trash,
dest_dir rather than moved') dest_dir rather than moved')
@click.option('--exclude-regex', '-e', default=set(), multiple=True, @click.option('--exclude-regex', '-e', default=set(), multiple=True,
help='Regular expression for directories or files to exclude.') help='Regular expression for directories or files to exclude.')
@click.option('--filter-by-ext', '-f', default=False, help='''Use filename @click.option('--filter-by-ext', '-f', default=set(), multiple=True, help='''Use filename
extension to filter files for sorting. If used without argument, use extension to filter files for sorting. If value is '*', use
common media file extension for filtering. Ignored files remain in common media file extension for filtering. Ignored files remain in
the same directory structure''' ) the same directory structure''' )
@click.option('--ignore-tags', '-i', default=set(), multiple=True, @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: 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) day_begins, filter_by_ext)
summary, has_errors = filesystem.sort_files(paths, destination, db, summary, has_errors = filesystem.sort_files(paths, destination, db,
remove_duplicates) remove_duplicates)

View File

@ -20,7 +20,7 @@ from elodie.config import load_config
from elodie import constants from elodie import constants
from elodie.localstorage import Db 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.plugins.plugins import Plugins
from elodie.summary import Summary from elodie.summary import Summary
@ -29,7 +29,7 @@ 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): 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 # 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',
@ -57,6 +57,7 @@ class FileSystem(object):
self.logger = logger self.logger = logger
self.summary = Summary() self.summary = Summary()
self.day_begins = day_begins self.day_begins = day_begins
self.filter_by_ext = filter_by_ext
# Instantiate a plugins object # Instantiate a plugins object
self.plugins = Plugins() self.plugins = Plugins()
@ -107,12 +108,15 @@ class FileSystem(object):
:param tuple(str) extensions: File extensions to include (whitelist) :param tuple(str) extensions: File extensions to include (whitelist)
:returns: generator :returns: generator
""" """
# If extensions is None then we get all files if self.filter_by_ext != () and not extensions:
# if not extensions: # Filtering files by extensions.
# extensions = set() if '%media' in self.filter_by_ext:
# subclasses = media.get_all_subclasses() extensions = set()
# for cls in subclasses: subclasses = get_all_subclasses()
# extensions.update(cls.extensions) 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 # 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] compiled_regex_list = [re.compile(regex) for regex in exclude_regex_list]

View File

@ -56,8 +56,7 @@ def get_test_location():
return (61.013710, 99.196656, 'Siberia') return (61.013710, 99.196656, 'Siberia')
def populate_folder(number_of_files, include_invalid=False): def populate_folder(number_of_files, include_invalid=False):
folder = '%s/%s' % (tempfile.gettempdir(), random_string(10)) temporary_folder, folder = create_working_folder()
os.makedirs(folder)
for x in range(0, number_of_files): for x in range(0, number_of_files):
ext = 'jpg' if x % 2 == 0 else 'txt' ext = 'jpg' if x % 2 == 0 else 'txt'

View File

@ -133,6 +133,12 @@ def test_get_all_files_by_extension():
length = len(files) length = len(files)
assert length == 5, length 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 = set()
files.update(filesystem.get_all_files(folder, 'jpg')) files.update(filesystem.get_all_files(folder, 'jpg'))
length = len(files) length = len(files)