-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject_platform_config.py
More file actions
199 lines (185 loc) · 8.27 KB
/
Copy pathinject_platform_config.py
File metadata and controls
199 lines (185 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
"""Inject platform-config globals into index.html before </head>."""
import json
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
SOURCE = ROOT / "web" / "strategy-switch-console" / "index.html"
CONFIG = ROOT / "platform-config.json"
def main() -> int:
config = json.loads(CONFIG.read_text(encoding="utf-8"))
platforms = config["platforms"]
strategies = config["strategies"]
meta = config.get("meta", {})
runtime_authority = meta.get("runtime_authority", {}) if isinstance(meta, dict) else {}
pc, dao, dca, inc_layer, opt_overlay = {}, {}, {}, {}, {}
strategy_profiles = []
domain_labels = {
did: {"zh": ddata.get("label_zh", did), "en": ddata.get("label_en", did)}
for did, ddata in config.get("domains", {}).items()
}
for pid, pdata in platforms.items():
caps, depl = pdata["capabilities"], pdata["deployment"]
pc[pid] = dict(
dry_run_only=depl.get("dry_run_only", False),
margin_policy=caps.get("margin_policy", False),
reserved_cash=caps.get("reserved_cash", False),
income_layer=caps.get("income_layer", False),
option_overlay=caps.get("option_overlay", False),
dca=caps.get("dca", False),
execution_mode=depl.get("default_execution_mode", "live"),
service_name=depl.get("service_name", ""),
default_execution_mode=depl.get("default_execution_mode", "live"),
)
acct = pdata.get("default_account", {})
entry = dict(
key=acct.get("key", pid),
label=acct.get("label", pdata.get("label", pid)),
target_name=acct.get("target_name", acct.get("key", pid)),
supported_domains=acct.get("supported_domains", pdata.get("supported_domains", [])),
cash_currency=acct.get("cash_currency", "USD"),
)
for fld in (
"service_name",
"account_scope",
"deployment_selector",
"account_selector",
"default_execution_mode",
"min_reserved_cash_usd",
"reserved_cash_ratio",
"cash_only_execution_mode",
"dca_mode",
"dca_base_investment_usd",
):
if acct.get(fld):
entry[fld] = acct[fld]
if "service_name" not in entry:
entry["service_name"] = depl.get("service_name", "")
if "default_execution_mode" not in entry:
entry["default_execution_mode"] = depl.get("default_execution_mode", "live")
dao[pid] = [entry]
for sid, sdata in strategies.items():
feat = sdata.get("features", {})
strategy_profiles.append(_strategy_profile_entry(sid, sdata))
dd = sdata.get("dca_defaults")
if dd:
dca[sid] = dict(
defaultMode=dd.get("default_mode", "fixed"),
defaultBaseInvestmentUsd=str(dd.get("default_base_investment_usd", "1000")),
)
if feat.get("income_layer"):
idl = sdata.get("income_layer_defaults", {})
inc_layer[sid] = dict(
startUsd=int(idl.get("start_usd", 0)),
maxRatio=str(idl.get("max_ratio", "")),
allocations=idl.get("allocations", {}),
)
if feat.get("option_overlay"):
odl = sdata.get("option_overlay_defaults", {})
families = []
if odl.get("growth_enabled"):
families.append(
dict(
family="growth",
recipe=odl["growth_recipe"],
startUsd=odl["growth_start_usd"],
ratio=str(odl.get("nav_budget_ratio", "")),
ratioKind="budget",
)
)
if odl.get("income_enabled"):
families.append(
dict(
family="income",
recipe=odl["income_recipe"],
startUsd=odl["income_start_usd"],
ratio=str(odl.get("nav_risk_ratio", "")),
ratioKind="risk",
)
)
opt_overlay[sid] = dict(
liveGate=odl.get("live_gate", ""), liveStatus=odl.get("live_status", ""), families=families
)
block = "\n".join(
[
"<!-- Generated by inject_platform_config.py -->",
'<script id="platform-config">',
"window.__PLATFORM_CONFIG__ = " + json.dumps(pc, ensure_ascii=False) + ";",
"window.__QSL_RUNTIME_AUTHORITY_STATUS__ = " + json.dumps(runtime_authority, ensure_ascii=False) + ";",
"window.__DEFAULT_ACCOUNT_OPTIONS__ = " + json.dumps(dao, ensure_ascii=False) + ";",
"window.__DOMAIN_LABELS__ = " + json.dumps(domain_labels, ensure_ascii=False) + ";",
"window.__DEFAULT_STRATEGY_PROFILES__ = " + json.dumps(strategy_profiles, ensure_ascii=False) + ";",
"window.__DCA_PROFILE_DEFAULTS__ = " + json.dumps(dca, ensure_ascii=False) + ";",
"window.__INCOME_LAYER_DEFAULTS__ = " + json.dumps(inc_layer, ensure_ascii=False) + ";",
"window.__OPTION_OVERLAY_DEFAULTS__ = " + json.dumps(opt_overlay, ensure_ascii=False) + ";",
"</script>",
]
)
html = SOURCE.read_text(encoding="utf-8")
marker = "<!-- Generated by inject_platform_config.py -->"
existing = html.find('<script id="platform-config">')
if existing >= 0:
start = existing
while True:
prefix = html[:start].rstrip()
marker_start = prefix.rfind(marker)
if marker_start < 0:
break
if prefix[marker_start:].strip() != marker:
break
start = marker_start
end = html.find("</script>", existing) + 9
html = html[:start].rstrip() + "\n\n" + block + html[end:]
else:
head_end = html.find("</head>")
html = html[:head_end] + "\n" + block + "\n" + html[head_end:]
SOURCE.write_text(html, encoding="utf-8")
print("Injected platform-config into index.html")
return 0
def _strategy_profile_entry(sid: str, sdata: dict) -> dict:
feat = sdata.get("features", {})
runtime_enabled = sdata.get("runtime_enabled", False)
lifecycle_stage = str(
sdata.get("lifecycle_stage") or ("runtime_enabled" if runtime_enabled else "research_active")
).strip()
can_switch_live = sdata.get(
"can_switch_live",
runtime_enabled and lifecycle_stage in {"live_enabled", "runtime_enabled"},
)
blocked_live_reason = sdata.get("blocked_live_reason")
if blocked_live_reason is None and not can_switch_live:
blocked_live_reason = lifecycle_stage or "not_runtime_enabled"
entry = {
"profile": sid,
"label": sdata.get("label", sid),
"label_en": sdata.get("label_en", sid),
"label_zh": sdata.get("label", sid),
"domain": sdata.get("domain", ""),
"runtime_enabled": runtime_enabled,
"lifecycle_stage": lifecycle_stage,
"can_switch_live": can_switch_live,
"allowed_execution_modes": _normalize_allowed_execution_modes(sdata.get("allowed_execution_modes")),
"blocked_live_reason": "" if blocked_live_reason is None else str(blocked_live_reason).strip(),
"income_layer_enabled": feat.get("income_layer", False),
"option_overlay_enabled": feat.get("option_overlay", False),
"combo_enabled": feat.get("combo", False),
}
if feat.get("combo"):
entry["combo_mode"] = feat.get("combo_mode", "dynamic")
return entry
def _normalize_allowed_execution_modes(raw_modes: object) -> list[str]:
if raw_modes is None:
return ["paper", "dry_run"]
if isinstance(raw_modes, str):
modes = [raw_modes.strip()]
elif isinstance(raw_modes, list):
modes = [str(mode).strip() for mode in raw_modes]
elif isinstance(raw_modes, tuple):
modes = [str(mode).strip() for mode in raw_modes]
elif isinstance(raw_modes, set):
modes = [str(mode).strip() for mode in sorted(raw_modes)]
else:
modes = ["paper", "dry_run"]
modes = [mode for mode in modes if mode]
return modes if modes else ["paper", "dry_run"]
if __name__ == "__main__":
raise SystemExit(main())