mirror of
https://github.com/Radiquum/anixart-patcher.git
synced 2025-09-06 11:13:51 +05:00
refactor: tqdm -> rich.progress
This commit is contained in:
parent
43b1406b4a
commit
7b5ba163bd
10 changed files with 260 additions and 141 deletions
|
@ -1,73 +1,88 @@
|
|||
"""Change app color theme"""
|
||||
|
||||
# patch settings
|
||||
# priority, default: -99 (run before last)
|
||||
priority = -99
|
||||
|
||||
import os
|
||||
# imports
|
||||
## bundled
|
||||
from typing import TypedDict
|
||||
from beaupy import select
|
||||
from tqdm import tqdm
|
||||
|
||||
## installed
|
||||
from lxml import etree
|
||||
|
||||
## custom
|
||||
from config import config, log, console
|
||||
|
||||
|
||||
class PatchConfig_ChangeColorThemeValue(TypedDict):
|
||||
attributes: list[dict[str, str]]
|
||||
text: list[dict[str, str]]
|
||||
files: list[dict[str, str]]
|
||||
|
||||
|
||||
class PatchConfig_ChangeColorTheme(TypedDict):
|
||||
src: str
|
||||
themes: str
|
||||
themes: list[str]
|
||||
key: PatchConfig_ChangeColorThemeValue
|
||||
|
||||
def apply(config: PatchConfig_ChangeColorTheme) -> bool:
|
||||
print("select color theme to apply")
|
||||
|
||||
theme = select(config["themes"], cursor="->", cursor_style="cyan")
|
||||
theme_attr = config[theme]['attributes']
|
||||
theme_text = config[theme]['text']
|
||||
theme_files = config[theme]['files']
|
||||
def apply(patch_config: PatchConfig_ChangeColorTheme) -> bool:
|
||||
|
||||
with tqdm(
|
||||
total=len(theme_attr),
|
||||
unit="attr",
|
||||
unit_divisor=1,
|
||||
desc="color attributes"
|
||||
) as bar:
|
||||
for attr in theme_attr:
|
||||
parser = etree.XMLParser(remove_blank_text=True)
|
||||
tree = etree.parse(f"{config['src']}/{attr['file_path']}", parser)
|
||||
root = tree.getroot()
|
||||
root.find(attr['tag_path']).set(attr['attr_name'], attr['attr_value']['to'])
|
||||
tree.write(
|
||||
f"{config['src']}/{attr['file_path']}",
|
||||
pretty_print=True,
|
||||
xml_declaration=True,
|
||||
encoding="utf-8",
|
||||
)
|
||||
bar.update()
|
||||
|
||||
with tqdm(
|
||||
total=len(theme_text),
|
||||
unit="attr",
|
||||
unit_divisor=1,
|
||||
desc="color values"
|
||||
) as bar:
|
||||
for text in theme_text:
|
||||
parser = etree.XMLParser(remove_blank_text=True)
|
||||
tree = etree.parse(f"{config['src']}/{text['file_path']}", parser)
|
||||
root = tree.getroot()
|
||||
root.find(text['tag_path']).text = text['text']['to']
|
||||
tree.write(
|
||||
f"{config['src']}/{text['file_path']}",
|
||||
pretty_print=True,
|
||||
xml_declaration=True,
|
||||
encoding="utf-8",
|
||||
)
|
||||
bar.update()
|
||||
console.print("select color theme to apply (press [bold]enter[/bold] to confirm)")
|
||||
theme = select(patch_config["themes"], cursor="->", cursor_style="cyan")
|
||||
if not theme:
|
||||
console.print(f"theme: default")
|
||||
return False
|
||||
console.print(f"theme: {theme}")
|
||||
|
||||
theme_attr = patch_config[theme]["attributes"]
|
||||
theme_text = patch_config[theme]["text"]
|
||||
theme_files = patch_config[theme]["files"]
|
||||
|
||||
for item in theme_attr:
|
||||
parser = etree.XMLParser(remove_blank_text=True)
|
||||
tree = etree.parse(
|
||||
f"{config['folders']['decompiled']}/{item['file_path']}", parser
|
||||
)
|
||||
root = tree.getroot()
|
||||
root.find(item["tag_path"]).set(item["attr_name"], item["attr_value"]["to"])
|
||||
tree.write(
|
||||
f"{config['folders']['decompiled']}/{item['file_path']}",
|
||||
pretty_print=True,
|
||||
xml_declaration=True,
|
||||
encoding="utf-8",
|
||||
)
|
||||
log.debug(
|
||||
f"[CHANGE_COLOR_THEME/ATTRIBUTES] set attribute `{item['attr_name']}` from `{item['attr_value']['from']}` to `{item['attr_value']['to']}`"
|
||||
)
|
||||
|
||||
for item in theme_text:
|
||||
parser = etree.XMLParser(remove_blank_text=True)
|
||||
tree = etree.parse(
|
||||
f"{config['folders']['decompiled']}/{item['file_path']}", parser
|
||||
)
|
||||
root = tree.getroot()
|
||||
root.find(item["tag_path"]).text = item["text"]["to"]
|
||||
tree.write(
|
||||
f"{config['folders']['decompiled']}/{item['file_path']}",
|
||||
pretty_print=True,
|
||||
xml_declaration=True,
|
||||
encoding="utf-8",
|
||||
)
|
||||
log.debug(
|
||||
f"[CHANGE_COLOR_THEME/VALUES] set text from `{item['text']['from']}` to `{item['text']['to']}`"
|
||||
)
|
||||
|
||||
if len(theme_files) > 0:
|
||||
with tqdm(
|
||||
total=len(theme_files),
|
||||
unit="files",
|
||||
unit_divisor=1,
|
||||
desc="color files"
|
||||
) as bar:
|
||||
for file in theme_files:
|
||||
with open(f"{config['src']}/{file['file_path']}", "w", encoding="utf-8") as f:
|
||||
f.write("\n".join(file['file_content']))
|
||||
bar.update()
|
||||
|
||||
return True
|
||||
for item in theme_files:
|
||||
with open(
|
||||
f"{config['folders']['decompiled']}/{item['file_path']}",
|
||||
"w",
|
||||
encoding="utf-8",
|
||||
) as f:
|
||||
f.write("\n".join(item["file_content"]))
|
||||
log.debug(f"[CHANGE_COLOR_THEME/FILES] replaced file {item['file_path']}")
|
||||
|
||||
log.debug(f"[CHANGE_COLOR_THEME] color theme `{theme}` has been applied")
|
||||
return True
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue