def testValueAddExistingSection(self): TEST_FILE_NAME = self._filenames[0] config.set(TEST_FILE_NAME, "Test") settings.get(TEST_FILE_NAME, "Test", "test_1", force=True).set("1") self.assertEqual(settings._META_OPTION_VALUE, config.get(TEST_FILE_NAME, "Test", settings._META_OPTION_NAME)) self.assertEqual("1", config.get(TEST_FILE_NAME, "Test", "test_1"))
def testValueGet(self): self._setUpTestFiles() self.assertTrue(self._filenames[1] in config.get()) self.assertTrue('Test_4' in config.get(self._filenames[1])) self.assertEqual(['test4_1', 'test4_2', 'test4_3', 'test4_4'], sorted(config.get(self._filenames[1], 'Test_4'))) self.assertEqual('44', config.get(self._filenames[1], 'Test_4', 'test4_2'))
def testAddSettingOption(self): TEST_FILE_NAME = self._filenames[0] config.set(TEST_FILE_NAME, "Test", "test_1", "1") self.assertFalse(settings._META_OPTION_NAME in config.get( TEST_FILE_NAME, "Test")) settings.get(TEST_FILE_NAME, "Test", force=True) self.assertTrue(settings._META_OPTION_NAME in config.get( TEST_FILE_NAME, "Test"))
def testSet(self): TEST_FILE_NAME = self._filenames[0] settings.set(TEST_FILE_NAME, "Test0") settings.set(TEST_FILE_NAME, "Test1", "test1_1", "1") self.assertTrue("Test0" in config.get(TEST_FILE_NAME)) self.assertTrue("Test1" in config.get(TEST_FILE_NAME)) self.assertTrue("test1_1" in config.get(TEST_FILE_NAME, "Test1")) self.assertEqual("1", config.get(TEST_FILE_NAME, "Test1", "test1_1"))
def testValueRemove(self): TEST_FILE_NAME = self._filenames[0] config.set(TEST_FILE_NAME, "Test", settings._META_OPTION_NAME, settings._META_OPTION_VALUE) config.set(TEST_FILE_NAME, "Test", "test_1", "1") item = settings.get(TEST_FILE_NAME, "Test", "test_1") self.assertTrue("test_1" in config.get(TEST_FILE_NAME, "Test")) item.remove() self.assertFalse("test_1" in config.get(TEST_FILE_NAME, "Test"))
def testValueAddToNewFile(self): TEST_FILE_NAME = self._filenames[0] self.assertFalse("Test" in config.get(TEST_FILE_NAME)) item = settings.get(TEST_FILE_NAME, "Test", "test_1", force=True) self.assertTrue("Test" in config.get(TEST_FILE_NAME)) item.set("1") self.assertEqual(settings._META_OPTION_VALUE, config.get(TEST_FILE_NAME, "Test", settings._META_OPTION_NAME)) self.assertEqual("1", config.get(TEST_FILE_NAME, "Test", "test_1"))
def testOptionsEqual(self): TEST_FILE_NAME0 = self._filenames[0] TEST_FILE_NAME1 = self._filenames[1] value = "item" config.set(TEST_FILE_NAME0, "Test0", "option0", value) config.set(TEST_FILE_NAME0, "Test0", settings._META_OPTION_NAME, settings._META_OPTION_VALUE) config.set(TEST_FILE_NAME1, "Test1", "option1", value) config.set(TEST_FILE_NAME1, "Test1", settings._META_OPTION_NAME, settings._META_OPTION_VALUE) self.assertTrue(config.get(TEST_FILE_NAME0, "Test0", "option0")== config.get(TEST_FILE_NAME1, "Test1", "option1"))
def testValueAddWrongSettOptVal(self): TEST_FILE_NAME = self._filenames[0] config.set(TEST_FILE_NAME, "Test", settings._META_OPTION_NAME, "Test") self.assertNotEqual(settings._META_OPTION_VALUE, config.get(TEST_FILE_NAME, "Test", settings._META_OPTION_NAME)) item = settings.get(TEST_FILE_NAME, "Test", force=True) item["test_1"] = "1" self.assertTrue(settings._META_OPTION_NAME in config.get( TEST_FILE_NAME, "Test")) self.assertEqual(settings._META_OPTION_VALUE, config.get( TEST_FILE_NAME, "Test", settings._META_OPTION_NAME)) self.assertEqual("1", config.get(TEST_FILE_NAME, "Test", "test_1"))
def _isSettings(name, section=None, option=None): ''' Checks if the given configuration or section, or option is a settings. ''' if option and option == _META_OPTION_NAME: return False if section: return config.get(name, section, _META_OPTION_NAME)== _META_OPTION_VALUE for section in config.get(name): if _isSettings(name, section): return True return False
def _isSettings(name, section=None, option=None): ''' Checks if the given configuration or section, or option is a settings. ''' if option and option == _META_OPTION_NAME: return False if section: return config.get(name, section, _META_OPTION_NAME) == _META_OPTION_VALUE for section in config.get(name): if _isSettings(name, section): return True return False
def testValueGetNotSettings(self): TEST_FILE_NAME = self._filenames[0] config.set(TEST_FILE_NAME, "Test", "test_1", "1") config.set(TEST_FILE_NAME, "Test", "test_2", "11") self.assertEqual(None, settings.get(TEST_FILE_NAME, "Test", "test_1")) self.assertEqual("1", settings.get(TEST_FILE_NAME, "Test", "test_1", force=True).get()) self.assertTrue(settings._META_OPTION_NAME in config.get( TEST_FILE_NAME, "Test")) self.assertEqual(config.get( TEST_FILE_NAME, "Test", settings._META_OPTION_NAME), settings._META_OPTION_VALUE)
def _loadState(self): """ Loads window's settings from configuration. """ log.debug("Loading main window's settings") if self._views: index = config.getInt(self._NAME, "views", "last", 0) view = self._views[0] try: view = self._views[index] except IndexError: log.error("Failed to load view #%d" % index) view.activate() section = "geometry" self.window.setGeometry( config.getInt(self._NAME, section, "window_x", 50), config.getInt(self._NAME, section, "window_y", 50), config.getInt(self._NAME, section, "window_w", 800), config.getInt(self._NAME, section, "window_h", 600), ) state = config.get(self._NAME, section, "window_state") if state: byteData = QtCore.QByteArray.fromBase64(state) self.window.restoreState(byteData)
def set(name, section, option=None, value=None, force=False): ''' Sets different settings values depending on number of given arguments: - if settings section, option and value are given then sets the specified option to the given value, - if only settings section and option are given then adds the specified option to the section, if it does not exist, - if only settings section is given then adds the specified section to the configuration file, if it does not exist yet. :param name: The name of configuration :type name: string :param section: The name of the section :type section: string :param option: The name of the option :type option: string :param value: The new value of the specific option :type value: string or integer or boolean :param force: Makes a configuration section to be a settings section :type force: boolean :return: True if success, False otherwise :rtype: boolean ''' if force or section not in config.get(name) or _isSettings(name, section): _setSettings(name, section) config.set(name, section, option, value) return True return False
def __init__(self, conf=None): ''' Initializes daemon by starting and configuring a server. :param conf: A path to a daemon configuration file :type conf: string ''' if conf is not None: if os.path.isfile(conf): config.update('daemon', conf) else: log.warning("Configuration file %s does not exist." " Skipped" % conf) port = config.getInt('daemon', 'connection', 'port') ip = config.get('daemon', 'connection', 'address') if ip is None: log.warning("No attribute IP in daemon configuration file. " "Using default value %s" % DEFAULT_IP) config.set('daemon', 'connection', 'address', DEFAULT_IP) ip = DEFAULT_IP if port is None: log.warning("No attribute port in daemon configuration file. " "Using default value %d" % DEFAULT_PORT) config.set('daemon', 'connection', 'port', DEFAULT_PORT) port = DEFAULT_PORT server.Server.__init__(self, (ip, port)) info = protocol.create(protocol.MSG_TYPE_RESPONSE, protocol.MSG_TARGET_SYSTEM, protocol.MSG_NAME_INFO, version=config.VERSION, locale=(locale.getdefaultlocale()[0] or ''), extensions=protocol.getExtensions(), status=True) self._infoData = info.marshal()
def testOptionValueEqual(self): TEST_FILE_NAME = self._filenames[0] value = "item" config.set(TEST_FILE_NAME, "Test", "option", value) config.set(TEST_FILE_NAME, "Test", settings._META_OPTION_NAME, settings._META_OPTION_VALUE) self.assertTrue(config.get(TEST_FILE_NAME, "Test", "option")==value)
def testRemoveSettings(self): TEST_FILE_NAME = self._filenames[0] config.set(TEST_FILE_NAME, "Test", settings._META_OPTION_NAME, settings._META_OPTION_VALUE) config.set(TEST_FILE_NAME, "Test", "test_1", "1") item_1 = settings.get(TEST_FILE_NAME, "Test") item_1.remove() self.assertFalse("Test" in config.get(TEST_FILE_NAME))
def get(name=None, section=None, option=None, default=None, force=False): ''' Returns different results depending on number of given arguments: - if settings name, section and option are given then returns a SettingsOption object representing the specified option, - if only settings name and section are given then returns a SettingsSection object representing the specified section, - if only settings name is given then returns a list of available sections in the configuration file, - if no argument is given then returns a list of available settings. :param name: A name of configuration :type name: string :param section: A name of the section :type section: string :param option: A name of the option :type option: string :param default: An optional argument that can specify a default value :type default: string :param force: Makes a configuration section to be a settings section :type force: boolean :return: A list of settings, SettingsSection or SettingsOption object :rtype: list or SettingsSection or SettingsOption ''' if force and section: _setSettings(name, section) if option: if _isSettings(name, section, option): return SettingsOption(name, section, option, default) return default elif section: if _isSettings(name, section): return SettingsSection(name, section) return None elif name: sections = [] for section in config.get(name): if force or _isSettings(name, section): sections.append(section) return sections else: names = [] for name in config.get(): if force or _isSettings(name): names.append(name) return names
def testRemove(self): TEST_FILE_NAME = self._filenames[0] config.set(TEST_FILE_NAME, "Test0", "test0_1", "10") config.set(TEST_FILE_NAME, "Test0", "test0_2", "110") config.set(TEST_FILE_NAME, "Test1", "test1_1", "1") config.set(TEST_FILE_NAME, "Test1", "test1_2", "11") config.set(TEST_FILE_NAME, "Test1", settings._META_OPTION_NAME, settings._META_OPTION_VALUE) settings.remove(TEST_FILE_NAME, "Test0", "test0_2") settings.remove(TEST_FILE_NAME, "Test1", "test1_2") self.assertFalse("test0_2" in config.get(TEST_FILE_NAME, "Test0")) self.assertFalse("test1_2" in config.get(TEST_FILE_NAME, "Test1")) settings.remove(TEST_FILE_NAME, "Test0") settings.remove(TEST_FILE_NAME, "Test1") self.assertFalse("Test0" in config.get(TEST_FILE_NAME)) self.assertFalse("Test1" in config.get(TEST_FILE_NAME)) settings.remove(TEST_FILE_NAME)
def get(self): ''' Gets a value of the settings option. If the option does not exist the 'None' is returned. :return: A value of the option :rtype: string ''' return config.get(self._name, self._section, self._option, self._default)
def __iter__(self): ''' Iterates over all settings option of the section. :return: Option :rtype: SettingsOption ''' for option in config.get(self._name, self._section): if option != _META_OPTION_NAME: yield SettingsOption(self._name, self._section, option)
def loadState(self): ''' Loads the view's settings from configuration. ''' log.debug("Loading settings of view: %s" % self.NAME) for name in filter(lambda name: name.lower().find("splitter") >= 0, self._elements): state = config.get(self.NAME, self._CONFIG_SECTION_GEOMETRY, name.lower()) if state: state = QtCore.QByteArray.fromBase64(state) self._elements[name].restoreState(state)
def __init__(self, view): QtCore.QObject.__init__(self, view) self._elements = view.loadUi(self._DIALOG_UI) self.dialog = self._elements['dialog'] self._character = self._elements['comboBoxChar'] self._keycode = self._elements['lineEditKeycode'] self._addMod = self._elements['buttonAdd'] self._removeMod = self._elements['buttonRemove'] self._ok = self._elements['buttonBox'].button( QtGui.QDialogButtonBox.Ok) self._modifiers = self._elements['listWidget'] self._addEditDialog = AddEditDialog(view) self._character.editTextChanged.connect(self._update) self._keycode.textEdited.connect(self._clearAndDisable) self._ok.clicked.connect(self._execute) self._addEditDialog.modifierAccepted.connect(self._addModifier) self._modifiers.setSortingEnabled(True) self._modifiers.itemActivated.connect(self._deselectItem) self._modifiers.itemSelectionChanged.connect(self._switchButtons) self._addMod.clicked.connect(self._showAddEditDialog) self._removeMod.clicked.connect(self._removeModifier) for name, code in constants.KEY_SYMS.iteritems(): prettyName = ' '.join([s.capitalize() for s in name.split('_')]) self._character.addItem(prettyName, userData=str(code)) self._character.setCompleter(None) self._keycode.setValidator(QtGui.QIntValidator(self.dialog)) for name in sorted(self._MODIFIERS): self._createItem(name, self._MODIFIERS[name]) self._userModifiers = [] for name in config.get(self._CONFIG_NAME, self._CONFIG_SECTION): code = config.getInt(self._CONFIG_NAME, self._CONFIG_SECTION, name) if code is not None and code not in self._userModifiers: self._createItem(name, code) self._userModifiers.append(name) self._deviceTab = None
def __init__(self, view): QtCore.QObject.__init__(self, view) self._elements = view.loadUi(self._DIALOG_UI) self.dialog = self._elements['dialog'] self._character = self._elements['comboBoxChar'] self._keycode = self._elements['lineEditKeycode'] self._addMod = self._elements['buttonAdd'] self._removeMod = self._elements['buttonRemove'] self._ok = self._elements['buttonBox'].button(QtGui.QDialogButtonBox.Ok) self._modifiers = self._elements['listWidget'] self._addEditDialog = AddEditDialog(view) self._character.editTextChanged.connect(self._update) self._keycode.textEdited.connect(self._clearAndDisable) self._ok.clicked.connect(self._execute) self._addEditDialog.modifierAccepted.connect(self._addModifier) self._modifiers.setSortingEnabled(True) self._modifiers.itemActivated.connect(self._deselectItem) self._modifiers.itemSelectionChanged.connect(self._switchButtons) self._addMod.clicked.connect(self._showAddEditDialog) self._removeMod.clicked.connect(self._removeModifier) for name, code in constants.KEY_SYMS.iteritems(): prettyName = ' '.join([s.capitalize() for s in name.split('_')]) self._character.addItem(prettyName, userData=str(code)) self._character.setCompleter(None) self._keycode.setValidator(QtGui.QIntValidator(self.dialog)) for name in sorted(self._MODIFIERS): self._createItem(name, self._MODIFIERS[name]) self._userModifiers = [] for name in config.get(self._CONFIG_NAME, self._CONFIG_SECTION): code = config.getInt(self._CONFIG_NAME, self._CONFIG_SECTION, name) if code is not None and code not in self._userModifiers: self._createItem(name, code) self._userModifiers.append(name) self._deviceTab = None
def _loadState(self): ''' Loads window's settings from configuration. ''' log.debug("Loading main window's settings") if self._views: index = config.getInt(self._NAME, "views", "last", 0) view = self._views[0] try: view = self._views[index] except IndexError: log.error("Failed to load view #%d" % index) view.activate() section = "geometry" self.window.setGeometry( config.getInt(self._NAME, section, "window_x", 50), config.getInt(self._NAME, section, "window_y", 50), config.getInt(self._NAME, section, "window_w", 800), config.getInt(self._NAME, section, "window_h", 600)) state = config.get(self._NAME, section, "window_state") if state: byteData = QtCore.QByteArray.fromBase64(state) self.window.restoreState(byteData)