forked from mxx1111/Homelab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
35 lines (26 loc) · 929 Bytes
/
Copy pathconfig.py
File metadata and controls
35 lines (26 loc) · 929 Bytes
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
import os
from pathlib import Path
import yaml
ROOT = Path(__file__).resolve().parent.parent
SEARCH_PATHS = [
os.environ.get("HOMELAB_CONFIG"),
"/etc/homelab-dashboard/config.yaml",
str(ROOT / "config.yaml"),
# 兜底:刚 clone 下来还没建 config.yaml 时也能起得来,先看到界面再配
str(ROOT / "config.example.yaml"),
]
def load_config():
for path in SEARCH_PATHS:
if path and os.path.isfile(path):
with open(path, encoding="utf-8") as fp:
return yaml.safe_load(fp) or {}, path
raise FileNotFoundError("未找到 config.yaml,可用 HOMELAB_CONFIG 指定路径")
CONFIG, CONFIG_PATH = load_config()
def get(path, default=None):
"""按 a.b.c 取配置值"""
node = CONFIG
for key in path.split("."):
if not isinstance(node, dict) or key not in node:
return default
node = node[key]
return node