From 80f1ea87d7760355699ee8eb6657cf8dd66bfc25 Mon Sep 17 00:00:00 2001 From: tretrauit Date: Wed, 3 Jan 2024 19:54:29 +0700 Subject: [PATCH] feat: implement VoicePackLanguage Enum better than string 8) --- vollerei/common/api/resource.py | 11 +++++++---- vollerei/common/enums.py | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 vollerei/common/enums.py diff --git a/vollerei/common/api/resource.py b/vollerei/common/api/resource.py index 865c8bf..733dfd2 100644 --- a/vollerei/common/api/resource.py +++ b/vollerei/common/api/resource.py @@ -3,6 +3,9 @@ Class wrapper for API endpoint /resource """ +from vollerei.common.enums import VoicePackLanguage + + class Segment: """ A segment of the game archive. @@ -41,7 +44,7 @@ class VoicePack: `name` maybe converted from `path` if the server returns empty string. Attributes: - language (str): Language of the voice pack. + language (VoicePackLanguage): Language of the voice pack. name (str): Voice pack archive name. path (str): Voice pack download path. size (int): Voice pack size. @@ -49,7 +52,7 @@ class VoicePack: package_size (int): Voice pack package size. """ - language: str + language: VoicePackLanguage name: str path: str # str -> int @@ -60,7 +63,7 @@ class VoicePack: def __init__( self, - language: str, + language: VoicePackLanguage, name: str, path: str, size: int, @@ -77,7 +80,7 @@ class VoicePack: @staticmethod def from_dict(data: dict) -> "VoicePack": return VoicePack( - data["language"], + VoicePackLanguage.from_remote_str(data["language"]), data["name"], data["path"], int(data["size"]), diff --git a/vollerei/common/enums.py b/vollerei/common/enums.py new file mode 100644 index 0000000..2ae981c --- /dev/null +++ b/vollerei/common/enums.py @@ -0,0 +1,27 @@ +from enum import Enum + + +class VoicePackLanguage(Enum): + Japanese = "ja-jp" + Chinese = "zh-cn" + Taiwanese = "zh-tw" + Korean = "ko-kr" + English = "en-us" + + @staticmethod + def from_remote_str(s: str) -> "VoicePackLanguage": + """ + Converts a language string from remote server to a VoicePackLanguage enum. + """ + if s == "ja-jp": + return VoicePackLanguage.Japanese + elif s == "zh-cn": + return VoicePackLanguage.Chinese + elif s == "zh-tw": + return VoicePackLanguage.Taiwanese + elif s == "ko-kr": + return VoicePackLanguage.Korean + elif s == "en-us": + return VoicePackLanguage.English + else: + raise ValueError(f"Invalid language string: {s}")