35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
"""加载 manual_trading_hub/.env(Windows 直接 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
|