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

215 lines
9.3 KiB
Python
Raw Normal View History

#!/bin/python
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
2016-07-12 17:09:11 -04:00
import importlib.util
2016-07-26 17:39:22 -04:00
import os
2016-07-14 16:15:10 -04:00
from configparser import ConfigParser
from core import inputManager
2016-07-14 17:00:02 -04:00
from core import outputManager
2016-07-14 16:15:10 -04:00
from core import commandManager
from core import screenManager
2016-09-22 10:10:40 -04:00
from core import punctuationManager
2016-09-23 06:29:23 -04:00
from core import cursorManager
2016-09-23 03:48:19 -04:00
from core import applicationManager
2016-07-14 16:15:10 -04:00
from core import environment
from core.settings import settings
from core import debug
2016-07-08 06:24:44 -04:00
class settingsManager():
def __init__(self):
2016-07-12 17:09:11 -04:00
self.settings = settings
def initialize(self, environment):
self.env = environment
def shutdown(self):
pass
def loadShortcuts(self, kbConfigPath='../../config/keyboard/desktop.conf'):
2016-07-11 05:37:43 -04:00
kbConfig = open(kbConfigPath,"r")
while(True):
line = kbConfig.readline()
if not line:
break
line = line.replace('\n','')
if line.replace(" ","").startswith("#"):
continue
if line.count("=") != 1:
continue
sepLine = line.split('=')
commandName = sepLine[1].upper()
2016-09-15 04:10:01 -04:00
sepLine[0] = sepLine[0].replace(" ","")
sepLine[0] = sepLine[0].replace("'","")
sepLine[0] = sepLine[0].replace('"',"")
2016-09-16 17:34:20 -04:00
keys = sepLine[0].split(',')
2016-09-14 18:04:36 -04:00
shortcutKeys = []
shortcutRepeat = 1
shortcut = []
2016-07-11 05:37:43 -04:00
for key in keys:
2016-09-14 18:04:36 -04:00
try:
shortcutRepeat = int(key)
except:
2016-09-15 04:10:01 -04:00
shortcutKeys.append(key.upper())
2016-09-14 18:04:36 -04:00
shortcut.append(shortcutRepeat)
shortcut.append(sorted(shortcutKeys))
print(str(shortcut), commandName)
self.env['bindings'][str(shortcut)] = commandName
2016-07-11 05:37:43 -04:00
kbConfig.close()
def loadSoundIcons(self, soundIconPath):
2016-07-26 09:44:03 -04:00
siConfig = open(soundIconPath + '/soundicons.conf',"r")
while(True):
line = siConfig.readline()
if not line:
break
line = line.replace('\n','')
if line.replace(" ","").startswith("#"):
continue
if line.count("=") != 1:
continue
Values = line.split('=')
2016-09-17 14:08:56 -04:00
soundIcon = Values[0].upper()
2016-07-26 09:44:03 -04:00
Values[1] = Values[1].replace("'","")
Values[1] = Values[1].replace('"',"")
2016-09-15 04:19:58 -04:00
soundIconFile = ''
2016-07-26 09:44:03 -04:00
if os.path.exists(Values[1]):
2016-09-15 04:19:58 -04:00
soundIconFile = Values[1]
2016-07-26 09:44:03 -04:00
else:
2016-07-28 17:52:20 -04:00
if not soundIconPath.endswith("/"):
soundIconPath += '/'
2016-07-26 17:39:22 -04:00
if os.path.exists(soundIconPath + Values[1]):
2016-09-15 04:19:58 -04:00
soundIconFile = soundIconPath + Values[1]
self.env['soundIcons'][soundIcon] = soundIconFile
2016-07-26 09:44:03 -04:00
siConfig.close()
2016-08-21 17:26:19 -04:00
def loadSettings(self, settingConfigPath):
2016-09-14 18:04:36 -04:00
if not os.path.exists(settingConfigPath):
return False
self.env['settings'] = ConfigParser()
self.env['settings'].read(settingConfigPath)
return True
2016-07-11 05:37:43 -04:00
def setSetting(self, section, setting, value):
self.env['settings'].set(section, setting, value)
2016-07-25 13:48:03 -04:00
def getSetting(self, section, setting):
2016-07-12 17:09:11 -04:00
value = ''
try:
value = self.env['settings'].get(section, setting)
2016-07-12 17:09:11 -04:00
except:
value = str(self.settings[section][setting])
2016-07-11 05:37:43 -04:00
return value
2016-07-12 17:09:11 -04:00
def getSettingAsInt(self, section, setting):
2016-07-25 13:48:03 -04:00
value = 0
2016-07-17 08:25:59 -04:00
try:
value = self.env['settings'].getint(section, setting)
2016-07-17 08:25:59 -04:00
except:
value = self.settings[section][setting]
return value
2016-08-21 17:26:19 -04:00
def getSettingAsFloat(self, section, setting):
2016-07-25 13:48:03 -04:00
value = 0.0
2016-07-17 08:25:59 -04:00
try:
value = self.env['settings'].getfloat(section, setting)
2016-07-17 08:25:59 -04:00
except:
value = self.settings[section][setting]
return value
2016-08-21 17:26:19 -04:00
def getSettingAsBool(self, section, setting):
2016-07-25 13:48:03 -04:00
value = False
2016-07-17 08:25:59 -04:00
try:
value = self.env['settings'].getboolean(section, setting)
2016-07-17 08:25:59 -04:00
except:
value = self.settings[section][setting]
2016-08-21 17:26:19 -04:00
return value
def loadDriver(self, driverName, driverType):
if self.env['runtime'][driverType] != None:
print('shutdown %s',driverType)
self.env['runtime'][driverType].shutdown(self.env)
2016-09-14 18:04:36 -04:00
spec = importlib.util.spec_from_file_location(driverName, driverType + '/' + driverName + '.py')
2016-07-12 17:09:11 -04:00
driver_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(driver_mod)
self.env['runtime'][driverType] = driver_mod.driver()
self.env['runtime'][driverType].initialize(self.env)
def shutdownDriver(self, driverType):
if self.env['runtime'][driverType] == None:
return
self.env['runtime'][driverType].shutdown()
del self.env['runtime'][driverType]
2016-08-21 17:26:19 -04:00
def setFenrirKeys(self, keys):
2016-09-15 04:15:02 -04:00
keys = keys.upper()
2016-08-11 17:16:44 -04:00
keyList = keys.split(',')
for key in keyList:
if not key in self.env['input']['fenrirKey']:
self.env['input']['fenrirKey'].append(key)
2016-08-21 17:26:19 -04:00
2016-08-11 17:16:44 -04:00
def keyIDasString(self, key):
try:
KeyID = self.getCodeForKeyID(key)
return str(KeyID)
except:
2016-08-21 17:26:19 -04:00
return ''
2016-09-14 18:04:36 -04:00
def initFenrirConfig(self, environment = environment.environment, settingsRoot = '/etc/fenrir/', settingsFile='settings.conf'):
2016-09-15 04:10:01 -04:00
environment['runtime']['debug'] = debug.debug()
environment['runtime']['debug'].initialize(environment)
2016-09-14 17:27:19 -04:00
if not os.path.exists(settingsRoot):
if os.path.exists('../../config/'):
settingsRoot = '../../config/'
else:
2016-09-14 18:04:36 -04:00
return None
2016-09-15 04:10:01 -04:00
environment['runtime']['settingsManager'] = self
environment['runtime']['settingsManager'].initialize(environment)
validConfig = environment['runtime']['settingsManager'].loadSettings(settingsRoot + '/settings/' + settingsFile)
if not validConfig:
2016-09-14 18:04:36 -04:00
return None
self.setFenrirKeys(self.getSetting('general','fenrirKeys'))
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']['settingsManager'].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']['settingsManager'].loadShortcuts(self.getSetting('keyboard','keyboardLayout'))
2016-07-26 09:44:03 -04:00
else:
environment['runtime']['settingsManager'].loadShortcuts(self.getSetting('keyboard','keyboardLayout'))
2016-07-26 09:23:38 -04:00
if not os.path.exists(self.getSetting('sound','theme') + '/soundicons.conf'):
if os.path.exists(settingsRoot + 'sound/'+ self.getSetting('sound','theme')):
self.setSetting('sound', 'theme', settingsRoot + 'sound/'+ self.getSetting('sound','theme'))
if os.path.exists(settingsRoot + 'sound/'+ self.getSetting('sound','theme') + '/soundicons.conf'):
environment['runtime']['settingsManager'].loadSoundIcons(self.getSetting('sound','theme'))
2016-07-26 09:44:03 -04:00
else:
environment['runtime']['settingsManager'].loadSoundIcons(self.getSetting('sound','theme'))
2016-07-14 16:15:10 -04:00
2016-09-14 17:27:19 -04:00
environment['runtime']['inputManager'] = inputManager.inputManager()
environment['runtime']['inputManager'].initialize(environment)
2016-09-14 17:27:19 -04:00
environment['runtime']['outputManager'] = outputManager.outputManager()
environment['runtime']['outputManager'].initialize(environment)
2016-09-14 17:27:19 -04:00
environment['runtime']['commandManager'] = commandManager.commandManager()
environment['runtime']['commandManager'].initialize(environment)
2016-09-22 10:10:40 -04:00
environment['runtime']['punctuationManager'] = punctuationManager.punctuationManager()
environment['runtime']['punctuationManager'].initialize(environment)
2016-09-23 06:29:23 -04:00
environment['runtime']['cursorManager'] = cursorManager.cursorManager()
environment['runtime']['cursorManager'].initialize(environment)
2016-09-23 03:48:19 -04:00
environment['runtime']['applicationManager'] = applicationManager.applicationManager()
environment['runtime']['applicationManager'].initialize(environment)
if environment['runtime']['screenManager'] == None:
environment['runtime']['screenManager'] = screenManager.screenManager()
environment['runtime']['screenManager'].initialize(environment)
environment['runtime']['debug'].writeDebugOut('\/-------environment-------\/',debug.debugLevel.ERROR)
environment['runtime']['debug'].writeDebugOut(str(environment),debug.debugLevel.ERROR)
environment['runtime']['debug'].writeDebugOut('\/-------settings.conf-------\/',debug.debugLevel.ERROR)
environment['runtime']['debug'].writeDebugOut(str(environment['settings']._sections
2016-08-21 17:26:19 -04:00
),debug.debugLevel.ERROR)
2016-07-14 16:15:10 -04:00
return environment
2016-08-11 17:16:44 -04:00