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