mirror of
https://github.com/HIllya51/LunaTranslator.git
synced 2025-01-01 10:04:12 +08:00
safe
This commit is contained in:
parent
01939f3b5a
commit
5c71fed578
@ -154,13 +154,14 @@ class MAINUI:
|
|||||||
self, text, is_auto_run=True, embedcallback=None, onlytrans=False
|
self, text, is_auto_run=True, embedcallback=None, onlytrans=False
|
||||||
):
|
):
|
||||||
with self.solvegottextlock:
|
with self.solvegottextlock:
|
||||||
return self.textgetmethod_1(text, is_auto_run, embedcallback, onlytrans)
|
self.textgetmethod_1(text, is_auto_run, embedcallback, onlytrans)
|
||||||
|
|
||||||
def textgetmethod_1(
|
def textgetmethod_1(
|
||||||
self, text, is_auto_run=True, embedcallback=None, onlytrans=False
|
self, text, is_auto_run=True, embedcallback=None, onlytrans=False
|
||||||
):
|
):
|
||||||
|
|
||||||
returnandembedcallback = lambda: embedcallback("") if embedcallback else ""
|
safe_embedcallback = embedcallback if embedcallback else lambda _: 1
|
||||||
|
safe_embedcallback_none = functools.partial(safe_embedcallback, "")
|
||||||
|
|
||||||
if text.startswith("<notrans>"):
|
if text.startswith("<notrans>"):
|
||||||
self.translation_ui.displayres.emit(
|
self.translation_ui.displayres.emit(
|
||||||
@ -189,7 +190,7 @@ class MAINUI:
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
if text == "" or len(text) > 100000:
|
if text == "" or len(text) > 100000:
|
||||||
return returnandembedcallback()
|
return safe_embedcallback_none()
|
||||||
if onlytrans == False:
|
if onlytrans == False:
|
||||||
self.currentsignature = time.time()
|
self.currentsignature = time.time()
|
||||||
try:
|
try:
|
||||||
@ -208,7 +209,7 @@ class MAINUI:
|
|||||||
> (max(globalconfig["maxoriginlength"], globalconfig["maxlength"]))
|
> (max(globalconfig["maxoriginlength"], globalconfig["maxlength"]))
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
return returnandembedcallback()
|
return safe_embedcallback_none()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.textsource.sqlqueueput(
|
self.textsource.sqlqueueput(
|
||||||
@ -248,10 +249,11 @@ class MAINUI:
|
|||||||
len(text_solved) < globalconfig["minlength"]
|
len(text_solved) < globalconfig["minlength"]
|
||||||
or len(text_solved) > globalconfig["maxlength"]
|
or len(text_solved) > globalconfig["maxlength"]
|
||||||
):
|
):
|
||||||
return returnandembedcallback()
|
return safe_embedcallback_none()
|
||||||
|
|
||||||
self.premtalready = ["premt"]
|
self.premtalready = ["premt"]
|
||||||
self.usefultranslators = list(self.translators.keys())
|
self.usefultranslators = list(self.translators.keys())
|
||||||
|
no_available_translator = True
|
||||||
if "premt" in self.translators:
|
if "premt" in self.translators:
|
||||||
try:
|
try:
|
||||||
res = self.translators["premt"].translate(text_solved)
|
res = self.translators["premt"].translate(text_solved)
|
||||||
@ -261,6 +263,7 @@ class MAINUI:
|
|||||||
_colork = k
|
_colork = k
|
||||||
else:
|
else:
|
||||||
_colork = "premt"
|
_colork = "premt"
|
||||||
|
no_available_translator = False
|
||||||
self.GetTranslationCallback(
|
self.GetTranslationCallback(
|
||||||
onlytrans,
|
onlytrans,
|
||||||
_colork,
|
_colork,
|
||||||
@ -289,6 +292,7 @@ class MAINUI:
|
|||||||
# print(keys,usenum,self.lasttranslatorindex)
|
# print(keys,usenum,self.lasttranslatorindex)
|
||||||
for engine in keys:
|
for engine in keys:
|
||||||
if engine not in self.premtalready:
|
if engine not in self.premtalready:
|
||||||
|
no_available_translator = False
|
||||||
self.translators[engine].gettask(
|
self.translators[engine].gettask(
|
||||||
(
|
(
|
||||||
partial(
|
partial(
|
||||||
@ -311,6 +315,8 @@ class MAINUI:
|
|||||||
self.lasttranslatorindex += 1
|
self.lasttranslatorindex += 1
|
||||||
if thistimeusednum >= usenum:
|
if thistimeusednum >= usenum:
|
||||||
break
|
break
|
||||||
|
if no_available_translator:
|
||||||
|
safe_embedcallback_none()
|
||||||
|
|
||||||
def GetTranslationCallback(
|
def GetTranslationCallback(
|
||||||
self,
|
self,
|
||||||
@ -330,8 +336,8 @@ class MAINUI:
|
|||||||
if embedcallback is None and currentsignature != self.currentsignature:
|
if embedcallback is None and currentsignature != self.currentsignature:
|
||||||
return
|
return
|
||||||
|
|
||||||
returnandembedcallback = lambda x: embedcallback(x) if embedcallback else ""
|
safe_embedcallback = embedcallback if embedcallback else lambda _: 1
|
||||||
|
safe_embedcallback_none = functools.partial(safe_embedcallback, "")
|
||||||
if res.startswith("<msg_translator>"):
|
if res.startswith("<msg_translator>"):
|
||||||
if currentsignature == self.currentsignature:
|
if currentsignature == self.currentsignature:
|
||||||
self.translation_ui.displaystatus.emit(
|
self.translation_ui.displaystatus.emit(
|
||||||
@ -343,7 +349,7 @@ class MAINUI:
|
|||||||
False,
|
False,
|
||||||
)
|
)
|
||||||
if len(self.usefultranslators) == 0:
|
if len(self.usefultranslators) == 0:
|
||||||
returnandembedcallback("")
|
safe_embedcallback_none()
|
||||||
return
|
return
|
||||||
|
|
||||||
res = self.solveaftertrans(res, optimization_params)
|
res = self.solveaftertrans(res, optimization_params)
|
||||||
@ -388,7 +394,7 @@ class MAINUI:
|
|||||||
globalconfig["embedded"]["as_fast_as_posible"]
|
globalconfig["embedded"]["as_fast_as_posible"]
|
||||||
or classname == globalconfig["embedded"]["translator_2"]
|
or classname == globalconfig["embedded"]["translator_2"]
|
||||||
):
|
):
|
||||||
return returnandembedcallback(
|
return safe_embedcallback(
|
||||||
kanjitrans(zhconv.convert(res, "zh-tw"))
|
kanjitrans(zhconv.convert(res, "zh-tw"))
|
||||||
if globalconfig["embedded"]["trans_kanji"]
|
if globalconfig["embedded"]["trans_kanji"]
|
||||||
else res
|
else res
|
||||||
|
Loading…
x
Reference in New Issue
Block a user