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

144 lines
6.7 KiB
Python
Raw Normal View History

#!/bin/python
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
2016-07-08 20:48:22 -04:00
import importlib.util
2016-09-16 19:59:38 -04:00
import glob, os, time
from core 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):
self.env = environment
self.env['runtime']['commandManager'].loadCommands('commands')
self.env['runtime']['commandManager'].loadCommands('onInput')
self.env['runtime']['commandManager'].loadCommands('onScreenUpdate')
2016-09-21 17:46:03 -04:00
self.env['runtime']['commandManager'].loadCommands('onScreenChanged')
2016-09-22 05:46:44 -04:00
self.env['runtime']['commandManager'].loadCommands('onApplicationChange')
self.env['runtime']['commandManager'].loadCommands('onSwitchApplicationProfile')
def shutdown(self):
self.env['runtime']['commandManager'].shutdownCommands('commands')
self.env['runtime']['commandManager'].shutdownCommands('onInput')
self.env['runtime']['commandManager'].shutdownCommands('onScreenUpdate')
self.env['runtime']['commandManager'].shutdownCommands('onScreenChanged')
2016-09-21 17:46:03 -04:00
self.env['runtime']['commandManager'].shutdownCommands('onApplicationChange')
2016-09-22 05:46:44 -04:00
self.env['runtime']['commandManager'].shutdownCommands('onSwitchApplicationProfile')
2016-09-17 15:03:03 -04:00
def loadCommands(self, section='commands'):
2016-07-10 09:43:15 -04:00
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)
self.env['commands'][section][fileName.upper()] = command_mod.command()
self.env['commands'][section][fileName.upper()].initialize(self.env)
2016-08-19 11:47:21 -04:00
except Exception as e:
2016-08-28 13:19:55 -04:00
print(e)
self.env['runtime']['debug'].writeDebugOut("Loading command:" + command ,debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(str(e),debug.debugLevel.ERROR)
2016-07-08 20:48:22 -04:00
continue
2016-08-23 18:41:16 -04:00
def shutdownCommands(self, section):
for command in sorted(self.env['commands'][section]):
2016-09-17 15:03:03 -04:00
try:
self.env['commands'][section][command].shutdown()
del self.env['commands'][section][command]
2016-09-17 15:03:03 -04:00
except Exception as e:
print(e)
self.env['runtime']['debug'].writeDebugOut("Shutdown command:" + section + "." + command ,debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(str(e),debug.debugLevel.ERROR)
2016-09-17 15:03:03 -04:00
continue
2016-09-22 05:46:44 -04:00
def executeSwitchTrigger(self, trigger, unLoadScript, loadScript):
if self.env['runtime']['screenManager'].isSuspendingScreen():
return
#unload
oldScript = ''
if isinstance(unLoadScript, list):
if len(unLoadScript) == 0:
oldScript = 'DEFAULT'
2016-09-22 05:46:44 -04:00
else:
oldScript = unLoadScript[0]
2016-09-22 05:46:44 -04:00
elif unLoadScript:
oldScript = str(unLoadScript)
2016-09-22 05:46:44 -04:00
if oldScript == '':
oldScript == 'DEFAULT'
2016-09-22 05:46:44 -04:00
if self.commandExists(oldScript, trigger):
try:
self.env['commands'][trigger][oldScript].unload()
2016-09-22 05:46:44 -04:00
except Exception as e:
print(e)
self.env['runtime']['debug'].writeDebugOut("Executing trigger:" + trigger + "." + oldScript ,debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(str(e),debug.debugLevel.ERROR)
#load
newScript = ''
if isinstance(loadScript, list):
if len(loadScript) == 0:
newScript = 'DEFAULT'
2016-09-22 05:46:44 -04:00
else:
newScript = loadScript[0]
2016-09-22 05:46:44 -04:00
elif unLoadScript:
newScript = str(loadScript)
2016-09-22 05:46:44 -04:00
if newScript == '':
newScript == 'DEFAULT'
2016-09-22 05:46:44 -04:00
if self.commandExists(newScript, trigger):
try:
self.env['commands'][trigger][newScript].load()
2016-09-22 05:46:44 -04:00
except Exception as e:
print(e)
self.env['runtime']['debug'].writeDebugOut("Executing trigger:" + trigger + "." + newScript ,debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(str(e),debug.debugLevel.ERROR)
def executeDefaultTrigger(self, trigger):
if self.env['runtime']['screenManager'].isSuspendingScreen():
2016-09-17 15:03:03 -04:00
return
for command in sorted(self.env['commands'][trigger]):
if self.commandExists(command, trigger):
2016-09-16 19:26:07 -04:00
try:
self.env['commands'][trigger][command].run()
2016-09-16 19:26:07 -04:00
except Exception as e:
print(e)
2016-09-22 05:46:44 -04:00
self.env['runtime']['debug'].writeDebugOut("Executing trigger:" + trigger + "." + command ,debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(str(e),debug.debugLevel.ERROR)
2016-07-13 15:40:19 -04:00
def executeCommand(self, command, section = 'commands'):
if self.env['runtime']['screenManager'].isSuspendingScreen():
2016-09-17 15:03:03 -04:00
return
if self.commandExists(command, section):
2016-07-10 17:02:17 -04:00
try:
if self.env['generalInformation']['tutorialMode']:
description = self.env['commands'][section][command].getDescription()
self.env['runtime']['outputManager'].presentText(description, interrupt=True)
2016-09-16 19:59:38 -04:00
else:
self.env['commands'][section][command].run()
2016-08-19 11:47:21 -04:00
except Exception as e:
print(e)
2016-09-18 09:13:24 -04:00
self.env['runtime']['debug'].writeDebugOut("Executing command:" + section + "." + command ,debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(str(e),debug.debugLevel.ERROR)
self.clearCommandQueued()
self.env['commandInfo']['lastCommandExecutionTime'] = time.time()
2016-08-19 11:47:21 -04:00
def isCommandQueued(self):
return self.env['commandInfo']['currCommand'] != ''
def clearCommandQueued(self):
self.env['commandInfo']['currCommand'] = ''
2016-09-16 19:26:07 -04:00
def queueCommand(self, command):
self.env['commandInfo']['currCommand'] = command
2016-09-16 19:26:07 -04:00
def commandExists(self, command, section = 'commands'):
return( command.upper() in self.env['commands'][section])