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

73 lines
3.5 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-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')
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+'*')
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()
environment['commands'][section][fileName].initialize(environment)
2016-08-19 11:47:21 -04:00
except Exception as e:
2016-08-28 13:19:55 -04:00
print(e)
environment['runtime']['debug'].writeDebugOut(environment,"Error while loading command:" + currCommand ,debug.debugLevel.ERROR)
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):
if environment['runtime']['screenManager'].isSuspendingScreen(environment):
2016-08-30 05:58:06 -04:00
return environment
2016-07-12 17:09:11 -04:00
for cmd in sorted(environment['commands'][trigger]):
2016-08-19 11:47:21 -04:00
try:
environment['commands'][trigger][cmd].run(environment)
2016-08-19 11:47:21 -04:00
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-07-10 09:43:15 -04:00
def executeCommand(self, environment, currCommand, section = 'commands'):
2016-09-14 17:27:19 -04:00
if environment['runtime']['screenManager'].isSuspendingScreen(environment) :
return environment
2016-07-08 12:33:32 -04:00
if self.isCommandDefined(environment):
2016-07-10 17:02:17 -04:00
try:
environment['commands'][section][currCommand].run(environment)
2016-08-19 11:47:21 -04:00
except Exception as e:
print(e)
environment['runtime']['debug'].writeDebugOut(environment,"Error while executing command:" + section + "." + currCommand ,debug.debugLevel.ERROR)
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-15 10:56:47 -04:00
def isShortcutDefined(self, environment, shortcut):
return( str(shortcut).upper() in environment['bindings'])
2016-07-08 12:33:32 -04:00
2016-09-05 13:48:19 -04:00
def setCurrCommandForExec(self, environment, currCommand):
environment['commandInfo']['currCommand'] = currCommand
2016-07-08 12:33:32 -04:00
return environment