79 lines
2.0 KiB

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:
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
return False
return True
def validBindType(data: dict) -> (dict, list):
validType = False
types = [
errMsgs = []
for potentialType in data.keys():
if potentialType in types:
validType = True
data, errMsgs = removeRelaventFields(data, potentialType)
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"]:
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()