-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
78 lines (65 loc) · 1.97 KB
/
config.py
File metadata and controls
78 lines (65 loc) · 1.97 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
import json
import subprocess
from dataclasses import asdict, dataclass
from typing import Any, Dict, List
@dataclass
class Config:
"""
Python representation of the config file.
"""
class InvalidConfig(Exception):
"""The config contains some invalid formatting or data."""
...
@dataclass
class App:
"""
An app to be deployed.
"""
name: str
endpoint: str
cwd: str
run_args: List[str]
@dataclass
class Gunicorn:
"""
Gunicorn options.
"""
app_name: str
options: Dict[str, Any]
apps: List[App]
logdir: str
max_email_payload_bytes: int
api_secret: str
gunicorn: Gunicorn
def __str__(self) -> str:
return "<Config: apps={}, logdir={}, max_email_payload_bytes={}, api_secret={}>".format(
self.apps, self.logdir, self.max_email_payload_bytes, "*****"
)
def to_dict(self) -> Dict[str, Any]:
d = asdict(self)
d["api_secret"] = "*****"
return d
def load_config(config_path="config.yaml") -> Config:
try:
result = subprocess.run(["yq", "-e", "-o=json", ".", config_path], capture_output=True)
data = json.loads(result.stdout)["config"]
return Config(
logdir=data["logs"]["dir"],
max_email_payload_bytes=data["security"]["max_payload_bytes"],
api_secret=data["security"]["api_secret"],
apps=[
Config.App(
name=app["name"],
endpoint=app["endpoint"],
cwd=app["cwd"],
run_args=app["run_args"],
)
for app in data["apps"]
],
gunicorn=Config.Gunicorn(
app_name=data["gunicorn"]["app_name"],
options=data["gunicorn"]["run_args"],
),
)
except (KeyError, ValueError) as e:
raise Config.InvalidConfig from e