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-09-10 14:00:57 +00:00
|
|
|
from .utils import to_slug_case, buglog
|
2022-08-24 18:51:14 +00:00
|
|
|
|
|
|
|
|
2022-09-12 21:11:21 +00:00
|
|
|
import aiohttp
|
|
|
|
import asyncio
|
2022-08-24 18:51:14 +00:00
|
|
|
|
2022-09-12 21:11:48 +00:00
|
|
|
|
|
|
|
|
2022-09-12 21:11:21 +00:00
|
|
|
async def main(repo):
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
try:
|
2022-09-14 13:38:01 +00:00
|
|
|
async with session.get(repo) as http:
|
|
|
|
try:
|
|
|
|
raw = json.loads(await http.text())
|
|
|
|
except json.JSONDecodeError as error:
|
|
|
|
buglog(f"Error with decoding JSON data. Exc: {error}")
|
|
|
|
return False, False
|
2022-09-12 21:11:21 +00:00
|
|
|
except aiohttp.ClientError as error:
|
|
|
|
buglog(f"Failed to establish a new connection. Exc: {error}")
|
|
|
|
return False, False
|
2022-09-11 12:22:46 +00:00
|
|
|
|
2022-09-12 21:11:21 +00:00
|
|
|
preset_dict = {}
|
|
|
|
url_list = []
|
2022-09-11 12:22:46 +00:00
|
|
|
|
2022-09-12 21:11:21 +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-09-12 21:11:21 +00:00
|
|
|
url = data[2]
|
|
|
|
data.pop(2) # Remove preset URL from list
|
2022-08-24 18:51:14 +00:00
|
|
|
|
2022-09-12 21:11:21 +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-09-12 21:11:21 +00:00
|
|
|
url_list.append(url)
|
2022-08-24 18:51:14 +00:00
|
|
|
|
2022-09-12 21:11:21 +00:00
|
|
|
return preset_dict, url_list
|
2022-08-24 22:29:14 +00:00
|
|
|
|
2022-08-24 18:51:14 +00:00
|
|
|
|
2022-09-12 21:11:21 +00:00
|
|
|
# TODO: Modify functions to be asynchronous
|
2022-08-24 22:29:14 +00:00
|
|
|
|
2022-09-11 12:22:46 +00:00
|
|
|
|
2022-09-12 21:11:21 +00:00
|
|
|
def fetch_presets(repo) -> [dict, list]:
|
|
|
|
return asyncio.run(main(repo))
|
|
|
|
|
2022-09-12 21:11:48 +00:00
|
|
|
|
2022-09-12 21:11:21 +00:00
|
|
|
async def _download_preset(name, repo_name, url) -> None:
|
2022-09-12 21:11:48 +00:00
|
|
|
async with aiohttp.ClientSession() as session:
|
2022-09-12 21:11:21 +00:00
|
|
|
try:
|
2022-09-14 13:38:01 +00:00
|
|
|
async with session.get(url) as http:
|
|
|
|
try:
|
|
|
|
raw = json.loads(await http.text())
|
|
|
|
except json.JSONDecodeError as error:
|
|
|
|
buglog(f"Error with decoding JSON data. Exc: {error}")
|
|
|
|
return False, False
|
2022-09-12 21:11:21 +00:00
|
|
|
except aiohttp.ClientError as error:
|
|
|
|
buglog(f"Failed to establish a new connection. Exc: {error}")
|
|
|
|
return False, False
|
|
|
|
|
|
|
|
data = json.dumps(raw)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(
|
|
|
|
os.path.join(
|
|
|
|
os.environ.get("XDG_CONFIG_HOME",
|
2022-09-12 21:11:48 +00:00
|
|
|
os.environ["HOME"] + "/.config"),
|
2022-09-12 21:11:21 +00:00
|
|
|
"presets",
|
|
|
|
repo_name,
|
|
|
|
to_slug_case(name) + ".json",
|
|
|
|
),
|
|
|
|
"w",
|
|
|
|
encoding="utf-8",
|
|
|
|
) as f:
|
|
|
|
f.write(data)
|
|
|
|
f.close()
|
|
|
|
except OSError as error:
|
|
|
|
buglog(f"Failed to write data to a file. Exc: {error}")
|
|
|
|
|
2022-09-12 21:11:48 +00:00
|
|
|
|
2022-09-10 14:00:57 +00:00
|
|
|
def download_preset(name, repo_name, url) -> None:
|
2022-09-12 21:11:21 +00:00
|
|
|
asyncio.run(_download_preset(name, repo_name, url))
|