fenrir/src/fenrirscreenreader/core/settingsManager.py

403 lines
20 KiB
Python
Raw Normal View History

#!/bin/python
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
import os, inspect
2018-09-08 17:15:18 -04:00
currentdir = os.path.dirname(os.path.realpath(os.path.abspath(inspect.getfile(inspect.currentframe()))))
fenrirPath = os.path.dirname(currentdir)
from configparser import ConfigParser
from fenrirscreenreader.core import debugManager
from fenrirscreenreader.core import memoryManager
from fenrirscreenreader.core import processManager
from fenrirscreenreader.core import eventManager
from fenrirscreenreader.core import inputManager
from fenrirscreenreader.core import outputManager
from fenrirscreenreader.core import commandManager
from fenrirscreenreader.core import screenManager
from fenrirscreenreader.core import punctuationManager
from fenrirscreenreader.core import cursorManager
from fenrirscreenreader.core import applicationManager
from fenrirscreenreader.core import helpManager
2019-01-21 18:16:41 -05:00
from fenrirscreenreader.core import vmenuManager
from fenrirscreenreader.core import textManager
from fenrirscreenreader.core import tableManager
2018-03-25 18:32:25 -04:00
from fenrirscreenreader.core import byteManager
2018-05-24 05:32:56 -04:00
from fenrirscreenreader.core import attributeManager
2018-06-17 18:15:25 -04:00
from fenrirscreenreader.core import barrierManager
2018-09-04 16:32:03 -04:00
from fenrirscreenreader.core import remoteManager
2018-09-17 18:04:03 -04:00
from fenrirscreenreader.core import sayAllManager
2019-02-13 16:14:13 -05:00
from fenrirscreenreader.core import quickMenuManager
from fenrirscreenreader.core import environment
from fenrirscreenreader.core.settingsData import settingsData
from fenrirscreenreader.core import debug
from fenrirscreenreader.utils import module_utils
class settingsManager():
def __init__(self):
self.settings = settingsData
self.settingArgDict = {}
2019-02-12 17:44:57 -05:00
self.bindingsBackup = None
def initialize(self, environment):
self.env = environment
def shutdown(self):
pass
2019-02-12 17:44:57 -05:00
def getBindingBackup(self):
return self.bindingsBackup.copy()
def loadSoundIcons(self, soundIconPath):
siConfig = open(soundIconPath + '/soundicons.conf',"r")
while(True):
line = siConfig.readline()
if not line:
break
line = line.replace('\n','')
if line.replace(" ","") == '':
2018-09-10 22:00:59 -04:00
continue
if line.replace(" ","").startswith("#"):
continue
if line.count("=") != 1:
continue
Values = line.split('=')
soundIcon = Values[0].upper()
Values[1] = Values[1].replace("'","")
Values[1] = Values[1].replace('"',"")
soundIconFile = ''
if os.path.exists(Values[1]):
soundIconFile = Values[1]
else:
if not soundIconPath.endswith("/"):
soundIconPath += '/'
if os.path.exists(soundIconPath + Values[1]):
soundIconFile = soundIconPath + Values[1]
self.env['soundIcons'][soundIcon] = soundIconFile
self.env['runtime']['debug'].writeDebugOut("SoundIcon: " + soundIcon + '.' + soundIconFile, debug.debugLevel.INFO, onAnyLevel=True)
siConfig.close()
def loadSettings(self, settingConfigPath):
if not os.path.exists(settingConfigPath):
return False
2018-03-22 18:50:14 -04:00
if not os.access(settingConfigPath, os.R_OK):
return False
self.env['settings'] = ConfigParser()
self.env['settings'].read(settingConfigPath)
return True
2018-09-08 16:23:27 -04:00
def saveSettings(self, settingConfigPath):
# set opt dict here
# save file
try:
#print('file: ',settingConfigPath)
for section, settings in self.settingArgDict.items():
for setting, value in settings.items():
#print(section, setting, value)
self.env['settings'].set(section, setting, value)
#print('full',self.env['settings'])
2018-09-08 16:25:40 -04:00
configFile = open(settingConfigPath, 'w')
2018-09-08 16:23:27 -04:00
self.env['settings'].write(configFile)
2018-09-08 17:15:18 -04:00
configFile.close()
os.chmod(settingConfigPath, 0o666)
2018-09-08 16:23:27 -04:00
except Exception as e:
2018-09-08 16:29:20 -04:00
self.env['runtime']['debug'].writeDebugOut('saveSettings: save settingsfile:' + settingConfigPath + 'failed. Error:' + str(e), debug.debugLevel.ERROR)
def setSetting(self, section, setting, value):
self.setOptionArgDict(section, setting, value)
#self.env['settings'].set(section, setting, value)
def getSetting(self, section, setting):
value = ''
try:
2018-09-10 22:00:59 -04:00
value = self.settingArgDict[section][setting]
2018-09-08 14:05:59 -04:00
return value
except:
pass
2018-09-08 14:05:59 -04:00
try:
value = self.env['settings'].get(section, setting)
except:
value = str(self.settings[section][setting])
return value
def getSettingAsInt(self, section, setting):
value = 0
try:
2018-09-10 22:00:59 -04:00
value = int(self.settingArgDict[section][setting])
2018-09-08 14:05:59 -04:00
return value
except Exception as e:
pass
try:
value = self.env['settings'].getint(section, setting)
except:
value = self.settings[section][setting]
return value
def getSettingAsFloat(self, section, setting):
value = 0.0
try:
2018-09-10 22:00:59 -04:00
value = float(self.settingArgDict[section][setting])
2018-09-08 14:05:59 -04:00
return value
except Exception as e:
2018-09-08 14:05:59 -04:00
pass
try:
value = self.env['settings'].getfloat(section, setting)
except:
value = self.settings[section][setting]
return value
def getSettingAsBool(self, section, setting):
value = False
try:
2018-09-10 22:00:59 -04:00
value = self.settingArgDict[section][setting].upper() in ['1','YES','JA','TRUE']
return value
except Exception as e:
2018-09-10 22:00:59 -04:00
pass
try:
value = self.env['settings'].getboolean(section, setting)
except:
value = self.settings[section][setting]
return value
def loadDriver(self, driverName, driverType):
try:
2018-09-10 20:53:05 -04:00
self.env['runtime'][driverType].shutdown(self.env)
except:
pass
try:
driver_mod = module_utils.importModule(driverName,
fenrirPath + "/" + driverType + '/' + driverName + '.py')
self.env['runtime'][driverType] = driver_mod.driver()
self.env['runtime'][driverType].initialize(self.env)
self.env['runtime']['debug'].writeDebugOut('Loading Driver ' + driverType + ' (' + driverName +") OK",debug.debugLevel.INFO, onAnyLevel=True)
except Exception as e:
2018-03-28 11:09:56 -04:00
self.env['runtime']['debug'].writeDebugOut('Loading Driver ' + driverType + ' (' + driverName +") FAILED:"+ str(e), debug.debugLevel.ERROR)
try:
driver_mod = module_utils.importModule(driverName,
fenrirPath + "/" + driverType + '/dummyDriver.py')
self.env['runtime'][driverType] = driver_mod.driver()
self.env['runtime'][driverType].initialize(self.env)
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('(fallback) Loading Driver ' + driverType + ' (dummyDriver) FAILED:'+ str(e), debug.debugLevel.ERROR)
2018-09-10 20:53:05 -04:00
def shutdownDriver(self, driverType):
2018-05-11 17:34:13 -04:00
try:
self.env['runtime'][driverType].shutdown()
except Exception as e:
2018-09-08 14:05:59 -04:00
pass
2018-09-04 16:32:03 -04:00
del self.env['runtime'][driverType]
def setFenrirKeys(self, keys):
keys = keys.upper()
keyList = keys.split(',')
for key in keyList:
if not key in self.env['input']['fenrirKey']:
self.env['input']['fenrirKey'].append(key)
def setScriptKeys(self, keys):
keys = keys.upper()
keyList = keys.split(',')
for key in keyList:
if not key in self.env['input']['scriptKey']:
self.env['input']['scriptKey'].append(key)
2018-09-05 16:27:56 -04:00
def resetSettingArgDict(self):
self.settingArgDict = {}
2018-09-10 22:00:59 -04:00
def setOptionArgDict(self, section, setting, value):
#section = section.lower()
#setting = setting.lower()
try:
e = self.settingArgDict[section]
except KeyError:
self.settingArgDict[section] = {}
2018-09-10 22:00:59 -04:00
try:
t = self.settings[section][setting]
except:
print(section,setting, 'not found')
return
try:
if isinstance(self.settings[section][setting], str):
v = str(value)
elif isinstance(self.settings[section][setting], bool):
if not value in ['True','False']:
raise ValueError('could not convert string to bool: '+ value)
elif isinstance(self.settings[section][setting], int):
v = int(value)
elif isinstance(self.settings[section][setting], float):
v = float(value)
self.settingArgDict[section][setting] = str(value)
except Exception as e:
print('settingsManager:setOptionArgDict:Datatype missmatch: '+ section + '#' + setting + '=' + value + ' Error:' + str(e))
#self.env['runtime']['debug'].writeDebugOut('settingsManager:setOptionArgDict:Datatype missmatch: '+ section + '#' + setting + '=' + value + ' Error:' + str(e), debug.debugLevel.ERROR)
return
2018-09-08 14:05:59 -04:00
def parseSettingArgs(self, settingArgs):
for optionElem in settingArgs.split(';'):
if len(optionElem.split('#',1)) != 2:
continue
if len(optionElem.split('#',1)[1].split('=',1)) != 2:
continue
2018-09-08 14:05:59 -04:00
2018-10-24 17:54:49 -04:00
section = str(optionElem.split('#',1)[0])
option = str(optionElem.split('#',1)[1].split('=',1)[0])
2018-09-04 16:32:03 -04:00
value = optionElem.split('#',1)[1].split('=',1)[1]
self.setOptionArgDict(section, option, value)
def initFenrirConfig(self, cliArgs, fenrirManager = None, environment = environment.environment):
settingsRoot = '/etc/fenrirscreenreader/'
settingsFile = cliArgs.setting
soundRoot = '/usr/share/sounds/fenrirscreenreader/'
# get fenrir settings root
if not os.path.exists(settingsRoot):
if os.path.exists(fenrirPath +'/../../config/'):
settingsRoot = fenrirPath +'/../../config/'
else:
return None
# get settings file
if not os.path.exists(settingsFile):
if os.path.exists(settingsRoot + '/settings/settings.conf'):
settingsFile = settingsRoot + '/settings/settings.conf'
else:
2018-09-04 16:32:03 -04:00
return None
# get sound themes root
if not os.path.exists(soundRoot):
if os.path.exists(fenrirPath + '/../../config/sound/'):
soundRoot = fenrirPath + '/../../config/sound/'
environment['runtime']['settingsManager'] = self
environment['runtime']['settingsManager'].initialize(environment)
validConfig = environment['runtime']['settingsManager'].loadSettings(settingsFile)
if not validConfig:
return None
if cliArgs.options != '':
self.parseSettingArgs(cliArgs.options)
if cliArgs.debug:
2018-09-10 22:00:59 -04:00
self.setSetting('general', 'debugLevel', 3)
if cliArgs.print:
2018-09-10 22:00:59 -04:00
self.setSetting('general', 'debugLevel', 3)
self.setSetting('general', 'debugMode', 'PRINT')
2018-03-26 18:40:15 -04:00
if cliArgs.emulated_pty:
2018-09-10 22:00:59 -04:00
self.setSetting('screen', 'driver', 'ptyDriver')
self.setSetting('keyboard', 'driver', 'ptyDriver')
2018-03-25 18:32:25 -04:00
# TODO needs cleanup use dict
#self.setOptionArgDict('keyboard', 'keyboardLayout', 'pty')
self.setSetting('keyboard', 'keyboardLayout', 'pty')
2018-09-10 22:00:59 -04:00
self.setSetting('general', 'debugFile', '/tmp/fenrir-pty.log')
2018-09-04 16:32:03 -04:00
if cliArgs.emulated_evdev:
2018-09-10 22:00:59 -04:00
self.setSetting('screen', 'driver', 'ptyDriver')
self.setSetting('keyboard', 'driver', 'evdevDriver')
2018-03-26 18:33:41 -04:00
self.setFenrirKeys(self.getSetting('general','fenrirKeys'))
2018-09-04 16:32:03 -04:00
self.setScriptKeys(self.getSetting('general','scriptKeys'))
2018-03-24 20:24:04 -04:00
environment['runtime']['debug'] = debugManager.debugManager(self.env['runtime']['settingsManager'].getSetting('general','debugFile'))
environment['runtime']['debug'].initialize(environment)
2018-03-25 18:32:25 -04:00
if not os.path.exists(self.getSetting('sound','theme') + '/soundicons.conf'):
2018-09-10 22:00:59 -04:00
if os.path.exists(soundRoot + self.getSetting('sound','theme')):
self.setSetting('sound', 'theme', soundRoot + self.getSetting('sound','theme'))
2018-09-10 22:00:59 -04:00
if os.path.exists(self.getSetting('sound','theme') + '/soundicons.conf'):
environment['runtime']['settingsManager'].loadSoundIcons(self.getSetting('sound','theme'))
else:
environment['runtime']['settingsManager'].loadSoundIcons(self.getSetting('sound','theme'))
environment['runtime']['punctuationManager'] = punctuationManager.punctuationManager()
2018-09-06 07:46:48 -04:00
environment['runtime']['punctuationManager'].initialize(environment)
environment['runtime']['textManager'] = textManager.textManager()
environment['runtime']['textManager'].initialize(environment)
if not os.path.exists(self.getSetting('general','punctuationProfile')):
if os.path.exists(settingsRoot + 'punctuation/' + self.getSetting('general','punctuationProfile')):
self.setSetting('general', 'punctuationProfile', settingsRoot + 'punctuation/' + self.getSetting('general','punctuationProfile'))
environment['runtime']['punctuationManager'].loadDicts(self.getSetting('general','punctuationProfile'))
if os.path.exists(settingsRoot + 'punctuation/' + self.getSetting('general','punctuationProfile') + '.conf'):
self.setSetting('general', 'punctuationProfile', settingsRoot + 'punctuation/' + self.getSetting('general','punctuationProfile') + '.conf')
environment['runtime']['punctuationManager'].loadDicts(self.getSetting('general','punctuationProfile'))
else:
environment['runtime']['punctuationManager'].loadDicts(self.getSetting('general','punctuationProfile'))
2018-03-25 18:32:25 -04:00
2018-09-04 16:32:03 -04:00
if fenrirManager:
environment['runtime']['fenrirManager'] = fenrirManager
2018-03-25 18:32:25 -04:00
environment['runtime']['memoryManager'] = memoryManager.memoryManager()
environment['runtime']['memoryManager'].initialize(environment)
2018-09-04 16:32:03 -04:00
2018-05-24 05:32:56 -04:00
environment['runtime']['attributeManager'] = attributeManager.attributeManager()
2018-09-08 14:05:59 -04:00
environment['runtime']['attributeManager'].initialize(environment)
2018-09-04 16:32:03 -04:00
environment['runtime']['eventManager'] = eventManager.eventManager()
2018-09-08 14:05:59 -04:00
environment['runtime']['eventManager'].initialize(environment)
2018-09-04 16:32:03 -04:00
environment['runtime']['processManager'] = processManager.processManager()
2018-09-08 14:05:59 -04:00
environment['runtime']['processManager'].initialize(environment)
2018-05-22 19:12:31 -04:00
environment['runtime']['outputManager'] = outputManager.outputManager()
2018-09-04 16:32:03 -04:00
environment['runtime']['outputManager'].initialize(environment)
2018-05-22 19:12:31 -04:00
environment['runtime']['byteManager'] = byteManager.byteManager()
2018-09-04 16:32:03 -04:00
environment['runtime']['byteManager'].initialize(environment)
environment['runtime']['inputManager'] = inputManager.inputManager()
2018-09-04 16:32:03 -04:00
environment['runtime']['inputManager'].initialize(environment)
2018-05-22 19:12:31 -04:00
environment['runtime']['screenManager'] = screenManager.screenManager()
2018-09-04 16:32:03 -04:00
environment['runtime']['screenManager'].initialize(environment)
2018-05-22 19:12:31 -04:00
2018-09-04 16:32:03 -04:00
environment['runtime']['commandManager'] = commandManager.commandManager()
environment['runtime']['commandManager'].initialize(environment)
2018-09-04 16:32:03 -04:00
2018-04-11 17:57:28 -04:00
environment['runtime']['helpManager'] = helpManager.helpManager()
2018-09-04 16:32:03 -04:00
environment['runtime']['helpManager'].initialize(environment)
environment['runtime']['remoteManager'] = remoteManager.remoteManager()
environment['runtime']['remoteManager'].initialize(environment)
2018-03-26 02:55:26 -04:00
if environment['runtime']['inputManager'].getShortcutType() == 'KEY':
if not os.path.exists(self.getSetting('keyboard','keyboardLayout')):
if os.path.exists(settingsRoot + 'keyboard/' + self.getSetting('keyboard','keyboardLayout')):
self.setSetting('keyboard', 'keyboardLayout', settingsRoot + 'keyboard/' + self.getSetting('keyboard','keyboardLayout'))
environment['runtime']['inputManager'].loadShortcuts(self.getSetting('keyboard','keyboardLayout'))
if os.path.exists(settingsRoot + 'keyboard/' + self.getSetting('keyboard','keyboardLayout') + '.conf'):
self.setSetting('keyboard', 'keyboardLayout', settingsRoot + 'keyboard/' + self.getSetting('keyboard','keyboardLayout') + '.conf')
environment['runtime']['inputManager'].loadShortcuts(self.getSetting('keyboard','keyboardLayout'))
else:
environment['runtime']['inputManager'].loadShortcuts(self.getSetting('keyboard','keyboardLayout'))
2018-03-26 02:55:26 -04:00
elif environment['runtime']['inputManager'].getShortcutType() == 'BYTE':
if not os.path.exists(self.getSetting('keyboard','keyboardLayout')):
if os.path.exists(settingsRoot + 'keyboard/' + self.getSetting('keyboard','keyboardLayout')):
self.setSetting('keyboard', 'keyboardLayout', settingsRoot + 'keyboard/' + self.getSetting('keyboard','keyboardLayout'))
environment['runtime']['byteManager'].loadByteShortcuts(self.getSetting('keyboard','keyboardLayout'))
if os.path.exists(settingsRoot + 'keyboard/' + self.getSetting('keyboard','keyboardLayout') + '.conf'):
self.setSetting('keyboard', 'keyboardLayout', settingsRoot + 'keyboard/' + self.getSetting('keyboard','keyboardLayout') + '.conf')
environment['runtime']['byteManager'].loadByteShortcuts(self.getSetting('keyboard','keyboardLayout'))
else:
environment['runtime']['byteManager'].loadByteShortcuts(self.getSetting('keyboard','keyboardLayout'))
2018-09-10 22:00:59 -04:00
environment['runtime']['cursorManager'] = cursorManager.cursorManager()
2018-09-10 20:53:05 -04:00
environment['runtime']['cursorManager'].initialize(environment)
environment['runtime']['applicationManager'] = applicationManager.applicationManager()
2018-09-10 20:53:05 -04:00
environment['runtime']['applicationManager'].initialize(environment)
environment['runtime']['textManager'] = textManager.textManager()
2018-09-10 20:53:05 -04:00
environment['runtime']['textManager'].initialize(environment)
environment['runtime']['tableManager'] = tableManager.tableManager()
2018-09-10 20:53:05 -04:00
environment['runtime']['tableManager'].initialize(environment)
2018-06-17 18:15:25 -04:00
environment['runtime']['barrierManager'] = barrierManager.barrierManager()
2018-09-10 20:53:05 -04:00
environment['runtime']['barrierManager'].initialize(environment)
2018-09-17 18:04:03 -04:00
environment['runtime']['sayAllManager'] = sayAllManager.sayAllManager()
environment['runtime']['sayAllManager'].initialize(environment)
2019-01-21 18:16:41 -05:00
environment['runtime']['vmenuManager'] = vmenuManager.vmenuManager()
environment['runtime']['vmenuManager'].initialize(environment)
2019-02-13 16:14:13 -05:00
environment['runtime']['quickMenuManager'] = quickMenuManager.quickMenuManager()
environment['runtime']['quickMenuManager'].initialize(environment)
2018-09-10 20:53:05 -04:00
environment['runtime']['debug'].writeDebugOut('\/-------environment-------\/',debug.debugLevel.INFO, onAnyLevel=True)
environment['runtime']['debug'].writeDebugOut(str(environment), debug.debugLevel.INFO, onAnyLevel=True)
2018-09-10 20:53:05 -04:00
environment['runtime']['debug'].writeDebugOut('\/-------settings.conf-------\/', debug.debugLevel.INFO, onAnyLevel=True)
environment['runtime']['debug'].writeDebugOut(str(environment['settings']._sections) , debug.debugLevel.INFO, onAnyLevel=True)
2018-09-10 20:53:05 -04:00
environment['runtime']['debug'].writeDebugOut('\/-------self.settingArgDict-------\/',debug.debugLevel.INFO, onAnyLevel=True)
environment['runtime']['debug'].writeDebugOut(str( self.settingArgDict) ,debug.debugLevel.INFO, onAnyLevel=True)
2019-02-12 17:44:57 -05:00
self.bindingsBackup = environment['bindings'].copy()
return environment