forked from Hyacinthe-primus/File_Converter_Pro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme_manager.py
More file actions
253 lines (198 loc) · 7.65 KB
/
Copy paththeme_manager.py
File metadata and controls
253 lines (198 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""ThemeManager — Handle custom .fctheme files (ZIP format)."""
import configparser
import zipfile
from dataclasses import dataclass
from pathlib import Path
ROOT_DIR = Path(__file__).resolve().parent
THEMES_DIR = ROOT_DIR / "styles" / "themes"
CUSTOM_THEMES_DIR = ROOT_DIR / "styles" / "custom_themes"
REQUIRED_FILES = {
"style.qss",
"advanced_conversions.qss",
"advanced_group.qss",
"contact_links.css",
"dashboard.qss",
"lang_scroll.qss",
"scrollbar.qss",
"templates.qss",
"terms_html.css",
"terms.qss",
}
METADATA_FILE = "metadata.ini"
@dataclass
class ThemeMetadata:
name: str
author: str
version: str
description: str
preview: str
folder_name: str
kind: str = "light"
def _ensure_custom_dir():
CUSTOM_THEMES_DIR.mkdir(parents=True, exist_ok=True)
def get_builtin_themes() -> list[str]:
themes = []
for d in THEMES_DIR.iterdir():
if d.is_dir() and (d / "style.qss").exists():
themes.append(d.name)
return sorted(themes)
def get_custom_themes() -> list[ThemeMetadata]:
_ensure_custom_dir()
themes = []
for d in CUSTOM_THEMES_DIR.iterdir():
if not d.is_dir():
continue
meta = _read_metadata(d)
if meta:
themes.append(meta)
return sorted(themes, key=lambda t: t.name.lower())
def get_all_theme_names() -> list[str]:
return get_builtin_themes() + [t.folder_name for t in get_custom_themes()]
def get_custom_theme_kind(folder_name: str) -> str:
theme_dir = CUSTOM_THEMES_DIR / folder_name
if not theme_dir.is_dir():
return "light"
meta = _read_metadata(theme_dir)
return meta.kind if meta else "light"
def get_theme_path(theme_name: str) -> Path | None:
builtin = THEMES_DIR / theme_name
if builtin.is_dir() and (builtin / "style.qss").exists():
return builtin
custom = CUSTOM_THEMES_DIR / theme_name
if custom.is_dir() and (custom / "style.qss").exists():
return custom
return None
def import_theme(zip_path: str) -> tuple[bool, str]:
try:
with zipfile.ZipFile(zip_path, "r") as zf:
names = zf.namelist()
qss_in_zip = [n for n in names if Path(n).name in REQUIRED_FILES]
qss_dirs = {}
for n in qss_in_zip:
parts = n.split("/")
if len(parts) >= 2:
d = parts[0]
qss_dirs.setdefault(d, []).append(n)
if qss_dirs:
theme_dir_name = next(iter(qss_dirs))
inner_prefix = theme_dir_name + "/"
inner_names = [
n[len(inner_prefix) :] for n in names if n.startswith(inner_prefix) and n != inner_prefix
]
else:
theme_dir_name = ""
inner_prefix = ""
inner_names = [n for n in names if not n.endswith("/")]
meta_name = None
meta_prefix = inner_prefix
for n in inner_names:
if n == METADATA_FILE:
meta_name = n
break
if not meta_name:
for n in names:
if n == METADATA_FILE:
meta_name = n
meta_prefix = ""
break
if not meta_name:
return False, "Metadata file missing in the theme."
meta_content = zf.read(meta_prefix + meta_name).decode("utf-8")
meta = _parse_metadata_content(meta_content)
if not meta:
return False, "Unable to read metadata.ini."
folder_name = _sanitize_folder_name(meta.name)
theme_dir = CUSTOM_THEMES_DIR / folder_name
if theme_dir.exists():
import shutil
counter = 2
while (CUSTOM_THEMES_DIR / f"{folder_name}_{counter}").exists():
counter += 1
folder_name = f"{folder_name}_{counter}"
theme_dir = CUSTOM_THEMES_DIR / folder_name
theme_dir.mkdir(parents=True)
qss_files_found = set()
dest = theme_dir / METADATA_FILE
with open(dest, "w", encoding="utf-8") as dst:
dst.write(meta_content)
for n in inner_names:
if n.endswith("/"):
continue
basename = Path(n).name
if basename.lower().endswith((".png", ".jpg", ".jpeg", ".webp")):
full_path = inner_prefix + n
dest = theme_dir / basename
with zf.open(full_path) as src, open(dest, "wb") as dst:
dst.write(src.read())
continue
if basename in REQUIRED_FILES:
qss_files_found.add(basename)
full_path = inner_prefix + n
dest = theme_dir / basename
with zf.open(full_path) as src, open(dest, "wb") as dst:
dst.write(src.read())
for n in names:
basename = Path(n).name
if basename.lower().endswith((".png", ".jpg", ".jpeg", ".webp")):
if n.startswith(inner_prefix) and inner_prefix:
continue
full_path = n
dest = theme_dir / basename
with zf.open(full_path) as src, open(dest, "wb") as dst:
dst.write(src.read())
missing = REQUIRED_FILES - qss_files_found
if missing:
import shutil
shutil.rmtree(theme_dir)
return False, f"Missing files : {', '.join(sorted(missing))}"
return True, meta.name
except zipfile.BadZipFile:
return False, "The file is not a valid ZIP archive."
except Exception as e:
return False, f"Error during import : {e}"
def remove_theme(folder_name: str) -> tuple[bool, str]:
theme_dir = CUSTOM_THEMES_DIR / folder_name
if not theme_dir.exists():
return False, "Theme not found."
import shutil
shutil.rmtree(theme_dir)
return True, folder_name
def _read_metadata(theme_dir: Path) -> ThemeMetadata | None:
for f in theme_dir.iterdir():
if f.suffix == ".ini" and f.name.startswith("metadata"):
try:
content = f.read_text(encoding="utf-8")
return _parse_metadata_content(content, theme_dir.name)
except Exception:
return None
return None
def _parse_metadata_content(content: str, folder_name: str = "") -> ThemeMetadata | None:
cp = configparser.ConfigParser()
cp.read_string(content)
if not cp.has_section("theme"):
return None
kind = cp.get("theme", "kind", fallback="light").lower()
if kind not in ("light", "dark"):
kind = "light"
return ThemeMetadata(
name=cp.get("theme", "name", fallback="Unknown Theme"),
author=cp.get("theme", "author", fallback=""),
version=cp.get("theme", "version", fallback="1.0"),
description=cp.get("theme", "description", fallback=""),
preview=cp.get("theme", "preview", fallback=""),
folder_name=folder_name,
kind=kind,
)
def _sanitize_folder_name(name: str) -> str:
import re
s = re.sub(r"[^\w\s-]", "", name)
s = re.sub(r"\s+", "_", s.strip())
return s[:50] or "custom_theme"
def read_theme_preview(theme_path: Path) -> bytes | None:
meta = _read_metadata(theme_path)
if not meta or not meta.preview:
return None
preview_path = theme_path / meta.preview
if not preview_path.exists():
return None
return preview_path.read_bytes()