-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
90 lines (66 loc) · 2.33 KB
/
Copy pathbuild.py
File metadata and controls
90 lines (66 loc) · 2.33 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
"""
Build configuration for ems-line-controller-dlr.
Loads configuration from cfg.yml based on ENV environment variable and
allows MQTT_PORT override from environment for dynamic test container ports.
Validates all config using Pydantic models.
"""
import enum
import os
from typing import Final
import yaml
from pydantic import BaseModel
class LogLevel(enum.StrEnum):
"""Logging levels for the application."""
ERROR = "ERROR"
WARN = "WARN"
INFO = "INFO"
DEBUG = "DEBUG"
class Config(BaseModel):
"""Configuration for ems-line-controller-dlr application."""
log_level: LogLevel
mqtt_host: str
wifi_ssid: str
class _ConfigMap(BaseModel):
"""Configuration map for all environments."""
local: Config
ci: Config
def load_config() -> Config:
"""
Load configuration from cfg.yml file based on environment.
Reads the configuration file and returns the appropriate config
based on the ENV environment variable (defaults to 'local').
Supports YAML merge keys (<<) for configuration inheritance.
Returns:
Config object for the current environment
Raises:
FileNotFoundError: If cfg.yml file is not found
yaml.YAMLError: If cfg.yml contains invalid YAML
KeyError: If environment not found in cfg.yml
Example:
>>> config = load_config() # Uses ENV=local by default
>>> config.log_level
<LogLevel.DEBUG: 'DEBUG'>
"""
# Determine environment (default to "local")
environment = os.environ.get("ENV", "local")
# Load and parse cfg.yml
try:
with open("cfg.yml") as file:
# yaml.safe_load automatically handles merge keys (<<)
yaml_content = yaml.safe_load(file)
config_map = _ConfigMap(**yaml_content)
except FileNotFoundError as e:
raise FileNotFoundError("Failed to read cfg.yml") from e
except yaml.YAMLError as e:
raise yaml.YAMLError(f"Failed to parse cfg.yml: {e}") from e
# Select environment config
match environment:
case "ci":
config = config_map.ci
case _:
config = config_map.local
return config
# Load config at module import time
CONFIG: Final[Config] = load_config()
# MQTT port override for dynamic test container ports
MQTT_PORT: Final[int] = int(os.environ.get("MQTT_PORT", "1883"))