mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-27 04:49:47 +01:00
refactor: 将枚举命名改为大驼峰
chore: 删除多余的 LibPath 枚举
This commit is contained in:
parent
10c06e25e0
commit
c3184be944
1 changed files with 28 additions and 38 deletions
|
|
@ -15,65 +15,56 @@ APP_DIR = str(Path(__file__).resolve().parent.parent)
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
||||||
|
|
||||||
class PLATFORM(Enum):
|
class Platform(Enum):
|
||||||
WINDOWS = "windows"
|
WINDOWS = "windows"
|
||||||
MACOS = "darwin"
|
MACOS = "darwin"
|
||||||
LINUX = "linux"
|
LINUX = "linux"
|
||||||
|
|
||||||
|
|
||||||
class ARCH(Enum):
|
class Arch(Enum):
|
||||||
WINDOWS = {"arm": "arm64", "intel": "x64"}
|
WINDOWS = {"arm": "arm64", "intel": "x64"}
|
||||||
MACOS = {"arm": "arm64", "intel": "x64"}
|
MACOS = {"arm": "arm64", "intel": "x64"}
|
||||||
LINUX = {"arm": "arm64", "intel": "x64"}
|
LINUX = {"arm": "arm64", "intel": "x64"}
|
||||||
|
|
||||||
|
|
||||||
class LIB_PATH(Enum):
|
class OpusInfo(Enum):
|
||||||
WINDOWS = "libs/win/{arch}"
|
|
||||||
MACOS = "libs/mac/{arch}"
|
|
||||||
LINUX = "libs/linux/{arch}"
|
|
||||||
|
|
||||||
|
|
||||||
class OPUS_INFO(Enum):
|
|
||||||
WINDOWS = {"name": "opus.dll", "system_name": ["opus"]}
|
WINDOWS = {"name": "opus.dll", "system_name": ["opus"]}
|
||||||
MACOS = {"name": "libopus.dylib", "system_name": ["libopus.dylib"]}
|
MACOS = {"name": "libopus.dylib", "system_name": ["libopus.dylib"]}
|
||||||
LINUX = {"name": "libopus.so", "system_name": ["libopus.so.0", "libopus.so"]}
|
LINUX = {"name": "libopus.so", "system_name": ["libopus.so.0", "libopus.so"]}
|
||||||
|
|
||||||
|
|
||||||
def _get_platform_dict() -> dict[PLATFORM, dict]:
|
def _get_platform_dict() -> dict[Platform, dict]:
|
||||||
"""获取平台映射字典"""
|
"""获取平台映射字典"""
|
||||||
return {
|
return {
|
||||||
PLATFORM.WINDOWS: {
|
Platform.WINDOWS: {
|
||||||
"arch": ARCH.WINDOWS,
|
"arch": Arch.WINDOWS,
|
||||||
"lib_path": LIB_PATH.WINDOWS,
|
"lib_info": OpusInfo.WINDOWS,
|
||||||
"lib_info": OPUS_INFO.WINDOWS,
|
|
||||||
"dir": "win",
|
"dir": "win",
|
||||||
},
|
},
|
||||||
PLATFORM.MACOS: {
|
Platform.MACOS: {
|
||||||
"arch": ARCH.MACOS,
|
"arch": Arch.MACOS,
|
||||||
"lib_path": LIB_PATH.MACOS,
|
"lib_info": OpusInfo.MACOS,
|
||||||
"lib_info": OPUS_INFO.MACOS,
|
|
||||||
"dir": "mac",
|
"dir": "mac",
|
||||||
},
|
},
|
||||||
PLATFORM.LINUX: {
|
Platform.LINUX: {
|
||||||
"arch": ARCH.LINUX,
|
"arch": Arch.LINUX,
|
||||||
"lib_path": LIB_PATH.LINUX,
|
"lib_info": OpusInfo.LINUX,
|
||||||
"lib_info": OPUS_INFO.LINUX,
|
|
||||||
"dir": "linux",
|
"dir": "linux",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_platform() -> PLATFORM:
|
def get_platform() -> Platform:
|
||||||
"""获取当前平台"""
|
"""获取当前平台"""
|
||||||
system = platform.system().lower()
|
system = platform.system().lower()
|
||||||
if system in ("windows", "win32", "cygwin"):
|
if system in ("windows", "win32", "cygwin"):
|
||||||
return PLATFORM.WINDOWS
|
return Platform.WINDOWS
|
||||||
if system == "darwin":
|
if system == "darwin":
|
||||||
return PLATFORM.MACOS
|
return Platform.MACOS
|
||||||
return PLATFORM.LINUX
|
return Platform.LINUX
|
||||||
|
|
||||||
|
|
||||||
def get_arch(system: PLATFORM) -> tuple[str, str]:
|
def get_arch(system: Platform) -> tuple[str, str]:
|
||||||
"""获取当前架构
|
"""获取当前架构
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -92,14 +83,14 @@ def get_arch(system: PLATFORM) -> tuple[str, str]:
|
||||||
return architecture, arch_name
|
return architecture, arch_name
|
||||||
|
|
||||||
|
|
||||||
def get_lib_name(system: PLATFORM, local: bool = True) -> str | list[str]:
|
def get_lib_name(system: Platform, local: bool = True) -> str | list[str]:
|
||||||
"""根据平台架构获取 Opus 库名称"""
|
"""根据平台架构获取 Opus 库名称"""
|
||||||
key = "name" if local else "system_name"
|
key = "name" if local else "system_name"
|
||||||
platform_dict = _get_platform_dict()
|
platform_dict = _get_platform_dict()
|
||||||
return platform_dict[system]["lib_info"].value[key]
|
return platform_dict[system]["lib_info"].value[key]
|
||||||
|
|
||||||
|
|
||||||
def get_system_info() -> tuple[PLATFORM, str]:
|
def get_system_info() -> tuple[Platform, str]:
|
||||||
"""获取当前系统信息
|
"""获取当前系统信息
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
|
|
@ -143,7 +134,7 @@ def _build_lib_candidates(
|
||||||
return candidates
|
return candidates
|
||||||
|
|
||||||
|
|
||||||
def get_search_paths(system: PLATFORM, arch_name: str) -> list[tuple[str, str]]:
|
def get_search_paths(system: Platform, arch_name: str) -> list[tuple[str, str]]:
|
||||||
"""获取库文件搜索路径列表
|
"""获取库文件搜索路径列表
|
||||||
|
|
||||||
按优先级:特定平台/架构 > 特定平台 > 通用 > 项目根目录
|
按优先级:特定平台/架构 > 特定平台 > 通用 > 项目根目录
|
||||||
|
|
@ -186,7 +177,7 @@ def get_search_paths(system: PLATFORM, arch_name: str) -> list[tuple[str, str]]:
|
||||||
return search_paths
|
return search_paths
|
||||||
|
|
||||||
|
|
||||||
def find_system_opus(system: PLATFORM) -> str:
|
def find_system_opus(system: Platform) -> str:
|
||||||
"""从系统路径查找 Opus 库
|
"""从系统路径查找 Opus 库
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -220,7 +211,7 @@ def find_system_opus(system: PLATFORM) -> str:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def _find_local_opus(system: PLATFORM, arch_name: str) -> str | None:
|
def _find_local_opus(system: Platform, arch_name: str) -> str | None:
|
||||||
"""从本地搜索路径查找 Opus 库
|
"""从本地搜索路径查找 Opus 库
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -228,7 +219,7 @@ def _find_local_opus(system: PLATFORM, arch_name: str) -> str | None:
|
||||||
arch_name: 架构名称
|
arch_name: 架构名称
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
找到的库文件路径,未找到返回空字符串
|
找到的库文件路径,未找到返回 None
|
||||||
"""
|
"""
|
||||||
search_paths = get_search_paths(system, arch_name)
|
search_paths = get_search_paths(system, arch_name)
|
||||||
|
|
||||||
|
|
@ -239,14 +230,14 @@ def _find_local_opus(system: PLATFORM, arch_name: str) -> str | None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _setup_dll_search_path(system: PLATFORM, lib_dir: str) -> None:
|
def _setup_dll_search_path(system: Platform, lib_dir: str) -> None:
|
||||||
"""在Windows上设置DLL搜索路径
|
"""在Windows上设置DLL搜索路径
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
system: 平台枚举值
|
system: 平台枚举值
|
||||||
lib_dir: 库文件所在目录
|
lib_dir: 库文件所在目录
|
||||||
"""
|
"""
|
||||||
if system != PLATFORM.WINDOWS or not lib_dir:
|
if system != Platform.WINDOWS or not lib_dir:
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -297,10 +288,9 @@ def setup_opus() -> bool:
|
||||||
Returns:
|
Returns:
|
||||||
加载成功返回True,否则返回False
|
加载成功返回True,否则返回False
|
||||||
"""
|
"""
|
||||||
# 检查是否已经由 runtime_hook 加载
|
# 检查 Opus 库是否已在当前进程中完成加载,避免重复初始化
|
||||||
if hasattr(sys, "_opus_loaded"):
|
if hasattr(sys, "_opus_loaded"):
|
||||||
logger.info("Opus 库已由运行时钩子加载")
|
logger.info("Opus 库已加载,跳过重复初始化")
|
||||||
return True
|
|
||||||
|
|
||||||
system, arch_name = get_system_info()
|
system, arch_name = get_system_info()
|
||||||
final_lib_path = ""
|
final_lib_path = ""
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue