2016-07-07 13:43:31 -04:00
|
|
|
#!/bin/python
|
2016-09-19 16:15:58 -04:00
|
|
|
# -*- 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
|
2016-09-19 16:15:58 -04:00
|
|
|
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
|
2016-09-02 15:37:36 -04:00
|
|
|
def initialize(self, environment):
|
2016-09-21 17:17:54 -04:00
|
|
|
self.env = environment
|
|
|
|
self.env['runtime']['commandManager'].loadCommands('commands')
|
2016-09-21 17:30:27 -04:00
|
|
|
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')
|
2016-09-17 11:54:49 -04:00
|
|
|
|
2016-09-21 17:17:54 -04:00
|
|
|
def shutdown(self):
|
|
|
|
self.env['runtime']['commandManager'].shutdownCommands('commands')
|
|
|
|
self.env['runtime']['commandManager'].shutdownCommands('onInput')
|
2016-09-21 17:30:27 -04:00
|
|
|
self.env['runtime']['commandManager'].shutdownCommands('onScreenUpdate')
|
2016-09-21 17:17:54 -04:00
|
|
|
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
|
|
|
|
2016-09-21 17:17:54 -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)
|
2016-09-21 17:17:54 -04:00
|
|
|
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)
|
2016-09-21 17:17:54 -04:00
|
|
|
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
|
|
|
|
2016-09-21 17:17:54 -04:00
|
|
|
def shutdownCommands(self, section):
|
|
|
|
for command in sorted(self.env['commands'][section]):
|
2016-09-17 15:03:03 -04:00
|
|
|
try:
|
2016-09-21 17:17:54 -04:00
|
|
|
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)
|
2016-09-21 17:17:54 -04:00
|
|
|
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:
|
2016-09-22 15:56:37 -04:00
|
|
|
oldScript = 'DEFAULT'
|
2016-09-22 05:46:44 -04:00
|
|
|
else:
|
2016-09-22 17:42:50 -04:00
|
|
|
oldScript = unLoadScript[0]
|
2016-09-22 05:46:44 -04:00
|
|
|
elif unLoadScript:
|
2016-09-22 17:42:50 -04:00
|
|
|
oldScript = str(unLoadScript)
|
2016-09-22 05:46:44 -04:00
|
|
|
if oldScript == '':
|
2016-09-22 15:56:37 -04:00
|
|
|
oldScript == 'DEFAULT'
|
2016-09-22 05:46:44 -04:00
|
|
|
if self.commandExists(oldScript, trigger):
|
|
|
|
try:
|
2016-09-22 15:56:37 -04:00
|
|
|
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:
|
2016-09-22 15:56:37 -04:00
|
|
|
newScript = 'DEFAULT'
|
2016-09-22 05:46:44 -04:00
|
|
|
else:
|
2016-09-22 17:42:50 -04:00
|
|
|
newScript = loadScript[0]
|
2016-09-22 05:46:44 -04:00
|
|
|
elif unLoadScript:
|
2016-09-22 17:42:50 -04:00
|
|
|
newScript = str(loadScript)
|
2016-09-22 05:46:44 -04:00
|
|
|
if newScript == '':
|
2016-09-22 15:56:37 -04:00
|
|
|
newScript == 'DEFAULT'
|
2016-09-22 05:46:44 -04:00
|
|
|
if self.commandExists(newScript, trigger):
|
|
|
|
try:
|
2016-09-22 15:56:37 -04:00
|
|
|
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):
|
2016-09-21 17:17:54 -04:00
|
|
|
if self.env['runtime']['screenManager'].isSuspendingScreen():
|
2016-09-17 15:03:03 -04:00
|
|
|
return
|
2016-09-21 17:17:54 -04:00
|
|
|
for command in sorted(self.env['commands'][trigger]):
|
|
|
|
if self.commandExists(command, trigger):
|
2016-09-16 19:26:07 -04:00
|
|
|
try:
|
2016-09-21 17:17:54 -04:00
|
|
|
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)
|
2016-09-21 17:17:54 -04:00
|
|
|
self.env['runtime']['debug'].writeDebugOut(str(e),debug.debugLevel.ERROR)
|
2016-07-13 15:40:19 -04:00
|
|
|
|
2016-09-21 17:17:54 -04:00
|
|
|
def executeCommand(self, command, section = 'commands'):
|
|
|
|
if self.env['runtime']['screenManager'].isSuspendingScreen():
|
2016-09-17 15:03:03 -04:00
|
|
|
return
|
2016-09-21 17:17:54 -04:00
|
|
|
if self.commandExists(command, section):
|
2016-07-10 17:02:17 -04:00
|
|
|
try:
|
2016-09-21 17:17:54 -04:00
|
|
|
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:
|
2016-09-21 17:17:54 -04:00
|
|
|
self.env['commands'][section][command].run()
|
2016-08-19 11:47:21 -04:00
|
|
|
except Exception as e:
|
2016-09-04 09:04:23 -04:00
|
|
|
print(e)
|
2016-09-18 09:13:24 -04:00
|
|
|
|
2016-09-21 17:17:54 -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
|
|
|
|
2016-09-21 17:17:54 -04:00
|
|
|
def isCommandQueued(self):
|
|
|
|
return self.env['commandInfo']['currCommand'] != ''
|
2016-09-17 11:54:49 -04:00
|
|
|
|
2016-09-21 17:17:54 -04:00
|
|
|
def clearCommandQueued(self):
|
|
|
|
self.env['commandInfo']['currCommand'] = ''
|
2016-09-16 19:26:07 -04:00
|
|
|
|
2016-09-21 17:17:54 -04:00
|
|
|
def queueCommand(self, command):
|
|
|
|
self.env['commandInfo']['currCommand'] = command
|
2016-09-16 19:26:07 -04:00
|
|
|
|
2016-09-21 17:17:54 -04:00
|
|
|
def commandExists(self, command, section = 'commands'):
|
|
|
|
return( command.upper() in self.env['commands'][section])
|