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

63 lines
2.4 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-12 11:13:59 -04:00
import time
2016-07-08 06:25:27 -04:00
class commandManager():
def __init__(self):
2016-07-08 06:26:00 -04:00
pass
2016-07-10 09:43:15 -04:00
def loadCommands(self, environment, section='commands'):
commandFolder = "commands/" + section +"/"
2016-07-08 20:48:22 -04:00
commandList = glob.glob(commandFolder+'*')
for currCommand in commandList:
2016-08-19 11:47:21 -04:00
try:
fileName, fileExtension = os.path.splitext(currCommand)
fileName = fileName.split('/')[-1]
if fileName in ['__init__','__pycache__']:
continue
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'][section][fileName] = command_mod.command()
except Exception as e:
print(e)
2016-07-08 20:48:22 -04:00
continue
2016-07-08 12:33:32 -04:00
return environment
2016-07-12 17:09:11 -04:00
def executeTriggerCommands(self, environment, trigger):
for cmd in sorted(environment['commands'][trigger]):
2016-08-19 11:47:21 -04:00
try:
environ = environment['commands'][trigger][cmd].run(environment)
if environ != None:
environment = environ
except Exception as e:
print(e)
2016-07-12 17:09:11 -04:00
return environment
2016-07-13 15:40:19 -04:00
2016-07-10 09:43:15 -04:00
def executeCommand(self, environment, currCommand, section = 'commands'):
2016-07-08 12:33:32 -04:00
if self.isCommandDefined(environment):
2016-07-10 17:02:17 -04:00
try:
environ = environment['commands'][section][currCommand].run(environment)
if environ != None:
environment = environ
2016-08-19 11:47:21 -04:00
except Exception as e:
print(e)
2016-07-08 12:33:32 -04:00
environment['commandInfo']['currCommand'] = ''
2016-07-13 15:40:19 -04:00
environment['commandInfo']['lastCommandTime'] = time.time()
2016-07-08 12:33:32 -04:00
return environment
2016-08-19 11:47:21 -04:00
2016-07-08 12:33:32 -04:00
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):
2016-07-10 09:43:15 -04:00
return( environment['commandInfo']['currCommand'] in environment['commands']['commands'])
2016-07-08 12:33:32 -04:00