refactor: centralize web gui host/port config in path_config
This commit is contained in:
1 parent
1ef7325ead
commit
3659d5b7da
2 files changed
+115
No files matched your search
@@ -3,6 +3,7 @@
|
||||
"""
|
||||
import os
|
||||
from typing import Optional
|
||||
import json
|
||||
|
||||
# 项目根目录(所有路径的基准)
|
||||
# __file__ 位于 <project_root>/douyin/core/path_config.py,
|
||||
@@ -11,12 +12,113 @@ PROJECT_ROOT = os.path.dirname(
|
||||
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
)
|
||||
|
||||
# 运行时路径与设备侧默认路径(支持环境变量覆盖,便于迁移)
|
||||
DEFAULT_ANDROID_SCREENSHOT_PATH = "/sdcard/DCIM/screenshot.png"
|
||||
DEFAULT_ANDROID_SCREENSHOT_ALT_PATH = "/sdcard/DCIM/screenshot1.png"
|
||||
DEFAULT_ANDROID_WINDOW_DUMP_PATH = "/sdcard/window_dump.xml"
|
||||
DEFAULT_DOUYIN_PACKAGES = (
|
||||
"com.ss.android.ugc.aweme",
|
||||
"com.ss.android.ugc.aweme.lite",
|
||||
"com.zhiliaoapp.musically",
|
||||
)
|
||||
DEFAULT_ADB_KEYBOARD_IME = "com.android.adbkeyboard/.AdbIME"
|
||||
DEFAULT_WEB_GUI_HOST = "127.0.0.1"
|
||||
DEFAULT_WEB_GUI_PORT = 8000
|
||||
|
||||
|
||||
def get_project_root() -> str:
|
||||
"""返回项目根目录(绝对路径)"""
|
||||
return PROJECT_ROOT
|
||||
|
||||
|
||||
def get_venv_python_path() -> str:
|
||||
"""返回项目虚拟环境 Python 路径(Windows)。"""
|
||||
return os.path.join(PROJECT_ROOT, ".venv", "Scripts", "python.exe")
|
||||
|
||||
|
||||
def get_android_screenshot_path() -> str:
|
||||
"""返回设备侧截图路径(可通过环境变量覆盖)。"""
|
||||
return (
|
||||
os.environ.get("ANDROID_SCREENSHOT_PATH", "").strip()
|
||||
or DEFAULT_ANDROID_SCREENSHOT_PATH
|
||||
)
|
||||
|
||||
|
||||
def get_android_screenshot_alt_path() -> str:
|
||||
"""返回设备侧备用截图路径(可通过环境变量覆盖)。"""
|
||||
return (
|
||||
os.environ.get("ANDROID_SCREENSHOT_ALT_PATH", "").strip()
|
||||
or DEFAULT_ANDROID_SCREENSHOT_ALT_PATH
|
||||
)
|
||||
|
||||
|
||||
def get_android_window_dump_path() -> str:
|
||||
"""返回设备侧 UI dump 路径(可通过环境变量覆盖)。"""
|
||||
return (
|
||||
os.environ.get("ANDROID_WINDOW_DUMP_PATH", "").strip()
|
||||
or DEFAULT_ANDROID_WINDOW_DUMP_PATH
|
||||
)
|
||||
|
||||
|
||||
def get_douyin_packages() -> tuple[str, ...]:
|
||||
"""返回抖音候选包名列表(支持环境变量覆盖,逗号分隔)。"""
|
||||
raw = (os.environ.get("DOUYIN_PACKAGES", "") or "").strip()
|
||||
if not raw:
|
||||
return DEFAULT_DOUYIN_PACKAGES
|
||||
items = tuple(x.strip() for x in raw.split(",") if x.strip())
|
||||
return items or DEFAULT_DOUYIN_PACKAGES
|
||||
|
||||
|
||||
def get_adb_keyboard_ime() -> str:
|
||||
"""返回 ADBKeyboard IME(支持环境变量覆盖)。"""
|
||||
return (os.environ.get("ADB_KEYBOARD_IME", "") or "").strip() or DEFAULT_ADB_KEYBOARD_IME
|
||||
|
||||
|
||||
def _read_runtime_config() -> dict:
|
||||
"""读取 config/config.json,失败时返回空字典。"""
|
||||
try:
|
||||
cfg_path = get_config_path()
|
||||
if not os.path.exists(cfg_path):
|
||||
return {}
|
||||
with open(cfg_path, "r", encoding="utf-8") as f:
|
||||
obj = json.load(f)
|
||||
return obj if isinstance(obj, dict) else {}
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def get_web_gui_host() -> str:
|
||||
"""返回 Web GUI 监听 host(环境变量优先,其次 config.json,最后默认值)。"""
|
||||
host = (os.environ.get("WEB_GUI_HOST") or os.environ.get("HOST") or "").strip()
|
||||
if host:
|
||||
return host
|
||||
cfg = _read_runtime_config()
|
||||
cfg_host = str(cfg.get("web_gui_host", "") or "").strip()
|
||||
return cfg_host or DEFAULT_WEB_GUI_HOST
|
||||
|
||||
|
||||
def get_web_gui_port() -> int:
|
||||
"""返回 Web GUI 监听端口(环境变量优先,其次 config.json,最后默认值)。"""
|
||||
env_raw = (os.environ.get("PORT") or os.environ.get("WEB_GUI_PORT") or "").strip()
|
||||
if env_raw:
|
||||
try:
|
||||
port = int(env_raw)
|
||||
if 1 <= port <= 65535:
|
||||
return port
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
cfg = _read_runtime_config()
|
||||
cfg_raw = cfg.get("web_gui_port", DEFAULT_WEB_GUI_PORT)
|
||||
try:
|
||||
port = int(cfg_raw)
|
||||
if 1 <= port <= 65535:
|
||||
return port
|
||||
except Exception:
|
||||
pass
|
||||
return DEFAULT_WEB_GUI_PORT
|
||||
|
||||
|
||||
def get_config_path() -> str:
|
||||
"""返回 config.json 的路径"""
|
||||
return os.path.join(PROJECT_ROOT, "config", "config.json")
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
"""Web GUI network configuration helpers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from douyin.core import path_config
|
||||
|
||||
|
||||
def get_web_gui_host() -> str:
|
||||
return path_config.get_web_gui_host()
|
||||
|
||||
|
||||
def get_web_gui_port() -> int:
|
||||
return path_config.get_web_gui_port()
|
||||
Reference in new issue
Block a user