"""Helper code for the data subdirectory""" from pathlib import Path GAME_ROOT = Path(__file__).parent.parent CARDS_DATA_DIR = GAME_ROOT / 'data' / 'cards' CARDS_TO_SETS = {} SETS_TO_CARDS = {} 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 CARDS_TO_SETS[f'{set_name}/{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. If a path including the set is given only the card part is considered. :param card: the english name of a card :returns: the card listing target """ if '/' in card: card = card.split('/')[-1] return card.lower().replace(' ', '-').replace('!', '')