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.isrunning = True
self.solvegottextlock = threading.Lock()
self.gettranslatelock = threading.Lock()
self.outputers = {}
self.processmethods = []
self.zhanweifu = 0
@ -323,7 +324,7 @@ class MAINUI:
pass
if donttrans:
return
_showrawfunction_unsafe = None
if not waitforresultcallback:
self.currenttext = text
self.currenttranslate = ""
@ -340,99 +341,78 @@ class MAINUI:
if globalconfig["textoutput_origin"]:
self.dispatchoutputer(text)
_showrawfunction = functools.partial(
_showrawfunction_unsafe = functools.partial(
self.translation_ui.displayraw1.emit,
dict(text=text, color=globalconfig["rawtextcolor"]),
)
self.thishastranslated = globalconfig["showfanyi"]
else:
_showrawfunction = None
_showrawfunction = lambda: (
_showrawfunction_unsafe() if _showrawfunction_unsafe else None
)
self.transhis.getnewsentencesignal.emit(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 _showrawfunction:
_showrawfunction()
return
return _showrawfunction()
text_solved, optimization_params = self.solvebeforetrans(text)
premtalready = ["premt"]
usefultranslators = list(self.translators.keys())
no_available_translator = True
maybehaspremt = {}
fix_rank = globalconfig["fix_translate_rank_rank"].copy()
if "premt" in self.translators:
try:
res = self.translators["premt"].translate(text_solved)
for engine in res:
premtalready.append(engine)
maybehaspremt = self.translators["premt"].translate(text_solved)
except:
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"]:
_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],
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
def ifuse_fix_translate_rank_preprare(self, engine, waitforresultcallback):
if waitforresultcallback:
return
def _delaypreparefixrank(self, _showrawfunction, real_fix_rank):
_showrawfunction()
for engine in real_fix_rank:
colorx = globalconfig["fanyi"].get(engine, globalconfig["fanyi"]["premt"])
displayreskwargs = dict(
name="",
color=globalconfig["fanyi"][engine]["color"],
color=colorx["color"],
res="",
iter_context=(1, engine),
)
@ -445,7 +425,6 @@ class MAINUI:
engine,
optimization_params,
_showrawfunction,
_showrawfunction_sig,
text,
text_solved,
waitforresultcallback,
@ -460,7 +439,6 @@ class MAINUI:
currentsignature,
optimization_params,
_showrawfunction,
_showrawfunction_sig,
text,
)
task = (
@ -485,23 +463,30 @@ class MAINUI:
currentsignature,
optimization_params,
_showrawfunction,
_showrawfunction_sig,
contentraw,
res,
iter_res_status,
iserror=False,
):
with self.gettranslatelock:
if classname in usefultranslators:
usefultranslators.remove(classname)
if waitforresultcallback is None and currentsignature != self.currentsignature:
if (
waitforresultcallback is None
and currentsignature != self.currentsignature
):
return
safe_callback = waitforresultcallback if waitforresultcallback else lambda _: 1
safe_callback = (
waitforresultcallback if waitforresultcallback else lambda _: 1
)
if iserror:
if currentsignature == self.currentsignature:
self.translation_ui.displaystatus.emit(
globalconfig["fanyi"][classname]["name"] + " " + res, True, False
globalconfig["fanyi"][classname]["name"] + " " + res,
True,
False,
)
if len(usefultranslators) == 0:
safe_callback("")
@ -514,13 +499,14 @@ class MAINUI:
return
needshowraw = (
_showrawfunction
and self.refresh_on_get_trans_signature != _showrawfunction_sig
and self.refresh_on_get_trans_signature != _showrawfunction
)
if needshowraw:
self.refresh_on_get_trans_signature = _showrawfunction_sig
self.refresh_on_get_trans_signature = _showrawfunction
_showrawfunction()
if (currentsignature == self.currentsignature) and (iter_res_status in (0, 1)):
if (currentsignature == self.currentsignature) and (
iter_res_status in (0, 1)
):
displayreskwargs = dict(
name=globalconfig["fanyi"][classname]["name"],
color=globalconfig["fanyi"][classname]["color"],