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

267 lines
12 KiB
Python
Raw Normal View History

#!/bin/python
2016-07-12 11:13:59 -04:00
import evdev
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-07-14 16:15:10 -04:00
from core import environment
from core.settings import settings
from utils 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):
return environment
def shutdown(self, environment):
return environment
2016-07-26 09:12:42 -04:00
def loadShortcuts(self, environment, 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('=')
commandString = sepLine[1]
keys = sepLine[0].replace(" ","").split(',')
currShortcut = []
validKeyString = True
2016-08-11 17:16:44 -04:00
keyIdent = ''
2016-07-11 05:37:43 -04:00
for key in keys:
if len(key) < 3:
validKeyString = False
break
if not key[0] in ['0','1','2']:
validKeyString = False
break
if key[1] != '-':
validKeyString = False
break
if key[2:] != '':
2016-08-11 17:16:44 -04:00
if key[2:] == 'FENRIR':
keyIdent= 'FENRIR'
else:
keyInt = self.getCodeForKeyID(key[2:])
keyIdent = str(keyInt)
2016-07-11 05:37:43 -04:00
else:
validKeyString = False
break
2016-08-11 17:16:44 -04:00
if keyIdent == '':
2016-07-11 05:37:43 -04:00
validKeyString = False
2016-08-11 17:16:44 -04:00
break
2016-07-11 05:37:43 -04:00
if not validKeyString:
break
else:
2016-08-11 17:16:44 -04:00
currShortcut.append(key[0] + '-' + keyIdent)
2016-07-11 05:37:43 -04:00
if validKeyString:
keyString = ''
for k in sorted(currShortcut):
if keyString != '':
keyString += ','
keyString += k
2016-08-11 17:16:44 -04:00
environment['bindings'][keyString] = commandString
2016-07-11 05:37:43 -04:00
kbConfig.close()
return environment
def getCodeForKeyID(self, keyID):
try:
return evdev.ecodes.ecodes[keyID.upper()]
except:
return 0
2016-07-26 09:44:03 -04:00
def loadSoundIcons(self, environment, soundIconPath=''):
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('=')
if len(Values) > 2:
continue
soundIcon = Values[0]
Values[1] = Values[1].replace("'","")
Values[1] = Values[1].replace('"',"")
validSoundIcon = False
2016-07-28 17:52:20 -04:00
FilePath = ''
2016-07-26 09:44:03 -04:00
if os.path.exists(Values[1]):
FilePath = Values[1]
2016-07-28 17:52:20 -04:00
validSoundIcon = True
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]):
FilePath = soundIconPath + Values[1]
validSoundIcon = True
2016-07-26 09:44:03 -04:00
if validSoundIcon:
environment['soundIcons'][soundIcon] = FilePath
siConfig.close()
return environment
2016-08-21 17:26:19 -04:00
2016-07-26 09:12:42 -04:00
def loadSettings(self, environment, settingConfigPath='../../config/settings/settings.conf'):
2016-07-11 05:37:43 -04:00
environment['settings'] = ConfigParser()
2016-07-26 17:39:22 -04:00
#if not exist what is ?????
2016-07-11 05:37:43 -04:00
environment['settings'].read(settingConfigPath)
return environment
2016-07-25 13:48:03 -04:00
def setSetting(self, environment, section, setting, value):
environment['settings'].set(section, setting, value)
return environment
2016-07-12 17:09:11 -04:00
def getSetting(self, environment, section, setting):
value = ''
try:
value = environment['settings'].get(section, setting)
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, environment, section, setting):
2016-07-25 13:48:03 -04:00
value = 0
2016-07-17 08:25:59 -04:00
try:
value = environment['settings'].getint(section, setting)
except:
value = self.settings[section][setting]
return value
2016-08-21 17:26:19 -04:00
2016-07-14 17:25:33 -04:00
def getSettingAsFloat(self, environment, section, setting):
2016-07-25 13:48:03 -04:00
value = 0.0
2016-07-17 08:25:59 -04:00
try:
value = environment['settings'].getfloat(section, setting)
except:
value = self.settings[section][setting]
return value
2016-08-21 17:26:19 -04:00
2016-07-12 17:09:11 -04:00
def getSettingAsBool(self, environment, section, setting):
2016-07-25 13:48:03 -04:00
value = False
2016-07-17 08:25:59 -04:00
try:
value = environment['settings'].getboolean(section, setting)
except:
value = self.settings[section][setting]
2016-08-21 17:26:19 -04:00
return value
2016-07-12 17:09:11 -04:00
def loadSpeechDriver(self, environment, driverName):
if environment['runtime']['speechDriver'] != None:
environment['runtime']['speechDriver'].shutdown()
spec = importlib.util.spec_from_file_location(driverName, 'speech/' + driverName + '.py')
driver_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(driver_mod)
environment['runtime']['speechDriver'] = driver_mod.speech()
environment['runtime']['speechDriver'].initialize(environment)
2016-07-12 17:09:11 -04:00
return environment
def loadSoundDriver(self, environment, driverName):
if environment['runtime']['soundDriver'] != None:
environment['runtime']['soundDriver'].shutdown()
spec = importlib.util.spec_from_file_location(driverName, 'sound/' + driverName + '.py')
driver_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(driver_mod)
environment['runtime']['soundDriver'] = driver_mod.sound()
environment['runtime']['soundDriver'].initialize(environment)
2016-07-12 17:09:11 -04:00
return environment
def loadScreenDriver(self, environment, driverName):
spec = importlib.util.spec_from_file_location(driverName, 'screen/' + driverName + '.py')
driver_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(driver_mod)
environment['runtime']['screenDriver'] = driver_mod.screen()
environment['runtime']['screenDriver'].initialize(environment)
return environment
def loadInputDriver(self, environment, driverName):
spec = importlib.util.spec_from_file_location(driverName, 'input/' + driverName + '.py')
driver_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(driver_mod)
environment['runtime']['inputDriver'] = driver_mod.input()
environment['runtime']['inputDriver'].initialize(environment)
2016-08-21 17:26:19 -04:00
return environment
2016-08-11 17:16:44 -04:00
def setFenrirKeys(self, environment, keys):
keyList = keys.split(',')
for key in keyList:
keyID = self.keyIDasString( key)
if keyID != '':
if not keyID in environment['input']['fenrirKey']:
environment['input']['fenrirKey'].append(keyID)
return environment
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-07-14 16:15:10 -04:00
def initFenrirConfig(self):
return self.reInitFenrirConfig(environment.environment)
2016-07-12 17:09:11 -04:00
2016-07-26 17:39:22 -04:00
def reInitFenrirConfig(self, environment, settingsRoot = '../../config/'):
2016-08-21 17:26:19 -04:00
environment['runtime']['settingsManager'] = self
environment['runtime']['debug'] = debug.debug()
2016-07-14 16:15:10 -04:00
environment = environment['runtime']['settingsManager'].loadSettings(environment)
2016-08-11 17:16:44 -04:00
environment = self.setFenrirKeys(environment, self.getSetting(environment, 'general','fenrirKeys'))
2016-07-26 17:39:22 -04:00
if not os.path.exists(self.getSetting(environment, 'keyboard','keyboardLayout')):
if os.path.exists(settingsRoot + 'keyboard/' + self.getSetting(environment, 'keyboard','keyboardLayout')):
self.setSetting(environment, 'keyboard', 'keyboardLayout', settingsRoot + 'keyboard/' + self.getSetting(environment, 'keyboard','keyboardLayout'))
2016-07-26 09:44:03 -04:00
environment = environment['runtime']['settingsManager'].loadShortcuts(environment, self.getSetting('keyboard','keyboardLayout'))
2016-07-26 17:39:22 -04:00
if os.path.exists(settingsRoot + 'keyboard/' + self.getSetting(environment, 'keyboard','keyboardLayout') + '.conf'):
self.setSetting(environment, 'keyboard', 'keyboardLayout', settingsRoot + 'keyboard/' + self.getSetting(environment, 'keyboard','keyboardLayout') + '.conf')
environment = environment['runtime']['settingsManager'].loadShortcuts(environment, self.getSetting(environment, 'keyboard','keyboardLayout'))
2016-07-26 09:44:03 -04:00
else:
2016-07-26 17:39:22 -04:00
environment = environment['runtime']['settingsManager'].loadShortcuts(environment, self.getSetting(environment, 'keyboard','keyboardLayout'))
2016-07-26 09:23:38 -04:00
2016-07-26 17:39:22 -04:00
if not os.path.exists(self.getSetting(environment, 'sound','theme') + '/soundicons.conf'):
if os.path.exists(settingsRoot + 'sound/'+ self.getSetting(environment, 'sound','theme')):
self.setSetting(environment, 'sound', 'theme', settingsRoot + 'sound/'+ self.getSetting(environment, 'sound','theme'))
if os.path.exists(settingsRoot + 'sound/'+ self.getSetting(environment, 'sound','theme') + '/soundicons.conf'):
environment = environment['runtime']['settingsManager'].loadSoundIcons(environment, self.getSetting(environment, 'sound','theme'))
2016-07-26 09:44:03 -04:00
else:
2016-07-26 17:39:22 -04:00
environment = environment['runtime']['settingsManager'].loadSoundIcons(environment, self.getSetting(environment, 'sound','theme'))
2016-07-14 16:15:10 -04:00
if environment['runtime']['inputManager'] == None:
environment['runtime']['inputManager'] = inputManager.inputManager()
environment = environment['runtime']['inputManager'].initialize(environment)
if environment['runtime']['outputManager'] == None:
environment['runtime']['outputManager'] = outputManager.outputManager()
environment = environment['runtime']['outputManager'].initialize(environment)
if environment['runtime']['commandManager'] == None:
environment['runtime']['commandManager'] = commandManager.commandManager()
environment = environment['runtime']['commandManager'].initialize(environment)
if environment['runtime']['screenManager'] == None:
environment['runtime']['screenManager'] = screenManager.screenManager()
environment = environment['runtime']['screenManager'].initialize(environment)
2016-07-14 16:15:10 -04:00
environment = environment['runtime']['commandManager'].loadCommands(environment,'commands')
environment = environment['runtime']['commandManager'].loadCommands(environment,'onInput')
environment = environment['runtime']['commandManager'].loadCommands(environment,'onScreenChanged')
2016-08-21 17:26:19 -04:00
2016-07-14 16:15:10 -04:00
environment = environment['runtime']['settingsManager'].loadSpeechDriver(environment,\
environment['runtime']['settingsManager'].getSetting(environment,'speech', 'driver'))
environment = environment['runtime']['settingsManager'].loadScreenDriver(environment,\
environment['runtime']['settingsManager'].getSetting(environment,'screen', 'driver'))
environment = environment['runtime']['settingsManager'].loadSoundDriver(environment,\
environment['runtime']['settingsManager'].getSetting(environment,'sound', 'driver'))
environment = environment['runtime']['settingsManager'].loadInputDriver(environment,\
environment['runtime']['settingsManager'].getSetting(environment,'keyboard', 'driver'))
2016-08-21 17:26:19 -04:00
environment['runtime']['debug'].writeDebugOut(environment,'\/-------environment-------\/',debug.debugLevel.ERROR)
environment['runtime']['debug'].writeDebugOut(environment,str(environment),debug.debugLevel.ERROR)
environment['runtime']['debug'].writeDebugOut(environment,'\/-------settings.conf-------\/',debug.debugLevel.ERROR)
environment['runtime']['debug'].writeDebugOut(environment,str(environment['settings']._sections
),debug.debugLevel.ERROR)
2016-07-14 16:15:10 -04:00
return environment
2016-08-11 17:16:44 -04:00