Skip to content

Commit 662c2c6

Browse files
author
NeoDev
authored
Add loading theme for settings
1 parent 0e9aa0f commit 662c2c6

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

assets/themes/loadThemes.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import json
2+
import os
3+
import importlib
4+
import gradio as gr
5+
6+
now_dir = os.getcwd()
7+
8+
folder = os.path.join(now_dir, "assets", "themes")
9+
config_file = os.path.join(now_dir, "assets", "config.json")
10+
11+
import sys
12+
13+
sys.path.append(folder)
14+
15+
16+
def get_class(filename):
17+
with open(filename, "r", encoding="utf8") as file:
18+
for line_number, line in enumerate(file, start=1):
19+
if "class " in line:
20+
found = line.split("class ")[1].split(":")[0].split("(")[0].strip()
21+
return found
22+
break
23+
return None
24+
25+
26+
def get_list():
27+
28+
themes_from_files = [
29+
os.path.splitext(name)[0]
30+
for root, _, files in os.walk(folder, topdown=False)
31+
for name in files
32+
if name.endswith(".py") and root == folder and name != "loadThemes.py"
33+
]
34+
35+
json_file_path = os.path.join(folder, "themes_list.json")
36+
37+
try:
38+
with open(json_file_path, "r", encoding="utf8") as json_file:
39+
themes_from_url = [item["id"] for item in json.load(json_file)]
40+
except FileNotFoundError:
41+
themes_from_url = []
42+
43+
combined_themes = set(themes_from_files + themes_from_url)
44+
45+
return list(combined_themes)
46+
47+
48+
def select_theme(name):
49+
selected_file = name + ".py"
50+
full_path = os.path.join(folder, selected_file)
51+
52+
if not os.path.exists(full_path):
53+
with open(config_file, "r", encoding="utf8") as json_file:
54+
config_data = json.load(json_file)
55+
56+
config_data["theme"]["file"] = None
57+
config_data["theme"]["class"] = name
58+
59+
with open(config_file, "w", encoding="utf8") as json_file:
60+
json.dump(config_data, json_file, indent=2)
61+
print(f"Theme {name} successfully selected, restart the App.")
62+
gr.Info(f"Theme {name} successfully selected, restart the App.")
63+
return
64+
65+
class_found = get_class(full_path)
66+
if class_found:
67+
with open(config_file, "r", encoding="utf8") as json_file:
68+
config_data = json.load(json_file)
69+
70+
config_data["theme"]["file"] = selected_file
71+
config_data["theme"]["class"] = class_found
72+
73+
with open(config_file, "w", encoding="utf8") as json_file:
74+
json.dump(config_data, json_file, indent=2)
75+
print(f"Theme {name} successfully selected, restart the App.")
76+
gr.Info(f"Theme {name} successfully selected, restart the App.")
77+
else:
78+
print(f"Theme {name} was not found.")
79+
80+
81+
def read_json():
82+
try:
83+
with open(config_file, "r", encoding="utf8") as json_file:
84+
data = json.load(json_file)
85+
selected_file = data["theme"]["file"]
86+
class_name = data["theme"]["class"]
87+
88+
if selected_file is not None and class_name:
89+
return class_name
90+
elif selected_file == None and class_name:
91+
return class_name
92+
else:
93+
return "NoCrypt/miku"
94+
except Exception as error:
95+
print(f"An error occurred loading the theme: {error}")
96+
return "NoCrypt/miku"
97+
98+
99+
def load_json():
100+
try:
101+
with open(config_file, "r", encoding="utf8") as json_file:
102+
data = json.load(json_file)
103+
selected_file = data["theme"]["file"]
104+
class_name = data["theme"]["class"]
105+
106+
if selected_file is not None and class_name:
107+
module = importlib.import_module(selected_file[:-3])
108+
obtained_class = getattr(module, class_name)
109+
instance = obtained_class()
110+
print(f"Theme {class_name} successfully loaded.")
111+
return instance
112+
elif selected_file == None and class_name:
113+
return class_name
114+
else:
115+
print("The theme is incorrect.")
116+
return None
117+
except Exception as error:
118+
print(f"An error occurred loading the theme: {error}")
119+
return None

0 commit comments

Comments
 (0)