41 lines
1.0 KiB
Python
41 lines
1.0 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from collections.abc import Mapping
|
|
from dataclasses import dataclass
|
|
|
|
DEFAULT_HISTORY_SIZE = 10
|
|
|
|
ENABLED_ENV = "WORLD_RUNTIME_ENABLED"
|
|
HISTORY_SIZE_ENV = "WORLD_HISTORY_SIZE"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class WorldConfig:
|
|
enabled: bool = True
|
|
history_size: int = DEFAULT_HISTORY_SIZE
|
|
|
|
|
|
def load_config(env: Mapping[str, str] | None = None) -> WorldConfig:
|
|
values = env or os.environ
|
|
return WorldConfig(
|
|
enabled=_parse_bool(values.get(ENABLED_ENV), default=True),
|
|
history_size=_parse_history_size(values.get(HISTORY_SIZE_ENV)),
|
|
)
|
|
|
|
|
|
def _parse_bool(value: str | None, *, default: bool) -> bool:
|
|
if value is None:
|
|
return default
|
|
return value.strip().lower() in {"1", "true", "yes", "on", "enabled"}
|
|
|
|
|
|
def _parse_history_size(value: str | None) -> int:
|
|
if value is None:
|
|
return DEFAULT_HISTORY_SIZE
|
|
try:
|
|
size = int(value)
|
|
except ValueError:
|
|
return DEFAULT_HISTORY_SIZE
|
|
return size if size > 0 else DEFAULT_HISTORY_SIZE
|