add validation schema for persistent keys

Former-commit-id: ceaf3f2891
This commit is contained in:
Dev Singh 2022-01-10 09:21:46 -06:00
parent b4c9ef22b6
commit 74542c1d8f
3 changed files with 45 additions and 25 deletions

View File

@ -2,6 +2,7 @@ import math
import json import json
from multiprocessing import Pool from multiprocessing import Pool
import os import os
from cerberus import Validator
from data import set_database_config, get_database_config from data import set_database_config, get_database_config
from interface import stderr, stdout, INF, ERR from interface import stderr, stdout, INF, ERR
@ -140,32 +141,16 @@ class ConfigurationError (Exception):
self.code = code self.code = code
def parse_config_persistent(send, config): def parse_config_persistent(send, config):
v = Validator(load_validation_schema(), allow_unknown = True)
isValidated = v.validate(config)
try: if not isValidated:
apikey = config["persistent"]["key"]["database"] raise ConfigurationError(v.errors, 101)
except:
raise ConfigurationError("persistent/key/database field is invalid or missing", 111)
try:
tbakey = config["persistent"]["key"]["tba"]
except:
raise ConfigurationError("persistent/key/tba field is invalid or missing", 112)
try:
preference = config["persistent"]["config-preference"]
except:
raise ConfigurationError("persistent/config-preference field is invalid or missing", 113)
try:
sync = config["persistent"]["synchronize-config"]
except:
raise ConfigurationError("persistent/synchronize-config field is invalid or missing", 114)
if apikey == None or apikey == "": apikey = config["persistent"]["key"]["database"]
raise ConfigurationError("persistent/key/database field is empty", 115) tbakey = config["persistent"]["key"]["tba"]
if tbakey == None or tbakey == "": preference = config["persistent"]["config-preference"]
raise ConfigurationError("persistent/key/tba field is empty", 116) sync = config["persistent"]["synchronize-config"]
if preference == None or preference == "":
raise ConfigurationError("persistent/config-preference field is empty", 117)
if sync != True and sync != False:
raise ConfigurationError("persistent/synchronize-config field is empty", 118)
return apikey, tbakey, preference, sync return apikey, tbakey, preference, sync
@ -249,6 +234,13 @@ def load_config(path, config_vector):
f.close() f.close()
return 1 return 1
def load_validation_schema():
try:
with open("validation-schema.json", "r") as f:
return json.load(f)
except:
raise FileNotFoundError("Validation schema not found at validation-schema.json")
def save_config(path, config_vector): def save_config(path, config_vector):
f = open(path, "w+") f = open(path, "w+")
json.dump(config_vector, f, ensure_ascii=False, indent=4) json.dump(config_vector, f, ensure_ascii=False, indent=4)

View File

@ -0,0 +1,26 @@
{
"persistent": {
"type": "dict",
"require_all": true,
"schema": {
"key": {
"type": "dict",
"require_all":true,
"schema": {
"database": {"type":"string"},
"tba": {"type": "string"},
"tra": {
"type": "dict",
"require_all": true,
"schema": {
"CLIENT_ID": {"type": "string"},
"CLIENT_SECRET": {"type": "string"}
}
}
}
},
"config-preference": {"type": "string", "required": true},
"synchronize-config": {"type": "boolean", "required": true}
}
}
}

View File

@ -15,3 +15,5 @@ kivy==2.0.0rc2
pyzmq pyzmq
python-daemon python-daemon
cerebrus