From 736b8ce4dc188c808473c689e245bb63b0313b02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Tue, 24 Oct 2023 16:22:39 +0800 Subject: [PATCH 1/3] :sparkles: Add Exception --- python_genshin_artifact/calculator.py | 13 +++++++++++-- python_genshin_artifact/enka/enka_parser.py | 20 +++++++++++++------- python_genshin_artifact/error.py | 12 ++++++++++++ 3 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 python_genshin_artifact/error.py diff --git a/python_genshin_artifact/calculator.py b/python_genshin_artifact/calculator.py index 6711369..7292946 100644 --- a/python_genshin_artifact/calculator.py +++ b/python_genshin_artifact/calculator.py @@ -1,17 +1,26 @@ +from json import JSONDecodeError + from genshin_artifact_core import ( get_damage_analysis as _get_damage_analysis, get_transformative_damage as _get_transformative_damage, ) +from python_genshin_artifact.error import JsonParseException from python_genshin_artifact.models.calculator import CalculatorConfig from python_genshin_artifact.models.damage.analysis import DamageAnalysis, TransformativeDamage def get_damage_analysis(value: CalculatorConfig) -> DamageAnalysis: - ret = _get_damage_analysis(value.json()) + try: + ret = _get_damage_analysis(value.json()) + except JSONDecodeError as exc: + raise JsonParseException from exc return DamageAnalysis.parse_raw(ret) def get_transformative_damage(value: CalculatorConfig) -> TransformativeDamage: - ret = _get_transformative_damage(value.json()) + try: + ret = _get_transformative_damage(value.json()) + except JSONDecodeError as exc: + raise JsonParseException from exc return TransformativeDamage.parse_raw(ret) diff --git a/python_genshin_artifact/enka/enka_parser.py b/python_genshin_artifact/enka/enka_parser.py index 37d7446..17914f1 100644 --- a/python_genshin_artifact/enka/enka_parser.py +++ b/python_genshin_artifact/enka/enka_parser.py @@ -6,6 +6,7 @@ from python_genshin_artifact.enka.characters import characters_map from python_genshin_artifact.enka.fight import fight_map from python_genshin_artifact.enka.weapon import weapon_name_map +from python_genshin_artifact.error import EnkaParseException from python_genshin_artifact.models.artifact import ArtifactInfo from python_genshin_artifact.models.characterInfo import CharacterInfo from python_genshin_artifact.models.weapon import WeaponInfo @@ -25,7 +26,7 @@ def is_ascend(level: int, promote_level: int) -> bool: def enka_parser(data: dict, avatar_id: int) -> Tuple[CharacterInfo, WeaponInfo, List[ArtifactInfo]]: character_info = assets.get_character(avatar_id) if character_info is None: - raise RuntimeError + raise EnkaParseException(f"avatarId={avatar_id} is not found in assets") _avatar_info_list = data["avatarInfoList"] _avatar_info: dict = {} for _value in _avatar_info_list: @@ -33,7 +34,7 @@ def enka_parser(data: dict, avatar_id: int) -> Tuple[CharacterInfo, WeaponInfo, _avatar_info = _value break else: - raise ValueError(f"avatarId={avatar_id} is not found") + raise EnkaParseException(f"avatarId={avatar_id} is not found") _prop_map = _avatar_info.get("propMap", {}) level = int(_prop_map["4001"].get("ival", 0)) if "4001" in _prop_map else 0 talent_id_list = _avatar_info.get("talentIdList", []) # 命之座 ID 列 如果未解锁任何命之座则此数据不存在 @@ -74,10 +75,13 @@ def de_equip_list(equip_list: list[dict]) -> Tuple[WeaponInfo, List[ArtifactInfo if _reliquary: sub_stats: List[Tuple[str, float]] = [] _flat = _equip["flat"] - artifact_id = int(re.findall(r"\d+", _flat["icon"])[0]) + try: + artifact_id = int(re.findall(r"\d+", _flat["icon"])[0]) + except IndexError: + raise EnkaParseException(f"artifact_id is not found in {_flat['icon']}") set_name = artifacts_name_map.get(artifact_id) if set_name is None: - raise RuntimeError + raise EnkaParseException(f"artifact_id={artifact_id} is not found") _level = _reliquary["level"] - 1 _reliquary_main_stat = _flat["reliquaryMainstat"] _main_prop_id = _reliquary_main_stat["mainPropId"] @@ -121,10 +125,12 @@ def de_equip_list(equip_list: list[dict]) -> Tuple[WeaponInfo, List[ArtifactInfo weapon_id = _equip["itemId"] weapon_name = weapon_name_map.get(weapon_id) if weapon_name is None: - raise RuntimeError + raise EnkaParseException(f"weapon_id={weapon_id} is not found in assets") _level = _weapon["level"] refinement_level = next(iter(_weapon["affixMap"].values())) + 1 - _promote_level = _weapon["promoteLevel"] - ascend = is_ascend(_level, _promote_level) + _promote_level = _weapon.get("promoteLevel") + ascend = False + if _promote_level is not None: + ascend = is_ascend(_level, _promote_level) weapon = WeaponInfo(level=_level, refine=refinement_level, ascend=ascend, name=weapon_name) return weapon, artifacts diff --git a/python_genshin_artifact/error.py b/python_genshin_artifact/error.py new file mode 100644 index 0000000..7d11634 --- /dev/null +++ b/python_genshin_artifact/error.py @@ -0,0 +1,12 @@ +class GenshinArtifactException(Exception): + """GenshinArtifactException""" + + +class JsonParseException(GenshinArtifactException): + """JSON 解析错误""" + + +class EnkaParseException(GenshinArtifactException): + """EnkaParse 解析错误""" + + From e11981a32175132b84a4fb105cea0e36c4d61a6a Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:24:31 +0000 Subject: [PATCH 2/3] style: format code with Black This commit fixes the style issues introduced in 736b8ce according to the output from Black. Details: https://github.com/PaiGramTeam/python-genshin-artifact/pull/10 --- python_genshin_artifact/error.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/python_genshin_artifact/error.py b/python_genshin_artifact/error.py index 7d11634..88e86b8 100644 --- a/python_genshin_artifact/error.py +++ b/python_genshin_artifact/error.py @@ -8,5 +8,3 @@ class JsonParseException(GenshinArtifactException): class EnkaParseException(GenshinArtifactException): """EnkaParse 解析错误""" - - From 0327b6eb4ae7155af32b7eebf298bca444992c0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Tue, 24 Oct 2023 16:25:08 +0800 Subject: [PATCH 3/3] :sparkles: Fix --- python_genshin_artifact/enka/enka_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_genshin_artifact/enka/enka_parser.py b/python_genshin_artifact/enka/enka_parser.py index 17914f1..17467e2 100644 --- a/python_genshin_artifact/enka/enka_parser.py +++ b/python_genshin_artifact/enka/enka_parser.py @@ -81,7 +81,7 @@ def de_equip_list(equip_list: list[dict]) -> Tuple[WeaponInfo, List[ArtifactInfo raise EnkaParseException(f"artifact_id is not found in {_flat['icon']}") set_name = artifacts_name_map.get(artifact_id) if set_name is None: - raise EnkaParseException(f"artifact_id={artifact_id} is not found") + raise EnkaParseException(f"artifact_id={artifact_id} is not found in assets") _level = _reliquary["level"] - 1 _reliquary_main_stat = _flat["reliquaryMainstat"] _main_prop_id = _reliquary_main_stat["mainPropId"]