-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.py
69 lines (51 loc) · 2.24 KB
/
configuration.py
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
import json
import os
from pathlib import Path
from typing import List
from hotkey import Hotkey
from keys import Modifier
DEFAULT_MODIFIER = Modifier.SUPER
class Configuration:
_XDG_CONFIG_HOME = Path(os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config')))
_CONFIG_DIR = _XDG_CONFIG_HOME / "monkey"
_HOTKEYS_FILE = _CONFIG_DIR / "hotkeys.json"
def __init__(self):
if not self._CONFIG_DIR.exists():
self._CONFIG_DIR.mkdir(parents=True)
if not self._HOTKEYS_FILE.exists():
with self._HOTKEYS_FILE.open('w') as hotkeys_file:
json.dump({
'modifier': DEFAULT_MODIFIER.name,
'hotkeys': []
}, hotkeys_file)
def modifier(self) -> Modifier:
return Modifier[self._read_configuration()['modifier']]
def hotkeys(self) -> List[Hotkey]:
return [Hotkey(*x) for x in self._read_configuration()['hotkeys']]
def set_modifier(self, new_modifier: Modifier):
new_configuration = self._read_configuration()
new_configuration['modifier'] = new_modifier.name
with self._HOTKEYS_FILE.open('w') as hotkeys_file:
json.dump(new_configuration, hotkeys_file)
def add_hotkey(self, hotkey: Hotkey):
updated_hotkeys = self.hotkeys()
updated_hotkeys.append(hotkey)
self._write_hotkeys(updated_hotkeys)
def remove_hotkey(self, hotkey: Hotkey):
updated_hotkeys = self.hotkeys()
updated_hotkeys.remove(hotkey)
self._write_hotkeys(updated_hotkeys)
def edit_hotkey(self, hotkey: Hotkey, updated_hotkey: Hotkey):
updated_hotkeys = self.hotkeys()
updated_hotkeys[updated_hotkeys.index(hotkey)] = updated_hotkey
self._write_hotkeys(updated_hotkeys)
def _write_hotkeys(self, hotkeys: List[Hotkey]):
updated_configuration = self._read_configuration()
updated_configuration['hotkeys'] = [
(x.key, x.window_class_name) for x in hotkeys
]
with self._HOTKEYS_FILE.open('w') as hotkeys_file:
json.dump(updated_configuration, hotkeys_file)
def _read_configuration(self):
with self._HOTKEYS_FILE.open('r') as hotkeys_file:
return json.load(hotkeys_file)