2022-08-24 18:51:14 +00:00
|
|
|
# custom_presets.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/>.
|
|
|
|
|
|
|
|
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-10-02 01:55:42 +00:00
|
|
|
from .preset import presets_dir
|
|
|
|
from .utils import to_slug_case, buglog
|
2022-08-24 18:51:14 +00:00
|
|
|
|
2022-09-14 14:55:35 +00:00
|
|
|
|
2022-10-02 01:55:42 +00:00
|
|
|
# Open Soup3 session
|
|
|
|
session = Soup.Session()
|
2022-09-12 21:11:48 +00:00
|
|
|
|
2022-10-02 01:55:42 +00:00
|
|
|
def fetch_presets(repo) -> [dict, list]:
|
|
|
|
try:
|
|
|
|
request = Soup.Message.new("GET", repo)
|
|
|
|
body = session.send_and_read(request, None)
|
|
|
|
except GLib.GError as e: # offline
|
|
|
|
if e.code == 1:
|
|
|
|
buglog(f"Failed to establish a new connection. Exc: {e}")
|
|
|
|
return False, False
|
|
|
|
else:
|
|
|
|
buglog(f"Unhandled Libsoup3 GLib.GError error code {e.code}. Exc: {e}")
|
2022-09-12 21:11:21 +00:00
|
|
|
return False, False
|
2022-10-02 01:55:42 +00:00
|
|
|
'''except urllib3.exceptions.MaxRetryError as e:
|
|
|
|
buglog(f"Maximum number of retries exceeded. Exc: {e}")
|
|
|
|
return False, False'''
|
2022-09-11 12:22:46 +00:00
|
|
|
|
2022-10-02 01:55:42 +00:00
|
|
|
try:
|
|
|
|
raw = json.loads(body.get_data())
|
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
buglog(f"Error with decoding JSON data. Exc: {e}")
|
|
|
|
return False, False
|
2022-09-11 12:22:46 +00:00
|
|
|
|
2022-10-02 01:55:42 +00:00
|
|
|
preset_dict = {}
|
|
|
|
url_list = []
|
2022-08-24 18:51:14 +00:00
|
|
|
|
2022-10-02 01:55:42 +00:00
|
|
|
for data in raw.items():
|
|
|
|
data = list(data)
|
|
|
|
data.insert(0, to_slug_case(data[0]))
|
2022-08-24 18:51:14 +00:00
|
|
|
|
2022-10-02 01:55:42 +00:00
|
|
|
url = data[2]
|
|
|
|
data.pop(2) # Remove preset URL from list
|
2022-08-24 18:51:14 +00:00
|
|
|
|
2022-10-02 01:55:42 +00:00
|
|
|
to_dict = iter(data)
|
|
|
|
# Convert list back to dict
|
|
|
|
preset_dict.update(dict(zip(to_dict, to_dict)))
|
2022-08-24 18:51:14 +00:00
|
|
|
|
2022-10-02 01:55:42 +00:00
|
|
|
url_list.append(url)
|
2022-08-24 22:29:14 +00:00
|
|
|
|
2022-10-02 01:55:42 +00:00
|
|
|
return preset_dict, url_list
|
2022-08-24 18:51:14 +00:00
|
|
|
|
2022-10-02 01:55:42 +00:00
|
|
|
def download_preset(name, repo_name, repo) -> None:
|
|
|
|
try:
|
|
|
|
request = Soup.Message.new("GET", repo)
|
|
|
|
body = session.send_and_read(request, None)
|
|
|
|
except GLib.GError as e: # offline
|
|
|
|
if e.code == 1:
|
|
|
|
buglog(f"Failed to establish a new connection. Exc: {e}")
|
2022-09-12 21:11:21 +00:00
|
|
|
return False, False
|
2022-10-02 01:55:42 +00:00
|
|
|
else:
|
|
|
|
buglog(f"Unhandled Libsoup3 GLib.GError error code {e.code}. Exc: {e}")
|
|
|
|
return False, False
|
|
|
|
'''except urllib3.exceptions.MaxRetryError as e:
|
|
|
|
buglog(f"Maximum number of retries exceeded. Exc: {e}")
|
|
|
|
return False, False'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
raw = json.loads(body.get_data())
|
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
buglog(f"Error with decoding JSON data. Exc: {e}")
|
|
|
|
return False, False
|
|
|
|
|
|
|
|
data = json.dumps(raw)
|
|
|
|
|
|
|
|
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:
|
|
|
|
buglog(f"Failed to write data to a file. Exc: {e}")
|