Skip to content

Commit 552e338

Browse files
authored
fix: coerce dump paths to Path and mkdir before JSON export (#705)
1 parent f1ffcb1 commit 552e338

5 files changed

Lines changed: 43 additions & 10 deletions

File tree

src/scribe_data/cli/download/wikidata_lexeme_dump.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def wd_lexeme_dump_download_wrapper(
234234
- Returns None if the user chooses not to proceed with the download or no valid dump URL is found.
235235
"""
236236
try:
237-
output_dir = output_dir or DEFAULT_WIKIDATA_DUMP_EXPORT_DIR
237+
output_dir = Path(output_dir or DEFAULT_WIKIDATA_DUMP_EXPORT_DIR)
238238

239239
os.makedirs(output_dir, exist_ok=True)
240240

src/scribe_data/cli/download/wiktionary_dump.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ def download_wiktionary_dumps(
6666
wiktionaries = [f"{iso}wiktionary" for iso in language_isos]
6767
wiktionary_urls = [f"https://dumps.wikimedia.org/{w}" for w in wiktionaries]
6868

69-
Path(output_dir).mkdir(parents=True, exist_ok=True)
69+
output_dir = Path(output_dir)
70+
output_dir.mkdir(parents=True, exist_ok=True)
7071
for i, w, u in zip(language_isos, wiktionaries, wiktionary_urls):
7172
# Note: Remove the snapshot from the resulting filename so Scribe-Server always looks for one file.
7273
filename = f"{w}-pages-articles.xml.bz2"

src/scribe_data/unicode/generate_emoji_keywords.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,6 @@ def generate_emoji(language: str, output_dir: Path = Path("")) -> None:
5656
)
5757
return
5858

59-
updated_path = (
60-
Path(str(output_dir)[2:])
61-
if str(output_dir).startswith("./")
62-
else output_dir
63-
)
64-
export_dir = Path(updated_path) / language.capitalize()
65-
export_dir.mkdir(parents=True, exist_ok=True)
66-
6759
if emoji_keywords_dict := gen_emoji_lexicon(
6860
language=language,
6961
emojis_per_keyword=EMOJI_KEYWORDS_DICT,

src/scribe_data/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ def export_formatted_data(
405405
/ language.lower().replace(" ", "_")
406406
/ f"{data_type.replace('-', '_')}.json"
407407
)
408+
export_path.parent.mkdir(parents=True, exist_ok=True)
408409

409410
with open(export_path, "w", encoding="utf-8") as file:
410411
json.dump(formatted_data, file, ensure_ascii=False, indent=0)

tests/cli/download/test_cli_download_wikidata_lexeme_dump.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,45 @@ def test_wd_lexeme_dump_download_wrapper_latest(
145145
for call in mock_get.call_args_list:
146146
self.assertEqual(call.kwargs.get("headers"), WMF_HEADERS)
147147

148+
@patch("scribe_data.cli.download.wikidata_lexeme_dump.requests.get")
149+
@patch(
150+
"scribe_data.cli.download.wikidata_lexeme_dump.check_lexeme_dump_prompt_download",
151+
return_value=False,
152+
)
153+
@patch("scribe_data.cli.download.wikidata_lexeme_dump.open", new_callable=mock_open)
154+
@patch("scribe_data.cli.download.wikidata_lexeme_dump.tqdm")
155+
@patch("scribe_data.cli.download.wikidata_lexeme_dump.os.makedirs")
156+
@patch("scribe_data.cli.download.wikidata_lexeme_dump.questionary.confirm")
157+
def test_wd_lexeme_dump_download_wrapper_accepts_str_output_dir(
158+
self,
159+
mock_confirm: MagicMock,
160+
mock_makedirs: MagicMock,
161+
mock_tqdm: MagicMock,
162+
mock_file: MagicMock,
163+
mock_check_prompt: MagicMock,
164+
mock_get: MagicMock,
165+
) -> None:
166+
"""
167+
CLI passes -wdp as str; Path join must not raise TypeError.
168+
"""
169+
mock_confirm.return_value.ask.return_value = True
170+
mock_get.return_value.text = 'href="latest-all.json.bz2"'
171+
mock_get.return_value.raise_for_status = MagicMock()
172+
mock_get.return_value.headers = {"content-length": "100"}
173+
mock_get.return_value.iter_content = lambda chunk_size: [b"data"] * 10
174+
175+
download_path = wd_lexeme_dump_download_wrapper(
176+
output_dir="./scribe_data_wikidata_dumps_export"
177+
)
178+
self.assertIsNotNone(download_path)
179+
self.assertEqual(
180+
download_path,
181+
Path("scribe_data_wikidata_dumps_export") / "latest-lexemes.json.bz2",
182+
)
183+
mock_makedirs.assert_called_with(
184+
Path("scribe_data_wikidata_dumps_export"), exist_ok=True
185+
)
186+
148187
@patch("scribe_data.utils.questionary.select")
149188
@patch(
150189
"scribe_data.utils.Path.glob",

0 commit comments

Comments
 (0)