Skip to content

Commit

Permalink
Simplify code + add support for seting only the long description
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit74 committed Jul 25, 2023
1 parent 7a082af commit d28589d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
47 changes: 18 additions & 29 deletions src/kolibri2zim/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,36 +942,25 @@ def sanitize_inputs(self):
self.title = channel_meta["name"]
self.title = self.title.strip()

if self.description and len(self.description) > MAX_DESC_LENGTH:
raise ValueError(
f"Description too long ({len(self.description)}>{MAX_DESC_LENGTH})"
)
if self.long_description and len(self.long_description) > MAX_LONG_DESC_LENGTH:
raise ValueError(
f"LongDescription too long ({len(self.long_description)}"
f">{MAX_LONG_DESC_LENGTH})"
)

kolibri_desc = channel_meta["description"].strip()
if not self.long_description and len(kolibri_desc) > MAX_DESC_LENGTH:
self.long_description = kolibri_desc[0:MAX_LONG_DESC_LENGTH]
if len(kolibri_desc) > MAX_LONG_DESC_LENGTH:
self.long_description = self.long_description[:-1] + "…"
if not self.description:
# User did not provided a description, we will infer it from channel
# metadata, limited to maximum length
if self.long_description:
raise ValueError(
"long_description cannot be set if description is not set"
)
self.description = channel_meta["description"].strip()
if len(self.description) > MAX_DESC_LENGTH:
self.long_description = self.description
self.description = f"{self.description[0:MAX_DESC_LENGTH-1]}…"
if len(self.long_description) > MAX_LONG_DESC_LENGTH:
self.long_description = (
f"{self.long_description[0:MAX_LONG_DESC_LENGTH-1]}…"
)
else:
self.description = self.description.strip()
if len(self.description) > MAX_DESC_LENGTH:
raise ValueError(
f"description is too long ({len(self.description)}"
f">{MAX_DESC_LENGTH})"
)
if (
self.long_description
and len(self.long_description) > MAX_LONG_DESC_LENGTH
):
raise ValueError(
f"long_description is too long ({len(self.long_description)}"
f">{MAX_LONG_DESC_LENGTH})"
)
self.description = kolibri_desc[0:MAX_DESC_LENGTH]
if len(kolibri_desc) > MAX_DESC_LENGTH:
self.description = self.description[:-1] + "…"

if not self.author:
self.author = channel_meta["author"] or "Kolibri"
Expand Down
12 changes: 6 additions & 6 deletions tests/test_sanitize_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_sanitize_defaults_ok(scraper_generator: Callable[..., Kolibri2Zim]):
LONG_TEXT[0:MAX_DESC_LEN],
None,
),
# CLI description set and is too long, channel description doe not matter
# CLI description set and is too long, channel description does not matter
(LONG_TEXT[0 : MAX_DESC_LEN + 1], None, TEXT_NOT_USED, True, None, None),
# CLI description not set and channel description is short enough
(None, None, LONG_TEXT[0:MAX_DESC_LEN], False, LONG_TEXT[0:MAX_DESC_LEN], None),
Expand Down Expand Up @@ -145,14 +145,14 @@ def test_sanitize_defaults_ok(scraper_generator: Callable[..., Kolibri2Zim]):
None,
),
# CLI description not set, CLI long descripion set and is short,
# channel description does not matter
# channel description set to something different than long desc
(
None,
LONG_TEXT[0:MAX_LONG_DESC_LEN],
TEXT_NOT_USED,
True,
None,
None,
LONG_TEXT[10:MAX_LONG_DESC_LEN],
False,
LONG_TEXT[10 : MAX_DESC_LEN + 9] + "…",
LONG_TEXT[0:MAX_LONG_DESC_LEN],
),
],
)
Expand Down

0 comments on commit d28589d

Please sign in to comment.