Skip to content

Commit d44fa1e

Browse files
rgaudinbenoit74
authored andcommitted
Revamped metadata module around single base Metadata
1 parent a5d2212 commit d44fa1e

6 files changed

Lines changed: 647 additions & 575 deletions

File tree

src/zimscraperlib/zim/creator.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@
2424
import pathlib
2525
import re
2626
import weakref
27-
from collections.abc import Callable
28-
from typing import Any
2927

3028
import libzim.writer # pyright: ignore
3129
import PIL.Image
3230

33-
import zimscraperlib.zim.metadata
3431
from zimscraperlib import logger
3532
from zimscraperlib.constants import FRONT_ARTICLE_MIMETYPES
3633
from zimscraperlib.filesystem import (
@@ -102,8 +99,8 @@ class Creator(libzim.writer.Creator):
10299
Use workaround_nocancel=False to disable the workaround.
103100
104101
By default, all metadata are validated for compliance with openZIM specification and
105-
conventions. Set check_metadata_conventions=True to disable this validation (you can
106-
still do checks manually with the validation methods or your own logic).
102+
conventions. Set metdata.APPLY_RECOMMENDATIONS to False to disable this validation
103+
# (you canstill do checks manually with the validation methods or your own logic).
107104
"""
108105

109106
def __init__(
@@ -114,7 +111,6 @@ def __init__(
114111
*,
115112
workaround_nocancel: bool | None = True,
116113
ignore_duplicates: bool | None = False,
117-
check_metadata_conventions: bool = True,
118114
):
119115
super().__init__(filename=filename)
120116
self._metadata: dict[str, Metadata] = {}
@@ -132,9 +128,6 @@ def __init__(
132128

133129
self.workaround_nocancel = workaround_nocancel
134130
self.ignore_duplicates = ignore_duplicates
135-
zimscraperlib.zim.metadata.check_metadata_conventions = (
136-
check_metadata_conventions
137-
)
138131

139132
def config_indexing(
140133
self, indexing: bool, language: str | None = None # noqa: FBT001
@@ -211,7 +204,7 @@ def _get_first_language_metadata_value(self) -> str | None:
211204
"""Private methods to get most popular lang code from Language metadata"""
212205
for metadata in self._metadata.values():
213206
if isinstance(metadata, LanguageMetadata):
214-
return metadata.libzim_value.decode().split(",")[0]
207+
return metadata.value[0]
215208
return None
216209

217210
def start(self):
@@ -244,26 +237,22 @@ def start(self):
244237

245238
for metadata in self._metadata.values():
246239
if isinstance(metadata, IllustrationMetadata):
247-
self.add_illustration(metadata.size, metadata.libzim_value)
240+
self.add_illustration(metadata.illustration_size, metadata.libzim_value)
248241
else:
249242
self.add_metadata(metadata)
250243
self._metadata.clear()
251244

252245
return self
253246

254-
def add_metadata(
255-
self,
256-
value: Metadata,
257-
mimetype: str = "text/plain;charset=UTF-8",
258-
):
247+
def add_metadata(self, value: Metadata):
259248
"""Really add the metadata to the ZIM, after ZIM creation has started.
260249
261250
You would probably prefer to use config_metadata methods to check metadata
262251
before starting the ZIM, ensure all mandatory metadata are set, and avoid
263252
duplicate metadata name.
264253
"""
265254

266-
super().add_metadata(value.name, value.libzim_value, mimetype)
255+
super().add_metadata(value.name, value.libzim_value, value.mimetype)
267256

268257
def config_metadata(
269258
self,

src/zimscraperlib/zim/filesystem.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,13 @@ def make_zim_file(
166166
with open(illustration_path, "rb") as fh:
167167
illustration_data = fh.read()
168168

169+
# disable recommendations if requested
170+
metadata.APPLY_RECOMMENDATIONS = not disable_metadata_checks
171+
169172
zim_file = Creator(
170173
filename=fpath,
171174
main_path=main_page,
172175
ignore_duplicates=ignore_duplicates,
173-
check_metadata_conventions=disable_metadata_checks,
174176
).config_metadata(
175177
metadata.StandardMetadataList(
176178
# mandatory
@@ -181,8 +183,8 @@ def make_zim_file(
181183
Language=metadata.LanguageMetadata(language),
182184
Creator=metadata.CreatorMetadata(creator),
183185
Publisher=metadata.PublisherMetadata(publisher),
184-
Illustration_48x48_at_1=metadata.IllustrationMetadata(
185-
"Illustration_48x48@1", illustration_data
186+
Illustration_48x48_at_1=metadata.DefaultIllustrationMetadata(
187+
illustration_data
186188
),
187189
# optional
188190
Tags=metadata.TagsMetadata(list(tags)) if tags else None,

0 commit comments

Comments
 (0)