Skip to content

Commit

Permalink
Add utility function to compute/check ZIM descriptions based on defau…
Browse files Browse the repository at this point in the history
…lt and user provided values
  • Loading branch information
benoit74 committed Aug 21, 2023
1 parent 4f8c3cc commit 70f5116
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/zimscraperlib/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import pathlib
import shutil
import tempfile
from dataclasses import dataclass
from typing import Optional, Union

from . import logger
from .constants import MAXIMUM_DESCRIPTION_METADATA_LENGTH as MAX_DESC_LENGTH
from .constants import MAXIMUM_LONG_DESCRIPTION_METADATA_LENGTH as MAX_LONG_DESC_LENGTH
from .download import stream_file


Expand Down Expand Up @@ -49,3 +52,62 @@ def handle_user_provided_file(
shutil.copy(source, dest)

return dest


@dataclass
class Descriptions:
description: str
long_description: Optional[str]


def compute_descriptions(
default_description: str,
user_description: Optional[str],
user_long_description: Optional[str],
) -> Descriptions:
"""Computes short and long descriptions compliant with ZIM standard.
Based on provided parameters, the function computes a short and a long description
which are compliant with the ZIM standard (in terms of length).
User description(s) are used if set. They are checked to not exceed ZIM standard
maximum length ; an error is thrown otherwise ; if ok, they are returned.
If user_description is not set, the description is computed based on the default
description, truncated if needed.
If user_long_description is not set and default description is too long for the
description field, the long_description is computed based on the default description
(truncated if needed), otherwise no long description is returned.
args:
default_description: the description which will be used if user descriptions
are not set (typically fetched online)
user_description: the description set by the user (typically set by a
CLI argument)
user_long_description: the long description set by the user (typically set by a
CLI argument)
"""

if user_description and len(user_description) > MAX_DESC_LENGTH:
raise ValueError(

Check warning on line 93 in src/zimscraperlib/inputs.py

View check run for this annotation

Codecov / codecov/patch

src/zimscraperlib/inputs.py#L92-L93

Added lines #L92 - L93 were not covered by tests
f"Description too long ({len(user_description)}>{MAX_DESC_LENGTH})"
)
if user_long_description and len(user_long_description) > MAX_LONG_DESC_LENGTH:
raise ValueError(

Check warning on line 97 in src/zimscraperlib/inputs.py

View check run for this annotation

Codecov / codecov/patch

src/zimscraperlib/inputs.py#L96-L97

Added lines #L96 - L97 were not covered by tests
f"LongDescription too long ({len(user_long_description)}"
f">{MAX_LONG_DESC_LENGTH})"
)

if not user_long_description and len(default_description) > MAX_DESC_LENGTH:
user_long_description = default_description[0:MAX_LONG_DESC_LENGTH]
if len(default_description) > MAX_LONG_DESC_LENGTH:
user_long_description = user_long_description[:-1] + "…"
if not user_description:
user_description = default_description[0:MAX_DESC_LENGTH]
if len(user_description) > MAX_DESC_LENGTH:
user_description = user_description[:-1] + "…"

Check warning on line 109 in src/zimscraperlib/inputs.py

View check run for this annotation

Codecov / codecov/patch

src/zimscraperlib/inputs.py#L102-L109

Added lines #L102 - L109 were not covered by tests

return Descriptions(

Check warning on line 111 in src/zimscraperlib/inputs.py

View check run for this annotation

Codecov / codecov/patch

src/zimscraperlib/inputs.py#L111

Added line #L111 was not covered by tests
description=user_description, long_description=user_long_description
)

0 comments on commit 70f5116

Please sign in to comment.