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 <crypto512x@gmail.com>
Signed-off-by: Quentin Schulz <foss+recitale@0leil.net>
  • Loading branch information
QSchulz committed Aug 26, 2023
1 parent 93fb906 commit bcde283
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/build.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ You need
1. Create a folder
2. Put all pics you want
3. Create settings.yaml file in the folder
4. Add title and date keys in folder/settings.yaml. Optionally provide a cover key, otherwise it is inferred from the oldest file in the gallery.
4. Add title key in folder/settings.yaml. Optionally provide a cover and date key, otherwise they are inferred from the oldest file in the gallery.
5. Use `recitale autogen -d folder`


Expand Down
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 bcde283

Please sign in to comment.