Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions pokeapi_ditto/commands/transform.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
import json
from pathlib import Path
from typing import Dict
from typing import Dict, Any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F401 'typing.Dict' imported but unused

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢


from tqdm import tqdm

from pokeapi_ditto.common import apply_base_url


def _is_id(s: str):
try:
int(s)
return True
except ValueError:
return False


def _dump(path: Path, content: Any):
if not path.parent.exists():
path.parent.mkdir(parents=True)
path.write_text(json.dumps(content, sort_keys=True, indent=4))


# TODO: blow all this up and make it good
# this is really bade code and hard to follow
# all this path.parent.parent nonsense is hard to understand
# clone.py is a cleaner model to follow


def do_transform(src_dir: str, dest_dir: str, base_url: str):
src_dir: Path = Path(src_dir)
dest_dir: Path = Path(dest_dir)
Expand All @@ -22,16 +42,24 @@ def do_transform(src_dir: str, dest_dir: str, base_url: str):
for src_path in tqdm(list(src_paths)):
content: Dict = json.loads(apply_base_url(src_path.read_text(), base_url))

# all files
dest_path = dest_dir.joinpath(src_path.relative_to(src_dir))
_dump(dest_path, content)

if not dest_path.parent.exists():
dest_path.parent.mkdir(parents=True)
dest_path.write_text(json.dumps(content, sort_keys=True, indent=4))

if "name" in content and "id" in content:
# named resource files
if _is_id(dest_path.parent.name) and "name" in content:
name = content["name"]
dest_path = dest_path.parent.parent.joinpath(name, "index.json")
_dump(dest_path, content)

if not dest_path.parent.exists():
dest_path.parent.mkdir(parents=True)
dest_path.write_text(json.dumps(content, sort_keys=True, indent=4))
# a hack for pokemon/ID/encounters
if (
_is_id(dest_path.parent.parent.name)
and dest_path.parent.name == "encounters"
):
pokemon_path = src_path.parent.parent.joinpath("index.json")
name = json.loads(pokemon_path.read_text())["name"]
dest_path = dest_path.parent.parent.parent.joinpath(
name, "encounters", "index.json"
)
_dump(dest_path, content)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pokeapi-ditto"
version = "0.6.0"
version = "0.6.1"
description = "Ditto is a server that serves a static copy of PokeAPI's data."
license = "Apache-2.0"
authors = ["Sargun Vohra <sargun.vohra@gmail.com>"]
Expand Down