Skip to content

Commit

Permalink
autogen: autofill date gallery setting if missing
Browse files Browse the repository at this point in the history
This allows to not provide a 'date' setting for autogenerated
galleries. The 'date' will automatically be retrieved from the oldest
file in the gallery, either from one of its EXIF date tags or from its
mtime.

Suggested-by: crypto512 <[email protected]>
Signed-off-by: Quentin Schulz <[email protected]>
  • Loading branch information
QSchulz committed Aug 26, 2023
1 parent ed1c7ba commit f0a0592
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
16 changes: 9 additions & 7 deletions recitale/autogen.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os
import sys
from time import gmtime, strftime
from time import gmtime, strftime, strptime
from jinja2 import Template
from pathlib import Path
from PIL import Image
Expand Down Expand Up @@ -33,6 +33,8 @@

types = ("*.JPG", "*.jpg", "*.JPEG", "*.jpeg", "*.png", "*.PNG")

TIME_FORMAT = "%Y:%m:%d %H:%M:00"


def get_exif(filename):
exif = Image.open(filename).getexif()
Expand All @@ -42,7 +44,7 @@ def get_exif(filename):
if ctime is not None:
return ctime

return strftime("%Y:%m:%d %H:%M:00", gmtime(os.path.getmtime(filename)))
return strftime(TIME_FORMAT, gmtime(os.path.getmtime(filename)))


def build_template(folder, force):
Expand All @@ -58,10 +60,6 @@ def build_template(folder, force):
logger.error("%s/settings.yaml: 'title' setting missing", folder)
sys.exit(1)

if not gallery_settings.get("date"):
logger.error("%s/settings.yaml: 'date' setting missing", folder)
sys.exit(1)

if "sections" in gallery_settings and force is not True:
logger.info("Skipped: %s gallery is already generated", folder)
return
Expand All @@ -73,10 +71,14 @@ def build_template(folder, force):
files = sorted(files_grabbed, key=get_exif)

cover = gallery_settings.get("cover", files[0].name)
date = gallery_settings.get("date")
if not date:
date_from_exif = strptime(get_exif(files[0]), TIME_FORMAT)
date = strftime("%Y-%m-%d", date_from_exif)

msg = template.render(
title=gallery_settings["title"],
date=gallery_settings["date"],
date=date,
cover=cover,
files=files,
)
Expand Down
35 changes: 26 additions & 9 deletions test/test_autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,6 @@ def test_missing_required_title(self, caplog):
assert sysexit.type == SystemExit
assert sysexit.value.code == 1

def test_missing_required_date(self, caplog):
with pytest.raises(SystemExit) as sysexit, patch(
"recitale.autogen.load_settings", return_value={"title": "test"}
):
recitale.autogen.build_template(".", False)
assert ": 'date' setting missing" in caplog.text
assert sysexit.type == SystemExit
assert sysexit.value.code == 1

@patch(
"recitale.autogen.load_settings",
return_value={
Expand All @@ -155,6 +146,32 @@ def test_existing_gallery(self, p, caplog):
recitale.autogen.build_template(".", False)
assert " gallery is already generated" in caplog.text

@patch(
"recitale.autogen.load_settings",
return_value={"title": "test", "cover": "test.jpg"},
)
@patch("recitale.autogen.get_exif", return_value="2023:06:10 10:10:00")
def test_missing_date(self, patch_exif, patch_load):
with TemporaryDirectory() as td:
f = "test.png"
Path(td).joinpath(f).touch()
recitale.autogen.build_template(td, False)
generated = Path(td).joinpath("settings.yaml")
assert generated.exists()
with open(generated) as content:
assert (
"".join(content.readlines())
== f"""title: test
date: 2023-06-10
cover: test.jpg
sections:
- type: pictures-group
images:
-
- {f}
"""
)

@patch(
"recitale.autogen.load_settings",
return_value={"title": "test", "date": "20230610"},
Expand Down

0 comments on commit f0a0592

Please sign in to comment.