This commit is contained in:
恍兮惚兮 2024-07-22 23:43:47 +08:00
parent 8c43dce2ae
commit a80cd426c5
6 changed files with 154 additions and 55 deletions

View File

@ -24,6 +24,7 @@ from myutils.utils import (
checkpostusing, checkpostusing,
stringfyerror, stringfyerror,
targetmod, targetmod,
translate_exits,
) )
from myutils.wrapper import threader from myutils.wrapper import threader
from gui.showword import searchwordW from gui.showword import searchwordW
@ -668,9 +669,13 @@ class MAINUI:
def fanyiinitmethod(self, classname): def fanyiinitmethod(self, classname):
try: try:
if not os.path.exists("./LunaTranslator/translator/" + classname + ".py"): which = translate_exits(classname, which=True)
if which is None:
return None return None
aclass = importlib.import_module("translator." + classname).TS if which == 0:
aclass = importlib.import_module("translator." + classname).TS
elif which == 1:
aclass = importlib.import_module("userconfig.copyed." + classname).TS
except Exception as e: except Exception as e:
print_exc() print_exc()
self.textgetmethod( self.textgetmethod(

View File

@ -4,7 +4,7 @@ import gobject, os, zipfile, shutil
from myutils.config import globalconfig, static_data from myutils.config import globalconfig, static_data
from gui.inputdialog import multicolorset, autoinitdialog from gui.inputdialog import multicolorset, autoinitdialog
from myutils.wrapper import tryprint from myutils.wrapper import tryprint
from myutils.utils import dynamiclink from myutils.utils import dynamiclink, translate_exits
from gui.usefulwidget import ( from gui.usefulwidget import (
D_getsimplecombobox, D_getsimplecombobox,
getsimplecombobox, getsimplecombobox,
@ -310,8 +310,7 @@ def __changeselectablestate(self, x):
def vistranslate_rank(self): def vistranslate_rank(self):
_not = [] _not = []
for i, k in enumerate(globalconfig["fix_translate_rank_rank"]): for i, k in enumerate(globalconfig["fix_translate_rank_rank"]):
_f = "./Lunatranslator/translator/{}.py".format(k) if not translate_exits(k):
if not os.path.exists(_f):
_not.append(i) _not.append(i)
for _ in reversed(_not): for _ in reversed(_not):
globalconfig["fix_translate_rank_rank"].pop(_) globalconfig["fix_translate_rank_rank"].pop(_)

View File

@ -1,7 +1,7 @@
from qtsymbols import * from qtsymbols import *
import os, functools, re import os, functools, re
from myutils.config import globalconfig from myutils.config import globalconfig
from myutils.utils import splittranslatortypes from myutils.utils import splittranslatortypes, translate_exits
from gui.usefulwidget import ( from gui.usefulwidget import (
D_getsimpleswitch, D_getsimpleswitch,
makegrid, makegrid,
@ -18,9 +18,13 @@ def getall(l, item="fanyi", name=None):
line = [] line = []
for fanyi in l: for fanyi in l:
if name: if name:
_f = name % fanyi if isinstance(name, str):
if not os.path.exists(_f): _f = name % fanyi
continue if not os.path.exists(_f):
continue
elif callable(name):
if not name(fanyi):
continue
i += 1 i += 1
line += [ line += [
@ -105,7 +109,7 @@ def makeproxytab(self, basel):
lixians, pre, mianfei, develop, shoufei = splittranslatortypes() lixians, pre, mianfei, develop, shoufei = splittranslatortypes()
mianfei = getall(l=mianfei, item="fanyi", name="./Lunatranslator/translator/%s.py") mianfei = getall(l=mianfei, item="fanyi", name="./Lunatranslator/translator/%s.py")
shoufei = getall(l=shoufei, item="fanyi", name="./Lunatranslator/translator/%s.py") shoufei = getall(l=shoufei, item="fanyi", name=translate_exits)
ocrs = getall( ocrs = getall(
l=getnotofflines("ocr"), l=getnotofflines("ocr"),
item="ocr", item="ocr",

View File

@ -9,6 +9,7 @@ from myutils.utils import (
splittranslatortypes, splittranslatortypes,
checkportavailable, checkportavailable,
dynamiclink, dynamiclink,
translate_exits,
) )
from gui.pretransfile import sqlite2json from gui.pretransfile import sqlite2json
from gui.inputdialog import autoinitdialog, autoinitdialog_items from gui.inputdialog import autoinitdialog, autoinitdialog_items
@ -60,7 +61,7 @@ def splitapillm(l):
return is_gpt_likes, not_is_gpt_like return is_gpt_likes, not_is_gpt_like
def loadvisinternal(btnplus): def loadvisinternal(btnplus, copy):
__vis = [] __vis = []
__uid = [] __uid = []
lixians, pre, mianfei, develop, shoufei = splittranslatortypes() lixians, pre, mianfei, develop, shoufei = splittranslatortypes()
@ -70,17 +71,21 @@ def loadvisinternal(btnplus):
is_gpt_likes, not_is_gpt_like = splitapillm(lixians) is_gpt_likes, not_is_gpt_like = splitapillm(lixians)
for _ in is_gpt_likes: for _ in is_gpt_likes:
_f = "./Lunatranslator/translator/{}.py".format(_) if copy:
if not os.path.exists(_f): which = translate_exits(_, which=True)
continue if which != 1:
continue
else:
if not translate_exits(_):
continue
__vis.append(globalconfig["fanyi"][_]["name"]) __vis.append(globalconfig["fanyi"][_]["name"])
__uid.append(_) __uid.append(_)
return __vis, __uid return __vis, __uid
def getalistname(parent, btnplus, callback): def getalistname(parent, copy, btnplus, callback):
__d = {"k": 0, "n": ""} __d = {"k": 0, "n": ""}
__vis, __uid = loadvisinternal(btnplus) __vis, __uid = loadvisinternal(btnplus, copy)
def __wrap(callback, __d, __uid): def __wrap(callback, __d, __uid):
if len(__uid) == 0: if len(__uid) == 0:
@ -89,36 +94,45 @@ def getalistname(parent, btnplus, callback):
uid = __uid[__d["k"]] uid = __uid[__d["k"]]
callback(uid, __d["n"]) callback(uid, __d["n"])
autoinitdialog( __ = []
parent, __.append(
"复制", {
600, "type": "combo",
[ "name": "目标",
{ "d": __d,
"type": "combo", "k": "k",
"name": "目标", "list": __vis,
"d": __d, }
"k": "k", )
"list": __vis, if not copy:
}, __.append(
{ {
"name": "名称", "name": "名称",
"type": "lineedit", "type": "lineedit",
"d": __d, "d": __d,
"k": "n", "k": "n",
}, }
{ )
"type": "okcancel",
"callback": functools.partial(__wrap, callback, __d, __uid), __.append(
}, {
], "type": "okcancel",
"callback": functools.partial(__wrap, callback, __d, __uid),
}
)
autoinitdialog(
parent,
"删除" if copy else "复制",
600,
__,
) )
def selectllmcallback(self, countnum, btn, btnplus, fanyi, name): def selectllmcallback(self, countnum, btnplus, fanyi, name):
uid = str(uuid.uuid4()) uid = str(uuid.uuid4())
_f1 = "./Lunatranslator/translator/{}.py".format(fanyi) _f1 = "./Lunatranslator/translator/{}.py".format(fanyi)
_f2 = "./Lunatranslator/translator/{}.py".format(uid) _f2 = "./userconfig/copyed/{}.py".format(uid)
os.makedirs("./userconfig/copyed", exist_ok=True)
shutil.copy(_f1, _f2) shutil.copy(_f1, _f2)
globalconfig["fanyi"][uid] = deepcopydict(globalconfig["fanyi"][fanyi]) globalconfig["fanyi"][uid] = deepcopydict(globalconfig["fanyi"][fanyi])
globalconfig["fanyi"][uid]["use"] = False globalconfig["fanyi"][uid]["use"] = False
@ -167,8 +181,9 @@ def selectllmcallback(self, countnum, btn, btnplus, fanyi, name):
) )
if len(countnum) % 3 == 0: if len(countnum) % 3 == 0:
layout.addWidget(btn, layout.rowCount(), 5 * 2, 1, 2) layout.addWidget(
layout.addWidget(self.btnquestion, layout.rowCount() - 1, 5 * 2 + 2, 1, 2) getattr(self, "btnmany" + btnplus), layout.rowCount(), 5 * 2, 1, 4
)
offset = 5 * (len(countnum) % 3) offset = 5 * (len(countnum) % 3)
layout.addWidget(name, layout.rowCount() - 2, offset + 0) layout.addWidget(name, layout.rowCount() - 2, offset + 0)
layout.addWidget(swc, layout.rowCount() - 2, offset + 1) layout.addWidget(swc, layout.rowCount() - 2, offset + 1)
@ -177,37 +192,98 @@ def selectllmcallback(self, countnum, btn, btnplus, fanyi, name):
if len(countnum) % 3 != 2: if len(countnum) % 3 != 2:
layout.addWidget(QLabel(), layout.rowCount() - 2, offset + 4) layout.addWidget(QLabel(), layout.rowCount() - 2, offset + 4)
countnum.append(0) countnum.append(uid)
def btnpluscallback(self, countnum, btn, btnplus): def btnpluscallback(self, countnum, btnplus):
getalistname( getalistname(
self, self,
False,
btnplus, btnplus,
functools.partial(selectllmcallback, self, countnum, btn, btnplus), functools.partial(selectllmcallback, self, countnum, btnplus),
) )
def createbtn(self, countnum, btnplus): def createbtn(self, countnum, btnplus):
btn = QPushButton(self) btn = QPushButton(self)
btn.setIcon(qtawesome.icon("fa.plus")) btn.setIcon(qtawesome.icon("fa.plus"))
btn.clicked.connect( btn.clicked.connect(functools.partial(btnpluscallback, self, countnum, btnplus))
functools.partial(btnpluscallback, self, countnum, btn, btnplus) setattr(self, "btnadd" + btnplus, btn)
)
return btn return btn
def createbtnquest(self, btnplus): class Shit(QWidget):
pass
def selectllmcallback_2(self, countnum, btnplus, fanyi, name):
_f2 = "./userconfig/copyed/{}.py".format(fanyi)
try:
os.remove(_f2)
except:
pass
globalconfig["fanyi"][fanyi]["use"] = False
layout: QGridLayout = getattr(self, "damoxinggridinternal" + btnplus)
idx = countnum.index(fanyi)
line = idx // 3
off = line * 14 + (idx % 3) * 5
do = 0
i = 0
while do < 4:
w = layout.itemAt(off + i).widget()
i += 1
if isinstance(w, Shit):
continue
elif isinstance(w, QLabel) and w.text() == "":
continue
elif not w.isEnabled():
continue
w.setEnabled(False)
do += 1
def btndeccallback(self, countnum, btnplus):
getalistname(
self,
True,
btnplus,
functools.partial(selectllmcallback_2, self, countnum, btnplus),
)
def createmanybtn(self, countnum, btnplus):
hbox = QHBoxLayout()
hbox.setContentsMargins(0, 0, 0, 0)
w = Shit()
w.setLayout(hbox)
btn = QPushButton(self)
btn.setIcon(qtawesome.icon("fa.plus"))
btn.clicked.connect(functools.partial(btnpluscallback, self, countnum, btnplus))
hbox.addWidget(btn)
btn = QPushButton(self)
btn.setIcon(qtawesome.icon("fa.minus"))
btn.clicked.connect(functools.partial(btndeccallback, self, countnum, btnplus))
hbox.addWidget(btn)
btn = QPushButton(self) btn = QPushButton(self)
btn.setIcon(qtawesome.icon("fa.question")) btn.setIcon(qtawesome.icon("fa.question"))
if btnplus == "offline": if btnplus == "offline":
btn.clicked.connect(lambda: os.startfile(dynamiclink("{docs_server}/#/zh/offlinellm"))) btn.clicked.connect(
lambda: os.startfile(dynamiclink("{docs_server}/#/zh/offlinellm"))
)
elif btnplus == "api": elif btnplus == "api":
btn.clicked.connect( btn.clicked.connect(
lambda: os.startfile(dynamiclink("{docs_server}/#/zh/guochandamoxing")) lambda: os.startfile(dynamiclink("{docs_server}/#/zh/guochandamoxing"))
) )
self.btnquestion = btn hbox.addWidget(btn)
return btn setattr(self, "btnmany" + btnplus, w)
return w
def initsome11(self, l, label=None, btnplus=False): def initsome11(self, l, label=None, btnplus=False):
@ -219,11 +295,10 @@ def initsome11(self, l, label=None, btnplus=False):
countnum = [] countnum = []
for fanyi in l: for fanyi in l:
_f = "./Lunatranslator/translator/{}.py".format(fanyi) if not translate_exits(fanyi):
if not os.path.exists(_f):
continue continue
i += 1 i += 1
countnum.append(0) countnum.append(fanyi)
if fanyi in translatorsetting: if fanyi in translatorsetting:
items = autoinitdialog_items(translatorsetting[fanyi]) items = autoinitdialog_items(translatorsetting[fanyi])
@ -280,8 +355,7 @@ def initsome11(self, l, label=None, btnplus=False):
grids.append( grids.append(
[ [
("", 10), ("", 10),
(functools.partial(createbtn, self, countnum, btnplus), 2), (functools.partial(createmanybtn, self, countnum, btnplus), 4),
(functools.partial(createbtnquest, self, btnplus), 2),
] ]
) )
@ -349,7 +423,7 @@ def checkconnected(self):
if not globalconfig["fanyi"][dev]["use"]: if not globalconfig["fanyi"][dev]["use"]:
continue continue
if not os.path.exists("./LunaTranslator/translator/" + dev + ".py"): if not translate_exits(dev):
continue continue
needstart = True needstart = True
break break

View File

@ -41,6 +41,22 @@ def __internal__getlang(k1, k2):
return static_data["language_list_translator_inner"][globalconfig[k2]] return static_data["language_list_translator_inner"][globalconfig[k2]]
def translate_exits(fanyi, which=False):
_fs = [
"./Lunatranslator/translator/{}.py".format(fanyi),
"./userconfig/copyed/{}.py".format(fanyi),
]
if not which:
if all([not os.path.exists(_) for _ in _fs]):
return False
return True
else:
for i, _ in enumerate(_fs):
if os.path.exists(_):
return i
return None
def getlangsrc(): def getlangsrc():
return __internal__getlang("private_srclang", "srclang3") return __internal__getlang("private_srclang", "srclang3")
@ -655,6 +671,7 @@ class LRUCache:
self.put(key) self.put(key)
return _ return _
globalcachedmodule = {} globalcachedmodule = {}

View File

@ -29,7 +29,7 @@ include(generate_product_version)
set(VERSION_MAJOR 5) set(VERSION_MAJOR 5)
set(VERSION_MINOR 15) set(VERSION_MINOR 15)
set(VERSION_PATCH 4) set(VERSION_PATCH 5)
add_library(pch pch.cpp) add_library(pch pch.cpp)
target_precompile_headers(pch PUBLIC pch.h) target_precompile_headers(pch PUBLIC pch.h)