Mercurial > system76-keyboard-colors
diff keyboard_colors/color_profile.py @ 0:11a9d346aa7a
Sort of working model
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Wed, 27 Jan 2021 21:02:30 -0500 |
parents | |
children | b6d0a1e6ba3a |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/keyboard_colors/color_profile.py Wed Jan 27 21:02:30 2021 -0500 @@ -0,0 +1,65 @@ +import gi +import importlib +import json +import os +import pathlib +import sys +import uuid +gi.require_version("Gtk", "3.0") +from gi.repository import Gtk +from gi.repository import GLib + +class ColorProfileBase: + + type_name = '(no name)' + + def __init__(self): + self.name = '' + self.id = uuid.uuid4().hex + + def build_settings_ui(self): + glade_file = sys.modules[self.__module__].__file__.replace('.py', '.glade') + builder = Gtk.Builder() + builder.add_from_file(glade_file) + return builder.get_object('edit_profile_container') + +class ProfileManager: + + def __init__(self): + self.profiles = self.load_profiles() + self.save_profiles() + + def get_types(self): + types = [] + profile_directory = os.path.dirname(os.path.realpath(__file__)) + (_, dirnames, _) = next(os.walk(profile_directory)) + for dirname in dirnames: + module_file = os.path.join(profile_directory, dirname, dirname + '.py') + if os.path.isfile(module_file): + profile_module = importlib.import_module('keyboard_colors.' + dirname + '.' + dirname) + profile_class = getattr(profile_module, 'ColorProfile') + types.append(profile_class) + return types + + def config_path(self): + dir = GLib.get_user_config_dir() + if not os.path.isdir(dir): + os.mkdir(dir) + return os.path.join(dir, 'keyboard-color-profiles.conf') + + def load_profiles(self): + if os.path.isfile(self.config_path()): + data = pathlib.Path(self.config_path()).read_text('utf-8') + try: + return json.loads(data, object_hook=lambda d: SimpleNamespace(**d)) + except json.decoder.JSONDecodeError: + return [] + else: + return [] + + def save_profiles(self): + data = json.dumps(self.profiles, default=lambda o: o.__dict__) + pathlib.Path(self.config_path()).write_text(data, 'utf-8') + + def get_profiles(self): + return self.profiles \ No newline at end of file