2016-07-07 13:43:31 -04:00
|
|
|
#!/bin/python
|
2016-07-08 20:48:22 -04:00
|
|
|
import importlib.util
|
2016-09-16 19:59:38 -04:00
|
|
|
import glob, os, time
|
2016-08-23 18:41:16 -04:00
|
|
|
from utils import debug
|
2016-07-08 06:25:27 -04:00
|
|
|
|
|
|
|
class commandManager():
|
|
|
|
def __init__(self):
|
2016-07-08 06:26:00 -04:00
|
|
|
pass
|
2016-09-02 15:37:36 -04:00
|
|
|
def initialize(self, environment):
|
2016-09-16 19:04:03 -04:00
|
|
|
environment['runtime']['commandManager'].loadCommands(environment,'commands')
|
|
|
|
environment['runtime']['commandManager'].loadCommands(environment,'onInput')
|
|
|
|
environment['runtime']['commandManager'].loadCommands(environment,'onScreenChanged')
|
2016-09-02 15:37:36 -04:00
|
|
|
return environment
|
|
|
|
def shutdown(self, environment):
|
|
|
|
return environment
|
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+'*')
|
2016-09-16 19:26:07 -04:00
|
|
|
for command in commandList:
|
2016-08-19 11:47:21 -04:00
|
|
|
try:
|
2016-09-16 19:26:07 -04:00
|
|
|
fileName, fileExtension = os.path.splitext(command)
|
2016-08-19 11:47:21 -04:00
|
|
|
fileName = fileName.split('/')[-1]
|
|
|
|
if fileName in ['__init__','__pycache__']:
|
|
|
|
continue
|
|
|
|
if fileExtension.lower() == '.py':
|
2016-09-16 19:26:07 -04:00
|
|
|
spec = importlib.util.spec_from_file_location(fileName, command)
|
2016-08-19 11:47:21 -04:00
|
|
|
command_mod = importlib.util.module_from_spec(spec)
|
|
|
|
spec.loader.exec_module(command_mod)
|
2016-09-16 19:26:07 -04:00
|
|
|
environment['commands'][section][fileName.upper()] = command_mod.command()
|
|
|
|
environment['commands'][section][fileName.upper()].initialize(environment)
|
2016-08-19 11:47:21 -04:00
|
|
|
except Exception as e:
|
2016-08-28 13:19:55 -04:00
|
|
|
print(e)
|
2016-09-16 19:26:07 -04:00
|
|
|
environment['runtime']['debug'].writeDebugOut(environment,"Error while loading command:" + command ,debug.debugLevel.ERROR)
|
2016-08-21 17:55:56 -04:00
|
|
|
environment['runtime']['debug'].writeDebugOut(environment,str(e),debug.debugLevel.ERROR)
|
2016-07-08 20:48:22 -04:00
|
|
|
continue
|
2016-07-08 12:33:32 -04:00
|
|
|
return environment
|
2016-08-23 18:41:16 -04:00
|
|
|
|
2016-07-12 17:09:11 -04:00
|
|
|
def executeTriggerCommands(self, environment, trigger):
|
2016-09-16 19:04:03 -04:00
|
|
|
if environment['runtime']['screenManager'].isSuspendingScreen(environment):
|
2016-08-30 05:58:06 -04:00
|
|
|
return environment
|
2016-09-16 19:26:07 -04:00
|
|
|
for command in sorted(environment['commands'][trigger]):
|
|
|
|
if self.commandExists(environment, command, trigger):
|
|
|
|
try:
|
|
|
|
environment['commands'][trigger][command].run(environment)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
environment['runtime']['debug'].writeDebugOut(environment,"Error while executing trigger:" + trigger + "." + cmd ,debug.debugLevel.ERROR)
|
|
|
|
environment['runtime']['debug'].writeDebugOut(environment,str(e),debug.debugLevel.ERROR)
|
2016-07-12 17:09:11 -04:00
|
|
|
return environment
|
2016-07-13 15:40:19 -04:00
|
|
|
|
2016-09-16 19:26:07 -04:00
|
|
|
def executeCommand(self, environment, command, section = 'commands'):
|
2016-09-14 17:27:19 -04:00
|
|
|
if environment['runtime']['screenManager'].isSuspendingScreen(environment) :
|
|
|
|
return environment
|
2016-09-16 19:26:07 -04:00
|
|
|
if self.commandExists(environment, command, section):
|
2016-07-10 17:02:17 -04:00
|
|
|
try:
|
2016-09-16 19:59:38 -04:00
|
|
|
if environment['generalInformation']['tutorialMode']:
|
2016-09-16 20:14:11 -04:00
|
|
|
description = environment['commands'][section][command].getDescription(environment)
|
|
|
|
environment['runtime']['outputManager'].presentText(environment, description, interrupt=True)
|
2016-09-16 19:59:38 -04:00
|
|
|
else:
|
|
|
|
environment['commands'][section][command].run(environment)
|
2016-08-19 11:47:21 -04:00
|
|
|
except Exception as e:
|
2016-09-04 09:04:23 -04:00
|
|
|
print(e)
|
2016-09-16 19:26:07 -04:00
|
|
|
environment['runtime']['debug'].writeDebugOut(environment,"Error while executing command:" + section + "." + command ,debug.debugLevel.ERROR)
|
2016-08-21 17:55:56 -04:00
|
|
|
environment['runtime']['debug'].writeDebugOut(environment,str(e),debug.debugLevel.ERROR)
|
2016-07-08 12:33:32 -04:00
|
|
|
environment['commandInfo']['currCommand'] = ''
|
2016-09-13 04:29:04 -04:00
|
|
|
environment['commandInfo']['lastCommandExecutionTime'] = time.time()
|
2016-07-08 12:33:32 -04:00
|
|
|
return environment
|
2016-08-19 11:47:21 -04:00
|
|
|
|
2016-09-16 19:26:07 -04:00
|
|
|
def isCommandQueued(self, environment):
|
|
|
|
return environment['commandInfo']['currCommand'] != ''
|
|
|
|
|
|
|
|
def queueCommand(self, environment, command):
|
|
|
|
environment['commandInfo']['currCommand'] = command
|
2016-07-08 12:33:32 -04:00
|
|
|
return environment
|
2016-09-16 19:26:07 -04:00
|
|
|
|
|
|
|
def commandExists(self, environment, command, section = 'commands'):
|
|
|
|
return( command.upper() in environment['commands'][section])
|