Add product-specific session and token setup

This commit is contained in:
2026-09-23 09:27:55 +05:30
parent 4749efe45f
commit 9d16b20eb4
8 changed files with 249 additions and 66 deletions
+56
View File
@@ -1,4 +1,5 @@
import json
from copy import deepcopy
from pathlib import Path
@@ -11,6 +12,61 @@ def load_config():
return json.load(f)
def get_product_config(product_name, config=None):
"""Resolve a product's setup flags and session defaults without mutating input."""
config = deepcopy(load_config() if config is None else config)
if "products" not in config:
if product_name != "IDAS":
raise ValueError(f"Unknown setup product: {product_name}")
config["token"] = config.get("trading_token_idas", {})
else:
product = config["products"][product_name]
setup = config.setdefault("setup", {})
for flag in ("run_session", "run_token"):
setup[flag] = product.get(flag, setup.get(flag, True))
if not isinstance(setup[flag], bool):
raise ValueError(f"products.{product_name}.{flag} must be true or false")
setup["session"] = {**setup.get("session", {}), **product.get("session", {})}
config["product"] = {"me_instance": product.get("me_instance")}
config["token"] = deepcopy(product.get("token", {}))
config["product_name"] = product_name
return config
def get_setup_configs(mode, config=None):
"""Partition products so a full setup run never creates a product twice."""
if mode not in ("combined", "session", "token"):
raise ValueError(f"Unknown setup mode: {mode}")
config = load_config() if config is None else config
products = config.get("products", {"IDAS": {}})
selected = []
for name, product in products.items():
if not product.get("enabled", True):
continue
resolved = get_product_config(name, config)
setup = resolved.get("setup", {})
session = setup.get("run_session", True)
token = setup.get("run_token", True)
matches = {
"combined": session and token,
"session": session and not token,
"token": token and not session,
}
if not matches[mode]:
continue
if session and not resolved.get("product", {}).get("me_instance"):
raise ValueError(f"products.{name}.me_instance is required for session creation")
if token:
settings = resolved["token"]
if not settings.get("market_code") or not (
parse_product_codes(settings.get("product_codes"))
or parse_product_codes(settings.get("product_code"))
):
raise ValueError(f"products.{name}.token requires market_code and product_code(s)")
selected.append(resolved)
return selected
def get_step_delay_seconds(default=5):
config = load_config()