修复bug

This commit is contained in:
dekun
2026-05-23 16:39:35 +08:00
parent c66c9f01c5
commit 952d57ab6d
7 changed files with 77 additions and 13 deletions
+34
View File
@@ -0,0 +1,34 @@
"""加载 manual_trading_hub/.envWindows 直接 python hub.py 时也需要)。"""
from __future__ import annotations
import os
from pathlib import Path
HUB_DIR = Path(__file__).resolve().parent
def load_hub_dotenv() -> None:
path = HUB_DIR / ".env"
if not path.is_file():
return
raw_bytes = path.read_bytes()
text = ""
for enc in ("utf-8-sig", "utf-16", "utf-16-le", "utf-16-be"):
try:
text = raw_bytes.decode(enc)
break
except Exception:
continue
if not text:
text = raw_bytes.decode("utf-8", errors="ignore")
text = text.replace("\x00", "")
for line in text.splitlines():
raw = line.strip()
if not raw or raw.startswith("#") or "=" not in raw:
continue
key, value = raw.split("=", 1)
clean_key = key.strip().lstrip("\ufeff")
if not clean_key.replace("_", "").isalnum():
continue
clean_value = value.strip().strip('"').strip("'")
os.environ[clean_key] = clean_value