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) 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, ignore_tags)
if verbose or debug: if verbose or debug:
summary.write() summary.write()

View File

@ -774,7 +774,8 @@ class FileSystem(object):
return files 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 has_errors = False
for path in paths: for path in paths:
@ -784,7 +785,7 @@ class FileSystem(object):
conflict_file_list = set() conflict_file_list = set()
for src_path in files: for src_path in files:
# Process files # Process files
media = get_media_class(src_path) media = get_media_class(src_path, ignore_tags)
if media: if media:
metadata = media.get_metadata() metadata = media.get_metadata()
# Get the destination path according to metadata # Get the destination path according to metadata

View File

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

View File

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

View File

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

View File

@ -27,8 +27,8 @@ class Video(Media):
#: Valid extensions for video files. #: Valid extensions for video files.
extensions = ('avi', 'm4v', 'mov', 'mp4', 'mpg', 'mpeg', '3gp', 'mts') extensions = ('avi', 'm4v', 'mov', 'mp4', 'mpg', 'mpeg', '3gp', 'mts')
def __init__(self, source=None): def __init__(self, source=None, ignore_tags=set()):
super().__init__(source) super().__init__(source, ignore_tags=set())
# self.set_gps_ref = False # 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 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(): def test_get_original_name():
temporary_folder, folder = helper.create_working_folder() temporary_folder, folder = helper.create_working_folder()