Basic verification to make sure the file is valid tfscript

pull/11/head
Nicholas Hope 2022-06-14 18:02:14 -04:00
parent f86e41b68e
commit ed89763adc
1 changed files with 78 additions and 0 deletions

78
verify.py Normal file
View File

@ -0,0 +1,78 @@
def verifyConfig(cfg: dict):
errors = []
for cclass in cfg:
for key, data in cfg[cclass].items():
errMessages = validBind(key, data)
if len(errMessages) > 0:
for msg in errMessages:
errors.append("Error in %s: %s" % (cclass, msg))
if len(errors) > 0:
cfg.update({"errors": errors})
return cfg
def validBind(key, data) -> list:
ret = []
if not validKey(key):
ret.append('Invalid key "%s"' % (key))
data, errMsgs = validBindType(data)
if len(errMsgs) > 0:
for msg in errMsgs:
ret.append(msg)
extras = extraFields(data)
if len(extras) > 0:
extrasString = "\n\t".join(extras)
ret.append('Extra fields in "%s":\n\t%s' % (key, extrasString))
return ret
def validKey(key):
key = str(key).lower()
# Expand this
if len(key) == 1:
if not (key.isalpha() or 1 <= int(key) <= 9):
return False
else:
return False
return True
def validBindType(data: dict) -> (dict, list):
validType = False
types = [
"impulse",
"hold",
"toggle",
"double"
]
errMsgs = []
for potentialType in data.keys():
if potentialType in types:
validType = True
data, errMsgs = removeRelaventFields(data, potentialType)
break
return data, errMsgs
def removeRelaventFields(data, bindType) -> (dict, list):
errMsgs = []
# These types are simple, just the bind type and argument
if bindType in ["impulse", "hold", "toggle"]:
data.pop(bindType)
elif bindType == "double":
if "primary" not in data["double"]:
errMsgs.append("Double requires primary action")
if "secondary" not in data["double"]:
errMsgs.append("Double requires secondary action")
if "condition" not in data:
errMsgs.append("Double requires condition to toggle")
return data, errMsgs
def extraFields(data) -> list:
return data.keys()