-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_config_support.py
More file actions
115 lines (98 loc) · 3.29 KB
/
runtime_config_support.py
File metadata and controls
115 lines (98 loc) · 3.29 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
from __future__ import annotations
import os
from dataclasses import dataclass
from typing import Callable
from strategy_registry import (
DEFAULT_STRATEGY_PROFILE as PLATFORM_DEFAULT_STRATEGY_PROFILE,
LONGBRIDGE_PLATFORM,
resolve_strategy_definition,
resolve_strategy_metadata,
)
DEFAULT_ACCOUNT_REGION = "DEFAULT"
DEFAULT_STRATEGY_PROFILE = PLATFORM_DEFAULT_STRATEGY_PROFILE
DEFAULT_LONGPORT_SECRET_NAME = "longport_token_hk"
@dataclass(frozen=True)
class PlatformRuntimeSettings:
project_id: str | None
secret_name: str
account_prefix: str
service_name: str
strategy_profile: str
strategy_display_name: str
strategy_domain: str
account_region: str
notify_lang: str
tg_token: str | None
tg_chat_id: str | None
dry_run_only: bool
def resolve_strategy_profile(raw_value: str | None) -> str:
return resolve_strategy_definition(
raw_value,
platform_id=LONGBRIDGE_PLATFORM,
).profile
def infer_account_region(
raw_value: str | None,
*,
account_prefix: str,
service_name: str,
) -> str:
for candidate in (
raw_value,
account_prefix,
_infer_region_from_service_name(service_name),
):
normalized = _normalize_region(candidate)
if normalized is not None:
return normalized
return DEFAULT_ACCOUNT_REGION
def load_platform_runtime_settings(
*,
project_id_resolver: Callable[[], str | None],
) -> PlatformRuntimeSettings:
account_prefix = os.getenv("ACCOUNT_PREFIX", "DEFAULT")
service_name = os.getenv("SERVICE_NAME", "longbridge-quant-semiconductor-rotation-income")
strategy_definition = resolve_strategy_definition(
os.getenv("STRATEGY_PROFILE"),
platform_id=LONGBRIDGE_PLATFORM,
)
strategy_metadata = resolve_strategy_metadata(
strategy_definition.profile,
platform_id=LONGBRIDGE_PLATFORM,
)
return PlatformRuntimeSettings(
project_id=project_id_resolver(),
secret_name=os.getenv("LONGPORT_SECRET_NAME", DEFAULT_LONGPORT_SECRET_NAME),
account_prefix=account_prefix,
service_name=service_name,
strategy_profile=strategy_definition.profile,
strategy_display_name=strategy_metadata.display_name,
strategy_domain=strategy_definition.domain,
account_region=infer_account_region(
os.getenv("ACCOUNT_REGION"),
account_prefix=account_prefix,
service_name=service_name,
),
notify_lang=os.getenv("NOTIFY_LANG", "en"),
tg_token=os.getenv("TELEGRAM_TOKEN"),
tg_chat_id=os.getenv("GLOBAL_TELEGRAM_CHAT_ID"),
dry_run_only=_resolve_bool_env("LONGBRIDGE_DRY_RUN_ONLY"),
)
def _normalize_region(raw_value: str | None) -> str | None:
if raw_value is None:
return None
value = str(raw_value).strip()
if not value:
return None
return value.upper()
def _infer_region_from_service_name(service_name: str) -> str | None:
name = str(service_name).strip().lower()
if name.endswith("-hk"):
return "HK"
if name.endswith("-sg"):
return "SG"
return None
def _resolve_bool_env(name: str) -> bool:
raw_value = os.getenv(name)
if raw_value is None:
return False
return str(raw_value).strip().lower() in {"1", "true", "yes", "on"}