-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_loader.py
More file actions
281 lines (227 loc) · 5.47 KB
/
Copy pathconfig_loader.py
File metadata and controls
281 lines (227 loc) · 5.47 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from importlib import util
import os
from pathlib import Path
ROOT = Path(__file__).resolve().parent
ENV_PREFIX = "JIN_"
def parse_env_value(
*,
name: str,
raw_value: str,
current_value,
):
if isinstance(current_value, bool):
normalized = (
raw_value
.strip()
.lower()
)
if normalized in {
"1",
"true",
"yes",
"on",
}:
return True
if normalized in {
"0",
"false",
"no",
"off",
}:
return False
raise ValueError(
f"Invalid boolean env value for {name}: {raw_value!r}"
)
if isinstance(current_value, int):
return int(
raw_value
)
if isinstance(current_value, float):
return float(
raw_value
)
return raw_value
def get_env_override(
name: str,
) -> str | None:
if name in os.environ:
return os.environ[name]
prefixed_name = f"{ENV_PREFIX}{name}"
return os.environ.get(
prefixed_name
)
def apply_env_overrides(
config_module,
):
for name in dir(config_module):
if not name.isupper():
continue
raw_value = get_env_override(
name
)
if raw_value is None:
continue
current_value = getattr(
config_module,
name,
)
setattr(
config_module,
name,
parse_env_value(
name=name,
raw_value=raw_value,
current_value=current_value,
),
)
return config_module
def normalize_model_role_config(
config_module,
):
"""Resolve the optional Service role onto the required Brain runtime.
``USE_SERVICE_AS_BRAIN`` is accepted only as a legacy config adapter. It
is deliberately removed from the normalized module so no runtime path can
branch on the old topology.
"""
legacy_service_as_brain = getattr(
config_module,
"USE_SERVICE_AS_BRAIN",
None,
)
if legacy_service_as_brain is True:
for suffix in (
"API_BASE",
"MODEL_UID",
):
service_name = f"SERVICE_{suffix}"
brain_name = f"BRAIN_{suffix}"
service_value = getattr(
config_module,
service_name,
None,
)
if service_value not in (
None,
"",
0,
0.0,
):
setattr(
config_module,
brain_name,
service_value,
)
# The legacy Service endpoint becomes the canonical Brain endpoint;
# background work therefore falls back to it instead of activating a
# second physical runtime accidentally.
setattr(
config_module,
"SERVICE_API_BASE",
"",
)
if hasattr(
config_module,
"USE_SERVICE_AS_BRAIN",
):
delattr(
config_module,
"USE_SERVICE_AS_BRAIN",
)
raw_service_api_base = str(
getattr(
config_module,
"SERVICE_API_BASE",
"",
)
or ""
).strip()
service_configured = bool(
raw_service_api_base
)
setattr(
config_module,
"SERVICE_CONFIGURED",
service_configured,
)
brain_fallbacks = {
"SERVICE_API_BASE": getattr(
config_module,
"BRAIN_API_BASE",
),
"SERVICE_MODEL_UID": getattr(
config_module,
"BRAIN_MODEL_UID",
),
}
for name, fallback in brain_fallbacks.items():
value = getattr(
config_module,
name,
None,
)
if (
not service_configured
or value in (
None,
"",
0,
0.0,
)
):
setattr(
config_module,
name,
fallback,
)
return config_module
def load_config_from_path(
path: Path,
):
spec = util.spec_from_file_location(
path.stem,
path,
)
if (
spec is None
or spec.loader is None
):
raise RuntimeError(
f"Unable to load config from {path}"
)
module = util.module_from_spec(
spec
)
spec.loader.exec_module(
module
)
return module
def load_config_module(
*,
config_path: Path | None = None,
example_path: Path | None = None,
):
if config_path is None:
config_path = ROOT / "config.py"
if example_path is None:
example_path = ROOT / "config.example.py"
if config_path.exists():
return normalize_model_role_config(
apply_env_overrides(
load_config_from_path(
config_path
)
)
)
if example_path.exists():
return normalize_model_role_config(
apply_env_overrides(
load_config_from_path(
example_path
)
)
)
raise FileNotFoundError(
f"config not found at {config_path} "
f"or {example_path}"
)
config = load_config_module()