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

92 lines
4.8 KiB
Python
Raw Normal View History

#!/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
def initialize(self, environment):
environment['runtime']['commandManager'].loadCommands(environment,'commands')
environment['runtime']['commandManager'].loadCommands(environment,'onInput')
environment['runtime']['commandManager'].loadCommands(environment,'onScreenChanged')
def shutdown(self, environment):
2016-09-17 15:03:03 -04:00
environment['runtime']['commandManager'].shutdownCommands(environment,'commands')
environment['runtime']['commandManager'].shutdownCommands(environment,'onInput')
environment['runtime']['commandManager'].shutdownCommands(environment,'onScreenChanged')
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-17 15:03:03 -04:00
environment['runtime']['debug'].writeDebugOut(environment,"Loading command:" + command ,debug.debugLevel.ERROR)
environment['runtime']['debug'].writeDebugOut(environment,str(e),debug.debugLevel.ERROR)
2016-07-08 20:48:22 -04:00
continue
2016-08-23 18:41:16 -04:00
2016-09-17 15:03:03 -04:00
def shutdownCommands(self, environment, section):
for command in sorted(environment['commands'][section]):
try:
environment['commands'][section][command].shutdown(environment)
environment['commands'][section][command] = None
except Exception as e:
print(e)
environment['runtime']['debug'].writeDebugOut(environment,"Shutdown command:" + section + "." + cmd ,debug.debugLevel.ERROR)
environment['runtime']['debug'].writeDebugOut(environment,str(e),debug.debugLevel.ERROR)
continue
2016-07-12 17:09:11 -04:00
def executeTriggerCommands(self, environment, trigger):
if environment['runtime']['screenManager'].isSuspendingScreen(environment):
2016-09-17 15:03:03 -04:00
return
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)
2016-09-17 15:03:03 -04:00
environment['runtime']['debug'].writeDebugOut(environment,"Executing trigger:" + trigger + "." + cmd ,debug.debugLevel.ERROR)
2016-09-16 19:26:07 -04:00
environment['runtime']['debug'].writeDebugOut(environment,str(e),debug.debugLevel.ERROR)
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) :
2016-09-17 15:03:03 -04:00
return
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:
print(e)
2016-09-18 09:13:24 -04:00
2016-09-17 15:03:03 -04:00
environment['runtime']['debug'].writeDebugOut(environment,"Executing command:" + section + "." + command ,debug.debugLevel.ERROR)
environment['runtime']['debug'].writeDebugOut(environment,str(e),debug.debugLevel.ERROR)
self.clearCommandQueued(environment)
2016-09-13 04:29:04 -04:00
environment['commandInfo']['lastCommandExecutionTime'] = time.time()
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 clearCommandQueued(self, environment):
environment['commandInfo']['currCommand'] = ''
2016-09-16 19:26:07 -04:00
def queueCommand(self, environment, command):
environment['commandInfo']['currCommand'] = command
def commandExists(self, environment, command, section = 'commands'):
return( command.upper() in environment['commands'][section])