Skip to content

Commit

Permalink
Fixed Pylint issues, added more typehints (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
sezanzeb authored Nov 12, 2022
1 parent b5345ad commit 430aef0
Show file tree
Hide file tree
Showing 61 changed files with 806 additions and 693 deletions.
1 change: 1 addition & 0 deletions bin/input-remapper-gtk
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.

"""Starts the user interface."""

from __future__ import annotations

import sys
Expand Down
33 changes: 20 additions & 13 deletions inputremapper/configs/base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
# You should have received a copy of the GNU General Public License
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.

from __future__ import annotations

import copy
from typing import Union, List, Optional, Callable, Any

from inputremapper.logger import logger, VERSION

Expand All @@ -37,27 +40,32 @@ class ConfigBase:
this base.
"""

def __init__(self, fallback=None):
def __init__(self, fallback: Optional[ConfigBase] = None):
"""Set up the needed members to turn your object into a config.
Parameters
----------
fallback : ConfigBase
fallback: ConfigBase
a configuration that contains fallback default configs, if your
object doesn't configure a certain key.
"""
self._config = {}
self.fallback = fallback

def _resolve(self, path, func, config=None):
def _resolve(
self,
path: Union[str, List[str]],
func: Callable,
config: Optional[dict] = None,
):
"""Call func for the given config value.
Parameters
----------
path : string or string[]
path
For example 'macros.keystroke_sleep_ms'
or ['macros', 'keystroke_sleep_ms']
config : dict
config
The dictionary to search. Defaults to self._config.
"""
chunks = path.copy() if isinstance(path, list) else path.split(".")
Expand All @@ -80,12 +88,12 @@ def _resolve(self, path, func, config=None):
parent[chunk] = {}
child = parent[chunk]

def remove(self, path):
def remove(self, path: Union[str, List[str]]):
"""Remove a config key.
Parameters
----------
path : string or string[]
path
For example 'macros.keystroke_sleep_ms'
or ['macros', 'keystroke_sleep_ms']
"""
Expand All @@ -96,15 +104,14 @@ def callback(parent, child, chunk):

self._resolve(path, callback)

def set(self, path, value):
def set(self, path: Union[str, List[str]], value: Any):
"""Set a config key.
Parameters
----------
path : string or string[]
path
For example 'macros.keystroke_sleep_ms'
or ['macros', 'keystroke_sleep_ms']
value : any
"""
logger.info('Changing "%s" to "%s" in %s', path, value, self.__class__.__name__)

Expand All @@ -113,14 +120,14 @@ def callback(parent, child, chunk):

self._resolve(path, callback)

def get(self, path, log_unknown=True):
def get(self, path: Union[str, List[str]], log_unknown: bool = True):
"""Get a config value. If not set, return the default
Parameters
----------
path : string or string[]
path
For example 'macros.keystroke_sleep_ms'
log_unknown : bool
log_unknown
If True, write an error if `path` does not exist in the config
"""

Expand Down
15 changes: 8 additions & 7 deletions inputremapper/configs/global_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import copy
import json
import os
from typing import Optional

from inputremapper.configs.base_config import ConfigBase, INITIAL_CONFIG
from inputremapper.configs.paths import CONFIG_PATH, USER, touch
Expand All @@ -46,18 +47,18 @@ def __init__(self):
super().__init__()

def get_dir(self) -> str:
"""the folder containing this config"""
"""The folder containing this config."""
return os.path.split(self.path)[0]

def set_autoload_preset(self, group_key, preset):
def set_autoload_preset(self, group_key: str, preset: Optional[str]):
"""Set a preset to be automatically applied on start.
Parameters
----------
group_key : string
group_key
the unique identifier of the group. This is used instead of the
name to enable autoloading two different presets when two similar
devices are connected.
preset : string or None
preset
if None, don't autoload something for this device.
"""
if preset is not None:
Expand All @@ -72,18 +73,18 @@ def iterate_autoload_presets(self):
"""Get tuples of (device, preset)."""
return self._config.get("autoload", {}).items()

def is_autoloaded(self, group_key, preset):
def is_autoloaded(self, group_key: Optional[str], preset: Optional[str]):
"""Should this preset be loaded automatically?"""
if group_key is None or preset is None:
raise ValueError("Expected group_key and preset to not be None")

return self.get(["autoload", group_key], log_unknown=False) == preset

def load_config(self, path=None):
def load_config(self, path: Optional[str] = None):
"""Load the config from the file system.
Parameters
----------
path : string or None
path
If set, will change the path to load from and save to.
"""
if path is not None:
Expand Down
Loading

0 comments on commit 430aef0

Please sign in to comment.