Skip to content

Significantly enhance the safety of metadata manipulation #217

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

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Renamed `filesystem.validate_zimfile_creatable` to `filesystem.file_creatable` to reflect general applicability to check file creation beyond ZIM files #200
- Remove any "ZIM" reference in exceptions while working with files #200
- Significantly enhance the safety of metadata manipulation (#205)
- make `config_metadata` much stricter around standard metadata
- add new `StandardMetadata` class for standard metadata
- add way more typing around metadata manipulation with `RawMetadataValue`, `CleanMetadataValue` and `Metadata`
- add `config_any_metadata` for those who need to add custom ones
- ensure that `add_metadata` does same cleanup / checks as `config_metadata` and `config_any_metadata`
- move constants and validation logic for metadata to zim/metadata.py
- from `creator.py`: `MANDATORY_ZIM_METADATA_KEYS`, `DEFAULT_DEV_ZIM_METADATA`, `RECOMMENDED_MAX_TITLE_LENGTH`, `MAXIMUM_DESCRIPTION_METADATA_LENGTH`, `MAXIMUM_LONG_DESCRIPTION_METADATA_LENGTH`, `ILLUSTRATIONS_METADATA_RE`
- from `creator.py`: `convert_and_check_metadata`

### Added

Expand Down
34 changes: 0 additions & 34 deletions src/zimscraperlib/constants.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#!/usr/bin/env python3
# vim: ai ts=4 sts=4 et sw=4 nu

import base64
import pathlib
import re

from zimscraperlib.__about__ import __version__

Expand All @@ -21,38 +19,6 @@
# list of mimetypes we consider articles using it should default to FRONT_ARTICLE
FRONT_ARTICLE_MIMETYPES = ["text/html"]

# list of mandatory meta tags of the zim file.
MANDATORY_ZIM_METADATA_KEYS = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is wise. I understand you want to keep metadata-related stuff together but the point of the constants module is for the developer to act as a config file. If some are spread into different places, then the existence of this file is in jeopardy.
Either we think it makes sense as it previously was or we harmonize and move other constants out as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it

"Name",
"Title",
"Creator",
"Publisher",
"Date",
"Description",
"Language",
"Illustration_48x48@1",
]

DEFAULT_DEV_ZIM_METADATA = {
"Name": "Test Name",
"Title": "Test Title",
"Creator": "Test Creator",
"Publisher": "Test Publisher",
"Date": "2023-01-01",
"Description": "Test Description",
"Language": "fra",
# blank 48x48 transparent PNG
"Illustration_48x48_at_1": base64.b64decode(
"iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAAGXRFWHRTb2Z0d2FyZQBB"
"ZG9iZSBJbWFnZVJlYWR5ccllPAAAAANQTFRFR3BMgvrS0gAAAAF0Uk5TAEDm2GYAAAAN"
"SURBVBjTY2AYBdQEAAFQAAGn4toWAAAAAElFTkSuQmCC"
),
}

RECOMMENDED_MAX_TITLE_LENGTH = 30
MAXIMUM_DESCRIPTION_METADATA_LENGTH = 80
MAXIMUM_LONG_DESCRIPTION_METADATA_LENGTH = 4000

ILLUSTRATIONS_METADATA_RE = re.compile(
r"^Illustration_(?P<height>\d+)x(?P<width>\d+)@(?P<scale>\d+)$"
)
5 changes: 5 additions & 0 deletions src/zimscraperlib/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,8 @@ def compute_tags(
return {
tag.strip() for tag in list(default_tags) + (user_tags or "").split(";") if tag
}


def unique_values(items: list) -> list:
"""Return unique values in input list while preserving list order"""
return list(dict.fromkeys(items))
Loading
Loading