79 lines
2.0 KiB
Python
79 lines
2.0 KiB
Python
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()
|
|
|