Skip to content

Fix handling of tag list in Creator.add_metadata (fix #125) #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,4 @@ exclude = [".env/**", ".venv/**"]
extraPaths = ["src"]
pythonVersion = "3.8"
typeCheckingMode="basic"
disableBytesTypePromotions = true
12 changes: 11 additions & 1 deletion src/zimscraperlib/zim/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- content stored on object
- can be used to store a filepath and content read from it (not stored) """

import collections.abc
import datetime
import pathlib
import re
Expand Down Expand Up @@ -183,10 +184,19 @@ def validate_metadata(
def add_metadata(
self,
name: str,
content: Union[str, bytes, datetime.date, datetime.datetime],
content: Union[str, bytes, datetime.date, datetime.datetime, Iterable[str]],
mimetype: str = "text/plain;charset=UTF-8",
):
self.validate_metadata(name, content)
if name == "Date" and isinstance(content, (datetime.date, datetime.datetime)):
content = content.strftime("%Y-%m-%d").encode("UTF-8")
if (
name == "Tags"
and not isinstance(content, str)
and not isinstance(content, bytes)
and isinstance(content, collections.abc.Iterable)
):
content = ";".join(content)
super().add_metadata(name, content, mimetype)

def config_metadata(
Expand Down
24 changes: 21 additions & 3 deletions tests/zim/test_zim_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,26 @@ def test_check_metadata(tmp_path):
Creator(tmp_path, "").config_dev_metadata(LongDescription="T" * 5000).start()


def test_config_metadata(tmp_path, png_image):
@pytest.mark.parametrize(
"tags",
[
(
"wikipedia;_category:wikipedia;_pictures:no;_videos:no;_details:yes;"
"_ftindex:yes"
),
(
[
"wikipedia",
"_category:wikipedia",
"_pictures:no",
"_videos:no",
"_details:yes",
"_ftindex:yes",
]
),
],
)
def test_config_metadata(tmp_path, png_image, tags):
fpath = tmp_path / "test_config.zim"
with open(png_image, "rb") as fh:
png_data = fh.read()
Expand All @@ -529,8 +548,7 @@ def test_config_metadata(tmp_path, png_image):
" from the english Wikipedia by 2009-11-10. The topics are...",
Language="eng",
License="CC-BY",
Tags="wikipedia;_category:wikipedia;_pictures:no;_videos:no;"
"_details:yes;_ftindex:yes",
Tags=tags,
Flavour="nopic",
Source="https://en.wikipedia.org/",
Scraper="mwoffliner 1.2.3",
Expand Down