From 791a83da34ff35f7dc159e7f8d5f060f02c6bc5a Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Tue, 4 Jan 2022 14:37:27 +0100 Subject: improve scripts * move more generic code handling data to data.py * fix markdown generation in generate_deck.py * make gen_hoverable_link more flexible by introducing callbacks to generate the relevant link components. * fix the link targets for hoverable links in the generated card markdown --- scripts/data.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 7 deletions(-) (limited to 'scripts/data.py') diff --git a/scripts/data.py b/scripts/data.py index b72fc094..fadf9613 100755 --- a/scripts/data.py +++ b/scripts/data.py @@ -7,10 +7,67 @@ CARDS_DATA_DIR = GAME_ROOT / 'data' / 'cards' CARDS_TO_SETS = {} SETS_TO_CARDS = {} -# build up the card lookup dictionaries -for set_dir in CARDS_DATA_DIR.iterdir(): - set_name = set_dir.name - SETS_TO_CARDS[set_name] = [] - for card in set_dir.iterdir(): - SETS_TO_CARDS[set_name].append(card.stem) - CARDS_TO_SETS[card.stem] = set_name + +def populate_lookup_dicts(): + """Populate the global lookup dictionaries""" + for set_dir in CARDS_DATA_DIR.iterdir(): + set_name = set_dir.name + SETS_TO_CARDS[set_name] = [] + for card in set_dir.iterdir(): + SETS_TO_CARDS[set_name].append(card.stem) + CARDS_TO_SETS[card.stem] = set_name + + +def name2set(card: str) -> str: + """Return the set containing the card + + :param card: the english card name + :returns: the set containing the card + """ + + if not CARDS_TO_SETS: + populate_lookup_dicts() + + return CARDS_TO_SETS[card] + + +def set2names(set_name: str) -> list[str]: + """Return a list of all card in a set + + :param set_name: the set name + :returns: a list of all cards in the set + """ + + if not SETS_TO_CARDS: + populate_lookup_dicts() + + return SETS_TO_CARDS[set_name] + + +def name2file(card: str) -> str: + """Return the file name of a card + + File names are lower case card names with '_' instead of whitespace. + + :param card: the english name of a card + :returns: the file name of the card + """ + + return card.lower().replace(' ', '_') + + +def name2cardlisting(card: str) -> str: + """Return the card listing target + + The returned target can be used to directly navigate the + card listing to the specific card. + + Card listing targets are all lower case and use '-' instead of + whitespace. + Ending '!' are stripped from the card names. + + :param card: the english name of a card + :returns: the card listing target + """ + + return card.lower().replace(' ', '-').replace('!', '') -- cgit v1.2.3