diff --git a/src/tfscript/__init__.py b/src/tfscript/__init__.py index df98ed4..fec9f67 100644 --- a/src/tfscript/__init__.py +++ b/src/tfscript/__init__.py @@ -1,287 +1,31 @@ -""" Makes the configs as a massive string """ +""" This has only one function. It mostly exists to allow imports of things like tftypes """ -# Used for the conditions in the type -condDict = {} -defaultDict = {} -bindOrAlias = "bind" from copy import deepcopy +from tfscript.tftypes import double -def makeCFG(cfg, default=False): - global bindOrAlias - global condDict - global defaultDict - for bind in cfg: - print(bind.key) - return +def makeCFG(bindList, default=False): - bindOrAlias = "bind" if default: # Write to defaultDict instead of condDict - condDict = defaultDict + double.condDict = double.defaultDict else: - condDict = deepcopy(defaultDict) + double.condDict = deepcopy(double.defaultDict) ret = '' - for key, data in cfg.items(): - isAlias = False - if "alias" in data: - isAlias = data.pop("alias") - if isAlias: - bindOrAlias = "alias" - else: - bindOrAlias = "bind" - ret += branch(key, data) + + for bind in bindList: + ret += bind.toTF2() # Doubles are weird. All of the toggles got put into a dictionary. # This takes all of the nested dictionaries and turns them into the right string - if default or condDict != defaultDict: + if default or double.condDict != double.defaultDict: # ==, and by extension !=, does in fact check # for dictionary equality in keys and values - for key, toggles in condDict.items(): + for key, toggles in double.condDict.items(): onCondPress = ';'.join(toggles["change_keys"]) onCondRelease = ';'.join(toggles["restore_keys"]) ret += f'alias +{key}_toggles "{onCondPress}"\n' +\ f'alias -{key}_toggles "{onCondRelease}"\n' +\ - f'{bindOrAlias} {key} "+{key}_toggles"\n' - - del condDict # free deep copy + f'bind {key} "+{key}_toggles"\n' return ret - -def typeOf(dictIn): - """ Find the first element common to both lists """ - types = [ - "impulse", - "hold", - "toggle", - "double", - "repeat" - ] - for t in types: - if t in dictIn.keys(): - return t - -def branch(keyName, bindContent): - """ Using the provided keyName and content, call the correct function """ - - """ - Terser syntax, ex. - impulse e: - - instead of - e: - impulse: - - """ - splitKey = keyName.split(' ', 1) - if len(splitKey) > 1: - keyName = splitKey[1] - bindContent = {splitKey[0]: bindContent} - - bindType = typeOf(bindContent) - bindContent = bindContent.pop(bindType) - - if bindType == "impulse": - return impulse(keyName, bindContent) - - elif bindType == "hold": - if isinstance(bindContent, str): - return simpleHold(keyName, bindContent) - else: - return listHold(keyName, bindContent) - - elif bindType == "toggle": - return toggle(keyName, bindContent) - - elif bindType == "double": - return double(keyName, bindContent) - - elif bindType == "repeat": - return repeat(keyName, bindContent) - -def impulse(key, instruction): - global bindOrAlias - if isinstance(instruction, dict): - instruction = instruction["command"] - - if not isinstance(instruction, list): - instruction = instruction.split(';') - - instuction = impulseShortcuts(instruction) - instruction = ';'.join(instruction) - - return f'{bindOrAlias} {key} "{instruction}"\n' - -def impulseShortcuts(instList): - for i, instruction in enumerate(instList): - splitCmd = instruction.split(' ') - cmd = splitCmd[0] - restOfCmd = ' '.join(splitCmd[1:]) - shortcuts = { - "primary": "slot1", - "secondary": "slot2", - "melee": "slot3" - } - if cmd in shortcuts: - cmd = shortcuts[cmd] - - if cmd == "voice": - cmd = "voicemenu" - restOfCmd = voice(restOfCmd) - - elif cmd == "build" or cmd == "destroy": - restOfCmd = expandBuildings(restOfCmd) - - elif cmd == "load_itempreset" and restOfCmd.isalpha(): - restOfCmd = restOfCmd.lower() - restOfCmd = ['a','b','c','d'].index(restOfCmd) - - if restOfCmd != "": - cmd += ' ' + restOfCmd - instList[i] = cmd - - return instList - -def voice(keyword): - keyword = keyword.lower() - - allLists = [ - ["medic", "thanks", "go", "move up", "go left", "go right", "yes", "no", "pass to me"], - ["incoming", "spy", "sentry ahead", "teleporter here", "dispenser here", "sentry here", "activate uber", "uber ready"], - ["help", "battle cry", "cheers", "jeers", "positive", "negative", "nice shot", "good job"] - ] - - for menu, voiceList in enumerate(allLists): - for selection, shortcut in enumerate(voiceList): - if keyword == shortcut: - return f'{menu} {selection}' - -def expandBuildings(building): - buildingNums = { - "dispenser": "0 0", - "entrance": "1 0", - "exit": "1 1", - "sentry": "2 0" - } - for shortBuild, num in buildingNums.items(): - if building == shortBuild: - return num - -def simpleHold(key, instruction): - global bindOrAlias - # This isn't quite right, fix later! - if instruction[0] == '+' or instruction[0] == '-': - return f'{bindOrAlias} {key} "{instruction}"\n' - else: - return f'{bindOrAlias} {key} "+{instruction}"\n' - -def listHold(key, options): - global bindOrAlias - - oldBindOrAlias = bindOrAlias - bindOrAlias = 'alias' - plus = impulse(f'+{key}_press', options["press"]) - minus = impulse(f'-{key}_press', options["release"]) - - bindOrAlias = oldBindOrAlias - - ret = plus + minus + f'{bindOrAlias} {key} "+{key}_press"\n' - - return ret - -def toggle(key, instruction): - global bindOrAlias - onStr = f'turn_{key}_on' - offStr = f'turn_{key}_off' - togStr = f'toggle_{key}' - if instruction[0] == '+': - instruction = instruction[1:] - - ret = f'alias {onStr} "+{instruction}; alias {togStr} {offStr}"\n' +\ - f'alias {offStr} "-{instruction}; alias {togStr} {onStr}"\n' +\ - f'alias {togStr} "{onStr}"\n' +\ - f'{bindOrAlias} {key} "{togStr}"\n' - return ret - -def double(key, options): - typeName = options["type"] - primaryAction = {typeName: options.pop("primary")} - - secAction = {typeName: options.pop("secondary")} - - mainStr = f'{key}_main' - altStr = f'{key}_alt' - pShiftStr = f'+shift_{key}' - mShiftStr = f'-shift_{key}' - - global bindOrAlias - oldBindOrAlias = bindOrAlias - bindOrAlias = "alias" - - mainCode = branch(mainStr, primaryAction) - altCode = branch(altStr, secAction) - cancelBoth = ("cancel" in options and options["cancel"] == "both") - if cancelBoth: - if isinstance(primaryAction["hold"], dict): - lines = mainCode.splitlines() - minusCmd = lines[1] - _, minusStr, previousMinus = minus.split(' ', 2) - newMinus = previousMinus[0:-2] + ';' + + '"\n' - lines[1] = f'alias {minusStr} "{newMinus}"\n' - else: - # simple - pass - - bindOrAlias = oldBindOrAlias - - ret = mainCode + altCode +\ - f'alias {pShiftStr} "{bindOrAlias} {key} {altStr}"\n' +\ - f'alias {mShiftStr} "{bindOrAlias} {key} {mainStr}"\n'+\ - f'{bindOrAlias} {key} "{mainStr}"\n' - - isToggle = ("toggle" in options and options.pop("toggle") == True) - if isToggle: - toggleStr = toggle(key, pShiftStr) - - condName = options["condition"] - global condDict - if condName in condDict: - # If the condition key (like "mouse4") already has toggles, - # just append another toggle string - changes = condDict[condName]["change_keys"] - restores = condDict[condName]["restore_keys"] - - if isToggle: - # "toggle: true" specified, add the toggle string - if toggleStr not in changes: - changes.append(toggleStr) - if pShiftStr in changes: - # If key already has normal shift, remove it - changes.remove(pShiftStr) - changes.remove(mShiftStr) - - elif pShiftStr not in changes: - # not toggle, not already in changes - changes.append(pShiftStr) - restores.append(mShiftStr) - else: - # If the condition key doesn't already exist, make it - if isToggle: - condDict.update( { - condName: { - "change_keys": [ toggleStr ], - "restore_keys": [ ] - } - } ) - else: - condDict.update( { - condName: { - "change_keys": [ pShiftStr ], - "restore_keys": [ mShiftStr ] - } - } ) - - return ret - -def repeat(key, options): - return f'placeholder for {key} (repeat)\n' diff --git a/src/tfscript/cli.py b/src/tfscript/cli.py index 801b4af..dc6d7a9 100644 --- a/src/tfscript/cli.py +++ b/src/tfscript/cli.py @@ -27,8 +27,7 @@ except ModuleNotFoundError: # Local libraries import tfscript -from tfscript import verify -from tfscript import writing +from tfscript import verify, writing, makeCFG args = {} targetDir = '' @@ -63,12 +62,12 @@ def parseConfig(config, defaults): if defaults is not None: config.update({'default': defaults}) - for currentClass in config: - bindList = config[currentClass] - stringToWrite = '' - for bind in bindList: - stringToWrite += bind.toTF2() - replaceDict = writing.writeOutput(stringToWrite, currentClass, args) + for class_ in config: + stringToWrite = makeCFG( + config[class_], + default=(class_ == 'default') + ) + replaceDict = writing.writeOutput(stringToWrite, class_, args) tempsAndReals.update(replaceDict) return tempsAndReals