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

100 lines
4.6 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')
self.env['runtime']['commandManager'].loadCommands('onScreenChanged')
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-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
def executeTriggerCommands(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)
self.env['runtime']['debug'].writeDebugOut("Executing trigger:" + trigger + "." + cmd ,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])