11import json
22from pathlib import Path
3- from typing import Dict
3+ from typing import Dict , Any
44
55from tqdm import tqdm
66
77from pokeapi_ditto .common import apply_base_url
88
99
10+ def _is_id (s : str ):
11+ try :
12+ int (s )
13+ return True
14+ except ValueError :
15+ return False
16+
17+
18+ def _dump (path : Path , content : Any ):
19+ if not path .parent .exists ():
20+ path .parent .mkdir (parents = True )
21+ path .write_text (json .dumps (content , sort_keys = True , indent = 4 ))
22+
23+
24+ # TODO: blow all this up and make it good
25+ # this is really bade code and hard to follow
26+ # all this path.parent.parent nonsense is hard to understand
27+ # clone.py is a cleaner model to follow
28+
29+
1030def do_transform (src_dir : str , dest_dir : str , base_url : str ):
1131 src_dir : Path = Path (src_dir )
1232 dest_dir : Path = Path (dest_dir )
@@ -22,16 +42,24 @@ def do_transform(src_dir: str, dest_dir: str, base_url: str):
2242 for src_path in tqdm (list (src_paths )):
2343 content : Dict = json .loads (apply_base_url (src_path .read_text (), base_url ))
2444
45+ # all files
2546 dest_path = dest_dir .joinpath (src_path .relative_to (src_dir ))
47+ _dump (dest_path , content )
2648
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 :
49+ # named resource files
50+ if _is_id (dest_path .parent .name ) and "name" in content :
3251 name = content ["name" ]
3352 dest_path = dest_path .parent .parent .joinpath (name , "index.json" )
53+ _dump (dest_path , content )
3454
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 ))
55+ # a hack for pokemon/ID/encounters
56+ if (
57+ _is_id (dest_path .parent .parent .name )
58+ and dest_path .parent .name == "encounters"
59+ ):
60+ pokemon_path = src_path .parent .parent .joinpath ("index.json" )
61+ name = json .loads (pokemon_path .read_text ())["name" ]
62+ dest_path = dest_path .parent .parent .parent .joinpath (
63+ name , "encounters" , "index.json"
64+ )
65+ _dump (dest_path , content )
0 commit comments