fix: #110 修复配置加载问题

This commit is contained in:
涵曦 2024-07-07 08:28:09 +00:00
parent c5c691b653
commit 5b8054abd9

View File

@ -73,7 +73,7 @@ class Config:
hostname: str = os.getenv("XIAOMUSIC_HOSTNAME", "192.168.2.5") hostname: str = os.getenv("XIAOMUSIC_HOSTNAME", "192.168.2.5")
port: int = int(os.getenv("XIAOMUSIC_PORT", "8090")) # 监听端口 port: int = int(os.getenv("XIAOMUSIC_PORT", "8090")) # 监听端口
public_port: int = int(os.getenv("XIAOMUSIC_PUBLIC_PORT", 0)) # 歌曲访问端口 public_port: int = int(os.getenv("XIAOMUSIC_PUBLIC_PORT", 0)) # 歌曲访问端口
proxy: str | None = os.getenv("XIAOMUSIC_PROXY", None) proxy: str = os.getenv("XIAOMUSIC_PROXY", None)
search_prefix: str = os.getenv( search_prefix: str = os.getenv(
"XIAOMUSIC_SEARCH", "bilisearch:" "XIAOMUSIC_SEARCH", "bilisearch:"
) # "bilisearch:" or "ytsearch:" ) # "bilisearch:" or "ytsearch:"
@ -162,43 +162,40 @@ class Config:
config[key] = value config[key] = value
return cls(**config) return cls(**config)
@classmethod
def convert_value(cls, k, v, type_hints):
if v is not None and k in type_hints:
expected_type = type_hints[k]
try:
if expected_type is bool:
converted_value = False
if str(v).lower() == "true":
converted_value = True
else:
converted_value = expected_type(v)
print(converted_value)
return converted_value
except (ValueError, TypeError) as e:
print(f"Error converting {k}:{v} to {expected_type}: {e}")
return None
@classmethod @classmethod
def read_from_file(cls, config_path: str) -> dict: def read_from_file(cls, config_path: str) -> dict:
result = {} result = {}
with open(config_path, "rb") as f: with open(config_path, "rb") as f:
config = json.load(f) data = json.load(f)
for key, value in config.items(): type_hints = get_type_hints(cls)
if value is not None and key in cls.__dataclass_fields__:
result[key] = value for k, v in data.items():
converted_value = cls.convert_value(k, v, type_hints)
if converted_value is not None:
result[k] = converted_value
return result return result
def update_config(self, data): def update_config(self, data):
# 获取类型提示
type_hints = get_type_hints(self) type_hints = get_type_hints(self)
for k, v in data.items(): for k, v in data.items():
if v and k in type_hints: converted_value = self.convert_value(k, v, type_hints)
# 获取字段的类型 if converted_value is not None:
expected_type = type_hints[k]
# 根据期望的类型进行转换
if isinstance(v, expected_type):
# 如果v已经是正确的类型则直接赋值
setattr(self, k, v)
else:
# 尝试转换类型
try:
# 特殊情况处理(例如对布尔值的转换)
if expected_type is bool:
converted_value = False
if v and v.lower() == "true":
converted_value = True
else:
# 使用期望类型的构造函数进行转换
converted_value = expected_type(v)
except (ValueError, TypeError) as e:
print(f"Error converting {v} to {expected_type}: {e}")
continue
# 设置转换后的值
setattr(self, k, converted_value) setattr(self, k, converted_value)