Verification for everything

class_based_refactor
Nicholas Hope 2022-08-21 21:12:44 -04:00
parent a806215054
commit 4944292483
1 changed files with 268 additions and 0 deletions

268
src/tfscript/types.py Normal file
View File

@ -0,0 +1,268 @@
validKeyList = [
# top row
'escape', 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10', 'f11', 'f12',
# keyboard
'`', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '=', 'backspace',
'tab', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\\',
'capslock', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'semicolon', '\'', 'enter',
'shift', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 'rshift',
'ctrl', 'lwin', 'alt', 'space', 'rwin', 'ralt', 'rctrl',
# mouse
'mouse1', 'mouse2', 'mouse3', 'mouse4', 'mouse5', 'mwheelup', 'mwheeldown',
# 8 of the 9 keys to the upper-right (PrtScn can't be bound)
'scrolllock', 'numlock',
'ins', 'home', 'pgup',
'del', 'end', 'pgdn',
# arrows
'uparrow', 'downarrow',
'leftarrow', 'rightarrow'
]
bindTypes = [
'impulse',
'hold',
'toggle',
'double',
'repeat'
]
class bind:
'''
Parent class for all bind types.
Verifies key, creates local variables
'''
def __init__(self, key, fields):
self.key = key
self.fields = fields
self.errors = []
self.targetType = None
# overloaded for each unique type, default just verifies key
# and some other universal fields like alias and finds targetType
self.verify()
# verify function should remove all fields relavent to the bind.
# Any extras are errors
self.errors.append(f'extra fields in "{self.key}":')
if isinstance(self.fields, str):
# iterating over a str returns each character,
# making meaningless error messages
self.errors.append(f' "{self.fields}"')
else:
for field in self.fields:
self.errors.append(f' "{field}"')
def verify(self):
veirfyErrors = []
splitKey = self.key.split(' ', 1)
splitLen = len(splitKey)
if splitLen == 1:
# catastrophic error
self.errors.append(f'could not find type in "{self.key}"')
return
elif splitLen == 2:
# temporarily storing string
typeName = splitKey[0]
self.fields = {typeName: self.fields}
self.key = splitKey[1]
try:
isalias = self.fields.pop('alias')
if not isinstance(isalias, bool):
self.errors.append(f'alias should be yes or no, not "{isalias}"')
isalias = False
except KeyError:
isalias = False
if not isalias and self.key not in validKeyList:
self.errors.append(f'Invalid key name: "{self.key}"')
types = {
'impulse': impulse,
'hold': hold,
'toggle': toggle,
'double': double,
'repeat': repeat
}
for loopName, typeClass in types.items():
if loopName == typeName:
self.targetType = typeClass
break
self.fields = self.fields.pop(typeName)
if self.targetType is None:
self.errors.append(f'could not find type in "{self.key}"')
class impulse(bind):
def verify(self):
self.command = None
try:
self.command = self.fields.pop('command')
if not strOrList(self.command):
self.errors.append('impulse command must be string or list')
self.command = None
except (KeyError, AttributeError):
self.command = self.fields
if not strOrList(self.command):
self.errors.append('impulse must be command or argument of string or list')
self.command = None
class hold(bind):
def verify(self):
self.press = None
self.release = None
if isinstance(self.fields, dict):
# verify press
try:
self.press = self.fields.pop('press')
if not strOrList(self.press):
self.errors.append('hold press must be string or list')
self.press = None
except KeyError:
self.errors.append('hold requires press field')
# verify release
try:
self.release = self.fields.pop('release')
if not strOrList(self.release):
self.errors.append('hold release must be string or list')
self.release = None
except KeyError:
self.errors.append('hold requires release field')
elif isinstance(self.fields, str):
self.press = self.fields
else:
self.errors.append('hold must be press and release, or argument of string')
class toggle(bind):
def verify(self):
self.start = None
self.end = None
if isinstance(self.fields, dict):
# verify start
try:
self.start = self.fields.pop('start')
if not strOrList(self.start):
self.errors.append('hold start must be string or list')
self.start = None
except KeyError:
self.errors.append('hold requires start field')
# verify end
try:
self.end = self.fields.pop('end')
if not strOrList(self.end):
self.errors.append('hold end must be string or list')
self.end = None
except KeyError:
self.errors.append('hold requires end field')
elif isinstance(self.fields, str):
self.start = self.fields
else:
self.errors.append('hold must be start and end, or argument of string')
class double(bind):
defaultDict = {}
condDict = {}
def verify(self):
self.primary = None
self.secondary = None
self.condition = None
# name of a bind type
self.type = None
# either "released" (default) or "both"
self.cancel = 'released'
self.
# toggler
try:
self.condition = self.fields.pop('condition')
if self.condition not in validKeyList:
self.errors.append(f'double has invalid condition to toggle: "{self.condition}"')
except KeyError:
self.errors.append('double requires condition to toggle')
# cancel mode
try:
self.cancel = self.fields.pop('cancel')
if self.cancel not in ['released', 'both']:
self.errors.append(f'double cancel must be either "released" or "both", not "{self.cancel}"')
except KeyError:
# maintain default
pass
# type
try:
self.type = self.fields.pop('type')
if self.type not in bindTypes:
# catastrophic: invalid type
self.errors.append(f'double has invalid type: "{self.type}"')
return
except KeyError:
# catastrophic: no type given
self.errors.append('double requires type')
return
# primary action
try:
mainSection = self.fields.pop('primary')
mainSection.update({'alias': True})
mainAction = bind(f'{self.type} primary', mainSection)
if mainAction.targetType is not None:
mainAction = mainAction.targetType(mainAction.key, mainAction.fields)
self.errors.extend(mainAction.errors)
except KeyError:
self.errors.append('double requires primary action')
# secondary action
try:
altSection = self.fields.pop('secondary')
altSection.update({'alias': True})
altBind = bind(f'{self.type} secondary', altSection)
if altBind.targetType is not None:
altBind = altBind.targetType(altBind.key, altBind.fields)
self.errors.extend(altBind.errors)
except KeyError:
self.errors.append('double requires secondary action')
class repeat(bind):
def verify(self):
self.interval = None
self.command = None
try:
intervalStr = str(self.fields.pop('interval'))
self.interval = int(intervalStr)
if self.interval <= 0:
self.errors.append('repeat interval must be greater than 0')
except KeyError:
self.errors.append('repeat requires interval')
except ValueError:
self.errors.append(f'repeat has invalid number of ticks: "{self.interval}"')
try:
self.command = self.fields.pop('command')
if not strOrList(self.command):
self.errors.append('repeat command must be string or list')
self.command = None
except KeyError:
self.errors.append('repeat requires command')
def strOrList(self, thing):
return (isinstance(thing, str) or isinstance(thing, list))