feat: 完成新版缓存管理器

This commit is contained in:
2025-12-09 15:49:01 +08:00
parent 23780a57c6
commit e9582e1dc0
2 changed files with 24 additions and 5 deletions

View File

@@ -1 +1 @@
print("Hello from HeurAMS Program :)") print("欢迎使用 HeurAMS 及其组件!")

View File

@@ -39,9 +39,10 @@ class PrecachingScreen(Screen):
self.current_file = "" self.current_file = ""
self.current_item = "" self.current_item = ""
self.progress = 0 self.progress = 0
self.total = 0 self.total = len(nucleons)
self.processed = 0 self.processed = 0
self.precache_worker = None self.precache_worker = None
self.cancel_flag = 0
self.desc = desc self.desc = desc
for i in nucleons: for i in nucleons:
i: pt.Nucleon i: pt.Nucleon
@@ -70,6 +71,10 @@ class PrecachingScreen(Screen):
yield Button("取消预缓存", id="cancel_precache", variant="error") yield Button("取消预缓存", id="cancel_precache", variant="error")
yield Button("清空缓存", id="clear_cache", variant="warning") yield Button("清空缓存", id="clear_cache", variant="warning")
yield Button("返回", id="go_back", variant="default") yield Button("返回", id="go_back", variant="default")
yield Static("若您离开此界面, 未完成的缓存进程会自动停止.")
yield Static("缓存程序支持 \"断点续传\".")
yield Footer() yield Footer()
def on_mount(self): def on_mount(self):
@@ -116,17 +121,22 @@ class PrecachingScreen(Screen):
def precache_by_list(self, nucleons: list): def precache_by_list(self, nucleons: list):
"""依据 Nucleons 列表缓存""" """依据 Nucleons 列表缓存"""
for idx, nucleon in enumerate(nucleons): for idx, nucleon in enumerate(nucleons):
print(f"PROC: {nucleon}") #print(f"PROC: {nucleon}")
worker = get_current_worker() worker = get_current_worker()
if worker and worker.is_cancelled: # 函数在worker中执行且已被取消 if worker and worker.is_cancelled: # 函数在worker中执行且已被取消
return False return False
text = nucleon.metadata['formation']['tts_text'] text = nucleon.metadata['formation']['tts_text']
#self.current_item = text[:30] + "..." if len(text) > 50 else text #self.current_item = text[:30] + "..." if len(text) > 50 else text
print(text) #print(text)
self.processed += 1 self.processed += 1
#print(self.processed)
#print(self.total)
progress = int((self.processed / self.total) * 100) if self.total > 0 else 0 progress = int((self.processed / self.total) * 100) if self.total > 0 else 0
#print(progress)
self.update_status( self.update_status(
f"正处理 ({idx + 1}/{len(nucleons)})" f"正处理 ({idx + 1}/{len(nucleons)})",
text,
progress
) )
ret = self.precache_by_nucleon(nucleon) ret = self.precache_by_nucleon(nucleon)
if not ret: if not ret:
@@ -136,6 +146,10 @@ class PrecachingScreen(Screen):
) )
import time import time
time.sleep(1) time.sleep(1)
if self.cancel_flag:
worker.cancel()
self.cancel_flag = 0
return False
return True return True
def precache_by_nucleons(self): def precache_by_nucleons(self):
@@ -197,6 +211,8 @@ class PrecachingScreen(Screen):
if self.precache_worker: if self.precache_worker:
self.precache_worker.cancel() self.precache_worker.cancel()
self.is_precaching = False self.is_precaching = False
self.processed = 0
self.progress = 0
self.update_status("已取消", "预缓存操作被用户取消", 0) self.update_status("已取消", "预缓存操作被用户取消", 0)
elif event.button.id == "clear_cache": elif event.button.id == "clear_cache":
@@ -208,6 +224,9 @@ class PrecachingScreen(Screen):
self.update_status("已清空", "音频缓存已清空", 0) self.update_status("已清空", "音频缓存已清空", 0)
except Exception as e: except Exception as e:
self.update_status("错误", f"清空缓存失败: {e}") self.update_status("错误", f"清空缓存失败: {e}")
self.cancel_flag = 1
self.processed = 0
self.progress = 0
elif event.button.id == "go_back": elif event.button.id == "go_back":
self.action_go_back() self.action_go_back()