Repository navigation
Expand file tree
/
Copy pathexample3.py
More file actions
56 lines (43 loc) · 1.63 KB
/
Copy pathexample3.py
File metadata and controls
56 lines (43 loc) · 1.63 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
"""
Validate application.
Run from the repository root with:
python examples/example3.py
"""
from pathlib import Path
from yini_parser import YiniParseError, load
def require(section: dict[str, object], key: str) -> object:
# A tiny validation helper for settings the program cannot run without.
if key not in section:
raise ValueError(f"Missing required setting: {key}")
return section[key]
def main() -> None:
try:
# Load the YINI file beside this script, regardless of the
# current directory.
config = load(Path(__file__).with_name("application.yini"))
except YiniParseError as exc:
raise SystemExit(f"Could not parse application.yini: {exc}") from exc
app = config["Application"]
server = app["Server"]
features = app["Features"]
limits = app["Limits"]
app_name = require(app, "name")
environment = require(app, "environment")
host = require(server, "host")
port = require(server, "port")
# Select boolean feature toggles, ignoring list/object settings.
enabled_features = [
feature_name
for feature_name, enabled in features.items()
if isinstance(enabled, bool) and enabled
]
enabled_text = ", ".join(enabled_features) or "none"
print("Application validation report")
print("=============================")
print(f"[ok] name: {app_name}")
print(f"[ok] environment: {environment}")
print(f"[ok] server: {host}:{port}")
print(f"[ok] enabled boolean features: {enabled_text}")
print(f"[ok] request timeout: {limits['requestTimeoutSeconds']} seconds")
if __name__ == "__main__":
main()