From a8ae1bacf3f82363f7b122e7a173ccd1fb396720 Mon Sep 17 00:00:00 2001 From: david-ajax Date: Wed, 10 Dec 2025 13:13:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.example/config.toml | 11 +++++++- src/heurams/interface/__main__.py | 2 +- src/heurams/interface/screens/dashboard.py | 2 +- src/heurams/interface/screens/memorizor.py | 3 ++- .../{nucleon_creator.py => nucreator.py} | 26 ++++++++++++------- src/heurams/interface/screens/precache.py | 26 ++++++------------- src/heurams/providers/audio/__init__.py | 11 +++++--- .../{system_audio.py => playsound_audio.py} | 7 +++++ src/heurams/providers/audio/protocol.py | 5 ++++ src/heurams/providers/audio/termux_audio.py | 3 ++- src/heurams/providers/llm/deepseek.py | 0 src/heurams/providers/tts/__init__.py | 6 ++--- 12 files changed, 62 insertions(+), 40 deletions(-) rename src/heurams/interface/screens/{nucleon_creator.py => nucreator.py} (58%) rename src/heurams/providers/audio/{system_audio.py => playsound_audio.py} (60%) create mode 100644 src/heurams/providers/audio/protocol.py delete mode 100644 src/heurams/providers/llm/deepseek.py diff --git a/config.example/config.toml b/config.example/config.toml index f44ff80..22e17ec 100644 --- a/config.example/config.toml +++ b/config.example/config.toml @@ -26,4 +26,13 @@ min_denominator = 3 nucleon_dir = "./data/nucleon" electron_dir = "./data/electron" orbital_dir = "./data/orbital" -cache_dir = "./data/cache" \ No newline at end of file +cache_dir = "./data/cache" + +[services] # 定义服务到提供者的映射 +audio = "playsound" # 可选项: playsound(通用), termux(仅用于支持 Android Termux), mpg123(TODO) +tts = "edgetts" # 可选项: edgetts +llm = "openai" # 可选项: openai + +[providers.llm.openai] # 与 OpenAI 相容的语言模型接口服务设置 +url = "" +key = "" \ No newline at end of file diff --git a/src/heurams/interface/__main__.py b/src/heurams/interface/__main__.py index d42b584..7cf77d1 100644 --- a/src/heurams/interface/__main__.py +++ b/src/heurams/interface/__main__.py @@ -3,7 +3,7 @@ from heurams.context import rootdir, workdir, config_var from textual.app import App from textual.widgets import Button from .screens.dashboard import DashboardScreen -from .screens.nucleon_creator import NucleonCreatorScreen +from .screens.nucreator import NucleonCreatorScreen from .screens.precache import PrecachingScreen class HeurAMSApp(App): diff --git a/src/heurams/interface/screens/dashboard.py b/src/heurams/interface/screens/dashboard.py index 45abe91..e8ff068 100644 --- a/src/heurams/interface/screens/dashboard.py +++ b/src/heurams/interface/screens/dashboard.py @@ -105,7 +105,7 @@ class DashboardScreen(Screen): def on_button_pressed(self, event) -> None: if event.button.id == "new_nucleon_button": # 切换到创建单元 - from .nucleon_creator import NucleonCreatorScreen + from .nucreator import NucleonCreatorScreen newscr = NucleonCreatorScreen() self.app.push_screen(newscr) elif event.button.id == "precache_all_button": diff --git a/src/heurams/interface/screens/memorizor.py b/src/heurams/interface/screens/memorizor.py index 8fa49ff..57dbbd6 100644 --- a/src/heurams/interface/screens/memorizor.py +++ b/src/heurams/interface/screens/memorizor.py @@ -18,7 +18,7 @@ class AtomState(Enum): class MemScreen(Screen): BINDINGS = [ ("q", "pop_screen", "返回"), - #("p", "prev", "上一个"), + ("p", "prev", "复习上一个"), ("d", "toggle_dark", "改变色调"), ("v", "play_voice", "朗读"), ] @@ -58,6 +58,7 @@ class MemScreen(Screen): def on_button_pressed(self, event): event.stop() + if def action_play_voice(self): """朗读当前内容""" diff --git a/src/heurams/interface/screens/nucleon_creator.py b/src/heurams/interface/screens/nucreator.py similarity index 58% rename from src/heurams/interface/screens/nucleon_creator.py rename to src/heurams/interface/screens/nucreator.py index 9e9f366..8a3b3bb 100644 --- a/src/heurams/interface/screens/nucleon_creator.py +++ b/src/heurams/interface/screens/nucreator.py @@ -12,8 +12,10 @@ from textual.widgets import ( from textual.containers import Container from textual.screen import Screen +from heurams.services.version import ver + class NucleonCreatorScreen(Screen): - BINDINGS = [("q", "go_back", "返回"), ("escape", "quit_app", "退出")] + BINDINGS = [("q", "go_back", "返回")] def __init__(self) -> None: super().__init__(name=None, id=None, classes=None) @@ -21,30 +23,34 @@ class NucleonCreatorScreen(Screen): def compose(self) -> ComposeResult: yield Header(show_clock=True) with Container(id="vice_container"): - yield Label(f"[b]新建空的单元集\n") + yield Label(f"[b]空白单元集创建向导\n") + yield Markdown("> 提示: 你可能注意到当选中文本框时底栏和操作按键绑定将被覆盖 \n只需选中(使用鼠标或 Tab)选择框即可恢复底栏功能") yield Markdown("1. 键入单元集名称") yield Input(placeholder="单元集名称") - yield Markdown("> 单元集名称不应与现有单元集重复, 新的单元集文件将创建在 ./nucleon/你输入的名称.toml") + yield Markdown("> 单元集名称不应与现有单元集重复. \n> 新的单元集文件将创建在 ./nucleon/你输入的名称.toml") yield Label(f"\n") - yield Markdown("2. 选择单元集类型") - LINES = """ -单一字符串 -主字符串(带有附加属性) -动态单元集(使用宏) + yield Markdown("2. 选择单元集模板") + LINES = f"""带有宏支持的空白单元集 ({ver}) +古诗词模板单元集 ({ver}) +英语词汇和短语模板单元集 ({ver}) """.splitlines() yield Select.from_values(LINES, prompt="选择类型") + yield Markdown("> 新单元集的版本号将和主程序版本保持同步") yield Label(f"\n") - yield Markdown("3. 输入附加元数据 (可选)") + yield Markdown("3. 输入常见附加元数据 (可选)") yield Input(placeholder="作者") yield Input(placeholder="内容描述") yield Button( - "新建空单元集", + "新建空白单元集", id="submit_button", variant="primary", classes="start-button", ) yield Footer() + def on_mount(self): + self.query_one("#submit_button").focus() + def action_go_back(self): self.app.pop_screen() diff --git a/src/heurams/interface/screens/precache.py b/src/heurams/interface/screens/precache.py index 3a4582f..b358283 100644 --- a/src/heurams/interface/screens/precache.py +++ b/src/heurams/interface/screens/precache.py @@ -177,26 +177,16 @@ class PrecachingScreen(Screen): nu = list() for file in nucleon_files: try: - nu += pt.load_nucleon(file)[0] - self.total = len(nu) + for i in pt.load_nucleon(file): + nu.append(i[0]) except: continue - - self.processed = 0 - self.is_precaching = True - - for file in nucleon_files: - try: - nu += pt.load_nucleon(file)[0] - if not self.precache_by_list(nu): - break # 用户取消 - except Exception as e: - print(f"处理文件失败 {file}: {e}") - continue - - self.is_precaching = False - self.update_status("完成", "所有单元的音频已被预缓存", 100) - + self.total = len(nu) + for i in nu: + 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: diff --git a/src/heurams/providers/audio/__init__.py b/src/heurams/providers/audio/__init__.py index f198ec3..b707390 100644 --- a/src/heurams/providers/audio/__init__.py +++ b/src/heurams/providers/audio/__init__.py @@ -1,10 +1,13 @@ # 音频播放器, 必须基于文件操作 -from .termux_audio import player as termux_player +from . import termux_audio +from . import playsound_audio __all__ = [ - "termux_player" + "termux_audio", + "playsound_audio", ] -players = { - "termux": termux_player +providers = { + "termux": termux_audio, + "playsound": playsound_audio } \ No newline at end of file diff --git a/src/heurams/providers/audio/system_audio.py b/src/heurams/providers/audio/playsound_audio.py similarity index 60% rename from src/heurams/providers/audio/system_audio.py rename to src/heurams/providers/audio/playsound_audio.py index bcef367..011d160 100644 --- a/src/heurams/providers/audio/system_audio.py +++ b/src/heurams/providers/audio/playsound_audio.py @@ -2,3 +2,10 @@ 基于 playsound 库的音频播放器, 在绝大多数 python 环境上提供音频服务 注意: 在未配置 pulseaudio 的 termux 不可用 """ + +import os +import pathlib +import playsound + +def play_by_path(path: pathlib.Path): + playsound.playsound(str(path)) \ No newline at end of file diff --git a/src/heurams/providers/audio/protocol.py b/src/heurams/providers/audio/protocol.py new file mode 100644 index 0000000..f012030 --- /dev/null +++ b/src/heurams/providers/audio/protocol.py @@ -0,0 +1,5 @@ +from typing import Protocol +import pathlib + +class PlayFunctionProtocol(Protocol): + def __call__(self, path: pathlib.Path) -> None: ... \ No newline at end of file diff --git a/src/heurams/providers/audio/termux_audio.py b/src/heurams/providers/audio/termux_audio.py index 7b4e5d8..0919dd6 100644 --- a/src/heurams/providers/audio/termux_audio.py +++ b/src/heurams/providers/audio/termux_audio.py @@ -5,6 +5,7 @@ import os import pathlib +#from .protocol import PlayFunctionProtocol -def player(path: pathlib.Path): +def play_by_path(path: pathlib.Path): os.system(f"play-audio {path}") \ No newline at end of file diff --git a/src/heurams/providers/llm/deepseek.py b/src/heurams/providers/llm/deepseek.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/heurams/providers/tts/__init__.py b/src/heurams/providers/tts/__init__.py index 41a78da..3d42182 100644 --- a/src/heurams/providers/tts/__init__.py +++ b/src/heurams/providers/tts/__init__.py @@ -6,7 +6,7 @@ __all__ = [ "EdgeTTS", ] -TTSs = { - "BaseTTS": BaseTTS, - "EdgeTTS": EdgeTTS, +providers = { + "basetts": BaseTTS, + "edgetts": EdgeTTS, } \ No newline at end of file