mirror of
https://github.com/titanscouting/tra-superscript.git
synced 2024-11-10 06:54:45 +00:00
add validation schema for persistent keys
This commit is contained in:
parent
03ec0dd8fd
commit
ceaf3f2891
@ -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)
|
||||||
|
|
||||||
|
if not isValidated:
|
||||||
|
raise ConfigurationError(v.errors, 101)
|
||||||
|
|
||||||
try:
|
|
||||||
apikey = config["persistent"]["key"]["database"]
|
apikey = config["persistent"]["key"]["database"]
|
||||||
except:
|
|
||||||
raise ConfigurationError("persistent/key/database field is invalid or missing", 111)
|
|
||||||
try:
|
|
||||||
tbakey = config["persistent"]["key"]["tba"]
|
tbakey = config["persistent"]["key"]["tba"]
|
||||||
except:
|
|
||||||
raise ConfigurationError("persistent/key/tba field is invalid or missing", 112)
|
|
||||||
try:
|
|
||||||
preference = config["persistent"]["config-preference"]
|
preference = config["persistent"]["config-preference"]
|
||||||
except:
|
|
||||||
raise ConfigurationError("persistent/config-preference field is invalid or missing", 113)
|
|
||||||
try:
|
|
||||||
sync = config["persistent"]["synchronize-config"]
|
sync = config["persistent"]["synchronize-config"]
|
||||||
except:
|
|
||||||
raise ConfigurationError("persistent/synchronize-config field is invalid or missing", 114)
|
|
||||||
|
|
||||||
if apikey == None or apikey == "":
|
|
||||||
raise ConfigurationError("persistent/key/database field is empty", 115)
|
|
||||||
if tbakey == None or tbakey == "":
|
|
||||||
raise ConfigurationError("persistent/key/tba field is empty", 116)
|
|
||||||
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)
|
||||||
|
26
src/cli/validation-schema.json
Normal file
26
src/cli/validation-schema.json
Normal 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}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -15,3 +15,5 @@ kivy==2.0.0rc2
|
|||||||
|
|
||||||
pyzmq
|
pyzmq
|
||||||
python-daemon
|
python-daemon
|
||||||
|
|
||||||
|
cerebrus
|
Loading…
Reference in New Issue
Block a user