From 853511b861903cf34c1866962a3a88a16dbd8f17 Mon Sep 17 00:00:00 2001 From: david-ajax Date: Mon, 8 Dec 2025 01:06:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BB=AA=E8=A1=A8=E7=9B=98=E6=94=B9?= =?UTF-8?q?=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/heurams/context.py | 3 +- src/heurams/interface/__main__.py | 12 ++--- src/heurams/interface/screens/dashboard.py | 60 +++++++++++++++------- src/heurams/kernel/algorithms/sm2.py | 2 +- src/heurams/kernel/particles/electron.py | 2 +- src/heurams/kernel/particles/loader.py | 11 +++- src/heurams/kernel/particles/probe.py | 9 +++- 7 files changed, 69 insertions(+), 30 deletions(-) diff --git a/src/heurams/context.py b/src/heurams/context.py index 9a4029b..425ea5f 100644 --- a/src/heurams/context.py +++ b/src/heurams/context.py @@ -20,7 +20,8 @@ try: print('已加载自定义用户配置') except: print('未能加载自定义用户配置') -runtime_var: ContextVar = ContextVar('runtime_var', default=dict()) # 运行时共享数据 + +#runtime_var: ContextVar = ContextVar('runtime_var', default=dict()) # 运行时共享数据 class ConfigContext: """ diff --git a/src/heurams/interface/__main__.py b/src/heurams/interface/__main__.py index 56bdfe8..d42b584 100644 --- a/src/heurams/interface/__main__.py +++ b/src/heurams/interface/__main__.py @@ -1,14 +1,14 @@ -import heurams.context as cxt -from textual.app import App, ComposeResult -from textual.widgets import Button, Header, Label, Footer +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.preparation import PreparationScreen -from .screens.memorizor import MemScreen from .screens.nucleon_creator import NucleonCreatorScreen from .screens.precache import PrecachingScreen class HeurAMSApp(App): TITLE = "潜进" + #CSS_PATH = str(cxt.rootdir / "interface" / "css" / "main.css") SUB_TITLE = "启发式先进记忆调度器" BINDINGS = [("q", "quit", "退出"), ("d", "toggle_dark", "改变色调"), @@ -30,7 +30,7 @@ class HeurAMSApp(App): def environment_check(): from pathlib import Path - for i in cxt.config_var.get()["paths"].values(): + for i in config_var.get()["paths"].values(): i = Path(i) if not i.exists(): print(f"创建 {i}") diff --git a/src/heurams/interface/screens/dashboard.py b/src/heurams/interface/screens/dashboard.py index 310aaf5..45abe91 100644 --- a/src/heurams/interface/screens/dashboard.py +++ b/src/heurams/interface/screens/dashboard.py @@ -34,16 +34,44 @@ class DashboardScreen(Screen): ) yield Footer() - def item_desc_generator(self, path) -> dict: + def item_desc_generator(self, filename) -> dict: + """简单分析以生成项目项显示文本 + + Returns: + dict: 以数字为列表, 分别呈现单行字符串 + """ res = dict() - path = pathlib.Path(path) - res[0] = f"{path.name}\0" - res[1] = f"" - res[1] = " 尚未激活" + filestem = pathlib.Path(filename).stem + 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) + pass + else: + electron_file_path.touch() + with open(electron_file_path, 'w') as f: + f.write("{}") + electron_dict = load_electron(path=electron_file_path) # TODO: 取消硬编码扩展名 + is_due = 0 + is_activated = 0 + nextdate = 0x3f3f3f3f + for i in electron_dict.values(): + i: pt.Electron + if i.is_due(): + is_due = 1 + if i.is_activated(): + is_activated = 1 + nextdate = min(nextdate, i.nextdate()) + res[1] = f"下一次复习: {nextdate}\n" + res[1] += f"{is_due if "需要复习" else "当前无需复习"}" + if not is_activated: + res[1] = " 尚未激活" return res def on_mount(self) -> None: union_list_widget = self.query_one("#union-list", ListView) + probe = probe_all(0) if len(probe["nucleon"]): @@ -66,31 +94,25 @@ class DashboardScreen(Screen): if "未找到任何 .toml 文件" in str(selected_label.renderable): return - selected_filename = pathlib.Path(str(selected_label.renderable).partition('\0')[0].replace('*', "")) + 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") - if electron_file_path.exists(): - pass - else: - electron_file_path.touch() - with open(electron_file_path, 'w') as f: - f.write("{}") 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 .nucleon_creator import NucleonCreatorScreen newscr = NucleonCreatorScreen() self.app.push_screen(newscr) elif event.button.id == "precache_all_button": - self.action_precache_all() - - def action_precache_all(self): - """预缓存所有单元集的音频""" - from .precache import PrecachingScreen - precache_screen = PrecachingScreen() - self.app.push_screen(precache_screen) + # 切换到缓存管理器 + from .precache import PrecachingScreen + precache_screen = PrecachingScreen() + self.app.push_screen(precache_screen) def action_quit_app(self) -> None: self.app.exit() diff --git a/src/heurams/kernel/algorithms/sm2.py b/src/heurams/kernel/algorithms/sm2.py index 783fc71..5f2f82b 100644 --- a/src/heurams/kernel/algorithms/sm2.py +++ b/src/heurams/kernel/algorithms/sm2.py @@ -77,5 +77,5 @@ class SM2Algorithm(BaseAlgorithm): return str(algodata[cls.algo_name]['efactor']) @classmethod - def nextdate(cls, algodata): + def nextdate(cls, algodata) -> int: return algodata[cls.algo_name]['next_date'] \ No newline at end of file diff --git a/src/heurams/kernel/particles/electron.py b/src/heurams/kernel/particles/electron.py index 4df12e4..8bf92f8 100644 --- a/src/heurams/kernel/particles/electron.py +++ b/src/heurams/kernel/particles/electron.py @@ -50,7 +50,7 @@ class Electron: "评价" return self.algo.rate(self.algodata) - def nextdate(self): + def nextdate(self) -> int: return self.algo.nextdate(self.algodata) def revisor(self, quality: int = 5, is_new_activation: bool = False): diff --git a/src/heurams/kernel/particles/loader.py b/src/heurams/kernel/particles/loader.py index 63ec73c..7f399f5 100644 --- a/src/heurams/kernel/particles/loader.py +++ b/src/heurams/kernel/particles/loader.py @@ -32,10 +32,19 @@ def load_nucleon(path: pathlib.Path, fmt = "toml"): return lst def load_electron(path: pathlib.Path, fmt = "json") -> dict: + """从文件路径加载电子对象 + + Args: + path (pathlib.Path): 路径 + fmt (str): 文件格式(可选, 默认 json) + + Returns: + dict: 键名是电子对象名称, 值是电子对象 + """ with open(path, "r") as f: dictdata = dict() dictdata = json.load(f) # type: ignore dic = dict() for item, attr in dictdata.items(): - dic["item"] = (Electron(hasher.hash(item), attr)) + dic[item] = (Electron(hasher.hash(item), attr)) return dic \ No newline at end of file diff --git a/src/heurams/kernel/particles/probe.py b/src/heurams/kernel/particles/probe.py index 5f4015a..36cb22c 100644 --- a/src/heurams/kernel/particles/probe.py +++ b/src/heurams/kernel/particles/probe.py @@ -14,7 +14,14 @@ def probe_by_filename(filename): return result def probe_all(is_stem = 1): - """依据目录探测所有信息""" + """依据目录探测所有信息 + + Args: + is_stem (boolean): 是否**删除**文件扩展名 + + Returns: + dict: 有三项, 每一项的键名都是文件组类型, 值都是文件组列表, 只包含文件名 + """ paths: dict = config_var.get().get("paths") result = {} for item, attr in paths.items():