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

84 lines
3.8 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):
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:
environ = environment['commands'][trigger][cmd].run(environment)
if environ != None:
environment = environ
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-08-30 09:47:10 -04:00
if environment['generalInformation']['suspend']:
2016-08-30 05:58:06 -04:00
return environment
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)
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-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
def isShortcutDefined(self, environment, currCommand):
2016-07-08 12:33:32 -04:00
return( environment['input']['currShortcutString'] in environment['bindings'])
2016-09-05 13:48:19 -04:00
def setCurrCommandForExec(self, environment, currCommand):
2016-07-08 12:33:32 -04:00
if not self.isShortcutDefined(environment):
return environment
environment['commandInfo']['currCommand'] = currCommand
2016-07-08 12:33:32 -04:00
return environment
def getCommandForShortcut(self, environment, currShortcutString):
if not self.isShortcutDefined(environment):
return environment
return environment['bindings'][currShortcutString]
2016-07-08 12:33:32 -04:00
def isCommandDefined(self, environment, currCommand):
return( currCommand in environment['commands']['commands'])
2016-07-08 12:33:32 -04:00