Skip to content

Commit

Permalink
autogen: autofill cover gallery setting if missing
Browse files Browse the repository at this point in the history
This allows to not provide a 'cover' setting for autogenerated
galleries.

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 38dc92e commit 93fb906
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 22 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, date and cover key in folder/settings.yaml
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.
5. Use `recitale autogen -d folder`


Expand Down
21 changes: 13 additions & 8 deletions recitale/autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ def build_template(folder, force):
logger.info("Skipped: Nothing to do in %s gallery", folder)
return

if any(req not in gallery_settings for req in ["title", "date", "cover"]):
logger.error(
"You need configure first, the title, date and cover in %s/settings.yaml "
"to use autogen",
folder,
)
if not gallery_settings.get("title"):
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:
Expand All @@ -69,11 +69,16 @@ def build_template(folder, force):
for files in types:
files_grabbed.extend(Path(folder).glob(files))
template = Template(DATA, trim_blocks=True)

files = sorted(files_grabbed, key=get_exif)

cover = gallery_settings.get("cover", files[0].name)

msg = template.render(
title=gallery_settings["title"],
date=gallery_settings["date"],
cover=gallery_settings["cover"],
files=sorted(files_grabbed, key=get_exif),
cover=cover,
files=files,
)
Path(folder).joinpath("settings.yaml").write_text(msg)
logger.info("Generation: %s gallery", folder)
Expand Down
83 changes: 70 additions & 13 deletions test/test_autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,21 @@ def test_static(self, p, caplog):
recitale.autogen.build_template(".", False)
assert "Skipped: Nothing to do in" in caplog.text

@pytest.mark.parametrize(
"d",
[
{},
{"title": "test"},
{"title": "test", "date": "20230610"},
{"title": "test", "cover": "test.jpg"},
{"date": "20230610", "cover": "test.jpg"},
],
)
def test_missing_required_key(self, caplog, d):
def test_missing_required_title(self, caplog):
with pytest.raises(SystemExit) as sysexit, patch(
"recitale.autogen.load_settings", return_value={}
):
recitale.autogen.build_template(".", False)
assert ": 'title' setting missing" in caplog.text
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=d
"recitale.autogen.load_settings", return_value={"title": "test"}
):
recitale.autogen.build_template(".", False)
assert "You need configure first, the title, date and cover in" in caplog.text
assert ": 'date' setting missing" in caplog.text
assert sysexit.type == SystemExit
assert sysexit.value.code == 1

Expand All @@ -156,6 +155,64 @@ 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", "date": "20230610"},
)
@patch("recitale.autogen.get_exif", return_value="2023:06:10 10:10:00")
def test_missing_cover(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: 20230610
cover: {f}
sections:
- type: pictures-group
images:
-
- {f}
"""
)

@patch(
"recitale.autogen.load_settings",
return_value={"title": "test", "date": "20230610"},
)
@patch(
"recitale.autogen.get_exif",
side_effect=["2023:06:10 10:10:00", "2016:10:08 01:01:00"],
)
def test_missing_cover_oldest_picked(self, patch_exif, patch_load):
with TemporaryDirectory() as td:
f = "test.png"
Path(td).joinpath(f).touch()
fold = "test-oldest.png"
Path(td).joinpath(fold).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: 20230610
cover: {fold}
sections:
- type: pictures-group
images:
-
- {fold}
- {f}
"""
)

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

0 comments on commit 93fb906

Please sign in to comment.