You've already forked HeurAMS-legacy
style: 代码格式化
This commit is contained in:
@@ -4,17 +4,20 @@ from .screens.dashboard import DashboardScreen
|
||||
from .screens.nucreator import NucleonCreatorScreen
|
||||
from .screens.precache import PrecachingScreen
|
||||
from .screens.about import AboutScreen
|
||||
|
||||
|
||||
class HeurAMSApp(App):
|
||||
TITLE = "潜进"
|
||||
#CSS_PATH = str(cxt.rootdir / "interface" / "css" / "main.css")
|
||||
# CSS_PATH = str(cxt.rootdir / "interface" / "css" / "main.css")
|
||||
SUB_TITLE = "启发式先进记忆调度器"
|
||||
BINDINGS = [("q", "quit", "退出"),
|
||||
("d", "toggle_dark", "改变色调"),
|
||||
("1", "app.push_screen('dashboard')", "仪表盘"),
|
||||
("2", "app.push_screen('precache_all')", "缓存管理器"),
|
||||
("3", "app.push_screen('nucleon_creator')", "创建新单元"),
|
||||
("0", "app.push_screen('about')", "版本信息"),
|
||||
]
|
||||
BINDINGS = [
|
||||
("q", "quit", "退出"),
|
||||
("d", "toggle_dark", "改变色调"),
|
||||
("1", "app.push_screen('dashboard')", "仪表盘"),
|
||||
("2", "app.push_screen('precache_all')", "缓存管理器"),
|
||||
("3", "app.push_screen('nucleon_creator')", "创建新单元"),
|
||||
("0", "app.push_screen('about')", "版本信息"),
|
||||
]
|
||||
SCREENS = {
|
||||
"dashboard": DashboardScreen,
|
||||
"nucleon_creator": NucleonCreatorScreen,
|
||||
@@ -28,16 +31,19 @@ class HeurAMSApp(App):
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
self.exit(event.button.id)
|
||||
|
||||
|
||||
def environment_check():
|
||||
from pathlib import Path
|
||||
|
||||
for i in config_var.get()["paths"].values():
|
||||
i = Path(i)
|
||||
if not i.exists():
|
||||
print(f"创建 {i}")
|
||||
i.mkdir(exist_ok = True, parents = True)
|
||||
i.mkdir(exist_ok=True, parents=True)
|
||||
else:
|
||||
print(f"找到 {i}")
|
||||
|
||||
|
||||
def is_subdir(parent, child):
|
||||
try:
|
||||
child.relative_to(parent)
|
||||
@@ -45,12 +51,14 @@ def is_subdir(parent, child):
|
||||
except:
|
||||
return 0
|
||||
|
||||
|
||||
# 开发模式
|
||||
from heurams.context import rootdir, workdir, config_var
|
||||
from pathlib import Path
|
||||
from heurams.context import rootdir
|
||||
import os
|
||||
if is_subdir(Path(rootdir),Path(os.getcwd())):
|
||||
|
||||
if is_subdir(Path(rootdir), Path(os.getcwd())):
|
||||
os.chdir(Path(rootdir) / ".." / "..")
|
||||
print(f'转入开发数据目录: {Path(rootdir)/".."/".."}')
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ from textual.screen import Screen
|
||||
import heurams.services.version as version
|
||||
from heurams.context import *
|
||||
|
||||
|
||||
class AboutScreen(Screen):
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
|
||||
@@ -21,6 +21,7 @@ from .about import AboutScreen
|
||||
|
||||
import pathlib
|
||||
|
||||
|
||||
class DashboardScreen(Screen):
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
@@ -31,7 +32,9 @@ class DashboardScreen(Screen):
|
||||
Label(f'时区修正: UTC+{config_var.get()["timezone_offset"] / 3600}'),
|
||||
Label("选择待学习或待修改的记忆单元集:", classes="title-label"),
|
||||
ListView(id="union-list", classes="union-list-view"),
|
||||
Label(f'"潜进" 开放源代码软件项目 | 版本 {version.ver} {version.codename.capitalize()} | Wang Zhiyu 2025'),
|
||||
Label(
|
||||
f'"潜进" 开放源代码软件项目 | 版本 {version.ver} {version.codename.capitalize()} | Wang Zhiyu 2025'
|
||||
),
|
||||
)
|
||||
yield Footer()
|
||||
|
||||
@@ -46,17 +49,20 @@ class DashboardScreen(Screen):
|
||||
res[0] = f"{filename}\0"
|
||||
from heurams.kernel.particles.loader import load_electron
|
||||
import heurams.kernel.particles as pt
|
||||
electron_file_path = (pathlib.Path(config_var.get()["paths"]["electron_dir"]) / (filestem + ".json"))
|
||||
if electron_file_path.exists(): # 未找到则创建电子文件 (json)
|
||||
|
||||
electron_file_path = pathlib.Path(config_var.get()["paths"]["electron_dir"]) / (
|
||||
filestem + ".json"
|
||||
)
|
||||
if electron_file_path.exists(): # 未找到则创建电子文件 (json)
|
||||
pass
|
||||
else:
|
||||
electron_file_path.touch()
|
||||
with open(electron_file_path, 'w') as f:
|
||||
with open(electron_file_path, "w") as f:
|
||||
f.write("{}")
|
||||
electron_dict = load_electron(path=electron_file_path) # TODO: 取消硬编码扩展名
|
||||
electron_dict = load_electron(path=electron_file_path) # TODO: 取消硬编码扩展名
|
||||
is_due = 0
|
||||
is_activated = 0
|
||||
nextdate = 0x3f3f3f3f
|
||||
nextdate = 0x3F3F3F3F
|
||||
for i in electron_dict.values():
|
||||
i: pt.Electron
|
||||
if i.is_due():
|
||||
@@ -78,12 +84,18 @@ class DashboardScreen(Screen):
|
||||
if len(probe["nucleon"]):
|
||||
for file in probe["nucleon"]:
|
||||
text = self.item_desc_generator(file)
|
||||
union_list_widget.append(ListItem(
|
||||
Label(text[0] + '\n' + text[1]),
|
||||
))
|
||||
union_list_widget.append(
|
||||
ListItem(
|
||||
Label(text[0] + "\n" + text[1]),
|
||||
)
|
||||
)
|
||||
else:
|
||||
union_list_widget.append(
|
||||
ListItem(Static("在 ./nucleon/ 中未找到任何内容源数据文件.\n请放置文件后重启应用.\n或者新建空的单元集."))
|
||||
ListItem(
|
||||
Static(
|
||||
"在 ./nucleon/ 中未找到任何内容源数据文件.\n请放置文件后重启应用.\n或者新建空的单元集."
|
||||
)
|
||||
)
|
||||
)
|
||||
union_list_widget.disabled = True
|
||||
|
||||
@@ -95,27 +107,36 @@ class DashboardScreen(Screen):
|
||||
if "未找到任何 .toml 文件" in str(selected_label.renderable):
|
||||
return
|
||||
|
||||
selected_filename = pathlib.Path(str(selected_label.renderable)
|
||||
.partition('\0')[0] # 文件名末尾截断, 保留文件名
|
||||
.replace('*', "")) # 去除markdown加粗
|
||||
selected_filename = pathlib.Path(
|
||||
str(selected_label.renderable)
|
||||
.partition("\0")[0] # 文件名末尾截断, 保留文件名
|
||||
.replace("*", "")
|
||||
) # 去除markdown加粗
|
||||
|
||||
nucleon_file_path = pathlib.Path(config_var.get()["paths"]["nucleon_dir"]) / selected_filename
|
||||
electron_file_path = pathlib.Path(config_var.get()["paths"]["electron_dir"]) / (str(selected_filename.stem) + ".json")
|
||||
nucleon_file_path = (
|
||||
pathlib.Path(config_var.get()["paths"]["nucleon_dir"]) / selected_filename
|
||||
)
|
||||
electron_file_path = pathlib.Path(config_var.get()["paths"]["electron_dir"]) / (
|
||||
str(selected_filename.stem) + ".json"
|
||||
)
|
||||
self.app.push_screen(PreparationScreen(nucleon_file_path, electron_file_path))
|
||||
|
||||
def on_button_pressed(self, event) -> None:
|
||||
if event.button.id == "new_nucleon_button":
|
||||
# 切换到创建单元
|
||||
from .nucreator import NucleonCreatorScreen
|
||||
|
||||
newscr = NucleonCreatorScreen()
|
||||
self.app.push_screen(newscr)
|
||||
elif event.button.id == "precache_all_button":
|
||||
# 切换到缓存管理器
|
||||
from .precache import PrecachingScreen
|
||||
|
||||
precache_screen = PrecachingScreen()
|
||||
self.app.push_screen(precache_screen)
|
||||
elif event.button.id == "about_button":
|
||||
from .about import AboutScreen
|
||||
|
||||
about_screen = AboutScreen()
|
||||
self.app.push_screen(about_screen)
|
||||
|
||||
|
||||
@@ -12,10 +12,12 @@ import heurams.kernel.particles as pt
|
||||
import heurams.kernel.puzzles as pz
|
||||
from .. import shim
|
||||
|
||||
|
||||
class AtomState(Enum):
|
||||
FAILED = auto()
|
||||
NORMAL = auto()
|
||||
|
||||
|
||||
class MemScreen(Screen):
|
||||
BINDINGS = [
|
||||
("q", "pop_screen", "返回"),
|
||||
@@ -27,15 +29,21 @@ class MemScreen(Screen):
|
||||
if config_var.get()["quick_pass"]:
|
||||
BINDINGS.append(("k", "quick_pass", "跳过"))
|
||||
rating = reactive(-1)
|
||||
def __init__(self, atoms: list, name: str | None = None, id: str | None = None, classes: str | None = None) -> None:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
atoms: list,
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
) -> None:
|
||||
super().__init__(name, id, classes)
|
||||
self.atoms = atoms
|
||||
self.phaser = Phaser(atoms)
|
||||
#print(self.phaser.state)
|
||||
self.procession: Procession = self.phaser.current_procession() # type: ignore
|
||||
#print(self.phaser.state)
|
||||
#self.procession.forward(1)
|
||||
|
||||
# print(self.phaser.state)
|
||||
self.procession: Procession = self.phaser.current_procession() # type: ignore
|
||||
# print(self.phaser.state)
|
||||
# self.procession.forward(1)
|
||||
|
||||
def on_mount(self):
|
||||
self.load_puzzle()
|
||||
@@ -45,22 +53,26 @@ class MemScreen(Screen):
|
||||
try:
|
||||
print(self.phaser.state)
|
||||
self.fission = Fission(self.procession.current_atom, self.phaser.state)
|
||||
#print(1)
|
||||
# print(1)
|
||||
puzzle_info = next(self.fission.generate())
|
||||
print(puzzle_info)
|
||||
return shim.puzzle2widget[puzzle_info["puzzle"]](atom = self.procession.current_atom, alia = puzzle_info["alia"])
|
||||
return shim.puzzle2widget[puzzle_info["puzzle"]](
|
||||
atom=self.procession.current_atom, alia=puzzle_info["alia"]
|
||||
)
|
||||
except (KeyError, StopIteration, AttributeError) as e:
|
||||
print(f"调度展开出错: {e}")
|
||||
return Static("无法生成谜题")
|
||||
#print(shim.puzzle2widget[puzzle_info["puzzle"]])
|
||||
# print(shim.puzzle2widget[puzzle_info["puzzle"]])
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Header(show_clock=True)
|
||||
with Center():
|
||||
yield Static(f"当前进度: {self.procession.process()}/{self.procession.total_length()}")
|
||||
#self.mount(self.current_widget()) # type: ignore
|
||||
yield Static(
|
||||
f"当前进度: {self.procession.process()}/{self.procession.total_length()}"
|
||||
)
|
||||
# self.mount(self.current_widget()) # type: ignore
|
||||
yield Container(id="puzzle-container")
|
||||
#yield Button("重新学习此单元", id="re-recognize", variant="warning")
|
||||
# yield Button("重新学习此单元", id="re-recognize", variant="warning")
|
||||
yield Footer()
|
||||
|
||||
def load_puzzle(self):
|
||||
|
||||
@@ -14,6 +14,7 @@ from textual.screen import Screen
|
||||
|
||||
from heurams.services.version import ver
|
||||
|
||||
|
||||
class NucleonCreatorScreen(Screen):
|
||||
BINDINGS = [("q", "go_back", "返回")]
|
||||
|
||||
@@ -23,18 +24,20 @@ class NucleonCreatorScreen(Screen):
|
||||
def search_templates(self):
|
||||
from pathlib import Path
|
||||
from heurams.context import config_var
|
||||
template_dir = Path(config_var.get()['paths']['template_dir'])
|
||||
|
||||
template_dir = Path(config_var.get()["paths"]["template_dir"])
|
||||
templates = list()
|
||||
for i in template_dir.iterdir():
|
||||
if i.name.endswith('.toml'):
|
||||
if i.name.endswith(".toml"):
|
||||
try:
|
||||
import toml
|
||||
with open(i, 'r') as f:
|
||||
|
||||
with open(i, "r") as f:
|
||||
dic = toml.load(f)
|
||||
desc = dic['__metadata__.attribution']['desc']
|
||||
templates.append(desc + ' (' + i.name + ')')
|
||||
desc = dic["__metadata__.attribution"]["desc"]
|
||||
templates.append(desc + " (" + i.name + ")")
|
||||
except Exception as e:
|
||||
templates.append(f'无描述模板 ({i.name})')
|
||||
templates.append(f"无描述模板 ({i.name})")
|
||||
print(e)
|
||||
print(templates)
|
||||
return templates
|
||||
@@ -43,10 +46,14 @@ class NucleonCreatorScreen(Screen):
|
||||
yield Header(show_clock=True)
|
||||
with Container(id="vice_container"):
|
||||
yield Label(f"[b]空白单元集创建向导\n")
|
||||
yield Markdown("> 提示: 你可能注意到当选中文本框时底栏和操作按键绑定将被覆盖 \n只需选中(使用鼠标或 Tab)选择框即可恢复底栏功能")
|
||||
yield Markdown(
|
||||
"> 提示: 你可能注意到当选中文本框时底栏和操作按键绑定将被覆盖 \n只需选中(使用鼠标或 Tab)选择框即可恢复底栏功能"
|
||||
)
|
||||
yield Markdown("1. 键入单元集名称")
|
||||
yield Input(placeholder="单元集名称")
|
||||
yield Markdown("> 单元集名称不应与现有单元集重复. \n> 新的单元集文件将创建在 ./nucleon/你输入的名称.toml")
|
||||
yield Markdown(
|
||||
"> 单元集名称不应与现有单元集重复. \n> 新的单元集文件将创建在 ./nucleon/你输入的名称.toml"
|
||||
)
|
||||
yield Label(f"\n")
|
||||
yield Markdown("2. 选择单元集模板")
|
||||
LINES = self.search_templates()
|
||||
@@ -79,5 +86,5 @@ class NucleonCreatorScreen(Screen):
|
||||
|
||||
def on_button_pressed(self, event) -> None:
|
||||
event.stop()
|
||||
if event.button.id == 'submit_button':
|
||||
if event.button.id == "submit_button":
|
||||
pass
|
||||
|
||||
@@ -21,17 +21,19 @@ import heurams.services.hasher as hasher
|
||||
from heurams.context import *
|
||||
from textual.worker import Worker, get_current_worker
|
||||
|
||||
|
||||
class PrecachingScreen(Screen):
|
||||
"""预缓存音频文件屏幕
|
||||
|
||||
|
||||
缓存记忆单元音频文件, 全部(默认) 或部分记忆单元(可选参数传入)
|
||||
|
||||
Args:
|
||||
nucleons (list): 可选列表, 仅包含 Nucleon 对象
|
||||
desc (list): 可选字符串, 包含对此次调用的文字描述
|
||||
"""
|
||||
|
||||
BINDINGS = [("q", "go_back", "返回")]
|
||||
|
||||
|
||||
def __init__(self, nucleons: list = [], desc: str = ""):
|
||||
super().__init__(name=None, id=None, classes=None)
|
||||
self.nucleons = nucleons
|
||||
@@ -47,23 +49,23 @@ class PrecachingScreen(Screen):
|
||||
for i in nucleons:
|
||||
i: pt.Nucleon
|
||||
i.do_eval()
|
||||
#print("完成 EVAL")
|
||||
# print("完成 EVAL")
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Header(show_clock=True)
|
||||
with Container(id="precache_container"):
|
||||
yield Label("[b]音频预缓存[/b]", classes="title-label")
|
||||
|
||||
|
||||
if self.nucleons:
|
||||
yield Static(f"目标单元归属: [b]{self.desc}[/b]", classes="target-info")
|
||||
yield Static(f"单元数量: {len(self.nucleons)}", classes="target-info")
|
||||
else:
|
||||
yield Static("目标: 所有单元", classes="target-info")
|
||||
|
||||
|
||||
yield Static(id="status", classes="status-info")
|
||||
yield Static(id="current_item", classes="current-item")
|
||||
yield ProgressBar(total=100, show_eta=False, id="progress_bar")
|
||||
|
||||
|
||||
with Horizontal(classes="button-group"):
|
||||
if not self.is_precaching:
|
||||
yield Button("开始预缓存", id="start_precache", variant="primary")
|
||||
@@ -71,9 +73,9 @@ class PrecachingScreen(Screen):
|
||||
yield Button("取消预缓存", id="cancel_precache", variant="error")
|
||||
yield Button("清空缓存", id="clear_cache", variant="warning")
|
||||
yield Button("返回", id="go_back", variant="default")
|
||||
|
||||
|
||||
yield Static("若您离开此界面, 未完成的缓存进程会自动停止.")
|
||||
yield Static("缓存程序支持 \"断点续传\".")
|
||||
yield Static('缓存程序支持 "断点续传".')
|
||||
|
||||
yield Footer()
|
||||
|
||||
@@ -86,10 +88,10 @@ class PrecachingScreen(Screen):
|
||||
status_widget = self.query_one("#status", Static)
|
||||
item_widget = self.query_one("#current_item", Static)
|
||||
progress_bar = self.query_one("#progress_bar", ProgressBar)
|
||||
|
||||
|
||||
status_widget.update(f"状态: {status}")
|
||||
item_widget.update(f"当前项目: {current_item}" if current_item else "")
|
||||
|
||||
|
||||
if progress is not None:
|
||||
progress_bar.progress = progress
|
||||
progress_bar.advance(0) # 刷新显示
|
||||
@@ -97,12 +99,14 @@ class PrecachingScreen(Screen):
|
||||
def precache_by_text(self, text: str):
|
||||
"""预缓存单段文本的音频"""
|
||||
from heurams.context import rootdir, workdir, config_var
|
||||
|
||||
cache_dir = pathlib.Path(config_var.get()["paths"]["cache_dir"])
|
||||
cache_dir.mkdir(parents=True, exist_ok=True)
|
||||
cache_file = cache_dir / f"{hasher.get_md5(text)}.wav"
|
||||
if not cache_file.exists():
|
||||
try: # TODO: 调用模块消除tts耦合
|
||||
try: # TODO: 调用模块消除tts耦合
|
||||
import edge_tts as tts
|
||||
|
||||
communicate = tts.Communicate(text, "zh-CN-XiaoxiaoNeural")
|
||||
communicate.save_sync(str(cache_file))
|
||||
return 1
|
||||
@@ -110,34 +114,30 @@ class PrecachingScreen(Screen):
|
||||
print(f"预缓存失败 '{text}': {e}")
|
||||
return 0
|
||||
return 1
|
||||
|
||||
|
||||
def precache_by_nucleon(self, nucleon: pt.Nucleon):
|
||||
"""依据 Nucleon 缓存"""
|
||||
#print(nucleon.metadata['formation']['tts_text'])
|
||||
ret = self.precache_by_text(nucleon.metadata['formation']['tts_text'])
|
||||
# print(nucleon.metadata['formation']['tts_text'])
|
||||
ret = self.precache_by_text(nucleon.metadata["formation"]["tts_text"])
|
||||
return ret
|
||||
#print(f"TTS 缓存: {nucleon.metadata['formation']['tts_text']}")
|
||||
|
||||
# print(f"TTS 缓存: {nucleon.metadata['formation']['tts_text']}")
|
||||
|
||||
def precache_by_list(self, nucleons: list):
|
||||
"""依据 Nucleons 列表缓存"""
|
||||
for idx, nucleon in enumerate(nucleons):
|
||||
#print(f"PROC: {nucleon}")
|
||||
# print(f"PROC: {nucleon}")
|
||||
worker = get_current_worker()
|
||||
if worker and worker.is_cancelled: # 函数在worker中执行且已被取消
|
||||
if worker and worker.is_cancelled: # 函数在worker中执行且已被取消
|
||||
return False
|
||||
text = nucleon.metadata['formation']['tts_text']
|
||||
#self.current_item = text[:30] + "..." if len(text) > 50 else text
|
||||
#print(text)
|
||||
text = nucleon.metadata["formation"]["tts_text"]
|
||||
# self.current_item = text[:30] + "..." if len(text) > 50 else text
|
||||
# print(text)
|
||||
self.processed += 1
|
||||
#print(self.processed)
|
||||
#print(self.total)
|
||||
# print(self.processed)
|
||||
# print(self.total)
|
||||
progress = int((self.processed / self.total) * 100) if self.total > 0 else 0
|
||||
#print(progress)
|
||||
self.update_status(
|
||||
f"正处理 ({idx + 1}/{len(nucleons)})",
|
||||
text,
|
||||
progress
|
||||
)
|
||||
# print(progress)
|
||||
self.update_status(f"正处理 ({idx + 1}/{len(nucleons)})", text, progress)
|
||||
ret = self.precache_by_nucleon(nucleon)
|
||||
if not ret:
|
||||
self.update_status(
|
||||
@@ -145,6 +145,7 @@ class PrecachingScreen(Screen):
|
||||
f"处理失败, 跳过: {self.current_item}",
|
||||
)
|
||||
import time
|
||||
|
||||
time.sleep(1)
|
||||
if self.cancel_flag:
|
||||
worker.cancel()
|
||||
@@ -153,9 +154,9 @@ class PrecachingScreen(Screen):
|
||||
return True
|
||||
|
||||
def precache_by_nucleons(self):
|
||||
#print("开始缓存")
|
||||
# print("开始缓存")
|
||||
ret = self.precache_by_list(self.nucleons)
|
||||
#print(f"返回 {ret}")
|
||||
# print(f"返回 {ret}")
|
||||
return ret
|
||||
|
||||
def precache_by_filepath(self, path: pathlib.Path):
|
||||
@@ -165,13 +166,15 @@ class PrecachingScreen(Screen):
|
||||
lst.append(i[0])
|
||||
return self.precache_by_list(lst)
|
||||
|
||||
|
||||
def precache_all_files(self):
|
||||
"""预缓存所有文件"""
|
||||
from heurams.context import rootdir, workdir, config_var
|
||||
|
||||
nucleon_path = pathlib.Path(config_var.get()["paths"]["nucleon_dir"])
|
||||
nucleon_files = [f for f in nucleon_path.iterdir() if f.suffix == ".toml"] # TODO: 解耦合
|
||||
|
||||
nucleon_files = [
|
||||
f for f in nucleon_path.iterdir() if f.suffix == ".toml"
|
||||
] # TODO: 解耦合
|
||||
|
||||
# 计算总项目数
|
||||
self.total = 0
|
||||
nu = list()
|
||||
@@ -186,16 +189,26 @@ class PrecachingScreen(Screen):
|
||||
i: pt.Nucleon
|
||||
i.do_eval()
|
||||
return self.precache_by_list(nu)
|
||||
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
event.stop()
|
||||
if event.button.id == "start_precache" and not self.is_precaching:
|
||||
# 开始预缓存
|
||||
if self.nucleons:
|
||||
self.precache_worker = self.run_worker(self.precache_by_nucleons, thread=True, exclusive=True, exit_on_error=True)
|
||||
self.precache_worker = self.run_worker(
|
||||
self.precache_by_nucleons,
|
||||
thread=True,
|
||||
exclusive=True,
|
||||
exit_on_error=True,
|
||||
)
|
||||
else:
|
||||
self.precache_worker = self.run_worker(self.precache_all_files, thread=True, exclusive=True, exit_on_error=True)
|
||||
|
||||
self.precache_worker = self.run_worker(
|
||||
self.precache_all_files,
|
||||
thread=True,
|
||||
exclusive=True,
|
||||
exit_on_error=True,
|
||||
)
|
||||
|
||||
elif event.button.id == "cancel_precache" and self.is_precaching:
|
||||
# 取消预缓存
|
||||
if self.precache_worker:
|
||||
@@ -204,20 +217,23 @@ class PrecachingScreen(Screen):
|
||||
self.processed = 0
|
||||
self.progress = 0
|
||||
self.update_status("已取消", "预缓存操作被用户取消", 0)
|
||||
|
||||
|
||||
elif event.button.id == "clear_cache":
|
||||
# 清空缓存
|
||||
try:
|
||||
import shutil
|
||||
from heurams.context import rootdir, workdir, config_var
|
||||
shutil.rmtree(f"{config_var.get()["paths"]["cache_dir"]}", ignore_errors=True)
|
||||
|
||||
shutil.rmtree(
|
||||
f"{config_var.get()["paths"]["cache_dir"]}", ignore_errors=True
|
||||
)
|
||||
self.update_status("已清空", "音频缓存已清空", 0)
|
||||
except Exception as e:
|
||||
self.update_status("错误", f"清空缓存失败: {e}")
|
||||
self.cancel_flag = 1
|
||||
self.processed = 0
|
||||
self.progress = 0
|
||||
|
||||
|
||||
elif event.button.id == "go_back":
|
||||
self.action_go_back()
|
||||
|
||||
|
||||
@@ -15,15 +15,11 @@ import heurams.kernel.particles as pt
|
||||
import heurams.services.hasher as hasher
|
||||
from heurams.context import *
|
||||
|
||||
class PreparationScreen(Screen):
|
||||
BINDINGS = [
|
||||
("q", "go_back", "返回"),
|
||||
("p", "precache", "预缓存音频")
|
||||
]
|
||||
|
||||
def __init__(
|
||||
self, nucleon_file: pathlib.Path, electron_file: pathlib.Path
|
||||
) -> None:
|
||||
class PreparationScreen(Screen):
|
||||
BINDINGS = [("q", "go_back", "返回"), ("p", "precache", "预缓存音频")]
|
||||
|
||||
def __init__(self, nucleon_file: pathlib.Path, electron_file: pathlib.Path) -> None:
|
||||
super().__init__(name=None, id=None, classes=None)
|
||||
self.nucleon_file = nucleon_file
|
||||
self.electron_file = electron_file
|
||||
@@ -39,10 +35,10 @@ class PreparationScreen(Screen):
|
||||
yield Label(f"\n单元数量: {len(self.nucleons_with_orbital)}\n")
|
||||
|
||||
yield Button(
|
||||
"开始记忆",
|
||||
id="start_memorizing_button",
|
||||
variant="primary",
|
||||
classes="start-button",
|
||||
"开始记忆",
|
||||
id="start_memorizing_button",
|
||||
variant="primary",
|
||||
classes="start-button",
|
||||
)
|
||||
yield Button(
|
||||
"预缓存音频",
|
||||
@@ -68,6 +64,7 @@ class PreparationScreen(Screen):
|
||||
|
||||
def action_precache(self):
|
||||
from ..screens.precache import PrecachingScreen
|
||||
|
||||
lst = list()
|
||||
for i in self.nucleons_with_orbital:
|
||||
lst.append(i[0])
|
||||
@@ -97,8 +94,8 @@ class PreparationScreen(Screen):
|
||||
atom.link("orbital_path", None)
|
||||
atoms.append(atom)
|
||||
from .memorizor import MemScreen
|
||||
|
||||
memscreen = MemScreen(atoms)
|
||||
self.app.push_screen(memscreen)
|
||||
elif event.button.id == "precache_button":
|
||||
self.action_precache()
|
||||
|
||||
@@ -1,24 +1,36 @@
|
||||
"""Kernel 操作先进函数库"""
|
||||
|
||||
import random
|
||||
import heurams.kernel.particles as pt
|
||||
import heurams.kernel.puzzles as pz
|
||||
import heurams.interface.widgets as pzw
|
||||
from typing import TypedDict
|
||||
staging = {} # 细粒度缓存区, 是 ident -> quality 的封装
|
||||
|
||||
staging = {} # 细粒度缓存区, 是 ident -> quality 的封装
|
||||
|
||||
|
||||
def report_to_staging(atom: pt.Atom, quality):
|
||||
staging[atom.ident] = min(quality, staging[atom.ident])
|
||||
|
||||
|
||||
def clear():
|
||||
staging = dict()
|
||||
|
||||
|
||||
def deploy_to_electron():
|
||||
for atom_ident, quality in staging.items():
|
||||
if pt.atom_registry[atom_ident].registry['electron'].is_activated:
|
||||
pt.atom_registry[atom_ident].registry['electron'].revisor(quality=quality)
|
||||
if pt.atom_registry[atom_ident].registry["electron"].is_activated:
|
||||
pt.atom_registry[atom_ident].registry["electron"].revisor(quality=quality)
|
||||
else:
|
||||
pt.atom_registry[atom_ident].registry['electron'].revisor(quality=quality, is_new_activation=True)
|
||||
pt.atom_registry[atom_ident].registry["electron"].revisor(
|
||||
quality=quality, is_new_activation=True
|
||||
)
|
||||
clear()
|
||||
|
||||
|
||||
puzzle2widget = {
|
||||
pz.RecognitionPuzzle: pzw.Recognition,
|
||||
pz.ClozePuzzle: pzw.ClozePuzzle,
|
||||
pz.MCQPuzzle: pzw.MCQPuzzle,
|
||||
pz.BasePuzzle: pzw.BasePuzzleWidget,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,24 @@
|
||||
from textual.widget import Widget
|
||||
import heurams.kernel.particles as pt
|
||||
|
||||
|
||||
class BasePuzzleWidget(Widget):
|
||||
def __init__(self, *children: Widget, atom: pt.Atom, name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
|
||||
super().__init__(*children, name=name, id=id, classes=classes, disabled=disabled, markup=markup)
|
||||
self.atom = atom
|
||||
def __init__(
|
||||
self,
|
||||
*children: Widget,
|
||||
atom: pt.Atom,
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
markup: bool = True
|
||||
) -> None:
|
||||
super().__init__(
|
||||
*children,
|
||||
name=name,
|
||||
id=id,
|
||||
classes=classes,
|
||||
disabled=disabled,
|
||||
markup=markup
|
||||
)
|
||||
self.atom = atom
|
||||
|
||||
@@ -9,9 +9,28 @@ import heurams.kernel.particles as pt
|
||||
from .base_puzzle_widget import BasePuzzleWidget
|
||||
from textual.message import Message
|
||||
|
||||
|
||||
class BasicEvaluation(BasePuzzleWidget):
|
||||
def __init__(self, *children: Widget, atom: pt.Atom, alia: str = "", name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
|
||||
super().__init__(*children, atom=atom, name=name, id=id, classes=classes, disabled=disabled, markup=markup)
|
||||
def __init__(
|
||||
self,
|
||||
*children: Widget,
|
||||
atom: pt.Atom,
|
||||
alia: str = "",
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
markup: bool = True,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
*children,
|
||||
atom=atom,
|
||||
name=name,
|
||||
id=id,
|
||||
classes=classes,
|
||||
disabled=disabled,
|
||||
markup=markup,
|
||||
)
|
||||
|
||||
class RatingChanged(Message):
|
||||
def __init__(self, rating: int) -> None:
|
||||
@@ -31,10 +50,10 @@ class BasicEvaluation(BasePuzzleWidget):
|
||||
def compose(self):
|
||||
# 显示主要内容
|
||||
yield Label(self.atom.registry["nucleon"]["content"], id="main")
|
||||
|
||||
|
||||
# 显示评估说明(可选)
|
||||
yield Static("请评估你对这个内容的记忆程度:", classes="instruction")
|
||||
|
||||
|
||||
# 按钮容器
|
||||
with Container(id="button_container"):
|
||||
btn = {}
|
||||
@@ -56,7 +75,7 @@ class BasicEvaluation(BasePuzzleWidget):
|
||||
btn["0"] = Button(
|
||||
"完全空白", variant="error", id="feedback_0", classes="choice"
|
||||
)
|
||||
|
||||
|
||||
# 布局按钮
|
||||
yield Horizontal(btn["5"], btn["4"])
|
||||
yield Horizontal(btn["3"], btn["2"])
|
||||
@@ -68,13 +87,15 @@ class BasicEvaluation(BasePuzzleWidget):
|
||||
|
||||
if button_id in self.feedback_mapping:
|
||||
feedback_info = self.feedback_mapping[button_id]
|
||||
|
||||
self.post_message(self.RatingChanged(
|
||||
rating=feedback_info["rating"],
|
||||
))
|
||||
|
||||
|
||||
self.post_message(
|
||||
self.RatingChanged(
|
||||
rating=feedback_info["rating"],
|
||||
)
|
||||
)
|
||||
|
||||
event.button.add_class("selected")
|
||||
|
||||
|
||||
self.disable_other_buttons(button_id)
|
||||
|
||||
def disable_other_buttons(self, selected_button_id: str) -> None:
|
||||
@@ -91,6 +112,8 @@ class BasicEvaluation(BasePuzzleWidget):
|
||||
button_id = f"feedback_{event.key}"
|
||||
if button_id in self.feedback_mapping:
|
||||
# 模拟按钮点击
|
||||
self.post_message(self.RatingChanged(
|
||||
rating=self.feedback_mapping[button_id]["rating"],
|
||||
))
|
||||
self.post_message(
|
||||
self.RatingChanged(
|
||||
rating=self.feedback_mapping[button_id]["rating"],
|
||||
)
|
||||
)
|
||||
|
||||
@@ -10,10 +10,29 @@ import copy
|
||||
import random
|
||||
from textual.message import Message
|
||||
|
||||
|
||||
class ClozePuzzle(BasePuzzleWidget):
|
||||
|
||||
def __init__(self, *children: Widget, atom: pt.Atom, alia: str = "", name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
|
||||
super().__init__(*children, atom=atom, name=name, id=id, classes=classes, disabled=disabled, markup=markup)
|
||||
def __init__(
|
||||
self,
|
||||
*children: Widget,
|
||||
atom: pt.Atom,
|
||||
alia: str = "",
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
markup: bool = True,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
*children,
|
||||
atom=atom,
|
||||
name=name,
|
||||
id=id,
|
||||
classes=classes,
|
||||
disabled=disabled,
|
||||
markup=markup,
|
||||
)
|
||||
self.inputlist = list()
|
||||
self.hashtable = {}
|
||||
self.alia = alia
|
||||
@@ -21,7 +40,11 @@ class ClozePuzzle(BasePuzzleWidget):
|
||||
|
||||
def _work(self):
|
||||
cfg = self.atom.registry["orbital"]["puzzles"][self.alia]
|
||||
self.puzzle = pz.ClozePuzzle(text=cfg["content"], delimiter=cfg["delimiter"], min_denominator=cfg["min_denominator"])
|
||||
self.puzzle = pz.ClozePuzzle(
|
||||
text=cfg["content"],
|
||||
delimiter=cfg["delimiter"],
|
||||
min_denominator=cfg["min_denominator"],
|
||||
)
|
||||
self.puzzle.refresh()
|
||||
self.ans = copy.copy(self.puzzle.answer)
|
||||
random.shuffle(self.ans)
|
||||
@@ -35,6 +58,7 @@ class ClozePuzzle(BasePuzzleWidget):
|
||||
|
||||
class InputChanged(Message):
|
||||
"""输入变化消息"""
|
||||
|
||||
def __init__(self, current_input: list, max_length: int) -> None:
|
||||
self.current_input = current_input # 当前输入
|
||||
self.max_length = max_length # 最大长度
|
||||
@@ -51,16 +75,17 @@ class ClozePuzzle(BasePuzzleWidget):
|
||||
|
||||
def update_preview(self):
|
||||
preview = self.query_one("#inputpreview")
|
||||
preview.update(f"当前输入: {self.inputlist}") # type: ignore
|
||||
|
||||
self.post_message(self.InputChanged(
|
||||
current_input=self.inputlist.copy(),
|
||||
max_length=len(self.puzzle.answer)
|
||||
))
|
||||
preview.update(f"当前输入: {self.inputlist}") # type: ignore
|
||||
|
||||
self.post_message(
|
||||
self.InputChanged(
|
||||
current_input=self.inputlist.copy(), max_length=len(self.puzzle.answer)
|
||||
)
|
||||
)
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
button_id = event.button.id
|
||||
|
||||
|
||||
if button_id == "delete":
|
||||
if len(self.inputlist) > 0:
|
||||
self.inputlist.pop()
|
||||
@@ -69,17 +94,17 @@ class ClozePuzzle(BasePuzzleWidget):
|
||||
answer_text = self.hashtable[button_id]
|
||||
self.inputlist.append(answer_text)
|
||||
self.update_preview()
|
||||
|
||||
|
||||
if len(self.inputlist) >= len(self.puzzle.answer):
|
||||
is_correct = self.inputlist == self.puzzle.answer
|
||||
rating = 4 if is_correct else 2
|
||||
|
||||
self.post_message(self.RatingChanged(
|
||||
atom=self.atom,
|
||||
rating=rating,
|
||||
is_correct=is_correct
|
||||
))
|
||||
|
||||
self.post_message(
|
||||
self.RatingChanged(
|
||||
atom=self.atom, rating=rating, is_correct=is_correct
|
||||
)
|
||||
)
|
||||
|
||||
if not is_correct:
|
||||
self.inputlist = []
|
||||
self.update_preview()
|
||||
self.update_preview()
|
||||
|
||||
@@ -4,10 +4,27 @@ from textual.widgets import (
|
||||
)
|
||||
from textual.widget import Widget
|
||||
|
||||
|
||||
class Finished(Widget):
|
||||
def __init__(self, *children: Widget, alia = "", name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
*children: Widget,
|
||||
alia="",
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
markup: bool = True
|
||||
) -> None:
|
||||
self.alia = alia
|
||||
super().__init__(*children, name=name, id=id, classes=classes, disabled=disabled, markup=markup)
|
||||
super().__init__(
|
||||
*children,
|
||||
name=name,
|
||||
id=id,
|
||||
classes=classes,
|
||||
disabled=disabled,
|
||||
markup=markup
|
||||
)
|
||||
|
||||
def compose(self):
|
||||
yield Label("本次记忆进程结束", id="finished_msg")
|
||||
@@ -15,5 +32,5 @@ class Finished(Widget):
|
||||
|
||||
def on_button_pressed(self, event):
|
||||
button_id = event.button.id
|
||||
if button_id == 'back-to-menu':
|
||||
if button_id == "back-to-menu":
|
||||
self.app.pop_screen()
|
||||
|
||||
@@ -1,137 +1,119 @@
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.events import Event
|
||||
from textual.widgets import (
|
||||
Collapsible,
|
||||
Header,
|
||||
Footer,
|
||||
Markdown,
|
||||
ListView,
|
||||
ListItem,
|
||||
Label,
|
||||
Static,
|
||||
Button,
|
||||
)
|
||||
from textual.containers import Container, Horizontal, Center
|
||||
from textual.screen import Screen
|
||||
from textual.widget import Widget
|
||||
from typing import Tuple, Dict
|
||||
import heurams.kernel.particles as pt
|
||||
import heurams.kernel.puzzles as pz
|
||||
from .base_puzzle_widget import BasePuzzleWidget
|
||||
import copy
|
||||
import random
|
||||
from textual.message import Message
|
||||
from typing import TypedDict
|
||||
|
||||
|
||||
class Setting(TypedDict):
|
||||
__origin__: str
|
||||
__hint__: str
|
||||
primary: str # 显示的提示文本
|
||||
mapping: dict # 谜题到答案的映射
|
||||
jammer: list # 干扰项
|
||||
max_riddles_num: int # 最大谜题数量
|
||||
prefix: str # 提示词前缀
|
||||
|
||||
|
||||
class MCQPuzzle(BasePuzzleWidget):
|
||||
def __init__(self, *children: Widget, atom: pt.Atom, alia: str = "", name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
|
||||
super().__init__(*children, atom=atom, name=name, id=id, classes=classes, disabled=disabled, markup=markup)
|
||||
def __init__(
|
||||
self,
|
||||
*children: Widget,
|
||||
atom: pt.Atom,
|
||||
alia: str = "",
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
markup: bool = True,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
*children,
|
||||
atom=atom,
|
||||
name=name,
|
||||
id=id,
|
||||
classes=classes,
|
||||
disabled=disabled,
|
||||
markup=markup,
|
||||
)
|
||||
self.inputlist = []
|
||||
self.alia = alia
|
||||
self.hashtable = {}
|
||||
self._work()
|
||||
self._load()
|
||||
|
||||
def _work(self):
|
||||
def _load(self):
|
||||
cfg = self.atom.registry["orbital"]["puzzles"][self.alia]
|
||||
self.puzzle = pz.MCQPuzzle(cfg["mapping"], cfg["jammer"], cfg["max_riddles_num"], cfg['prefix'])
|
||||
self.puzzle = pz.MCQPuzzle(
|
||||
cfg["mapping"], cfg["jammer"], cfg["max_riddles_num"], cfg["prefix"]
|
||||
)
|
||||
self.puzzle.refresh()
|
||||
|
||||
class PuzzleCompleted(Message):
|
||||
"""选择题完成消息"""
|
||||
def __init__(self, atom: pt.Atom, rating: int, is_correct: bool, user_answers: list, correct_answers: list) -> None:
|
||||
self.atom = atom
|
||||
self.rating = rating # 评分
|
||||
self.is_correct = is_correct # 是否正确
|
||||
self.user_answers = user_answers # 用户答案
|
||||
self.correct_answers = correct_answers # 正确答案
|
||||
super().__init__()
|
||||
|
||||
class InputChanged(Message):
|
||||
"""输入变化消息"""
|
||||
def __init__(self, current_input: list, current_question: int, total_questions: int, current_question_text: str) -> None:
|
||||
self.current_input = current_input # 当前输入
|
||||
self.current_question = current_question # 当前题号
|
||||
self.total_questions = total_questions # 总题数
|
||||
self.current_question_text = current_question_text # 当前问题文本
|
||||
self.progress = current_question / total_questions # 进度
|
||||
super().__init__()
|
||||
|
||||
class QuestionAdvanced(Message):
|
||||
"""题目切换消息"""
|
||||
def __init__(self, question_index: int, question_text: str, options: list) -> None:
|
||||
self.question_index = question_index # 题目索引
|
||||
self.question_text = question_text
|
||||
self.options = options # 选项列表
|
||||
super().__init__()
|
||||
|
||||
def compose(self):
|
||||
yield Label(self.atom[1].content.replace("/",""), id="sentence")
|
||||
setting: Setting = self.atom.registry["nucleon"].metadata["orbital"]["puzzle"][
|
||||
self.alia
|
||||
]
|
||||
yield Label(setting["primary"], id="sentence")
|
||||
yield Label(self.puzzle.wording[len(self.inputlist)], id="puzzle")
|
||||
yield Label(f"当前输入: {self.inputlist}", id="inputpreview")
|
||||
|
||||
|
||||
# 渲染当前问题的选项
|
||||
current_options = self.puzzle.options[len(self.inputlist)]
|
||||
for i in current_options:
|
||||
self.hashtable[str(hash(i))] = i
|
||||
yield Button(i, id=f"select{hash(i)}")
|
||||
|
||||
|
||||
yield Button("退格", id="delete")
|
||||
|
||||
def update_display(self):
|
||||
# 更新预览标签
|
||||
preview = self.query_one("#inputpreview")
|
||||
preview.update(f"当前输入: {self.inputlist}") # type: ignore
|
||||
|
||||
preview.update(f"当前输入: {self.inputlist}") # type: ignore
|
||||
|
||||
# 更新问题标签
|
||||
puzzle_label = self.query_one("#puzzle")
|
||||
current_question_index = len(self.inputlist)
|
||||
if current_question_index < len(self.puzzle.wording):
|
||||
puzzle_label.update(self.puzzle.wording[current_question_index]) # type: ignore
|
||||
|
||||
puzzle_label.update(self.puzzle.wording[current_question_index]) # type: ignore
|
||||
|
||||
# 发送输入变化消息
|
||||
self.post_message(self.InputChanged(
|
||||
current_input=self.inputlist.copy(),
|
||||
current_question=current_question_index,
|
||||
total_questions=len(self.puzzle.answer),
|
||||
current_question_text=self.puzzle.wording[current_question_index] if current_question_index < len(self.puzzle.wording) else ""
|
||||
))
|
||||
|
||||
|
||||
# 如果还有下一题,发送题目切换消息
|
||||
if current_question_index < len(self.puzzle.options):
|
||||
self.post_message(self.QuestionAdvanced(
|
||||
question_index=current_question_index,
|
||||
question_text=self.puzzle.wording[current_question_index],
|
||||
options=self.puzzle.options[current_question_index]
|
||||
))
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
"""处理按钮点击事件"""
|
||||
button_id = event.button.id
|
||||
|
||||
|
||||
if button_id == "delete":
|
||||
# 退格处理
|
||||
if len(self.inputlist) > 0:
|
||||
self.inputlist.pop()
|
||||
self.refresh_buttons()
|
||||
self.update_display()
|
||||
elif button_id.startswith("select"): # type: ignore
|
||||
elif button_id.startswith("select"): # type: ignore
|
||||
# 选项选择处理
|
||||
answer_text = self.hashtable[button_id[6:]] # type: ignore
|
||||
answer_text = self.hashtable[button_id[6:]] # type: ignore
|
||||
self.inputlist.append(answer_text)
|
||||
|
||||
|
||||
# 检查是否完成所有题目
|
||||
if len(self.inputlist) >= len(self.puzzle.answer):
|
||||
is_correct = self.inputlist == self.puzzle.answer
|
||||
rating = 4 if is_correct else 2
|
||||
|
||||
|
||||
# 发送完成消息
|
||||
self.post_message(self.PuzzleCompleted(
|
||||
atom=self.atom,
|
||||
rating=rating,
|
||||
is_correct=is_correct,
|
||||
user_answers=self.inputlist.copy(),
|
||||
correct_answers=self.puzzle.answer.copy()
|
||||
))
|
||||
|
||||
self.post_message(
|
||||
self.PuzzleCompleted(
|
||||
atom=self.atom,
|
||||
rating=rating,
|
||||
is_correct=is_correct,
|
||||
user_answers=self.inputlist.copy(),
|
||||
correct_answers=self.puzzle.answer.copy(),
|
||||
)
|
||||
)
|
||||
|
||||
# 重置输入(如果回答错误)
|
||||
if not is_correct:
|
||||
self.inputlist = []
|
||||
@@ -145,10 +127,14 @@ class MCQPuzzle(BasePuzzleWidget):
|
||||
def refresh_buttons(self):
|
||||
"""刷新按钮显示(用于题目切换)"""
|
||||
# 移除所有选项按钮
|
||||
buttons_to_remove = [child for child in self.children if hasattr(child, 'id') and child.id and child.id.startswith('select')]
|
||||
buttons_to_remove = [
|
||||
child
|
||||
for child in self.children
|
||||
if hasattr(child, "id") and child.id and child.id.startswith("select")
|
||||
]
|
||||
for button in buttons_to_remove:
|
||||
self.remove_child(button) # type: ignore
|
||||
|
||||
self.remove_child(button) # type: ignore
|
||||
|
||||
# 添加当前题目的选项按钮
|
||||
current_question_index = len(self.inputlist)
|
||||
if current_question_index < len(self.puzzle.options):
|
||||
|
||||
@@ -6,8 +6,24 @@ from textual.widget import Widget
|
||||
|
||||
|
||||
class Placeholder(Widget):
|
||||
def __init__(self, *children: Widget, name: str | None = None, alia: str = "", id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
|
||||
super().__init__(*children, name=name, id=id, classes=classes, disabled=disabled, markup=markup)
|
||||
def __init__(
|
||||
self,
|
||||
*children: Widget,
|
||||
name: str | None = None,
|
||||
alia: str = "",
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
markup: bool = True
|
||||
) -> None:
|
||||
super().__init__(
|
||||
*children,
|
||||
name=name,
|
||||
id=id,
|
||||
classes=classes,
|
||||
disabled=disabled,
|
||||
markup=markup
|
||||
)
|
||||
|
||||
def compose(self):
|
||||
yield Label("示例标签", id="testlabel")
|
||||
|
||||
@@ -14,6 +14,7 @@ from .base_puzzle_widget import BasePuzzleWidget
|
||||
from typing import TypedDict, List
|
||||
from textual.message import Message
|
||||
|
||||
|
||||
class RecognitionConfig(TypedDict):
|
||||
__origin__: str
|
||||
__hint__: str
|
||||
@@ -21,16 +22,35 @@ class RecognitionConfig(TypedDict):
|
||||
secondary: List[str]
|
||||
top_dim: List[str]
|
||||
|
||||
|
||||
class Recognition(BasePuzzleWidget):
|
||||
def __init__(self, *children: Widget, atom: pt.Atom, alia: str = "", name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
|
||||
super().__init__(*children, atom=atom, name=name, id=id, classes=classes, disabled=disabled, markup=markup)
|
||||
def __init__(
|
||||
self,
|
||||
*children: Widget,
|
||||
atom: pt.Atom,
|
||||
alia: str = "",
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
markup: bool = True,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
*children,
|
||||
atom=atom,
|
||||
name=name,
|
||||
id=id,
|
||||
classes=classes,
|
||||
disabled=disabled,
|
||||
markup=markup,
|
||||
)
|
||||
if alia == "":
|
||||
alia = "Recognition"
|
||||
self.alia = alia
|
||||
|
||||
def compose(self):
|
||||
cfg: RecognitionConfig = self.atom.registry["orbital"]["puzzles"][self.alia]
|
||||
delim = self.atom.registry['nucleon'].metadata["formation"]["delimiter"]
|
||||
delim = self.atom.registry["nucleon"].metadata["formation"]["delimiter"]
|
||||
replace_dict = {
|
||||
", ": ",",
|
||||
". ": ".",
|
||||
@@ -43,8 +63,8 @@ class Recognition(BasePuzzleWidget):
|
||||
f":{delim}": ":",
|
||||
}
|
||||
|
||||
nucleon = self.atom.registry['nucleon']
|
||||
metadata = self.atom.registry['nucleon'].metadata
|
||||
nucleon = self.atom.registry["nucleon"]
|
||||
metadata = self.atom.registry["nucleon"].metadata
|
||||
primary = cfg["primary"]
|
||||
|
||||
with Center():
|
||||
@@ -53,7 +73,7 @@ class Recognition(BasePuzzleWidget):
|
||||
|
||||
for old, new in replace_dict.items():
|
||||
primary = primary.replace(old, new)
|
||||
primary_splited = re.split(r"(?<=[,;:|])", cfg['primary'])
|
||||
primary_splited = re.split(r"(?<=[,;:|])", cfg["primary"])
|
||||
for item in primary_splited:
|
||||
with Center():
|
||||
yield Label(
|
||||
@@ -68,7 +88,7 @@ class Recognition(BasePuzzleWidget):
|
||||
continue
|
||||
if isinstance(item, Dict):
|
||||
total = ""
|
||||
for j, k in item.items(): # type: ignore
|
||||
for j, k in item.items(): # type: ignore
|
||||
total += f"> **{j}**: {k} \n"
|
||||
yield Markdown(total)
|
||||
if isinstance(item, str):
|
||||
@@ -79,4 +99,4 @@ class Recognition(BasePuzzleWidget):
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
if event.button.id == "ok":
|
||||
self.screen.rating = 5 # type: ignore
|
||||
self.screen.rating = 5 # type: ignore
|
||||
|
||||
Reference in New Issue
Block a user