Add --ignore-tags option

This commit is contained in:
Cédric Leporcq 2021-07-17 16:47:31 +02:00
parent 98f4947917
commit eba6fc991b
7 changed files with 35 additions and 15 deletions

View File

@ -246,7 +246,7 @@ def _sort(debug, dry_run, destination, copy, exclude_regex, filter_by_ext, ignor
day_begins, filter_by_ext)
summary, has_errors = filesystem.sort_files(paths, destination, db,
remove_duplicates)
remove_duplicates, ignore_tags)
if verbose or debug:
summary.write()

View File

@ -774,7 +774,8 @@ class FileSystem(object):
return files
def sort_files(self, paths, destination, db, remove_duplicates=False):
def sort_files(self, paths, destination, db, remove_duplicates=False,
ignore_tags=set()):
has_errors = False
for path in paths:
@ -784,7 +785,7 @@ class FileSystem(object):
conflict_file_list = set()
for src_path in files:
# Process files
media = get_media_class(src_path)
media = get_media_class(src_path, ignore_tags)
if media:
metadata = media.get_metadata()
# Get the destination path according to metadata

View File

@ -22,8 +22,8 @@ class Audio(Media):
#: Valid extensions for audio files.
extensions = ('m4a',)
def __init__(self, source=None):
super().__init__(source)
def __init__(self, source=None, ignore_tags=set()):
super().__init__(source, ignore_tags=set())
def is_valid(self):
"""Check the file extension against valid file extensions.

View File

@ -47,7 +47,7 @@ class Media():
extensions = PHOTO + AUDIO + VIDEO
def __init__(self, sources=None):
def __init__(self, sources=None, ignore_tags=set()):
self.source = sources
self.reset_cache()
self.date_original = [
@ -85,6 +85,7 @@ class Media():
self.set_gps_ref = True
self.metadata = None
self.exif_metadata = None
self.ignore_tags = ignore_tags
def format_metadata(self, **kwargs):
@ -223,7 +224,7 @@ class Media():
@classmethod
def get_class_by_file(cls, _file, classes):
def get_class_by_file(cls, _file, classes, ignore_tags=set()):
"""Static method to get a media object by file.
"""
basestring = (bytes, str)
@ -235,13 +236,13 @@ class Media():
if len(extension) > 0:
for i in classes:
if(extension in i.extensions):
return i(_file)
return i(_file, ignore_tags=ignore_tags)
exclude_list = ['.DS_Store', '.directory']
if os.path.basename(_file) == '.DS_Store':
return None
else:
return Media(_file)
return Media(_file, ignore_tags=ignore_tags)
@classmethod
@ -329,6 +330,14 @@ class Media():
#Cache exif metadata results and use if already exists for media
if(self.exif_metadata is None):
self.exif_metadata = ExifTool().get_metadata(source)
for tag_regex in self.ignore_tags:
ignored_tags = set()
for tag in self.exif_metadata:
if re.search(tag_regex, tag) is not None:
ignored_tags.add(tag)
for ignored_tag in ignored_tags:
del self.exif_metadata[ignored_tag]
if not self.exif_metadata:
return False
@ -606,13 +615,13 @@ def get_all_subclasses(cls=None):
return subclasses
def get_media_class(_file):
def get_media_class(_file, ignore_tags=set()):
if not os.path.exists(_file):
logging.warning(f'Could not find {_file}')
logging.error(f'Could not find {_file}')
return False
media = Media.get_class_by_file(_file, get_all_subclasses())
media = Media.get_class_by_file(_file, get_all_subclasses(), ignore_tags=set())
if not media:
logging.warning(f'File{_file} is not supported')
logging.error(f'File {_file} can\'t be imported')

View File

@ -24,8 +24,8 @@ class Photo(Media):
#: Valid extensions for photo files.
extensions = ('arw', 'cr2', 'dng', 'gif', 'heic', 'jpeg', 'jpg', 'nef', 'png', 'rw2')
def __init__(self, source=None):
super().__init__(source)
def __init__(self, source=None, ignore_tags=set()):
super().__init__(source, ignore_tags)
# We only want to parse EXIF once so we store it here
self.exif = None

View File

@ -27,8 +27,8 @@ class Video(Media):
#: Valid extensions for video files.
extensions = ('avi', 'm4v', 'mov', 'mp4', 'mpg', 'mpeg', '3gp', 'mts')
def __init__(self, source=None):
super().__init__(source)
def __init__(self, source=None, ignore_tags=set()):
super().__init__(source, ignore_tags=set())
# self.set_gps_ref = False

View File

@ -40,6 +40,16 @@ def test_get_class_by_file_without_extension():
assert cls is not None, cls
def test_get_exiftool_attribute():
file_path = helper.get_file('invalid.jpg')
ignore_tags = ('File:FileModifyDate', 'File:FileAccessDate')
media = Media.get_class_by_file(file_path, [Photo], ignore_tags)
exif = media.get_exiftool_attributes()
for tag in ignore_tags:
assert tag not in exif
def test_get_original_name():
temporary_folder, folder = helper.create_working_folder()