From e0fc31e5438f3d7e96151af28185426066d12f23 Mon Sep 17 00:00:00 2001 From: Cedric Leporcq Date: Sat, 29 Jan 2022 08:08:30 +0100 Subject: [PATCH] Move get_date_format method in media and fixes --- ordigi/collection.py | 3 +-- ordigi/config.py | 2 +- ordigi/media.py | 50 ++++++++++++++++++++-------------------- tests/test_collection.py | 14 +++++------ 4 files changed, 33 insertions(+), 36 deletions(-) diff --git a/ordigi/collection.py b/ordigi/collection.py index 695463c..b56ee45 100644 --- a/ordigi/collection.py +++ b/ordigi/collection.py @@ -979,8 +979,7 @@ class Collection(SortMedias): return self.summary def sort_files( - self, src_dirs, path_format, loc, - imp=False, remove_duplicates=False + self, src_dirs, path_format, loc, imp=False, remove_duplicates=False ): """ Sort files into appropriate folder diff --git a/ordigi/config.py b/ordigi/config.py index 2fc218a..5ba62ce 100644 --- a/ordigi/config.py +++ b/ordigi/config.py @@ -60,7 +60,7 @@ class Config: return { 'Exif': { 'album_from_folder': False, - 'cache': False, + 'cache': True, 'ignore_tags': None, 'use_date_filename': False, 'use_file_dates': False, diff --git a/ordigi/media.py b/ordigi/media.py index cb8a77d..52173dd 100644 --- a/ordigi/media.py +++ b/ordigi/media.py @@ -12,7 +12,6 @@ from ordigi import utils from ordigi import request - class ExifMetadata: def __init__(self, file_path, ignore_tags=None): @@ -20,6 +19,7 @@ class ExifMetadata: if ignore_tags is None: ignore_tags = set() self.ignore_tags = ignore_tags + self.log = LOG.getChild(self.__class__.__name__) self.tags_keys = self.get_tags() def get_tags(self) -> dict: @@ -67,6 +67,27 @@ class ExifMetadata: return tags_keys + def get_date_format(self, value): + """ + Formatting date attribute. + :returns: datetime object or None + """ + # We need to parse a string to datetime format. + # EXIF DateTimeOriginal and EXIF DateTime are both stored + # in %Y:%m:%d %H:%M:%S format + if value is None: + return None + + try: + # correct nasty formated date + regex = re.compile(r'(\d{4}):(\d{2}):(\d{2})') + if re.match(regex, value) is not None: # noqa + value = re.sub(regex, r'\g<1>-\g<2>-\g<3>', value) + return parser.parse(value) + except BaseException or parser._parser.ParserError as e: + self.log.warning(e.args, value) + return None + class ReadExif(ExifMetadata): """Read exif metadata to file""" @@ -76,7 +97,7 @@ class ReadExif(ExifMetadata): file_path, exif_metadata=None, ignore_tags=None, - ): + ): super().__init__(file_path, ignore_tags) @@ -96,7 +117,7 @@ class ReadExif(ExifMetadata): def get_key_values(self, key): """ - Get the first value of a tag set + Get tags values of a key :returns: str or None if no exif tag """ if self.exif_metadata is None: @@ -224,6 +245,7 @@ class WriteExif(ExifMetadata): # TODO use tag key return self.set_value('Album', self.file_path.parent.name) + class Media(ReadExif): """ Extract matadatas from exiftool and sort them to dict structure @@ -257,7 +279,6 @@ class Media(ReadExif): self.theme = request.load_theme() - def get_mimetype(self): """ Get the mimetype of the file. @@ -270,27 +291,6 @@ class Media(ReadExif): return mimetype[0] - def get_date_format(self, value): - """ - Formatting date attribute. - :returns: datetime object or None - """ - # We need to parse a string to datetime format. - # EXIF DateTimeOriginal and EXIF DateTime are both stored - # in %Y:%m:%d %H:%M:%S format - if value is None: - return None - - try: - # correct nasty formated date - regex = re.compile(r'(\d{4}):(\d{2}):(\d{2})') - if re.match(regex, value) is not None: # noqa - value = re.sub(regex, r'\g<1>-\g<2>-\g<3>', value) - return parser.parse(value) - except BaseException or parser._parser.ParserError as e: - self.log.warning(e.args, value) - return None - def _get_date_media_interactive(self, choices, default): print(f"Date conflict for file: {self.file_path}") choices_list = [ diff --git a/tests/test_collection.py b/tests/test_collection.py index d31f544..7d8d26b 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -137,7 +137,7 @@ class TestCollection: assert summary.success_table.sum('sort') == nb def test_sort_files(self, tmp_path): - cli_options = {'album_from_folder': True} + cli_options = {'album_from_folder': True, 'cache': False} collection = Collection(tmp_path, cli_options=cli_options) loc = GeoLocation() summary = collection.sort_files([self.src_path], @@ -160,24 +160,22 @@ class TestCollection: paths = Paths(filters).get_files(tmp_path) for file_path in paths: if '.db' not in str(file_path): - media = Media(file_path, tmp_path, album_from_folder=True) for value in ReadExif(file_path).get_key_values('album'): assert value != '' or None collection = Collection(tmp_path, cli_options=cli_options) # Try to change path format and sort files again - path = '{city}/{%Y}-{name}.%l{ext}' - summary = collection.sort_files([tmp_path], - self.path_format, loc) + path_format = 'test_exif/{city}/{%Y}-{name}.%l{ext}' + summary = collection.sort_files([tmp_path], path_format, loc) - self.assert_sort(summary, 24) + self.assert_sort(summary, 27) shutil.copytree(tmp_path / 'test_exif', tmp_path / 'test_exif_copy') collection.summary = Summary(tmp_path) assert collection.summary.success_table.sum() == 0 summary = collection.update(loc) - assert summary.success_table.sum('update') == 2 - assert summary.success_table.sum() == 2 + assert summary.success_table.sum('update') == 30 + assert summary.success_table.sum() == 30 assert not summary.errors collection.summary = Summary(tmp_path) summary = collection.update(loc)