All checks were successful
Build and Deploy Front / deploy (push) Successful in 7m17s
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
"""本地配置持久化。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from dataclasses import asdict, dataclass
|
|
from pathlib import Path
|
|
|
|
|
|
@dataclass
|
|
class AgentSettings:
|
|
oms_url: str = "https://api.test.ideship.com/admin"
|
|
token: str = ""
|
|
device_name: str = ""
|
|
bridge_port: int = 9333
|
|
web_origin: str = "http://localhost:5173"
|
|
|
|
@staticmethod
|
|
def config_path() -> Path:
|
|
base = Path.home() / ".oms-xhs-agent"
|
|
base.mkdir(parents=True, exist_ok=True)
|
|
return base / "settings.json"
|
|
|
|
@classmethod
|
|
def load(cls) -> AgentSettings:
|
|
path = cls.config_path()
|
|
if not path.is_file():
|
|
return cls()
|
|
try:
|
|
raw = json.loads(path.read_text(encoding="utf-8"))
|
|
if not isinstance(raw, dict):
|
|
return cls()
|
|
return cls(
|
|
oms_url=str(raw.get("oms_url") or cls().oms_url),
|
|
token=str(raw.get("token") or ""),
|
|
device_name=str(raw.get("device_name") or ""),
|
|
bridge_port=int(raw.get("bridge_port") or 9333),
|
|
web_origin=str(raw.get("web_origin") or cls().web_origin),
|
|
)
|
|
except (json.JSONDecodeError, OSError, ValueError):
|
|
return cls()
|
|
|
|
def save(self) -> None:
|
|
path = self.config_path()
|
|
path.write_text(
|
|
json.dumps(asdict(self), ensure_ascii=False, indent=2),
|
|
encoding="utf-8",
|
|
)
|