Skip to content

Commit e3610d2

Browse files
committed
Create files under names next to IDs during ditto transform
1 parent 2adb913 commit e3610d2

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

pokeapi_ditto/commands/transform.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import json
12
from pathlib import Path
3+
from typing import Dict
24

35
from tqdm import tqdm
46

@@ -15,12 +17,21 @@ def do_transform(src_dir: str, dest_dir: str, base_url: str):
1517
if not dest_dir.exists():
1618
dest_dir.mkdir(parents=True)
1719

18-
orig_paths = src_dir.glob("**/*.json")
20+
src_paths = src_dir.glob("**/*.json")
1921

20-
for orig in tqdm(list(orig_paths)):
21-
new = dest_dir.joinpath(orig.relative_to(src_dir))
22+
for src_path in tqdm(list(src_paths)):
23+
content: Dict = json.loads(apply_base_url(src_path.read_text(), base_url))
2224

23-
if not new.parent.exists():
24-
new.parent.mkdir(parents=True)
25+
dest_path = dest_dir.joinpath(src_path.relative_to(src_dir))
2526

26-
new.write_text(apply_base_url(orig.read_text(), base_url))
27+
if not dest_path.parent.exists():
28+
dest_path.parent.mkdir(parents=True)
29+
dest_path.write_text(json.dumps(content, sort_keys=True, indent=4))
30+
31+
if "name" in content and "id" in content:
32+
name = content["name"]
33+
dest_path = dest_path.parent.parent.joinpath(name, "index.json")
34+
35+
if not dest_path.parent.exists():
36+
dest_path.parent.mkdir(parents=True)
37+
dest_path.write_text(json.dumps(content, sort_keys=True, indent=4))

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pokeapi-ditto"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
description = "Ditto is a server that serves a static copy of PokeAPI's data."
55
license = "Apache-2.0"
66
authors = ["Sargun Vohra <[email protected]>"]

0 commit comments

Comments
 (0)