input-remapper/keymapper/config.py

182 lines
5.0 KiB
Python
Raw Normal View History

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# key-mapper - GUI for device specific keyboard mappings
# Copyright (C) 2020 sezanzeb <proxima@hip70890b.de>
#
# This file is part of key-mapper.
#
# key-mapper is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# key-mapper is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with key-mapper. If not, see <https://www.gnu.org/licenses/>.
"""Store which presets should be enabled for which device on login."""
import os
import json
2020-11-20 20:38:19 +00:00
import shutil
import copy
from keymapper.paths import CONFIG, USER, touch
from keymapper.logger import logger
CONFIG_PATH = os.path.join(CONFIG, 'config')
INITIAL_CONFIG = {
2020-11-28 21:54:22 +00:00
'autoload': {},
'macros': {
# some time between keystrokes might be required for them to be
# detected properly in software.
'keystroke_sleep_ms': 10
2020-11-30 19:57:09 +00:00
},
'gamepad': {
2020-12-02 18:33:31 +00:00
'joystick': {
'non_linearity': 4,
'pointer_speed': 80,
},
2020-11-28 21:54:22 +00:00
}
}
2020-11-20 20:38:19 +00:00
class _Config:
def __init__(self):
self._config = {}
self.load_config()
2020-12-02 18:33:31 +00:00
def _resolve(self, path, func, config=None):
2020-11-30 19:57:09 +00:00
"""Call func for the given config value."""
chunks = path.split('.')
2020-12-02 18:33:31 +00:00
if config is None:
child = self._config
else:
child = config
2020-11-30 19:57:09 +00:00
while True:
chunk = chunks.pop(0)
parent = child
child = child.get(chunk)
if len(chunks) == 0:
# child is the value _resolve is looking for
return func(parent, child, chunk)
2020-12-01 23:02:41 +00:00
# child is another object
if child is None:
parent[chunk] = {}
child = parent[chunk]
2020-11-30 19:57:09 +00:00
def remove(self, path):
"""Remove a config key.
Parameters
----------
path : string
For example 'macros.keystroke_sleep_ms'
"""
def do(parent, child, chunk):
if child is not None:
del parent[chunk]
self._resolve(path, do)
def set(self, path, value):
"""Set a config key.
Parameters
----------
path : string
For example 'macros.keystroke_sleep_ms'
value : any
"""
def do(parent, child, chunk):
parent[chunk] = value
self._resolve(path, do)
2020-12-02 18:33:31 +00:00
def get(self, path, log_unknown=True):
"""Get a config value. If not set, return the default
2020-11-30 19:57:09 +00:00
Parameters
----------
path : string
For example 'macros.keystroke_sleep_ms'
2020-12-02 18:33:31 +00:00
log_unknown : bool
If True, write an error.
2020-11-30 19:57:09 +00:00
"""
2020-12-02 18:33:31 +00:00
def do(parent, child, chunk):
return child
resolved = self._resolve(path, do)
if resolved is None:
resolved = self._resolve(path, do, INITIAL_CONFIG)
if resolved is None and log_unknown:
logger.error('Unknown config key "%s"', path)
return resolved
2020-11-30 19:57:09 +00:00
def set_autoload_preset(self, device, preset):
"""Set a preset to be automatically applied on start.
Parameters
----------
device : string
preset : string or None
2020-12-02 18:33:31 +00:00
if None, don't autoload something for this device.
2020-11-30 19:57:09 +00:00
"""
if preset is not None:
self.set(f'autoload.{device}', preset)
else:
self.remove(f'autoload.{device}')
2020-11-28 21:54:22 +00:00
2020-11-20 20:38:19 +00:00
def iterate_autoload_presets(self):
2020-11-26 20:33:31 +00:00
"""Get tuples of (device, preset)."""
return self._config.get('autoload', {}).items()
2020-11-20 20:38:19 +00:00
2020-11-26 20:33:31 +00:00
def is_autoloaded(self, device, preset):
"""Should this preset be loaded automatically?"""
2020-12-02 18:33:31 +00:00
return self.get(f'autoload.{device}', '') == preset
2020-11-26 20:33:31 +00:00
2020-11-20 20:38:19 +00:00
def load_config(self):
"""Load the config from the file system."""
self.clear_config()
2020-11-20 20:38:19 +00:00
if not os.path.exists(CONFIG_PATH):
# treated like an empty config
2020-12-02 18:33:31 +00:00
logger.debug('Config "%s" doesn\'t exist yet', CONFIG_PATH)
self.clear_config()
self.save_config()
2020-11-20 20:38:19 +00:00
return
2020-11-22 20:41:29 +00:00
with open(CONFIG_PATH, 'r') as file:
self._config.update(json.load(file))
logger.info('Loaded config from "%s"', CONFIG_PATH)
2020-11-20 20:38:19 +00:00
def clear_config(self):
"""Reset the configuration to the initial values."""
2020-11-20 20:38:19 +00:00
self._config = copy.deepcopy(INITIAL_CONFIG)
def save_config(self):
"""Save the config to the file system."""
touch(CONFIG_PATH)
2020-11-21 14:00:52 +00:00
2020-11-22 20:41:29 +00:00
with open(CONFIG_PATH, 'w') as file:
json.dump(self._config, file, indent=4)
2020-11-20 20:38:19 +00:00
logger.info('Saved config to %s', CONFIG_PATH)
2020-12-01 10:01:11 +00:00
shutil.chown(CONFIG_PATH, USER, USER)
2020-11-22 20:41:29 +00:00
file.write('\n')
2020-11-20 20:38:19 +00:00
config = _Config()