creating config structure

xkb
sezanzeb 4 years ago committed by sezanzeb
parent f50a7063e8
commit a3f814f063

@ -27,7 +27,8 @@ import os
from keymapper.logger import logger from keymapper.logger import logger
_config = None # one config file per preset, one folder per device
_configs = {}
_defaults = {} _defaults = {}
@ -72,18 +73,23 @@ def _modify_config(config_contents, key, value):
class Config: class Config:
"""Read and set config values.""" """Read and set config values."""
def __init__(self, path=None): def __init__(self, device, preset, path=None):
"""Initialize the interface to the config file. """Initialize the interface to the config file.
Parameters Parameters
---------- ----------
path : string or None 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: 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) logger.debug('Using config file at %s', path)
self.device = device
self.preset = preset
self._path = path self._path = path
self._config = {} self._config = {}
self.mtime = 0 self.mtime = 0
@ -172,15 +178,16 @@ class Config:
return True return True
def get_config(*args, **kwargs): def get_config(device, preset, path):
"""Ask for the config. Initialize it if not yet done so. """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 # don't initialize it right away in the global scope, to avoid having
# the wrong logging verbosity. # the wrong logging verbosity.
global _config global _configs
if _config is None: if _configs.get(device) is None:
_config = Config(*args, **kwargs) _configs[device] = {}
return _config
if _configs[device].get(preset) is None:
_configs[device][preset] = Config(device, preset, path)
return _configs[device][preset]

@ -29,3 +29,8 @@ def find_devices():
"""Get a list of (id, name) for each input device.""" """Get a list of (id, name) for each input device."""
# `xinput list` # `xinput list`
pass pass
def get_presets(device):
"""Get all configured presets for the device"""
pass

@ -19,9 +19,11 @@
# along with key-mapper. If not, see <https://www.gnu.org/licenses/>. # along with key-mapper. If not, see <https://www.gnu.org/licenses/>.
import os
import unittest 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): class ConfigTest(unittest.TestCase):
@ -40,6 +42,20 @@ class ConfigTest(unittest.TestCase):
contents = _modify_config(contents, 'test', '1234') contents = _modify_config(contents, 'test', '1234')
self.assertEqual(contents, """a=1\n # test=3\n abc=123\ntest=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__": if __name__ == "__main__":
unittest.main() unittest.main()

@ -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"]))
Loading…
Cancel
Save