frontend-mondvogel/source/conf.py

211 lines
3.9 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",
]
},
"session": {
"type": "object",
"properties": {
"lifetime": {
"type": "string",
},
"key_path": {
"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
def refine_log(data_raw_log):
return {
"format": data_raw_log.get("format", "human_readable"),
"min_level": data_raw_log.get("min_level", "notice"),
}
# api
def refine_api(data_raw_api):
return {
"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
def refine_account(data_raw_account):
return {
"name": data_raw_account["name"],
"password": data_raw_account["password"],
}
# session
def refine_session(data_raw_session):
return {
"lifetime": data_raw_session.get("lifetime", 3600),
"key_path": data_raw_session.get("key_path", _os.path.join(_os.path.expanduser("~"), ".mondvogel", "session_key")),
}
data = {
"log": refine_log(data_raw.get("log", {})),
"api": refine_api(data_raw.get("api", {})),
"account": refine_account(data_raw.get("account", {})),
"session": refine_session(data_raw.get("session", {})),
}
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