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