From eba6fc991b3b0c17f6e4f9c24bb6c0a40425cb68 Mon Sep 17 00:00:00 2001 From: Cedric Leporcq Date: Sat, 17 Jul 2021 16:47:31 +0200 Subject: [PATCH] Add --ignore-tags option --- elodie.py | 2 +- elodie/filesystem.py | 5 +++-- elodie/media/audio.py | 4 ++-- elodie/media/media.py | 21 +++++++++++++++------ elodie/media/photo.py | 4 ++-- elodie/media/video.py | 4 ++-- tests/media/test_media.py | 10 ++++++++++ 7 files changed, 35 insertions(+), 15 deletions(-) diff --git a/elodie.py b/elodie.py index d7fbb88..515a386 100755 --- a/elodie.py +++ b/elodie.py @@ -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() diff --git a/elodie/filesystem.py b/elodie/filesystem.py index b05becc..3012e88 100644 --- a/elodie/filesystem.py +++ b/elodie/filesystem.py @@ -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 diff --git a/elodie/media/audio.py b/elodie/media/audio.py index 17684e2..edc5b4a 100644 --- a/elodie/media/audio.py +++ b/elodie/media/audio.py @@ -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. diff --git a/elodie/media/media.py b/elodie/media/media.py index 2227038..50051ac 100644 --- a/elodie/media/media.py +++ b/elodie/media/media.py @@ -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') diff --git a/elodie/media/photo.py b/elodie/media/photo.py index a655c27..7475230 100644 --- a/elodie/media/photo.py +++ b/elodie/media/photo.py @@ -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 diff --git a/elodie/media/video.py b/elodie/media/video.py index 448b2fe..aac9727 100644 --- a/elodie/media/video.py +++ b/elodie/media/video.py @@ -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 diff --git a/tests/media/test_media.py b/tests/media/test_media.py index fe72b6a..44702ea 100644 --- a/tests/media/test_media.py +++ b/tests/media/test_media.py @@ -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()