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