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

110 lines
4.1 KiB
Python
Raw Normal View History

#!/bin/python
2016-07-11 05:37:43 -04:00
from configparser import ConfigParser
2016-07-11 18:17:44 -04:00
from core.settings import settings
2016-07-12 11:13:59 -04:00
import evdev
2016-07-12 17:09:11 -04:00
import importlib.util
2016-07-08 06:24:44 -04:00
class settingsManager():
def __init__(self):
2016-07-12 17:09:11 -04:00
self.settings = settings
2016-07-11 05:37:43 -04:00
def loadShortcuts(self, environment, kbConfigPath='../../config/keyboard/desktop.kb'):
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
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:] != '':
keyInt = self.getCodeForKeyID(key[2:])
else:
validKeyString = False
break
if keyInt == 0:
validKeyString = False
break
if not validKeyString:
break
else:
currShortcut.append(key[0] + '-' + str(keyInt))
if validKeyString:
keyString = ''
for k in sorted(currShortcut):
if keyString != '':
keyString += ','
keyString += k
environment['bindings'][keyString] = commandString
kbConfig.close()
return environment
def getCodeForKeyID(self, keyID):
try:
return evdev.ecodes.ecodes[keyID.upper()]
except:
return 0
def loadSettings(self, environment, settingConfigPath='../../config/settings/settings.cfg'):
environment['settings'] = ConfigParser()
environment['settings'].read(settingConfigPath)
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 = 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):
return int(getSetting(self, environment, section, setting))
def getSettingAsBool(self, environment, section, setting):
return bool(getSetting(self, environment, section, setting))
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()
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()
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()
return environment
2016-07-11 05:37:43 -04:00