Files
aoe2-flashcard-creator/flashcards/maps.py
Nate Bowman 82ce8590b1 Add more data
Also renamed some of the files and had to change some code to account
for the new names.
2026-10-14 19:07:16 -04:00

59 lines
1.4 KiB
Python

# Converts a list of lnames to two maps:
# lname -> num and num -> lname
from flashcards.config import config
import json
from pathlib import Path
def _fill_maps(big, key, whitelist):
names = {} # num -> localized name
nums = {} # localized name -> num
for k, v in big[key].items():
lname = v['localised_name'].strip()
if lname in whitelist and lname not in nums.keys():
names[k] = lname
nums[lname] = k
return names, nums
def create_maps(namelist_fname, key):
# Read in the full data dictionary
with open(config.name_data_file, 'r') as f:
big = json.load(f)
# Use whitelist to filter data and map only as needed
namelist_path = config.namelists_dir / namelist_fname
with open(namelist_path, 'r') as f:
whitelist = [s.strip() for s in f.readlines()]
names, nums = _fill_maps(big, key, whitelist)
return names, nums
def read_string_data_to_dict(keys):
return _read_file_to_dict(config.string_data_file, keys)
def read_name_data_to_dict(keys):
return _read_file_to_dict(config.name_data_file, keys)
def read_data_to_dict(keys):
return _read_file_to_dict(config.data_file, keys)
def _read_file_to_dict(fname, keys):
if not type(keys) is tuple:
keys = (keys,)
with open(fname, 'r') as f:
all_data = json.load(f)
for key in keys:
all_data = all_data[key]
return all_data