fenrir/src/fenrir-package/core/commandManager.py

58 lines
2.1 KiB
Python
Raw Normal View History

#!/bin/python
2016-07-08 20:48:22 -04:00
import importlib.util
import glob
import os
2016-07-08 06:25:27 -04:00
class commandManager():
def __init__(self):
2016-07-08 06:26:00 -04:00
pass
2016-07-08 12:33:32 -04:00
def loadCommands(self, environment):
2016-07-08 20:48:22 -04:00
commandFolder = "commands/"
commandList = glob.glob(commandFolder+'*')
for currCommand in commandList:
try:
fileName, fileExtension = os.path.splitext(currCommand)
fileName = fileName.split('/')[-1]
if fileName in ['__init__','__pycache__']:
continue
print(fileName)
if fileExtension.lower() == 'py':
spec = importlib.util.spec_from_file_location(fileName, currCommand)
command_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(command_mod)
environment['commands']['fileName'] = command_mod()
except:
continue
2016-07-08 12:33:32 -04:00
return environment
2016-07-08 20:48:22 -04:00
2016-07-08 12:33:32 -04:00
def executeCommand(self, environment):
print(environment['commandInfo']['currCommand'])
if self.isCommandDefined(environment):
2016-07-08 19:10:01 -04:00
try:
environ = environment['commands'][environment['commandInfo']['currCommand']].run(environment)
if environ != None:
environment = environ
2016-07-08 19:11:02 -04:00
except:
2016-07-08 19:10:01 -04:00
pass
2016-07-08 12:33:32 -04:00
environment['commandInfo']['currCommand'] = ''
return environment
def executeNextCommand(self, environment):
pass
def isShortcutDefined(self, environment):
return( environment['input']['currShortcutString'] in environment['bindings'])
def getCommandForShortcut(self, environment):
if not self.isShortcutDefined(environment):
return environment
environment['commandInfo']['currCommand'] = environment['bindings'][environment['input']['currShortcutString']]
return environment
def isCommandDefined(self, environment):
return( environment['commandInfo']['currCommand'] in environment['commands'])
def enqueueCommand(self, environment):
if not self.isCommandDefined(environment):
return False
return True