diff --git a/keymapper/config.py b/keymapper/config.py index 85f98dca..2ccff25f 100644 --- a/keymapper/config.py +++ b/keymapper/config.py @@ -27,7 +27,8 @@ import os from keymapper.logger import logger -_config = None +# one config file per preset, one folder per device +_configs = {} _defaults = {} @@ -72,18 +73,23 @@ def _modify_config(config_contents, key, value): class Config: """Read and set config values.""" - def __init__(self, path=None): + def __init__(self, device, preset, path=None): """Initialize the interface to the config file. Parameters ---------- path : string or None - If none, will default to '~/.config/key-mapper/config' + If none, will default to '~/.config/key-mapper/'. + In that directory, a folder for the device and a file for + the preset will be created. """ if path is None: - path = os.path.expanduser('~/.config/key-mapper/config') + path = os.path.expanduser('~/.config/key-mapper/') + path = os.path.join(path, device, preset) logger.debug('Using config file at %s', path) + self.device = device + self.preset = preset self._path = path self._config = {} self.mtime = 0 @@ -172,15 +178,16 @@ class Config: return True -def get_config(*args, **kwargs): +def get_config(device, preset, path): """Ask for the config. Initialize it if not yet done so. - - Will pass any parameters to the config constructor. Only needed in tests - to avoid writing the users config. """ # don't initialize it right away in the global scope, to avoid having # the wrong logging verbosity. - global _config - if _config is None: - _config = Config(*args, **kwargs) - return _config + global _configs + if _configs.get(device) is None: + _configs[device] = {} + + if _configs[device].get(preset) is None: + _configs[device][preset] = Config(device, preset, path) + + return _configs[device][preset] diff --git a/keymapper/util.py b/keymapper/devices.py similarity index 92% rename from keymapper/util.py rename to keymapper/devices.py index 42efd2d9..bd171932 100644 --- a/keymapper/util.py +++ b/keymapper/devices.py @@ -29,3 +29,8 @@ def find_devices(): """Get a list of (id, name) for each input device.""" # `xinput list` pass + + +def get_presets(device): + """Get all configured presets for the device""" + pass diff --git a/tests/testcases/config.py b/tests/testcases/config.py index 2b84ee21..3164ce67 100644 --- a/tests/testcases/config.py +++ b/tests/testcases/config.py @@ -19,9 +19,11 @@ # along with key-mapper. If not, see . +import os import unittest -from keymapper.config import _modify_config +from keymapper.logger import update_verbosity +from keymapper.config import _modify_config, get_config class ConfigTest(unittest.TestCase): @@ -40,6 +42,20 @@ class ConfigTest(unittest.TestCase): contents = _modify_config(contents, 'test', '1234') self.assertEqual(contents, """a=1\n # test=3\n abc=123\ntest=1234""") + def test_get_config(self): + update_verbosity(True) + + config = get_config('device1', 'preset1', '/tmp/key-mapper') + self.assertEqual(config.device, 'device1') + self.assertEqual(config.preset, 'preset1') + self.assertTrue(os.path.isfile('/tmp/key-mapper/device1/preset1')) + + get_config('device1', 'preset2', '/tmp/key-mapper') + self.assertTrue(os.path.isfile('/tmp/key-mapper/device1/preset2')) + + get_config('device2', 'preset3', '/tmp/key-mapper') + self.assertTrue(os.path.isfile('/tmp/key-mapper/device2/preset3')) + if __name__ == "__main__": unittest.main() diff --git a/tests/util.py b/tests/util.py deleted file mode 100644 index 8c367426..00000000 --- a/tests/util.py +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/python3 -# -*- coding: utf-8 -*- - -"""utils used by tests""" - - -from soundconverter.util.settings import settings - - -DEFAULT_SETTINGS = settings.copy() - - -def reset_settings(): - """Reset the global settings to their initial state.""" - global settings - # convert to list otherwise del won't work - for key in list(settings.keys()): - if key in DEFAULT_SETTINGS: - settings[key] = DEFAULT_SETTINGS[key] - else: - del settings[key] - # batch tests assume that recursive is off by default: - assert (("recursive" not in settings) or (not settings["recursive"]))