2022-10-02 01:57:29 +00:00
|
|
|
# preset.py
|
|
|
|
#
|
|
|
|
# Change the look of Adwaita, with ease
|
|
|
|
# Copyright (C) 2022 Gradience Team
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2022-09-14 12:49:39 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
2022-12-02 21:52:47 +00:00
|
|
|
from gradience.frontend.settings_schema import settings_schema
|
2022-12-03 23:00:18 +00:00
|
|
|
from gradience.backend.utils.common import to_slug_case
|
2022-12-08 11:43:12 +00:00
|
|
|
from gradience.backend.globals import presets_dir
|
2022-12-03 23:00:18 +00:00
|
|
|
|
|
|
|
from gradience.backend.logger import Logger
|
|
|
|
|
|
|
|
logging = Logger()
|
2022-09-14 12:49:39 +00:00
|
|
|
|
2022-10-02 01:57:29 +00:00
|
|
|
|
2022-12-08 11:43:12 +00:00
|
|
|
# Adwaita default colors palette
|
|
|
|
adw_palette = {
|
|
|
|
"blue_": {
|
|
|
|
"1": "#99c1f1",
|
|
|
|
"2": "#62a0ea",
|
|
|
|
"3": "#3584e4",
|
|
|
|
"4": "#1c71d8",
|
|
|
|
"5": "#1a5fb4",
|
|
|
|
},
|
|
|
|
"green_": {
|
|
|
|
"1": "#8ff0a4",
|
|
|
|
"2": "#57e389",
|
|
|
|
"3": "#33d17a",
|
|
|
|
"4": "#2ec27e",
|
|
|
|
"5": "#26a269",
|
|
|
|
},
|
|
|
|
"yellow_": {
|
|
|
|
"1": "#f9f06b",
|
|
|
|
"2": "#f8e45c",
|
|
|
|
"3": "#f6d32d",
|
|
|
|
"4": "#f5c211",
|
|
|
|
"5": "#e5a50a",
|
|
|
|
},
|
|
|
|
"orange_": {
|
|
|
|
"1": "#ffbe6f",
|
|
|
|
"2": "#ffa348",
|
|
|
|
"3": "#ff7800",
|
|
|
|
"4": "#e66100",
|
|
|
|
"5": "#c64600",
|
|
|
|
},
|
|
|
|
"red_": {
|
|
|
|
"1": "#f66151",
|
|
|
|
"2": "#ed333b",
|
|
|
|
"3": "#e01b24",
|
|
|
|
"4": "#c01c28",
|
|
|
|
"5": "#a51d2d",
|
|
|
|
},
|
|
|
|
"purple_": {
|
|
|
|
"1": "#dc8add",
|
|
|
|
"2": "#c061cb",
|
|
|
|
"3": "#9141ac",
|
|
|
|
"4": "#813d9c",
|
|
|
|
"5": "#613583",
|
|
|
|
},
|
|
|
|
"brown_": {
|
|
|
|
"1": "#cdab8f",
|
|
|
|
"2": "#b5835a",
|
|
|
|
"3": "#986a44",
|
|
|
|
"4": "#865e3c",
|
|
|
|
"5": "#63452c",
|
|
|
|
},
|
|
|
|
"light_": {
|
|
|
|
"1": "#ffffff",
|
|
|
|
"2": "#f6f5f4",
|
|
|
|
"3": "#deddda",
|
|
|
|
"4": "#c0bfbc",
|
|
|
|
"5": "#9a9996",
|
|
|
|
},
|
|
|
|
"dark_": {
|
|
|
|
"1": "#77767b",
|
|
|
|
"2": "#5e5c64",
|
|
|
|
"3": "#3d3846",
|
|
|
|
"4": "#241f31",
|
|
|
|
"5": "#000000",
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 12:49:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Preset:
|
|
|
|
variables = {}
|
2022-12-08 11:43:12 +00:00
|
|
|
palette = adw_palette
|
2022-09-14 12:49:39 +00:00
|
|
|
custom_css = {
|
|
|
|
"gtk4": "",
|
|
|
|
"gtk3": "",
|
|
|
|
}
|
|
|
|
plugins = {}
|
2022-10-11 18:34:04 +00:00
|
|
|
display_name = "New Preset"
|
2022-11-20 03:02:16 +00:00
|
|
|
preset_path = "new_preset"
|
2022-09-15 18:12:34 +00:00
|
|
|
badges = {}
|
2022-09-14 12:49:39 +00:00
|
|
|
|
2022-12-08 11:43:12 +00:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def new(self, display_name: str, variables: dict, palette=None, custom_css=None, badges=None):
|
|
|
|
self.display_name = display_name
|
|
|
|
self.variables = variables
|
|
|
|
|
|
|
|
if palette:
|
|
|
|
self.palette = palette
|
|
|
|
if custom_css:
|
|
|
|
self.custom_css = custom_css
|
|
|
|
if badges:
|
|
|
|
self.badges = badges
|
|
|
|
|
|
|
|
def new_from_path(self, preset_path: str):
|
|
|
|
self.preset_path = preset_path
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(self.preset_path, "r", encoding="utf-8") as file:
|
|
|
|
preset_text = file.read()
|
|
|
|
file.close()
|
|
|
|
except OSError as e:
|
|
|
|
logging.error(f"Failed to read contents of a preset in location: {self.preset_path}. Exc: {e}")
|
|
|
|
|
|
|
|
try:
|
|
|
|
preset = json.loads(preset_text)
|
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
logging.error(f"Error while decoding JSON data. Exc: {e}")
|
|
|
|
|
|
|
|
self.__load_values(preset)
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
def new_from_resource(self, text: str):
|
|
|
|
preset_text = text
|
2022-09-14 12:49:39 +00:00
|
|
|
|
|
|
|
try:
|
2022-12-08 11:43:12 +00:00
|
|
|
preset = json.loads(preset_text)
|
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
logging.error(f"Error while decoding JSON data. Exc: {e}")
|
|
|
|
|
|
|
|
self.__load_values(preset)
|
|
|
|
|
|
|
|
return self
|
2022-09-14 12:49:39 +00:00
|
|
|
|
2022-12-08 11:43:12 +00:00
|
|
|
def new_from_dict(self, preset: dict):
|
|
|
|
self.__load_values(preset)
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __load_values(self, preset):
|
|
|
|
try:
|
2022-10-11 18:34:04 +00:00
|
|
|
self.display_name = preset["name"]
|
2022-09-14 12:49:39 +00:00
|
|
|
self.variables = preset["variables"]
|
|
|
|
self.palette = preset["palette"]
|
|
|
|
|
2022-09-15 18:12:34 +00:00
|
|
|
if "badges" in preset:
|
|
|
|
self.badges = preset["badges"]
|
|
|
|
else:
|
|
|
|
self.badges = {}
|
|
|
|
|
2022-09-14 12:49:39 +00:00
|
|
|
if "custom_css" in preset:
|
|
|
|
self.custom_css = preset["custom_css"]
|
|
|
|
else:
|
|
|
|
for app_type in settings_schema["custom_css_app_types"]:
|
|
|
|
self.custom_css[app_type] = ""
|
2022-11-20 16:15:56 +00:00
|
|
|
except Exception as e:
|
2022-12-08 11:43:12 +00:00
|
|
|
logging.error(f"Failed to create a new preset object. Exc: {e}")
|
2022-09-14 12:49:39 +00:00
|
|
|
|
2022-11-20 03:46:55 +00:00
|
|
|
# Rename an existing preset
|
2022-12-08 11:43:12 +00:00
|
|
|
def rename(self, name):
|
2022-11-20 04:11:57 +00:00
|
|
|
self.display_name = name
|
|
|
|
old_path = self.preset_path
|
|
|
|
self.preset_path = os.path.join(
|
2022-11-20 03:46:55 +00:00
|
|
|
os.path.dirname(self.preset_path),
|
|
|
|
to_slug_case(name) + ".json")
|
|
|
|
|
2022-12-08 11:43:12 +00:00
|
|
|
self.save_to_file(to=self.preset_path)
|
2022-11-20 04:11:57 +00:00
|
|
|
os.remove(old_path)
|
2022-11-20 03:46:55 +00:00
|
|
|
|
|
|
|
# Save a new user preset (or overwrite one)
|
2022-12-08 11:43:12 +00:00
|
|
|
def save_to_file(self, name=None, plugins_list=None, to=None):
|
2022-10-11 18:34:04 +00:00
|
|
|
self.display_name = name if name else self.display_name
|
|
|
|
|
2022-09-14 14:55:35 +00:00
|
|
|
if to is None:
|
2022-11-20 03:46:55 +00:00
|
|
|
filename = to_slug_case(name) if name else "new_preset"
|
2022-09-14 14:56:35 +00:00
|
|
|
self.preset_path = os.path.join(
|
2022-11-20 03:46:55 +00:00
|
|
|
presets_dir, "user", filename + ".json")
|
2022-09-14 14:55:35 +00:00
|
|
|
else:
|
|
|
|
self.preset_path = to
|
2022-11-20 03:46:55 +00:00
|
|
|
|
2022-09-14 12:49:39 +00:00
|
|
|
if not os.path.exists(
|
2022-09-14 14:56:35 +00:00
|
|
|
os.path.join(
|
2022-10-02 01:57:29 +00:00
|
|
|
presets_dir,
|
2022-09-14 14:56:35 +00:00
|
|
|
"user",
|
|
|
|
)
|
2022-09-14 12:49:39 +00:00
|
|
|
):
|
|
|
|
os.makedirs(
|
|
|
|
os.path.join(
|
2022-10-02 01:57:29 +00:00
|
|
|
presets_dir,
|
2022-09-14 12:49:39 +00:00
|
|
|
"user",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if plugins_list is None:
|
|
|
|
plugins_list = {}
|
|
|
|
else:
|
|
|
|
plugins_list = plugins_list.save()
|
|
|
|
|
2022-11-20 16:15:56 +00:00
|
|
|
with open(self.preset_path, "w", encoding="utf-8") as file:
|
2022-09-14 12:49:39 +00:00
|
|
|
object_to_write = {
|
2022-10-11 18:34:04 +00:00
|
|
|
"name": self.display_name,
|
2022-09-14 12:49:39 +00:00
|
|
|
"variables": self.variables,
|
|
|
|
"palette": self.palette,
|
|
|
|
"custom_css": self.custom_css,
|
|
|
|
"plugins": plugins_list,
|
|
|
|
}
|
|
|
|
file.write(json.dumps(object_to_write, indent=4))
|
2022-11-20 16:15:56 +00:00
|
|
|
file.close()
|
2022-09-14 12:49:39 +00:00
|
|
|
|
2022-12-08 11:43:12 +00:00
|
|
|
# TODO: Add validation
|
2022-09-14 12:49:39 +00:00
|
|
|
def validate(self):
|
|
|
|
return True
|