-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_reader.py
More file actions
78 lines (60 loc) · 2.06 KB
/
command_reader.py
File metadata and controls
78 lines (60 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import file_process as fp
from typing import Dict, Callable, List, Iterable, Union, Any
class HotKey:
def __init__(self, key: Union[str, List[str]] = "", command=""):
self.key = key
self.command_name = command
@property
def command_name(self) -> str:
return self._command_name
@command_name.setter
def command_name(self, command: str) -> None:
self._command_name = command
@property
def key(self) -> str:
return self._key
@key.setter
def key(self, key: Union[str, List[str]]) -> None:
if type(key) == str:
self._key = key
else:
self._key = HotKey.from_list(key)
@staticmethod
def from_list(keys: List[str]) -> str:
new_keys = [f"<{key}>" if len(key) > 1 else key for key in keys]
return "+".join(new_keys)
def __str__(self):
return self.key + " --- " + self.command_name
def __repr__(self):
return str(self)
class KeyMapper:
def __init__(self):
self.maps: Dict[str, List[HotKey]] = {}
self.active_map_name: str = ""
@staticmethod
def from_file(file_name: str) -> "KeyMapper":
return fp.read(file_name)
def write(self, file_name: str) -> None:
fp.write(file_name, self)
def keys(self, map_name: str) -> List[HotKey]:
return self.maps[map_name]
def map(self, map_name: str, hotkeys: List[HotKey]) -> None:
self.maps[map_name] = hotkeys
def active_keys(self):
return self.keys(self.active_map_name)
def connect(self, func: Dict[str, Callable]) -> Dict[HotKey, Callable]:
keys = self.active_keys()
connected_hotkeys: Dict[HotKey, Callable] = {}
for key in keys:
connected_hotkeys[key] = func[key.command_name]
return connected_hotkeys
# k = KeyMapper()
# for name in "default", "my", "custom":
# l = []
# for i in range(5):
# l.append(HotKey(f"{name[0]}+{i}", f"{name[0]}{i}"))
# k.map(name, l.copy())
#
# print(k.maps)
# k.active_map_name = "default"
# k.write("keys.json")