|
| 1 | +import copy |
| 2 | +import json |
| 3 | +import os |
| 4 | +import typing |
| 5 | + |
| 6 | +KWDM_INSTANCE = None |
| 7 | +MANIFEST = None |
| 8 | +ADDITIONAL_CARDS = None |
| 9 | + |
| 10 | +KWD_TO_ALIAS: typing.Dict[str, str] = {} |
| 11 | +ALIAS_TO_KWD: typing.Dict[str, str] = {} |
| 12 | + |
| 13 | +from .insertion import Insertion |
| 14 | + |
| 15 | + |
| 16 | +def get_card(setting: typing.Dict[str, str]): |
| 17 | + source = setting["source"] |
| 18 | + if source == "kwd-data": |
| 19 | + data = KWDM_INSTANCE.get_keyword_data_dict(setting["keyword-name"]) |
| 20 | + card = data[setting["card-index"]] |
| 21 | + return card |
| 22 | + |
| 23 | + if source == "additional-cards": |
| 24 | + return ADDITIONAL_CARDS[setting["card-name"]] |
| 25 | + |
| 26 | + raise Exception() |
| 27 | + |
| 28 | + |
| 29 | +def add_alias(keyword: str, alias: str): |
| 30 | + KWD_TO_ALIAS[keyword] = alias |
| 31 | + ALIAS_TO_KWD[alias] = keyword |
| 32 | + |
| 33 | + |
| 34 | +def get_alias(keyword: str) -> typing.Optional[str]: |
| 35 | + return KWD_TO_ALIAS.get(keyword, None) |
| 36 | + |
| 37 | + |
| 38 | +def get_aliased_by(keyword: str): |
| 39 | + return ALIAS_TO_KWD.get(keyword, None) |
| 40 | + |
| 41 | + |
| 42 | +def is_aliased(keyword: str): |
| 43 | + return keyword in ALIAS_TO_KWD.keys() |
| 44 | + |
| 45 | + |
| 46 | +def _load_manifest(filename) -> typing.Dict: |
| 47 | + with open(filename) as f: |
| 48 | + manifest = json.load(f) |
| 49 | + return manifest |
| 50 | + |
| 51 | + |
| 52 | +class AdditionalCards: |
| 53 | + def __init__(self, filename): |
| 54 | + with open(filename) as f: |
| 55 | + self._cards = json.load(f) |
| 56 | + |
| 57 | + def __getitem__(self, name): |
| 58 | + """return a copy of the additional card, since the client may mutate it.""" |
| 59 | + return copy.deepcopy(self._cards[name]) |
| 60 | + |
| 61 | + |
| 62 | +class KWDM: |
| 63 | + def __init__(self, filename): |
| 64 | + with open(filename, encoding="utf-8") as f: |
| 65 | + self._data: typing.Dict = json.load(f) |
| 66 | + |
| 67 | + def get_keywords_list(self) -> typing.List[str]: |
| 68 | + return list(self._data.keys()) |
| 69 | + |
| 70 | + def get_keyword_data_dict(self, name: str) -> typing.Dict: |
| 71 | + return copy.deepcopy(self[name]) |
| 72 | + |
| 73 | + def __getitem__(self, name: str) -> typing.Dict: |
| 74 | + return self._data[name] |
| 75 | + |
| 76 | + |
| 77 | +def load(this_folder: str, kwd_file: str, manifest: str, additional_cards: str): |
| 78 | + global KWDM_INSTANCE, MANIFEST, ADDITIONAL_CARDS |
| 79 | + if kwd_file == "": |
| 80 | + KWDM_INSTANCE = KWDM(os.path.join(this_folder, "kwd.json")) |
| 81 | + else: |
| 82 | + KWDM_INSTANCE = KWDM(kwd_file) |
| 83 | + if manifest == "": |
| 84 | + MANIFEST = _load_manifest(this_folder / "manifest.json") |
| 85 | + else: |
| 86 | + MANIFEST = _load_manifest(manifest) |
| 87 | + if additional_cards == "": |
| 88 | + ADDITIONAL_CARDS = AdditionalCards(this_folder / "additional-cards.json") |
| 89 | + else: |
| 90 | + ADDITIONAL_CARDS = AdditionalCards(additional_cards) |
0 commit comments