From e18ece5ae92f44b42d502a1afcce0df64ca84fba Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Fri, 15 Oct 2021 09:06:57 +0200 Subject: [PATCH] Add test and docs --- docs/image_information.rst | 3 +++ sigal/gallery.py | 18 +++++++++--------- tests/sample/pictures/video/example video.md | 1 + tests/test_video.py | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 tests/sample/pictures/video/example video.md diff --git a/docs/image_information.rst b/docs/image_information.rst index 529b46c..671ed2d 100644 --- a/docs/image_information.rst +++ b/docs/image_information.rst @@ -6,6 +6,7 @@ Additional information on an image can be given in a file using the `markdown`_ syntax, named ``.md`` (example: ``IMG_5206.md``):: Title: My awesome photo + Date: 2020-01-01T09:00:00 And a description with *Markdown* syntax. @@ -13,6 +14,8 @@ EXIF data is directly extracted, see :ref:`simple-exif-data`. Some meta-data keys are used by Sigal to get the useful informations on the gallery: - *Title*: the image title. +- *Date*: the file date, useful when it cannot be read from the EXIF metadata, + e.g. for videos and some image formats. Any additional meta-data is available in the templates. For instance:: diff --git a/sigal/gallery.py b/sigal/gallery.py index 51f9ee8..048f0ef 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -307,16 +307,16 @@ class Video(Media): @cached_property def date(self): """The date from the Date metadata if available, or from the file date.""" + if 'date' in self.meta: + try: + self.logger.debug("Reading date from image metadata : %s", + self.src_filename) + return datetime.fromisoformat(self.meta['date'][0]) + except Exception: + self.logger.debug("Reading date from image metadata failed : %s", + self.src_filename) # If no date is found in the metadata, return the file date. - assetdate = datetime.now() - try: - assetdate = datetime.fromisoformat(self.meta['date'][0]) - except: - self.logger.debug( - "Either self.meta.data was not set, or was in an incorrect format : %s", - self.src_filename) - assetdate = self._get_file_date() - return assetdate + return self._get_file_date() class Album: diff --git a/tests/sample/pictures/video/example video.md b/tests/sample/pictures/video/example video.md new file mode 100644 index 0000000..850aeaa --- /dev/null +++ b/tests/sample/pictures/video/example video.md @@ -0,0 +1 @@ +Date: 2020-01-01T09:00:00 diff --git a/tests/test_video.py b/tests/test_video.py index f606d93..7d161e4 100644 --- a/tests/test_video.py +++ b/tests/test_video.py @@ -1,4 +1,5 @@ import os +from datetime import datetime from unittest.mock import patch import pytest @@ -48,6 +49,21 @@ def test_process_video(tmpdir): assert process_video(video) == Status.FAILURE +def test_metadata(tmpdir): + base, ext = os.path.splitext(TEST_VIDEO) + + settings = create_settings( + video_format='ogv', + use_orig=True, + orig_link=True, + source=os.path.join(SRCDIR, 'video'), + destination=str(tmpdir), + ) + video = Video(TEST_VIDEO, '.', settings) + assert video.meta == {'date': ['2020-01-01T09:00:00']} + assert video.date == datetime(2020, 1, 1, 9, 0) + + @pytest.mark.parametrize("fmt", ['webm', 'mp4']) def test_generate_video_fit_height(tmpdir, fmt): """largest fitting dimension is height"""