This commit is contained in:
恍兮惚兮 2024-09-15 20:08:17 +08:00
parent 620f4e84fd
commit e0534fc547

View File

@ -66,6 +66,7 @@ class MAINUI:
self.currentsignature = None self.currentsignature = None
self.isrunning = True self.isrunning = True
self.solvegottextlock = threading.Lock() self.solvegottextlock = threading.Lock()
self.gettranslatelock = threading.Lock()
self.outputers = {} self.outputers = {}
self.processmethods = [] self.processmethods = []
self.zhanweifu = 0 self.zhanweifu = 0
@ -323,7 +324,7 @@ class MAINUI:
pass pass
if donttrans: if donttrans:
return return
_showrawfunction_unsafe = None
if not waitforresultcallback: if not waitforresultcallback:
self.currenttext = text self.currenttext = text
self.currenttranslate = "" self.currenttranslate = ""
@ -340,103 +341,82 @@ class MAINUI:
if globalconfig["textoutput_origin"]: if globalconfig["textoutput_origin"]:
self.dispatchoutputer(text) self.dispatchoutputer(text)
_showrawfunction = functools.partial( _showrawfunction_unsafe = functools.partial(
self.translation_ui.displayraw1.emit, self.translation_ui.displayraw1.emit,
dict(text=text, color=globalconfig["rawtextcolor"]), dict(text=text, color=globalconfig["rawtextcolor"]),
) )
self.thishastranslated = globalconfig["showfanyi"] self.thishastranslated = globalconfig["showfanyi"]
else: _showrawfunction = lambda: (
_showrawfunction = None _showrawfunction_unsafe() if _showrawfunction_unsafe else None
)
self.transhis.getnewsentencesignal.emit(text) self.transhis.getnewsentencesignal.emit(text)
self.maybesetedittext(text) self.maybesetedittext(text)
if globalconfig["refresh_on_get_trans"] == False:
if _showrawfunction:
_showrawfunction()
_showrawfunction = None
_showrawfunction_sig = 0
else:
_showrawfunction_sig = uuid.uuid4()
if not globalconfig["showfanyi"]: if not globalconfig["showfanyi"]:
if _showrawfunction: return _showrawfunction()
_showrawfunction()
return
text_solved, optimization_params = self.solvebeforetrans(text) text_solved, optimization_params = self.solvebeforetrans(text)
premtalready = ["premt"]
usefultranslators = list(self.translators.keys()) usefultranslators = list(self.translators.keys())
no_available_translator = True maybehaspremt = {}
fix_rank = globalconfig["fix_translate_rank_rank"].copy()
if "premt" in self.translators: if "premt" in self.translators:
try: try:
res = self.translators["premt"].translate(text_solved) maybehaspremt = self.translators["premt"].translate(text_solved)
for engine in res:
premtalready.append(engine)
if engine in globalconfig["fanyi"]:
_colork = engine
else:
_colork = "premt"
no_available_translator = False
self.create_translate_task(
currentsignature,
usefultranslators,
_colork,
optimization_params,
_showrawfunction,
_showrawfunction_sig,
text,
text_solved,
waitforresultcallback,
is_auto_run,
res[engine],
)
except: except:
print_exc() print_exc()
if len(self.translators): other = list(set(maybehaspremt.keys()) - set(fix_rank))
collect_this_time_use_engines = [] idx = fix_rank.index("premt")
for engine in self.translators: fix_rank = fix_rank[:idx] + other + fix_rank[idx + 1 :]
if engine not in premtalready:
no_available_translator = False
collect_this_time_use_engines.append(engine)
for engine in globalconfig["fix_translate_rank_rank"]: real_fix_rank = []
if engine not in collect_this_time_use_engines:
continue
if globalconfig["fix_translate_rank"]: for engine in fix_rank:
self.ifuse_fix_translate_rank_preprare( if (engine not in self.translators) and (engine not in maybehaspremt):
engine, waitforresultcallback continue
) real_fix_rank.append(engine)
self.create_translate_task( if len(real_fix_rank) == 0:
currentsignature, return _showrawfunction()
usefultranslators, if globalconfig["fix_translate_rank"] and (not waitforresultcallback):
engine, _showrawfunction = functools.partial(
optimization_params, self._delaypreparefixrank, _showrawfunction, real_fix_rank
_showrawfunction, )
_showrawfunction_sig, if not globalconfig["refresh_on_get_trans"]:
text, _showrawfunction()
text_solved, _showrawfunction = None
waitforresultcallback, for engine in real_fix_rank:
is_auto_run, if engine in globalconfig["fanyi"]:
) _colork = engine
if no_available_translator: else:
if _showrawfunction: _colork = "premt"
_showrawfunction() self.create_translate_task(
return currentsignature,
usefultranslators,
_colork,
optimization_params,
_showrawfunction,
text,
text_solved,
waitforresultcallback,
is_auto_run,
result=maybehaspremt.get(engine),
)
return True return True
def ifuse_fix_translate_rank_preprare(self, engine, waitforresultcallback): def _delaypreparefixrank(self, _showrawfunction, real_fix_rank):
if waitforresultcallback: _showrawfunction()
return for engine in real_fix_rank:
displayreskwargs = dict( colorx = globalconfig["fanyi"].get(engine, globalconfig["fanyi"]["premt"])
name="", displayreskwargs = dict(
color=globalconfig["fanyi"][engine]["color"], name="",
res="", color=colorx["color"],
iter_context=(1, engine), res="",
) iter_context=(1, engine),
self.translation_ui.displayres.emit(displayreskwargs) )
self.translation_ui.displayres.emit(displayreskwargs)
def create_translate_task( def create_translate_task(
self, self,
@ -445,7 +425,6 @@ class MAINUI:
engine, engine,
optimization_params, optimization_params,
_showrawfunction, _showrawfunction,
_showrawfunction_sig,
text, text,
text_solved, text_solved,
waitforresultcallback, waitforresultcallback,
@ -460,7 +439,6 @@ class MAINUI:
currentsignature, currentsignature,
optimization_params, optimization_params,
_showrawfunction, _showrawfunction,
_showrawfunction_sig,
text, text,
) )
task = ( task = (
@ -485,81 +463,89 @@ class MAINUI:
currentsignature, currentsignature,
optimization_params, optimization_params,
_showrawfunction, _showrawfunction,
_showrawfunction_sig,
contentraw, contentraw,
res, res,
iter_res_status, iter_res_status,
iserror=False, iserror=False,
): ):
if classname in usefultranslators: with self.gettranslatelock:
usefultranslators.remove(classname) if classname in usefultranslators:
if waitforresultcallback is None and currentsignature != self.currentsignature: usefultranslators.remove(classname)
return if (
waitforresultcallback is None
safe_callback = waitforresultcallback if waitforresultcallback else lambda _: 1 and currentsignature != self.currentsignature
if iserror:
if currentsignature == self.currentsignature:
self.translation_ui.displaystatus.emit(
globalconfig["fanyi"][classname]["name"] + " " + res, True, False
)
if len(usefultranslators) == 0:
safe_callback("")
return
res = self.solveaftertrans(res, optimization_params)
if not res:
if len(usefultranslators) == 0:
safe_callback("")
return
needshowraw = (
_showrawfunction
and self.refresh_on_get_trans_signature != _showrawfunction_sig
)
if needshowraw:
self.refresh_on_get_trans_signature = _showrawfunction_sig
_showrawfunction()
if (currentsignature == self.currentsignature) and (iter_res_status in (0, 1)):
displayreskwargs = dict(
name=globalconfig["fanyi"][classname]["name"],
color=globalconfig["fanyi"][classname]["color"],
res=res,
iter_context=(iter_res_status, classname),
)
self.translation_ui.displayres.emit(displayreskwargs)
if iter_res_status in (0, 2): # 0为普通1为iter2为iter终止
self.transhis.getnewtranssignal.emit(
globalconfig["fanyi"][classname]["name"], res
)
if not waitforresultcallback:
if (
globalconfig["read_trans"]
and globalconfig["read_translator2"] == classname
):
self.currentread = res
self.readcurrent()
if globalconfig["textoutput_trans"]:
self.dispatchoutputer(res)
try:
self.textsource.sqlqueueput((contentraw, classname, res))
except:
pass
if len(self.currenttranslate):
self.currenttranslate += "\n"
self.currenttranslate += res
if globalconfig["embedded"]["as_fast_as_posible"] or (
classname == globalconfig["embedded"]["translator_2"]
): ):
safe_callback( return
kanjitrans(zhconv.convert(res, "zh-tw"))
if globalconfig["embedded"]["trans_kanji"] safe_callback = (
else res waitforresultcallback if waitforresultcallback else lambda _: 1
)
if iserror:
if currentsignature == self.currentsignature:
self.translation_ui.displaystatus.emit(
globalconfig["fanyi"][classname]["name"] + " " + res,
True,
False,
)
if len(usefultranslators) == 0:
safe_callback("")
return
res = self.solveaftertrans(res, optimization_params)
if not res:
if len(usefultranslators) == 0:
safe_callback("")
return
needshowraw = (
_showrawfunction
and self.refresh_on_get_trans_signature != _showrawfunction
)
if needshowraw:
self.refresh_on_get_trans_signature = _showrawfunction
_showrawfunction()
if (currentsignature == self.currentsignature) and (
iter_res_status in (0, 1)
):
displayreskwargs = dict(
name=globalconfig["fanyi"][classname]["name"],
color=globalconfig["fanyi"][classname]["color"],
res=res,
iter_context=(iter_res_status, classname),
) )
elif len(usefultranslators) == 0: self.translation_ui.displayres.emit(displayreskwargs)
safe_callback("") if iter_res_status in (0, 2): # 0为普通1为iter2为iter终止
self.transhis.getnewtranssignal.emit(
globalconfig["fanyi"][classname]["name"], res
)
if not waitforresultcallback:
if (
globalconfig["read_trans"]
and globalconfig["read_translator2"] == classname
):
self.currentread = res
self.readcurrent()
if globalconfig["textoutput_trans"]:
self.dispatchoutputer(res)
try:
self.textsource.sqlqueueput((contentraw, classname, res))
except:
pass
if len(self.currenttranslate):
self.currenttranslate += "\n"
self.currenttranslate += res
if globalconfig["embedded"]["as_fast_as_posible"] or (
classname == globalconfig["embedded"]["translator_2"]
):
safe_callback(
kanjitrans(zhconv.convert(res, "zh-tw"))
if globalconfig["embedded"]["trans_kanji"]
else res
)
elif len(usefultranslators) == 0:
safe_callback("")
def __usewhich(self): def __usewhich(self):