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

216 lines
10 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('=')
2016-09-14 18:04:36 -04:00
commandName = sepLine[1]
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))
environment['bindings'][str(shortcut)] = commandName
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
2016-09-15 04:10:01 -04:00
def loadSoundIcons(self, environment, 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('=')
soundIcon = Values[0]
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]
environment['soundIcons'][soundIcon] = soundIconFile
2016-07-26 09:44:03 -04:00
siConfig.close()
return environment
2016-08-21 17:26:19 -04:00
2016-09-14 18:04:36 -04:00
def loadSettings(self, environment, settingConfigPath):
if not os.path.exists(settingConfigPath):
return None
2016-07-11 05:37:43 -04:00
environment['settings'] = ConfigParser()
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-09-14 18:04:36 -04:00
def loadDriver(self, environment, driverName, driverType):
if environment['runtime'][driverType] != None:
environment['runtime'][driverType].shutdown()
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)
2016-09-14 18:04:36 -04:00
environment['runtime'][driverType] = driver_mod.driver()
environment['runtime'][driverType].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):
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:
2016-09-15 04:15:02 -04:00
if not key in environment['input']['fenrirKey']:
environment['input']['fenrirKey'].append(key)
2016-08-11 17:16:44 -04:00
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-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()
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
2016-09-14 18:04:36 -04:00
environment['runtime']['settingsManager'] = self
2016-09-14 17:30:00 -04:00
environment = environment['runtime']['settingsManager'].loadSettings(environment, settingsRoot + '/settings/' + settingsFile)
2016-09-14 18:04:36 -04:00
if environment == None:
return None
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
2016-09-14 17:27:19 -04:00
environment['runtime']['inputManager'] = inputManager.inputManager()
environment = environment['runtime']['inputManager'].initialize(environment)
environment['runtime']['outputManager'] = outputManager.outputManager()
environment = environment['runtime']['outputManager'].initialize(environment)
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-09-14 18:04:36 -04:00
environment = environment['runtime']['settingsManager'].loadDriver(environment,\
environment['runtime']['settingsManager'].getSetting(environment,'speech', 'driver'), 'speechDriver')
environment = environment['runtime']['settingsManager'].loadDriver(environment,\
environment['runtime']['settingsManager'].getSetting(environment,'screen', 'driver'), 'screenDriver')
environment = environment['runtime']['settingsManager'].loadDriver(environment,\
environment['runtime']['settingsManager'].getSetting(environment,'sound', 'driver'), 'soundDriver')
environment = environment['runtime']['settingsManager'].loadDriver(environment,\
environment['runtime']['settingsManager'].getSetting(environment,'keyboard', 'driver'), 'inputDriver')
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