Skip to content

Commit 057663a

Browse files
authored
Dependency injection (#964)
1 parent 08226a2 commit 057663a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1303
-1050
lines changed

bin/input-remapper-control

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import sys
2929
from enum import Enum
3030
from typing import Union, Optional
3131

32-
from inputremapper.configs.global_config import global_config
32+
from inputremapper.configs.global_config import GlobalConfig
3333
from inputremapper.configs.migrations import Migrations
34+
from inputremapper.injection.global_uinputs import GlobalUInputs, UInput, FrontendUInput
3435
from inputremapper.logging.logger import logger
3536

3637

@@ -66,6 +67,14 @@ class Options:
6667

6768

6869
class InputRemapperControl:
70+
def __init__(
71+
self,
72+
global_config: GlobalConfig,
73+
migrations: Migrations,
74+
):
75+
self.global_config = global_config
76+
self.migrations = migrations
77+
6978
def run(self, cmd) -> None:
7079
"""Run and log a command."""
7180
logger.info("Running `%s`...", cmd)
@@ -81,9 +90,9 @@ class InputRemapperControl:
8190
print(group.key)
8291

8392
def list_key_names(self):
84-
from inputremapper.configs.system_mapping import system_mapping
93+
from inputremapper.configs.keyboard_layout import keyboard_layout
8594

86-
print("\n".join(system_mapping.list_names()))
95+
print("\n".join(keyboard_layout.list_names()))
8796

8897
def communicate(
8998
self,
@@ -131,7 +140,7 @@ class InputRemapperControl:
131140
sys.exit(6)
132141

133142
logger.info('Using config from "%s" instead', path)
134-
global_config.load_config(path)
143+
self.global_config.load_config(path)
135144

136145
def ensure_migrated(self) -> None:
137146
# import stuff late to make sure the correct log level is applied
@@ -144,9 +153,9 @@ class InputRemapperControl:
144153
# This will also refresh the config of the daemon if the user changed
145154
# it in the meantime.
146155
# config_dir is either the cli arg or the default path in home
147-
config_dir = os.path.dirname(global_config.path)
156+
config_dir = os.path.dirname(self.global_config.path)
148157
self.daemon.set_config_dir(config_dir)
149-
Migrations.migrate()
158+
self.migrations.migrate()
150159

151160
def _stop(self, device: str) -> None:
152161
group = self._require_group(device)
@@ -267,7 +276,14 @@ class InputRemapperControl:
267276

268277

269278
def main(options: Options) -> None:
270-
input_remapper_control = InputRemapperControl()
279+
global_config = GlobalConfig()
280+
global_uinputs = GlobalUInputs(FrontendUInput)
281+
migrations = Migrations(global_uinputs)
282+
input_remapper_control = InputRemapperControl(
283+
global_config,
284+
migrations,
285+
)
286+
271287
if options.debug:
272288
logger.update_verbosity(True)
273289

bin/input-remapper-gtk

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,33 +74,38 @@ if __name__ == "__main__":
7474
# log-level is set.
7575
# import input-remapper stuff after setting the log verbosity
7676
from inputremapper.gui.messages.message_broker import MessageBroker, MessageType
77-
from inputremapper.configs.system_mapping import system_mapping
77+
from inputremapper.configs.keyboard_layout import keyboard_layout
7878
from inputremapper.gui.data_manager import DataManager
7979
from inputremapper.gui.user_interface import UserInterface
8080
from inputremapper.gui.controller import Controller
81-
from inputremapper.injection.global_uinputs import GlobalUInputs
81+
from inputremapper.injection.global_uinputs import GlobalUInputs, FrontendUInput
8282
from inputremapper.groups import _Groups
8383
from inputremapper.gui.reader_client import ReaderClient
8484
from inputremapper.daemon import Daemon
8585
from inputremapper.configs.global_config import GlobalConfig
8686
from inputremapper.configs.migrations import Migrations
8787

88-
Migrations.migrate()
88+
global_uinputs = GlobalUInputs(FrontendUInput)
89+
migrations = Migrations(global_uinputs)
90+
91+
Migrations(global_uinputs).migrate()
8992

9093
message_broker = MessageBroker()
9194

95+
global_config = GlobalConfig()
96+
9297
# create the reader before we start the reader-service (start_processes) otherwise
9398
# it can come to race conditions with the creation of pipes
9499
reader_client = ReaderClient(message_broker, _Groups())
95100
daemon = start_processes()
96101

97102
data_manager = DataManager(
98103
message_broker,
99-
GlobalConfig(),
104+
global_config,
100105
reader_client,
101106
daemon,
102-
GlobalUInputs(),
103-
system_mapping,
107+
global_uinputs,
108+
keyboard_layout,
104109
)
105110
controller = Controller(message_broker, data_manager)
106111
user_interface = UserInterface(message_broker, controller)

bin/input-remapper-reader-service

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import sys
2828
from argparse import ArgumentParser
2929

3030
from inputremapper.groups import _Groups
31+
from inputremapper.injection.global_uinputs import GlobalUInputs, FrontendUInput
3132
from inputremapper.logging.logger import logger
3233

3334
if __name__ == "__main__":
@@ -57,5 +58,6 @@ if __name__ == "__main__":
5758

5859
atexit.register(on_exit)
5960
groups = _Groups()
60-
reader_service = ReaderService(groups)
61+
global_uinputs = GlobalUInputs(FrontendUInput)
62+
reader_service = ReaderService(groups, global_uinputs)
6163
asyncio.run(reader_service.run())

bin/input-remapper-service

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import sys
2626
from argparse import ArgumentParser
2727

28+
from inputremapper.configs.global_config import GlobalConfig
29+
from inputremapper.injection.global_uinputs import GlobalUInputs, UInput
30+
from inputremapper.injection.mapping_handlers.mapping_parser import MappingParser
2831
from inputremapper.logging.logger import logger
2932

3033
if __name__ == "__main__":
@@ -55,6 +58,10 @@ if __name__ == "__main__":
5558
if not options.hide_info:
5659
logger.log_info("input-remapper-service")
5760

58-
daemon = Daemon()
61+
global_config = GlobalConfig()
62+
global_uinputs = GlobalUInputs(UInput)
63+
mapping_parser = MappingParser(global_uinputs)
64+
65+
daemon = Daemon(global_config, global_uinputs, mapping_parser)
5966
daemon.publish()
6067
daemon.run()

inputremapper/configs/global_config.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
from inputremapper.configs.base_config import ConfigBase, INITIAL_CONFIG
2727
from inputremapper.configs.paths import PathUtils
28-
from inputremapper.user import UserUtils
2928
from inputremapper.logging.logger import logger
29+
from inputremapper.user import UserUtils
3030

3131
MOUSE = "mouse"
3232
WHEEL = "wheel"
@@ -35,7 +35,7 @@
3535

3636

3737
class GlobalConfig(ConfigBase):
38-
"""Global default configuration.
38+
"""Global default configuration, from which all presets inherit.
3939
It can also contain some extra stuff not relevant for presets, like the
4040
autoload stuff. If presets have a config key set, it will ignore
4141
the default global configuration for that one. If none of the configs
@@ -129,7 +129,3 @@ def _save_config(self):
129129
json.dump(self._config, file, indent=4)
130130
logger.info("Saved config to %s", self.path)
131131
file.write("\n")
132-
133-
134-
# TODO DI
135-
global_config = GlobalConfig()

inputremapper/configs/input_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
except ImportError:
3232
from pydantic import BaseModel, root_validator, validator
3333

34-
from inputremapper.configs.system_mapping import system_mapping
34+
from inputremapper.configs.keyboard_layout import keyboard_layout
3535
from inputremapper.gui.messages.message_types import MessageType
3636
from inputremapper.logging.logger import logger
3737
from inputremapper.utils import get_evdev_constant_name
@@ -142,7 +142,7 @@ def _get_name(self) -> Optional[str]:
142142
# first try to find the name in xmodmap to not display wrong
143143
# names due to the keyboard layout
144144
if self.type == ecodes.EV_KEY:
145-
key_name = system_mapping.get_name(self.code)
145+
key_name = keyboard_layout.get_name(self.code)
146146

147147
if key_name is None:
148148
# if no result, look in the linux combination constants. On a german

inputremapper/configs/system_mapping.py renamed to inputremapper/configs/keyboard_layout.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@
4141
LAZY_LOAD = None
4242

4343

44-
class SystemMapping:
44+
class KeyboardLayout:
4545
"""Stores information about all available keycodes."""
4646

4747
_mapping: Optional[dict] = LAZY_LOAD
4848
_xmodmap: Optional[List[Tuple[str, str]]] = LAZY_LOAD
4949
_case_insensitive_mapping: Optional[dict] = LAZY_LOAD
5050

5151
def __getattribute__(self, wanted: str):
52-
"""To lazy load system_mapping info only when needed.
52+
"""To lazy load keyboard_layout info only when needed.
5353
5454
For example, this helps to keep logs of input-remapper-control clear when it
5555
doesn't need it the information.
@@ -160,7 +160,7 @@ def _set(self, name: str, code: int):
160160

161161
def get(self, name: str) -> int:
162162
"""Return the code mapped to the key."""
163-
# the correct casing should be shown when asking the system_mapping
163+
# the correct casing should be shown when asking the keyboard_layout
164164
# for stuff. indexing case insensitive to support old presets.
165165
if name not in self._mapping:
166166
# only if not e.g. both "a" and "A" are in the mapping
@@ -216,4 +216,4 @@ def _find_legit_mappings(self) -> dict:
216216

217217
# TODO DI
218218
# this mapping represents the xmodmap output, which stays constant
219-
system_mapping = SystemMapping()
219+
keyboard_layout = KeyboardLayout()

inputremapper/configs/mapping.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
)
6363

6464
from inputremapper.configs.input_config import InputCombination
65-
from inputremapper.configs.system_mapping import system_mapping, DISABLE_NAME
65+
from inputremapper.configs.keyboard_layout import keyboard_layout, DISABLE_NAME
6666
from inputremapper.configs.validation_errors import (
6767
OutputSymbolUnknownError,
6868
SymbolNotAvailableInTargetError,
@@ -286,13 +286,13 @@ def remove_combination_changed_callback(self):
286286

287287
def get_output_type_code(self) -> Optional[Tuple[int, int]]:
288288
"""Returns the output_type and output_code if set,
289-
otherwise looks the output_symbol up in the system_mapping
289+
otherwise looks the output_symbol up in the keyboard_layout
290290
return None for unknown symbols and macros
291291
"""
292292
if self.output_code and self.output_type:
293293
return self.output_type, self.output_code
294294
if self.output_symbol and not is_this_a_macro(self.output_symbol):
295-
return EV_KEY, system_mapping.get(self.output_symbol)
295+
return EV_KEY, keyboard_layout.get(self.output_symbol)
296296
return None
297297

298298
def get_output_name_constant(self) -> str:
@@ -385,7 +385,7 @@ def validate_symbol(cls, values):
385385
parse(symbol, mapping=mapping_mock, verbose=False)
386386
return values
387387

388-
code = system_mapping.get(symbol)
388+
code = keyboard_layout.get(symbol)
389389
if code is None:
390390
raise OutputSymbolUnknownError(symbol)
391391

@@ -450,7 +450,7 @@ def validate_output_integrity(cls, values):
450450
if type_ is not None or code is not None:
451451
raise MacroButTypeOrCodeSetError()
452452

453-
if code is not None and code != system_mapping.get(symbol) or type_ != EV_KEY:
453+
if code is not None and code != keyboard_layout.get(symbol) or type_ != EV_KEY:
454454
raise SymbolAndCodeMismatchError(symbol, code)
455455
return values
456456

0 commit comments

Comments
 (0)