From d49d3cbff6f74821a07df32d837ff26e4a4b918b Mon Sep 17 00:00:00 2001 From: LDericher <40151420+ldericher@users.noreply.github.com> Date: Tue, 14 Sep 2021 01:21:32 +0200 Subject: [PATCH] card.py scope refactoring --- fftcgtool/book.py | 1 - fftcgtool/card.py | 135 +++++++++++++++++++++++-------------------- fftcgtool/ttsdeck.py | 3 +- 3 files changed, 73 insertions(+), 66 deletions(-) diff --git a/fftcgtool/book.py b/fftcgtool/book.py index f8f3d64..3695dae 100644 --- a/fftcgtool/book.py +++ b/fftcgtool/book.py @@ -1,7 +1,6 @@ import dataclasses import logging import os -from dataclasses import replace from PIL import Image diff --git a/fftcgtool/card.py b/fftcgtool/card.py index 56da422..9f3affb 100644 --- a/fftcgtool/card.py +++ b/fftcgtool/card.py @@ -2,6 +2,7 @@ from __future__ import annotations import re from dataclasses import dataclass +from typing import Any from .code import Code from .language import Language, API_LANGS @@ -15,20 +16,67 @@ class CardContent: face: str +_ELEMENTS_JAP = [ + "火", "氷", "風", "土", "雷", "水", "光", "闇" +] + +_ELEMENTS_ENG = [ + "Fire", "Ice", "Wind", "Earth", "Lightning", "Water", "Light", "Darkness" +] + +_ELEMENTS_MAP = { + elem_j: elem_e + for elem_j, elem_e in zip(_ELEMENTS_JAP, _ELEMENTS_ENG) +} + + +def _sub_encircle(match: re.Match) -> str: + return encircle_symbol(match.group(1), False) + + +def _sub_elements(match: re.Match) -> str: + return encircle_symbol(_ELEMENTS_MAP[match.group(1)], False) + + +def _load_name(language: Language, data: dict[str, Any]) -> str: + return data[f"Name{language.key_suffix}"] + + +def _load_text(language: Language, data: dict) -> str: + # load text + text = str(data[f"Text{language.key_suffix}"]) + # place "S" symbols + text = text.replace("《S》", encircle_symbol("S", False)) + # place elemental cost symbols + text = re.sub(rf"《([{''.join(_ELEMENTS_JAP)}])》", _sub_elements, text, flags=re.UNICODE) + # place dull symbols + text = text.replace("《ダル》", "[⤵]") + # relocate misplaced line break markers + text = re.sub(r"(\[\[[a-z]+]][^\[]*?)(\[\[br]])([^\[]*?\[\[/]])", r"\2\1\3", text, + flags=re.IGNORECASE | re.UNICODE) + # place EX-BURST markers + text = re.sub(r"\[\[ex]]\s*EX BURST\s*\[\[/]]\s*", r"[EX BURST] ", text, + flags=re.IGNORECASE | re.UNICODE) + # also place unmarked EX-BURST markers + text = re.sub(r"([^\[]|^)(EX BURST)\s*([^]]|$)", r"\1[\2] \3", text, flags=re.UNICODE) + # replace Damage hints with brackets and en-dash + text = re.sub(r"\[\[i]](Schaden|Damage|Daños|Dégâts|Danni)\s*([0-9]+)\s*--\s*\[\[/]]\s*", r"[\1 \2] – ", + text, flags=re.IGNORECASE | re.UNICODE) + # place other letter and numerical cost symbols + text = re.sub(r"《([a-z0-9])》", _sub_encircle, text, flags=re.IGNORECASE | re.UNICODE) + # remove empty formatting hints + text = re.sub(r"\[\[[a-z]]]\s*\[\[/]]\s*", r" ", text, flags=re.IGNORECASE | re.UNICODE) + # replace formatting hints with brackets + text = re.sub(r"\[\[[a-z]]]([^\[]*?)\s*\[\[/]]\s*", r"[\1] ", text, flags=re.IGNORECASE | re.UNICODE) + # relocate misplaced spaces at start of bracketed string + text = re.sub(r"\s*(\[)\s+([^]]*?])", r" \1\2", text, flags=re.IGNORECASE | re.UNICODE) + # relocate misplaced spaces at end of bracketed string + text = re.sub(r"(\[[^]]*?)\s+(])\s*", r"\1\2 ", text, flags=re.IGNORECASE | re.UNICODE) + # place line breaks + return re.sub(r"\s*\[\[br]]\s*", "\n\n", text, flags=re.IGNORECASE | re.UNICODE) + + class Card: - __ELEMENTS_JAP = [ - "火", "氷", "風", "土", "雷", "水", "光", "闇" - ] - - __ELEMENTS_ENG = [ - "Fire", "Ice", "Wind", "Earth", "Lightning", "Water", "Light", "Darkness" - ] - - __ELEMENTS_MAP = { - elem_j: elem_e - for elem_j, elem_e in zip(__ELEMENTS_JAP, __ELEMENTS_ENG) - } - def __init__(self, code: Code, elements: list[str], content: dict[Language, CardContent], index: int = 0): self.__code: Code = code self.__elements: list[str] = elements @@ -36,7 +84,7 @@ class Card: self.__index = index @classmethod - def from_square_api_data(cls, data: dict[str, any]) -> Card: + def from_square_api_data(cls, data: dict[str, Any]) -> Card: if not data: return cls( code=Code(""), @@ -45,61 +93,20 @@ class Card: ) else: - def sub_encircle(match: re.Match) -> str: - return encircle_symbol(match.group(1), False) - - def sub_elements(match: re.Match) -> str: - return encircle_symbol(Card.__ELEMENTS_MAP[match.group(1)], False) - - def load_name(language: Language) -> str: - return data[f"Name{language.key_suffix}"] - - def load_text(language: Language) -> str: - # load text - text = str(data[f"Text{language.key_suffix}"]) - # place "S" symbols - text = text.replace("《S》", encircle_symbol("S", False)) - # place elemental cost symbols - text = re.sub(rf"《([{''.join(Card.__ELEMENTS_JAP)}])》", sub_elements, text, flags=re.UNICODE) - # place dull symbols - text = text.replace("《ダル》", "[⤵]") - # relocate misplaced line break markers - text = re.sub(r"(\[\[[a-z]+]][^\[]*?)(\[\[br]])([^\[]*?\[\[/]])", r"\2\1\3", text, - flags=re.IGNORECASE | re.UNICODE) - # # relocate misplaced spaces - # text = re.sub(r"(\[\[[a-z]+]][^\[]*?)\s+(\[\[/]])", r"\1\2 ", text, flags=re.IGNORECASE | re.UNICODE) - # text = re.sub(r"(\[\[[a-z]+]])\s+([^\[]*?\[\[/]])", r" \1\2", text, flags=re.IGNORECASE | re.UNICODE) - # place EX-BURST markers - text = re.sub(r"\[\[ex]]\s*EX BURST\s*\[\[/]]\s*", r"[EX BURST] ", text, - flags=re.IGNORECASE | re.UNICODE) - text = re.sub(r"([^\[]|^)(EX BURST)\s*([^]]|$)", r"\1[\2] \3", text, flags=re.UNICODE) - # replace Damage hints with brackets and en-dash - text = re.sub(r"\[\[i]](Schaden|Damage|Daños|Dégâts|Danni)\s*([0-9]+)\s*--\s*\[\[/]]\s*", r"[\1 \2] – ", - text, flags=re.IGNORECASE | re.UNICODE) - # place other letter and numerical cost symbols - text = re.sub(r"《([a-z0-9])》", sub_encircle, text, flags=re.IGNORECASE | re.UNICODE) - # remove empty formatting hints - text = re.sub(r"\[\[[a-z]]]\s*\[\[/]]\s*", r" ", text, flags=re.IGNORECASE | re.UNICODE) - # replace formatting hints with brackets - text = re.sub(r"\[\[[a-z]]]([^\[]*?)\s*\[\[/]]\s*", r"[\1] ", text, flags=re.IGNORECASE | re.UNICODE) - # relocate misplaced spaces - text = re.sub(r"(\[[^]]*?)\s+(])\s*", r"\1\2 ", text, flags=re.IGNORECASE | re.UNICODE) - text = re.sub(r"\s*(\[)\s+([^]]*?])", r" \1\2", text, flags=re.IGNORECASE | re.UNICODE) - # place line breaks - return re.sub(r"\s*\[\[br]]\s*", "\n\n", text, flags=re.IGNORECASE | re.UNICODE) - - content = { - language: CardContent(load_name(language), load_text(language), "") - for language in API_LANGS - } - return cls( code=Code(data["Code"]), elements=[ - Card.__ELEMENTS_MAP[element] + _ELEMENTS_MAP[element] for element in data["Element"].split("/") ], - content=content, + content={ + language: CardContent( + _load_name(language, data), + _load_text(language, data), + "" + ) + for language in API_LANGS + }, ) def __repr__(self) -> str: diff --git a/fftcgtool/ttsdeck.py b/fftcgtool/ttsdeck.py index e6b881f..0b7c9ba 100644 --- a/fftcgtool/ttsdeck.py +++ b/fftcgtool/ttsdeck.py @@ -3,6 +3,7 @@ from __future__ import annotations import json import logging import os +from typing import Any from .carddb import CardDB from .cards import Cards @@ -44,7 +45,7 @@ class TTSDeck(Cards): def file_name(self) -> str: return f"{super().file_name}.json" - def get_tts_object(self, language: Language) -> dict[str, any]: + def get_tts_object(self, language: Language) -> dict[str, Any]: carddb = CardDB() # unique face urls used