Add --filter-by-ext option
This commit is contained in:
parent
2e84888c59
commit
98f4947917
|
@ -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)
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue