-
Notifications
You must be signed in to change notification settings - Fork 1
/
theme.py
64 lines (55 loc) · 2.48 KB
/
theme.py
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
#!/usr/bin/env python3
import os
import glob
import pathlib
import oa_core as core
import re
import otfdlib
from collections import OrderedDict
def change(css_theme_path: str) -> None:
if os.path.exists(f"resource/css/{css_theme_path}") is False:
with open("resource/css/theme/light_theme.css", mode="r", encoding="utf-8_sig") as light_theme_file:
light_theme = light_theme_file.read()
with open(f"resource/css/{css_theme_path}", mode="w", encoding="utf-8_sig") as new_theme:
new_theme.write(light_theme)
with open("resource/css/theme_setting.css", mode="w", encoding="utf-8_sig") as css_file:
css_file.write(f"@import url('{css_theme_path}');")
core.write_setting("resource/setting/setting.json",
"theme", css_theme_path)
return
def return_dict() -> dict[str, str]:
result = {}
for file_path in glob.glob("resource/css/theme/**/*.css", recursive=True):
with open(file_path, mode="r", encoding="utf-8_sig") as f:
css_file = f.read()
p = pathlib.Path(file_path)
file_path = str(p.resolve().relative_to(
f"{p.cwd()}/resource/css")).replace("\\", "/")
result[file_path] = css_file.splitlines()[0].replace(
"/*", "").replace("*/", "").strip()
return result
def write_custom(theme_dictionary: dict[str, str]) -> None:
value = ""
for key in theme_dictionary:
value += f" {key}: {theme_dictionary[key]};\n"
content = "/* カスタムテーマ */\n\n:root {\n" + value + "}"
with open("resource/css/theme/user/custom_theme.css", mode="w", encoding="utf-8_sig") as f:
f.write(content)
return
def current() -> OrderedDict:
css_file_path = core.read_setting("resource/setting/setting.json", "theme")
with open(f"resource/css/{css_file_path}", mode="r", encoding="utf-8_sig") as f:
css = f.read()
pattern = re.compile(r"( *?--.*?:.*?;(\n|))+",
re.MULTILINE | re.DOTALL)
value = re.search(pattern, css).group()
delete_space_pattern = re.compile(r"^ *", re.MULTILINE | re.DOTALL)
value = re.sub(delete_space_pattern, "", value)
delete_semicolon_pattern = re.compile(r";$", re.MULTILINE | re.DOTALL)
value = re.sub(delete_semicolon_pattern, "", value)
value = re.sub(r" *: *", ":", value)
root = otfdlib.Otfd()
root.load_from_string(value)
root.parse()
result = root.read()
return result