2020-11-20 00:50:40 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper - GUI for device specific keyboard mappings
|
2022-01-01 12:52:33 +00:00
|
|
|
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
|
2020-11-20 00:50:40 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2020-11-20 00:50:40 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2020-11-20 00:50:40 +00:00
|
|
|
# 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.
|
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is distributed in the hope that it will be useful,
|
2020-11-20 00:50:40 +00:00
|
|
|
# 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
|
2022-01-01 12:00:49 +00:00
|
|
|
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
|
2020-11-20 00:50:40 +00:00
|
|
|
|
2021-02-12 19:29:26 +00:00
|
|
|
import copy
|
2020-11-20 00:50:40 +00:00
|
|
|
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.logger import logger, VERSION
|
2020-11-20 00:50:40 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
NONE = "none"
|
2020-12-19 15:04:07 +00:00
|
|
|
|
2020-11-20 00:50:40 +00:00
|
|
|
INITIAL_CONFIG = {
|
2021-12-15 14:06:23 +00:00
|
|
|
"version": VERSION,
|
2021-09-26 10:44:56 +00:00
|
|
|
"autoload": {},
|
2020-11-20 00:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-19 20:50:27 +00:00
|
|
|
class ConfigBase:
|
|
|
|
"""Base class for config objects.
|
|
|
|
|
|
|
|
Loading and saving is optional and handled by classes that derive from
|
|
|
|
this base.
|
|
|
|
"""
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2020-12-19 20:50:27 +00:00
|
|
|
def __init__(self, fallback=None):
|
|
|
|
"""Set up the needed members to turn your object into a config.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
fallback : ConfigBase
|
|
|
|
a configuration that contains fallback default configs, if your
|
|
|
|
object doesn't configure a certain key.
|
|
|
|
"""
|
2020-11-20 20:38:19 +00:00
|
|
|
self._config = {}
|
2020-12-19 20:50:27 +00:00
|
|
|
self.fallback = fallback
|
2020-11-20 20:38:19 +00:00
|
|
|
|
2020-12-02 18:33:31 +00:00
|
|
|
def _resolve(self, path, func, config=None):
|
2020-12-19 20:50:27 +00:00
|
|
|
"""Call func for the given config value.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-12-31 10:59:06 +00:00
|
|
|
path : string or string[]
|
|
|
|
For example 'macros.keystroke_sleep_ms'
|
|
|
|
or ['macros', 'keystroke_sleep_ms']
|
2020-12-19 20:50:27 +00:00
|
|
|
config : dict
|
|
|
|
The dictionary to search. Defaults to self._config.
|
|
|
|
"""
|
2021-09-26 10:44:56 +00:00
|
|
|
chunks = path.copy() if isinstance(path, list) else 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
|
|
|
|
----------
|
2021-01-03 11:39:09 +00:00
|
|
|
path : string or string[]
|
2020-11-30 19:57:09 +00:00
|
|
|
For example 'macros.keystroke_sleep_ms'
|
2021-12-31 10:59:06 +00:00
|
|
|
or ['macros', 'keystroke_sleep_ms']
|
2020-11-30 19:57:09 +00:00
|
|
|
"""
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2020-12-04 16:38:04 +00:00
|
|
|
def callback(parent, child, chunk):
|
2020-11-30 19:57:09 +00:00
|
|
|
if child is not None:
|
|
|
|
del parent[chunk]
|
|
|
|
|
2020-12-04 16:38:04 +00:00
|
|
|
self._resolve(path, callback)
|
2020-11-30 19:57:09 +00:00
|
|
|
|
|
|
|
def set(self, path, value):
|
|
|
|
"""Set a config key.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-01-03 11:39:09 +00:00
|
|
|
path : string or string[]
|
2020-11-30 19:57:09 +00:00
|
|
|
For example 'macros.keystroke_sleep_ms'
|
2021-01-03 11:39:09 +00:00
|
|
|
or ['macros', 'keystroke_sleep_ms']
|
2020-11-30 19:57:09 +00:00
|
|
|
value : any
|
|
|
|
"""
|
2021-09-26 10:44:56 +00:00
|
|
|
logger.info('Changing "%s" to "%s" in %s', path, value, self.__class__.__name__)
|
2020-12-12 14:22:34 +00:00
|
|
|
|
2020-12-04 16:38:04 +00:00
|
|
|
def callback(parent, child, chunk):
|
2020-11-30 19:57:09 +00:00
|
|
|
parent[chunk] = value
|
|
|
|
|
2020-12-04 16:38:04 +00:00
|
|
|
self._resolve(path, callback)
|
2020-11-30 19:57:09 +00:00
|
|
|
|
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
|
|
|
|
----------
|
2021-01-07 16:15:12 +00:00
|
|
|
path : string or string[]
|
2020-11-30 19:57:09 +00:00
|
|
|
For example 'macros.keystroke_sleep_ms'
|
2020-12-02 18:33:31 +00:00
|
|
|
log_unknown : bool
|
2020-12-24 00:26:34 +00:00
|
|
|
If True, write an error if `path` does not exist in the config
|
2020-11-30 19:57:09 +00:00
|
|
|
"""
|
2021-09-29 18:50:32 +00:00
|
|
|
|
2020-12-04 16:38:04 +00:00
|
|
|
def callback(parent, child, chunk):
|
2020-12-02 18:33:31 +00:00
|
|
|
return child
|
|
|
|
|
2020-12-04 16:38:04 +00:00
|
|
|
resolved = self._resolve(path, callback)
|
2020-12-19 20:50:27 +00:00
|
|
|
if resolved is None and self.fallback is not None:
|
|
|
|
resolved = self.fallback._resolve(path, callback)
|
2020-12-02 18:33:31 +00:00
|
|
|
if resolved is None:
|
2021-03-21 18:15:20 +00:00
|
|
|
# don't create new empty stuff in INITIAL_CONFIG with _resolve
|
|
|
|
initial_copy = copy.deepcopy(INITIAL_CONFIG)
|
|
|
|
resolved = self._resolve(path, callback, initial_copy)
|
2020-12-02 18:33:31 +00:00
|
|
|
|
|
|
|
if resolved is None and log_unknown:
|
|
|
|
logger.error('Unknown config key "%s"', path)
|
|
|
|
|
2021-02-12 19:29:26 +00:00
|
|
|
# modifications are only allowed via set
|
|
|
|
return copy.deepcopy(resolved)
|
2020-11-30 19:57:09 +00:00
|
|
|
|
2020-12-19 20:50:27 +00:00
|
|
|
def clear_config(self):
|
|
|
|
"""Remove all configurations in memory."""
|
|
|
|
self._config = {}
|