Mercurial > system76-keyboard-colors
comparison keyboard_colors/color_profile.py @ 2:091a1f59a79c
Load profiles into list view
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Tue, 02 Mar 2021 20:17:36 -0500 |
parents | b6d0a1e6ba3a |
children | eb2aa09653bd |
comparison
equal
deleted
inserted
replaced
1:b6d0a1e6ba3a | 2:091a1f59a79c |
---|---|
4 import os | 4 import os |
5 import pathlib | 5 import pathlib |
6 import sys | 6 import sys |
7 import types | 7 import types |
8 import uuid | 8 import uuid |
9 from types import SimpleNamespace | |
9 gi.require_version("Gtk", "3.0") | 10 gi.require_version("Gtk", "3.0") |
10 from gi.repository import Gtk # noqa: E402 | 11 from gi.repository import Gtk # noqa: E402 |
11 from gi.repository import GLib # noqa: E402 | 12 from gi.repository import GLib # noqa: E402 |
12 | 13 |
13 | 14 |
15 | 16 |
16 type_name = '(no name)' | 17 type_name = '(no name)' |
17 | 18 |
18 def __init__(self): | 19 def __init__(self): |
19 self.name = '' | 20 self.name = '' |
20 self.id = uuid.uuid4().hex | 21 self.id = str(uuid.uuid4()) |
21 | 22 |
22 def build_settings_ui(self): | 23 def build_settings_ui(self): |
23 glade_file = sys.modules[self.__module__].__file__.replace('.py', '.glade') | 24 glade_file = sys.modules[self.__module__].__file__.replace('.py', '.glade') |
24 builder = Gtk.Builder() | 25 builder = Gtk.Builder() |
25 builder.add_from_file(glade_file) | 26 builder.add_from_file(glade_file) |
26 return builder.get_object('edit_profile_container') | 27 return builder.get_object('edit_profile_container') |
27 | 28 |
29 def serialize(self): | |
30 properties = self.__dict__ | |
31 properties['module'] = self.__class__.__module__ | |
32 return properties | |
33 | |
34 @staticmethod | |
35 def unserialize(data): | |
36 profile_module = importlib.import_module(data['module']) | |
37 profile_class = getattr(profile_module, 'ColorProfile') | |
38 profile = profile_class() | |
39 profile.__dict__.update(data) | |
40 return profile | |
41 | |
28 | 42 |
29 class ProfileManager: | 43 class ProfileManager: |
30 | 44 |
31 def __init__(self): | 45 def __init__(self): |
32 self.profiles = self.load_profiles() | 46 profile_list = self.load_profiles() |
33 self.save_profiles() | 47 self.profiles = {p.id: p for p in profile_list} |
34 | 48 |
35 def get_types(self): | 49 def get_types(self): |
36 types = [] | 50 types = [] |
37 profile_directory = os.path.dirname(os.path.realpath(__file__)) | 51 profile_directory = os.path.dirname(os.path.realpath(__file__)) |
38 (_, dirnames, _) = next(os.walk(profile_directory)) | 52 (_, dirnames, _) = next(os.walk(profile_directory)) |
53 | 67 |
54 def load_profiles(self): | 68 def load_profiles(self): |
55 if os.path.isfile(self.config_path()): | 69 if os.path.isfile(self.config_path()): |
56 data = pathlib.Path(self.config_path()).read_text('utf-8') | 70 data = pathlib.Path(self.config_path()).read_text('utf-8') |
57 try: | 71 try: |
58 return json.loads(data, object_hook=lambda d: types.SimpleNamespace(**d)) | 72 return json.loads(data, object_hook=lambda d: ColorProfileBase.unserialize(d)) |
59 except json.decoder.JSONDecodeError: | 73 except json.decoder.JSONDecodeError: |
60 return [] | 74 return [] |
61 else: | 75 else: |
62 return [] | 76 return [] |
63 | 77 |
64 def save_profiles(self): | 78 def save_profiles(self): |
65 data = json.dumps(self.profiles, default=lambda o: o.__dict__) | 79 data = json.dumps(self.profiles, default=lambda o: o.serialize()) |
66 pathlib.Path(self.config_path()).write_text(data, 'utf-8') | 80 pathlib.Path(self.config_path()).write_text(data, 'utf-8') |
67 | 81 |
68 def get_profiles(self): | 82 def get_profiles(self): |
69 return self.profiles | 83 return self.profiles |
84 | |
85 def get_profile(self, profile_id): | |
86 return self.get_profiles()[profile_id] |