Skip to content

Commit

Permalink
Merge pull request #126 from IMayBeABitShy/issue_125
Browse files Browse the repository at this point in the history
Fix handling of tag list in Creator.add_metadata (fix #125)
  • Loading branch information
benoit74 authored Feb 12, 2024
2 parents cfee792 + 10ca3fa commit a7fe235
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
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

0 comments on commit a7fe235

Please sign in to comment.