Copying over the bestiary from dnd_monster_stats and working on a similar SpellList object and Spell DB.
29 lines
896 B
Python
29 lines
896 B
Python
import json
|
|
import pathlib
|
|
import typing
|
|
|
|
|
|
def parse_index(data_dir: pathlib.Path, db_type: str) -> typing.Dict[str, pathlib.Path]:
|
|
try:
|
|
index_file = data_dir / db_type / "index.json"
|
|
with index_file.open("rt") as index:
|
|
index_data = json.load(index)
|
|
except Exception as e:
|
|
raise Exception(f"Failed to read {db_type} index file: {e}")
|
|
return {
|
|
code: data_dir.joinpath(db_type, bfile) for code, bfile in index_data.items()
|
|
}
|
|
|
|
|
|
class DbIndex:
|
|
def __init__(self, data_dir: pathlib.Path, db_type: str) -> None:
|
|
self.data_dir = data_dir
|
|
self.db_type = db_type
|
|
self.source_index = parse_index(self.data_dir, self.db_type)
|
|
|
|
def validate(self) -> None:
|
|
if not self.source_index:
|
|
raise Exception(
|
|
f"DB for {self.db_type} appears empty in data_dir{self.data_dir}"
|
|
)
|