0
|
1 import gi
|
|
2 import importlib
|
|
3 import json
|
|
4 import os
|
|
5 import pathlib
|
|
6 import sys
|
1
|
7 import types
|
0
|
8 import uuid
|
|
9 gi.require_version("Gtk", "3.0")
|
1
|
10 from gi.repository import Gtk # noqa: E402
|
|
11 from gi.repository import GLib # noqa: E402
|
|
12
|
0
|
13
|
|
14 class ColorProfileBase:
|
|
15
|
1
|
16 type_name = '(no name)'
|
0
|
17
|
1
|
18 def __init__(self):
|
|
19 self.name = ''
|
|
20 self.id = uuid.uuid4().hex
|
0
|
21
|
1
|
22 def build_settings_ui(self):
|
|
23 glade_file = sys.modules[self.__module__].__file__.replace('.py', '.glade')
|
|
24 builder = Gtk.Builder()
|
|
25 builder.add_from_file(glade_file)
|
|
26 return builder.get_object('edit_profile_container')
|
|
27
|
0
|
28
|
|
29 class ProfileManager:
|
|
30
|
1
|
31 def __init__(self):
|
|
32 self.profiles = self.load_profiles()
|
|
33 self.save_profiles()
|
0
|
34
|
1
|
35 def get_types(self):
|
|
36 types = []
|
|
37 profile_directory = os.path.dirname(os.path.realpath(__file__))
|
|
38 (_, dirnames, _) = next(os.walk(profile_directory))
|
|
39 for dirname in dirnames:
|
|
40 module_file = os.path.join(profile_directory, dirname, dirname + '.py')
|
|
41 if os.path.isfile(module_file):
|
|
42 module_name = 'keyboard_colors.' + dirname + '.' + dirname
|
|
43 profile_module = importlib.import_module(module_name)
|
|
44 profile_class = getattr(profile_module, 'ColorProfile')
|
|
45 types.append(profile_class)
|
|
46 return types
|
0
|
47
|
1
|
48 def config_path(self):
|
|
49 dir = GLib.get_user_config_dir()
|
|
50 if not os.path.isdir(dir):
|
|
51 os.mkdir(dir)
|
|
52 return os.path.join(dir, 'keyboard-color-profiles.conf')
|
0
|
53
|
1
|
54 def load_profiles(self):
|
|
55 if os.path.isfile(self.config_path()):
|
|
56 data = pathlib.Path(self.config_path()).read_text('utf-8')
|
|
57 try:
|
|
58 return json.loads(data, object_hook=lambda d: types.SimpleNamespace(**d))
|
|
59 except json.decoder.JSONDecodeError:
|
|
60 return []
|
|
61 else:
|
|
62 return []
|
0
|
63
|
1
|
64 def save_profiles(self):
|
|
65 data = json.dumps(self.profiles, default=lambda o: o.__dict__)
|
|
66 pathlib.Path(self.config_path()).write_text(data, 'utf-8')
|
0
|
67
|
1
|
68 def get_profiles(self):
|
|
69 return self.profiles
|