Gradience/gradience/utils/preset.py

142 lines
4.5 KiB
Python
Raw Normal View History

# 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
from gradience.settings_schema import settings_schema
2022-10-10 19:45:47 +00:00
from gradience.utils.utils import buglog, to_slug_case
2022-09-14 12:49:39 +00:00
presets_dir = os.path.join(
2022-09-14 12:49:39 +00:00
os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config"),
"presets",
)
class Preset:
variables = {}
palette = {}
custom_css = {
"gtk4": "",
"gtk3": "",
}
plugins = {}
display_name = "New Preset"
preset_path = "new_preset"
2022-09-15 18:12:34 +00:00
badges = {}
2022-09-14 12:49:39 +00:00
def __init__(self, preset_path=None, text=None, preset=None):
if preset_path:
self.load_preset(preset_path=preset_path)
elif text: # load from resource
2022-09-14 12:49:39 +00:00
self.load_preset(text=text)
elif preset: # css or dict
2022-09-14 12:49:39 +00:00
self.load_preset(preset=preset)
else:
raise Exception("Failed to create a new Preset object: Preset created without content")
2022-09-14 12:49:39 +00:00
def load_preset(self, preset_path=None, text=None, preset=None):
2022-09-14 12:49:39 +00:00
try:
if not preset:
if text:
preset_text = text
elif preset_path:
self.preset_path = preset_path
with open(self.preset_path, "r", encoding="utf-8") as file:
2022-09-14 12:49:39 +00:00
preset_text = file.read()
file.close()
else:
raise Exception("load_preset must be called with a path, text, or preset")
2022-09-14 12:49:39 +00:00
preset = json.loads(preset_text)
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] = ""
except Exception as e:
if self.preset_path:
buglog(f"Failed to load preset {self.preset_path}. Exc: {e}")
else:
buglog(f"Failed to load preset with unknown path. Exc: {e}")
2022-09-14 12:49:39 +00:00
2022-11-20 03:46:55 +00:00
# Rename an existing preset
def rename_preset(self, name):
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")
self.save_preset(to=self.preset_path)
os.remove(old_path)
2022-11-20 03:46:55 +00:00
# Save a new user preset (or overwrite one)
2022-09-14 12:49:39 +00:00
def save_preset(self, name=None, plugins_list=None, to=None):
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"
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(
os.path.join(
presets_dir,
"user",
)
2022-09-14 12:49:39 +00:00
):
os.makedirs(
os.path.join(
presets_dir,
2022-09-14 12:49:39 +00:00
"user",
)
)
if plugins_list is None:
plugins_list = {}
else:
plugins_list = plugins_list.save()
with open(self.preset_path, "w", encoding="utf-8") as file:
2022-09-14 12:49:39 +00:00
object_to_write = {
"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))
file.close()
2022-09-14 12:49:39 +00:00
def validate(self):
return True