192 lines
3.3 KiB
Python
192 lines
3.3 KiB
Python
|
import os as _os
|
||
|
import json as _json
|
||
|
|
||
|
from helpers import *
|
||
|
|
||
|
|
||
|
_conf_data = None
|
||
|
|
||
|
def conf_schema(
|
||
|
):
|
||
|
return {
|
||
|
"type": "object",
|
||
|
"properties": {
|
||
|
"version": {
|
||
|
"type": "number",
|
||
|
"enum": [
|
||
|
1
|
||
|
]
|
||
|
},
|
||
|
"log": {
|
||
|
"type": "object",
|
||
|
"properties": {
|
||
|
"format": {
|
||
|
"type": "string",
|
||
|
"enum": [
|
||
|
"jsonl",
|
||
|
"human_readable",
|
||
|
],
|
||
|
"default": "human_readable",
|
||
|
"description": "Format der Log-Einträge"
|
||
|
},
|
||
|
"min_level": {
|
||
|
"type": "string",
|
||
|
"enum": [
|
||
|
"debug",
|
||
|
"info",
|
||
|
"notice",
|
||
|
"warning",
|
||
|
"error",
|
||
|
],
|
||
|
"default": "notice",
|
||
|
"description": "Mindest-Level für Log-Einträge um aufgeführt zu werden"
|
||
|
},
|
||
|
},
|
||
|
"additionalProperties": False,
|
||
|
"required": [
|
||
|
]
|
||
|
},
|
||
|
"api": {
|
||
|
"type": "object",
|
||
|
"properties": {
|
||
|
"scheme": {
|
||
|
"type": "string",
|
||
|
"default": "https"
|
||
|
},
|
||
|
"host": {
|
||
|
"type": "string",
|
||
|
},
|
||
|
"port": {
|
||
|
"type": "number",
|
||
|
"default": 4916
|
||
|
},
|
||
|
"path": {
|
||
|
"type": "string",
|
||
|
"default": ""
|
||
|
}
|
||
|
},
|
||
|
"additionalProperties": False,
|
||
|
"required": [
|
||
|
"host"
|
||
|
]
|
||
|
},
|
||
|
"account": {
|
||
|
"type": "object",
|
||
|
"properties": {
|
||
|
"name": {
|
||
|
"type": "string",
|
||
|
},
|
||
|
"password": {
|
||
|
"type": "string",
|
||
|
},
|
||
|
},
|
||
|
"additionalProperties": False,
|
||
|
"required": [
|
||
|
"name",
|
||
|
"password",
|
||
|
]
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
def conf_refine(
|
||
|
data_raw
|
||
|
):
|
||
|
flaws = []
|
||
|
if (not ("version" in data_raw)):
|
||
|
flaws.append(
|
||
|
{
|
||
|
"incident": "mandatory_value_missing",
|
||
|
"details": {
|
||
|
"path": "version",
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
data = None
|
||
|
else:
|
||
|
version = data_raw["version"]
|
||
|
if (version == 1):
|
||
|
# log
|
||
|
if True:
|
||
|
data_raw_log = data_raw.get("log", {})
|
||
|
data_log = {
|
||
|
"format": data_raw_log.get("format", "human_readable"),
|
||
|
"min_level": data_raw_log.get("min_level", "notice"),
|
||
|
}
|
||
|
# api
|
||
|
if True:
|
||
|
data_raw_api = data_raw.get("api", {})
|
||
|
data_api = {
|
||
|
"scheme": data_raw_api.get("scheme", "https"),
|
||
|
"host": data_raw_api["host"],
|
||
|
"port": data_raw_api.get("port", 4916),
|
||
|
"path": data_raw_api.get("path", ""),
|
||
|
}
|
||
|
# account
|
||
|
if True:
|
||
|
data_raw_account = data_raw.get("account", {})
|
||
|
data_account = {
|
||
|
"name": data_raw_account["name"],
|
||
|
"password": data_raw_account["password"],
|
||
|
}
|
||
|
data = {
|
||
|
"log": data_log,
|
||
|
"api": data_api,
|
||
|
"account": data_account,
|
||
|
}
|
||
|
else:
|
||
|
flaws.append(
|
||
|
{
|
||
|
"incident": "invalid_version",
|
||
|
"details": {
|
||
|
"value": version,
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
data = None
|
||
|
return {
|
||
|
"flaws": flaws,
|
||
|
"data": data,
|
||
|
}
|
||
|
|
||
|
|
||
|
def conf_load(
|
||
|
path : str
|
||
|
):
|
||
|
global _conf_data
|
||
|
if (not _os.path.exists(path)):
|
||
|
raise ValueError("configuration missing: %s" % path)
|
||
|
else:
|
||
|
data_raw = _json.loads(file_text_read(path))
|
||
|
refinement = conf_refine(data_raw)
|
||
|
if (len(refinement["flaws"]) > 0):
|
||
|
raise ValueError(
|
||
|
"configuration invalid:\n%s"
|
||
|
% convey(
|
||
|
refinement["flaws"],
|
||
|
[
|
||
|
lambda flaws: map(
|
||
|
lambda flaw: string_coin(
|
||
|
"- {{incident}} | {{details}}",
|
||
|
{
|
||
|
"incident": flaw["incident"],
|
||
|
"details": _json.dumps(flaw["details"]),
|
||
|
}
|
||
|
),
|
||
|
flaws
|
||
|
),
|
||
|
"\n".join,
|
||
|
]
|
||
|
)
|
||
|
)
|
||
|
else:
|
||
|
_conf_data = refinement["data"]
|
||
|
|
||
|
|
||
|
def conf_get(
|
||
|
):
|
||
|
global _conf_data
|
||
|
return _conf_data
|
||
|
|