Skip to content

Commit 5f7a3c2

Browse files
committed
Add default ZIM tags based on Kiwix convention
1 parent 0119431 commit 5f7a3c2

3 files changed

Lines changed: 95 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Add map scale control (metric units) to display real-world distances at current zoom level (#46)
1313
- Toggle map scale units between metric and imperial when clicking the scale control (#77)
14+
- Add default ZIM tags based on Kiwix convention (#76)
1415

1516
### Fixed
1617

scraper/src/maps2zim/zimconfig.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
from maps2zim.errors import InvalidFormatError
44

5+
DEFAULT_TAGS: list[str] = [
6+
"_sw:no",
7+
"_ftindex:yes",
8+
"_pictures:yes",
9+
"_videos:no",
10+
"_details:yes",
11+
]
12+
513

614
class ZimConfig(BaseModel):
715
"""Common configuration for building ZIM files."""
@@ -43,6 +51,13 @@ def fmt(string: str) -> str:
4351
f"valid placeholders are: {valid_placeholders}"
4452
) from e
4553

54+
formatted_tags = [fmt(tag) for tag in self.tags] if self.tags else []
55+
# Build a dict of tag key -> tag value from default tags, then override
56+
# with user-provided tags (key is the part before the colon).
57+
merged: dict[str, str] = {}
58+
for tag in DEFAULT_TAGS + formatted_tags:
59+
key = tag.split(":")[0] if ":" in tag else tag
60+
merged[key] = tag
4661
return ZimConfig(
4762
secondary_color=self.secondary_color,
4863
file_name=fmt(self.file_name),
@@ -54,5 +69,5 @@ def fmt(string: str) -> str:
5469
long_description=(
5570
fmt(self.long_description) if self.long_description else None
5671
),
57-
tags=[fmt(tag) for tag in self.tags] if self.tags else [],
72+
tags=list(merged.values()),
5873
)

scraper/tests/test_zimconfig.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import pytest
2+
3+
from maps2zim.errors import InvalidFormatError
4+
from maps2zim.zimconfig import DEFAULT_TAGS, ZimConfig
5+
6+
7+
def make_config(tags: list[str] | None = None) -> ZimConfig:
8+
return ZimConfig(
9+
file_name="test_{region}",
10+
name="test_{region}",
11+
title="Test {region}",
12+
publisher="Publisher",
13+
creator="Creator",
14+
description="Desc {region}",
15+
long_description=None,
16+
tags=tags,
17+
secondary_color="#FFFFFF",
18+
)
19+
20+
21+
PLACEHOLDERS = {"region": "world"}
22+
23+
24+
def test_format_includes_default_tags_when_no_tags():
25+
config = make_config(tags=None)
26+
result = config.format(PLACEHOLDERS)
27+
assert result.tags == DEFAULT_TAGS
28+
29+
30+
def test_format_default_tags_overridden_by_user_tag():
31+
config = make_config(tags=["_videos:yes"])
32+
result = config.format(PLACEHOLDERS)
33+
assert result.tags is not None
34+
# _videos:no from defaults should be replaced by _videos:yes
35+
assert "_videos:yes" in result.tags
36+
assert "_videos:no" not in result.tags
37+
38+
39+
def test_format_all_default_tags_present_with_override():
40+
config = make_config(tags=["_videos:yes"])
41+
result = config.format(PLACEHOLDERS)
42+
assert result.tags is not None
43+
for default_tag in DEFAULT_TAGS:
44+
key = default_tag.split(":")[0]
45+
assert any(t.startswith(key + ":") for t in result.tags)
46+
47+
48+
def test_format_extra_user_tag_added():
49+
config = make_config(tags=["custom_tag"])
50+
result = config.format(PLACEHOLDERS)
51+
assert result.tags is not None
52+
assert "custom_tag" in result.tags
53+
# All defaults still present
54+
for default_tag in DEFAULT_TAGS:
55+
assert default_tag in result.tags
56+
57+
58+
def test_format_multiple_overrides():
59+
config = make_config(tags=["_videos:yes", "_pictures:no"])
60+
result = config.format(PLACEHOLDERS)
61+
assert result.tags is not None
62+
assert "_videos:yes" in result.tags
63+
assert "_videos:no" not in result.tags
64+
assert "_pictures:no" in result.tags
65+
assert "_pictures:yes" not in result.tags
66+
67+
68+
def test_format_tags_with_placeholder():
69+
config = make_config(tags=["region_{region}"])
70+
result = config.format(PLACEHOLDERS)
71+
assert result.tags is not None
72+
assert "region_world" in result.tags
73+
74+
75+
def test_format_invalid_placeholder_raises():
76+
config = make_config(tags=["_{bad_key}:yes"])
77+
with pytest.raises(InvalidFormatError):
78+
config.format(PLACEHOLDERS)

0 commit comments

Comments
 (0)