From 21aa04382ed845c97d94ac7122bc58a2093919c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:06:35 +0800 Subject: [PATCH] bug fix --- .../LunaTranslator/gui/selecthook.py | 2 +- .../translator/basetranslator.py | 77 ++++++++++--------- .../files/defaultconfig/static_data.json | 2 +- 3 files changed, 44 insertions(+), 37 deletions(-) diff --git a/LunaTranslator/LunaTranslator/gui/selecthook.py b/LunaTranslator/LunaTranslator/gui/selecthook.py index e9a09126..b51d5ebc 100644 --- a/LunaTranslator/LunaTranslator/gui/selecthook.py +++ b/LunaTranslator/LunaTranslator/gui/selecthook.py @@ -599,7 +599,7 @@ class hookselect(closeashidewindow): "embedablehook" ]: hc, ad, c1, c2 = _ - if (hc, ad, c1, c2) == (hc, tp.addr, tp.ctx, tp.ctx2): + if (hc, 0, c1, c2) == (hc, 0, tp.ctx, tp.ctx2): save.append(_) for _ in save: savehook_new_data[gobject.baseobject.textsource.pname][ diff --git a/LunaTranslator/LunaTranslator/translator/basetranslator.py b/LunaTranslator/LunaTranslator/translator/basetranslator.py index 148df961..32535267 100644 --- a/LunaTranslator/LunaTranslator/translator/basetranslator.py +++ b/LunaTranslator/LunaTranslator/translator/basetranslator.py @@ -268,6 +268,38 @@ class basetrans(commonbase): return False return globalconfig["fanyi"][self.typename]["manual"] + def _iterget(self, __callback, rid, __res): + succ = True + for i, _res in enumerate(__res): + if i == 0: + __callback("", 3) + if self.requestid != rid: + succ = False + break + __callback(_res, 1) + if succ: + __callback("", 2) + + def __callback(self, collectiterres, callback, embedcallback, _, is_iter_res): + if self.needzhconv: + _ = zhconv.convert(_, "zh-tw") + if _ == "\0": # 清除前面的输出 + collectiterres.clear() + pass + else: + collectiterres.append(_) + callback("".join(collectiterres), embedcallback, is_iter_res) + + def reinitandtrans(self, contentraw, contentsolved, is_auto_run): + if self.needreinit or self.initok == False: + self.needreinit = False + self.renewsesion() + try: + self._private_init() + except Exception as e: + raise Exception("inittranslator failed : " + str(stringfyerror(e))) + return self.maybecachetranslate(contentraw, contentsolved, is_auto_run) + def _fythread(self): self.needreinit = False while self.using: @@ -289,48 +321,23 @@ class basetrans(commonbase): ) if checktutukufunction(): - def reinitandtrans(): - if self.needreinit or self.initok == False: - self.needreinit = False - self.renewsesion() - try: - self._private_init() - except Exception as e: - raise Exception( - "inittranslator failed : " + str(stringfyerror(e)) - ) - return self.maybecachetranslate( - contentraw, contentsolved, is_auto_run - ) - res = timeoutfunction( - reinitandtrans, checktutukufunction=checktutukufunction + functools.partial( + self.reinitandtrans, contentraw, contentsolved, is_auto_run + ), + checktutukufunction=checktutukufunction, ) collectiterres = [] - def __callback(_, is_iter_res): - if self.needzhconv: - _ = zhconv.convert(_, "zh-tw") - if _ == "\0": # 清除前面的输出 - collectiterres.clear() - pass - else: - collectiterres.append(_) - callback("".join(collectiterres), embedcallback, is_iter_res) - + __callback = functools.partial( + self.__callback, collectiterres, callback, embedcallback + ) if isinstance(res, types.GeneratorType): - def _iterget(rid, __res): - for i, _res in enumerate(__res): - if i == 0: - __callback("", 3) - if self.requestid != rid: - break - __callback(_res, 1) - __callback("", 2) - timeoutfunction( - functools.partial(_iterget, self.requestid, res), + functools.partial( + self._iterget, __callback, self.requestid, res + ), checktutukufunction=checktutukufunction, ) diff --git a/LunaTranslator/files/defaultconfig/static_data.json b/LunaTranslator/files/defaultconfig/static_data.json index f8b81c55..e56121a2 100644 --- a/LunaTranslator/files/defaultconfig/static_data.json +++ b/LunaTranslator/files/defaultconfig/static_data.json @@ -1,5 +1,5 @@ { - "version":"v2.45.6", + "version":"v2.45.7", "themes":{ "dark":[ {"file":"dark1.qss","name":"PyQtDarkTheme"},