#!/usr/bin/env python3 from textual.app import ComposeResult from textual.widgets import ( Header, Footer, Label, Static, Button, Markdown, ) from textual.containers import Container from textual.screen import Screen 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: super().__init__(name=None, id=None, classes=None) self.nucleon_file = nucleon_file self.electron_file = electron_file self.nucleons_with_orbital = pt.load_nucleon(self.nucleon_file) self.electrons = pt.load_electron(self.electron_file) def compose(self) -> ComposeResult: yield Header(show_clock=True) with Container(id="vice_container"): yield Label(f"准备就绪: [b]{self.nucleon_file.stem}[/b]\n") yield Label(f"内容源文件对象: ./nucleon/[b]{self.nucleon_file.name}[/b]") yield Label(f"元数据文件对象: ./electron/[b]{self.electron_file.name}[/b]") yield Label(f"\n单元数量: {len(self.nucleons_with_orbital)}\n") yield Button( "开始记忆", id="start_memorizing_button", variant="primary", classes="start-button", ) yield Button( "预缓存音频", id="precache_button", variant="success", classes="precache-button", ) yield Static(f"\n单元预览:\n") yield Markdown(self._get_full_content().replace("/", ""), classes="full") yield Footer() def _get_full_content(self): content = "" for nucleon, orbital in self.nucleons_with_orbital: nucleon: pt.Nucleon # print(nucleon.payload) content += " - " + nucleon["content"] + " \n" return content def action_go_back(self): self.app.pop_screen() def action_precache(self): from ..screens.precache import PrecachingScreen lst = list() for i in self.nucleons_with_orbital: lst.append(i[0]) precache_screen = PrecachingScreen(lst) self.app.push_screen(precache_screen) def action_quit_app(self): self.app.exit() def on_button_pressed(self, event: Button.Pressed) -> None: event.stop() if event.button.id == "start_memorizing_button": atoms = list() for nucleon, orbital in self.nucleons_with_orbital: atom = pt.Atom(nucleon.ident) atom.link("nucleon", nucleon) try: atom.link("electron", self.electrons[nucleon.ident]) except KeyError: atom.link("electron", pt.Electron(nucleon.ident)) atom.link("orbital", orbital) atom.link("nucleon_fmt", "toml") atom.link("electron_fmt", "json") atom.link("orbital_fmt", "toml") atom.link("nucleon_path", self.nucleon_file) atom.link("electron_path", self.electron_file) 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()