feat: implement VoicePackLanguage

Enum better than string 8)
This commit is contained in:
tretrauit 2024-01-03 19:54:29 +07:00
parent a9f3ee2e3e
commit 80f1ea87d7
2 changed files with 34 additions and 4 deletions

View File

@ -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"]),

27
vollerei/common/enums.py Normal file
View File

@ -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}")