feat(config): Schema validator, initial config merger setup
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
from typing import Any, cast
|
||||
import yaml
|
||||
|
||||
from config import validator
|
||||
from config.merger import merge_configs
|
||||
|
||||
|
||||
def _load_config_file(file: str):
|
||||
with open(file, "r") as f:
|
||||
parsed = yaml.load(f, Loader=yaml.FullLoader)
|
||||
return parsed
|
||||
|
||||
|
||||
def load_config(file: str):
|
||||
# Load and validate initial config
|
||||
try:
|
||||
loaded_conf = _load_config_file(file)
|
||||
except Exception:
|
||||
return {}
|
||||
if not validator.validate(loaded_conf):
|
||||
return {}
|
||||
|
||||
conf = cast(dict[str, Any], loaded_conf)
|
||||
requires = cast(list[str], conf["requires"])
|
||||
conf.pop("requires")
|
||||
|
||||
# Recursively load files
|
||||
for conf_file in requires:
|
||||
conf = merge_configs(conf, load_config(conf_file))
|
||||
|
||||
return conf
|
||||
@@ -0,0 +1,6 @@
|
||||
def merge_configs(config: dict, new_config: dict):
|
||||
if len(new_config) == 0 or len(config) == 0:
|
||||
return config
|
||||
|
||||
# Merge configs
|
||||
return config
|
||||
+18
-2
@@ -1,2 +1,18 @@
|
||||
def validate():
|
||||
pass
|
||||
import json
|
||||
import jsonschema
|
||||
|
||||
with open("config.schema.json") as file:
|
||||
schema = json.load(file)
|
||||
|
||||
|
||||
def validate(config):
|
||||
try:
|
||||
jsonschema.validate(config, schema)
|
||||
except jsonschema.SchemaError:
|
||||
print("Schema invalid")
|
||||
return False
|
||||
except jsonschema.ValidationError:
|
||||
print("Config invalid")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user