2022-12-01 18:29:15 +00:00
|
|
|
# preset_downloader.py
|
2022-08-24 18:51:14 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
|
2022-10-02 01:55:42 +00:00
|
|
|
from gi.repository import GLib, Soup
|
2022-08-24 18:51:14 +00:00
|
|
|
|
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.utils.common import to_slug_case
|
|
|
|
|
|
|
|
from gradience.backend.logger import Logger
|
|
|
|
|
|
|
|
logging = Logger()
|
2022-08-24 18:51:14 +00:00
|
|
|
|
2022-09-14 14:55:35 +00:00
|
|
|
|
2022-12-11 16:51:33 +00:00
|
|
|
class PresetDownloader:
|
|
|
|
def __init__(self):
|
2022-12-17 17:42:29 +00:00
|
|
|
# Open Soup3 session
|
|
|
|
self.session = Soup.Session()
|
2022-12-11 16:51:33 +00:00
|
|
|
|
2022-12-17 17:42:29 +00:00
|
|
|
def fetch_presets(self, repo) -> [dict, list]:
|
2022-12-11 16:51:33 +00:00
|
|
|
try:
|
|
|
|
request = Soup.Message.new("GET", repo)
|
|
|
|
body = self.session.send_and_read(request, None)
|
2022-12-17 17:42:29 +00:00
|
|
|
except GLib.GError as e:
|
|
|
|
if e.code == 1: # offline
|
2022-12-27 20:28:58 +00:00
|
|
|
logging.error("Failed to establish a new connection.", exc=e)
|
2022-12-17 17:42:29 +00:00
|
|
|
raise
|
2022-12-11 16:51:33 +00:00
|
|
|
else:
|
2022-12-26 21:26:14 +00:00
|
|
|
logging.error(f"Unhandled Libsoup3 GLib.GError error code {e.code}.", exc=e)
|
2022-12-17 17:42:29 +00:00
|
|
|
raise
|
2022-12-11 16:51:33 +00:00
|
|
|
try:
|
|
|
|
raw = json.loads(body.get_data())
|
|
|
|
except json.JSONDecodeError as e:
|
2022-12-27 20:28:58 +00:00
|
|
|
logging.error("Error while decoding JSON data.", exc=e)
|
2022-12-17 17:42:29 +00:00
|
|
|
raise
|
2022-08-24 18:51:14 +00:00
|
|
|
|
2022-12-11 16:51:33 +00:00
|
|
|
preset_dict = {}
|
|
|
|
url_list = []
|
|
|
|
|
|
|
|
for data in raw.items():
|
|
|
|
data = list(data)
|
|
|
|
data.insert(0, to_slug_case(data[0]))
|
|
|
|
|
|
|
|
url = data[2]
|
|
|
|
data.pop(2) # Remove preset URL from list
|
|
|
|
|
|
|
|
to_dict = iter(data)
|
|
|
|
# Convert list back to dict
|
|
|
|
preset_dict.update(dict(zip(to_dict, to_dict)))
|
|
|
|
|
|
|
|
url_list.append(url)
|
|
|
|
|
|
|
|
return preset_dict, url_list
|
|
|
|
|
|
|
|
def download_preset(self, name, repo_name, repo) -> None:
|
|
|
|
try:
|
|
|
|
request = Soup.Message.new("GET", repo)
|
|
|
|
body = self.session.send_and_read(request, None)
|
2022-12-17 17:42:29 +00:00
|
|
|
except GLib.GError as e:
|
|
|
|
if e.code == 1: # offline
|
2022-12-27 20:28:58 +00:00
|
|
|
logging.error("Failed to establish a new connection.", exc=e)
|
2022-12-11 16:51:33 +00:00
|
|
|
raise
|
|
|
|
else:
|
2022-12-26 21:26:14 +00:00
|
|
|
logging.error(f"Unhandled Libsoup3 GLib.GError error code {e.code}.", exc=e)
|
2022-12-11 16:51:33 +00:00
|
|
|
raise
|
|
|
|
try:
|
|
|
|
raw = json.loads(body.get_data())
|
|
|
|
except json.JSONDecodeError as e:
|
2022-12-27 20:28:58 +00:00
|
|
|
logging.error("Error while decoding JSON data.", exc=e)
|
2022-12-11 16:51:33 +00:00
|
|
|
raise
|
|
|
|
|
|
|
|
data = json.dumps(raw, indent=4)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(
|
|
|
|
os.path.join(
|
|
|
|
presets_dir,
|
|
|
|
repo_name,
|
|
|
|
to_slug_case(name) + ".json",
|
|
|
|
),
|
|
|
|
"w",
|
|
|
|
encoding="utf-8",
|
|
|
|
) as f:
|
|
|
|
f.write(data)
|
|
|
|
f.close()
|
|
|
|
except OSError as e:
|
2022-12-27 20:28:58 +00:00
|
|
|
logging.error("Failed to write data to a file.", exc=e)
|
2022-12-11 16:51:33 +00:00
|
|
|
raise
|