-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_loader.py
34 lines (28 loc) · 994 Bytes
/
config_loader.py
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
import logging
import json
from pathlib import Path
logger = logging.getLogger(__name__)
class ConfigData:
def __init__ (
self,
host: str
):
self.host = host
@staticmethod
def load_json_config(filename: Path) -> "ConfigData":
try:
with open(filename, "r") as file:
json_object = json.load(file)
except FileNotFoundError:
logger.error(f"Could not find the config file {filename}")
json_object = {}
except json.JSONDecodeError:
logger.error(f"Failed decoding json in config file {filename}")
json_object = {}
data = json_object.get("data", None)
if not data:
raise ValueError("Required key 'data' could not be found")
host = data.get("host", None)
if not host:
raise ValueError("Required key 'host' could not be found in 'data'")
return ConfigData(host=host)