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,99 +341,78 @@ 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: except:
premtalready.append(engine) print_exc()
other = list(set(maybehaspremt.keys()) - set(fix_rank))
idx = fix_rank.index("premt")
fix_rank = fix_rank[:idx] + other + fix_rank[idx + 1 :]
real_fix_rank = []
for engine in fix_rank:
if (engine not in self.translators) and (engine not in maybehaspremt):
continue
real_fix_rank.append(engine)
if len(real_fix_rank) == 0:
return _showrawfunction()
if globalconfig["fix_translate_rank"] and (not waitforresultcallback):
_showrawfunction = functools.partial(
self._delaypreparefixrank, _showrawfunction, real_fix_rank
)
if not globalconfig["refresh_on_get_trans"]:
_showrawfunction()
_showrawfunction = None
for engine in real_fix_rank:
if engine in globalconfig["fanyi"]: if engine in globalconfig["fanyi"]:
_colork = engine _colork = engine
else: else:
_colork = "premt" _colork = "premt"
no_available_translator = False
self.create_translate_task( self.create_translate_task(
currentsignature, currentsignature,
usefultranslators, usefultranslators,
_colork, _colork,
optimization_params, optimization_params,
_showrawfunction, _showrawfunction,
_showrawfunction_sig,
text, text,
text_solved, text_solved,
waitforresultcallback, waitforresultcallback,
is_auto_run, is_auto_run,
res[engine], result=maybehaspremt.get(engine),
) )
except:
print_exc()
if len(self.translators):
collect_this_time_use_engines = []
for engine in self.translators:
if engine not in premtalready:
no_available_translator = False
collect_this_time_use_engines.append(engine)
for engine in globalconfig["fix_translate_rank_rank"]:
if engine not in collect_this_time_use_engines:
continue
if globalconfig["fix_translate_rank"]:
self.ifuse_fix_translate_rank_preprare(
engine, waitforresultcallback
)
self.create_translate_task(
currentsignature,
usefultranslators,
engine,
optimization_params,
_showrawfunction,
_showrawfunction_sig,
text,
text_solved,
waitforresultcallback,
is_auto_run,
)
if no_available_translator:
if _showrawfunction:
_showrawfunction()
return
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:
colorx = globalconfig["fanyi"].get(engine, globalconfig["fanyi"]["premt"])
displayreskwargs = dict( displayreskwargs = dict(
name="", name="",
color=globalconfig["fanyi"][engine]["color"], color=colorx["color"],
res="", res="",
iter_context=(1, engine), iter_context=(1, engine),
) )
@ -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,23 +463,30 @@ 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,
): ):
with self.gettranslatelock:
if classname in usefultranslators: if classname in usefultranslators:
usefultranslators.remove(classname) usefultranslators.remove(classname)
if waitforresultcallback is None and currentsignature != self.currentsignature: if (
waitforresultcallback is None
and currentsignature != self.currentsignature
):
return return
safe_callback = waitforresultcallback if waitforresultcallback else lambda _: 1 safe_callback = (
waitforresultcallback if waitforresultcallback else lambda _: 1
)
if iserror: if iserror:
if currentsignature == self.currentsignature: if currentsignature == self.currentsignature:
self.translation_ui.displaystatus.emit( self.translation_ui.displaystatus.emit(
globalconfig["fanyi"][classname]["name"] + " " + res, True, False globalconfig["fanyi"][classname]["name"] + " " + res,
True,
False,
) )
if len(usefultranslators) == 0: if len(usefultranslators) == 0:
safe_callback("") safe_callback("")
@ -514,13 +499,14 @@ class MAINUI:
return return
needshowraw = ( needshowraw = (
_showrawfunction _showrawfunction
and self.refresh_on_get_trans_signature != _showrawfunction_sig and self.refresh_on_get_trans_signature != _showrawfunction
) )
if needshowraw: if needshowraw:
self.refresh_on_get_trans_signature = _showrawfunction_sig self.refresh_on_get_trans_signature = _showrawfunction
_showrawfunction() _showrawfunction()
if (currentsignature == self.currentsignature) and (
if (currentsignature == self.currentsignature) and (iter_res_status in (0, 1)): iter_res_status in (0, 1)
):
displayreskwargs = dict( displayreskwargs = dict(
name=globalconfig["fanyi"][classname]["name"], name=globalconfig["fanyi"][classname]["name"],
color=globalconfig["fanyi"][classname]["color"], color=globalconfig["fanyi"][classname]["color"],