-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
63 lines (50 loc) · 1.83 KB
/
config.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
import json
import os
from pathlib import Path
import remap
import inputdevice
import outputdevice
import util
class Config:
CONFIG_FILE = str(Path.home()) + '/.config/remapper/config.json'
def __init__(self):
self.data = {}
if os.path.exists(self.CONFIG_FILE):
with open(self.CONFIG_FILE) as json_file:
self.data = json.load(json_file)
else:
path = os.path.dirname(os.path.abspath(self.CONFIG_FILE))
os.makedirs(path)
self.save()
def save(self):
with open(self.CONFIG_FILE, 'w') as outfile:
json.dump(self.data, outfile)
def add_remapper(self, remapper):
if 'remappers' not in self.data:
self.data['remappers'] = []
self.data['remappers'].append(remapper.to_dict())
self.save()
def remove_remapper(self, remapper):
if 'remappers' not in self.data:
self.data['remappers'] = []
to_delete = None
for val in self.data['remappers']:
if val['outputdevice']['name'] == remapper.outputdevice.name:
to_delete = val
break
if to_delete is not None:
self.data['remappers'].remove(to_delete)
self.save()
def get_remappers(self):
if 'remappers' not in self.data:
self.data['remappers'] = []
remappers = []
for remapper in self.data['remappers']:
in_dev = inputdevice.from_json(remapper['inputdevice'])
out_dev = outputdevice.from_json(remapper['outputdevice'])
event_map = {}
for key, value in remapper['event_map'].items():
event_map[int(key)] = value
mapper = remap.Remap(event_map, in_dev, out_dev, remapper['grab_device'])
remappers.append(mapper)
return remappers