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