mirror of
https://github.com/283375/arcaea-offline-pyside-ui.git
synced 2025-04-21 18:20:18 +00:00
refactor: DatabaseChecker
This commit is contained in:
parent
0f53cb8d5b
commit
2be39d6bb3
10
index.py
10
index.py
@ -11,7 +11,7 @@ import ui.resources.resources_rc
|
|||||||
from ui.extends.shared.language import changeAppLanguage
|
from ui.extends.shared.language import changeAppLanguage
|
||||||
from ui.extends.shared.settings import Settings
|
from ui.extends.shared.settings import Settings
|
||||||
from ui.implements.mainwindow import MainWindow
|
from ui.implements.mainwindow import MainWindow
|
||||||
from ui.startup.databaseChecker import DatabaseChecker
|
from ui.startup.databaseChecker import DatabaseChecker, DatabaseCheckerResult
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging.INFO,
|
level=logging.INFO,
|
||||||
@ -32,6 +32,10 @@ if __name__ == "__main__":
|
|||||||
changeAppLanguage(locale)
|
changeAppLanguage(locale)
|
||||||
|
|
||||||
databaseChecker = DatabaseChecker()
|
databaseChecker = DatabaseChecker()
|
||||||
|
databaseChecker.setWindowIcon(QIcon(":/images/icon.png"))
|
||||||
|
databaseCheckResult = databaseChecker.confirmDb()
|
||||||
|
|
||||||
|
if not databaseCheckResult & DatabaseCheckerResult.Initted:
|
||||||
result = databaseChecker.exec()
|
result = databaseChecker.exec()
|
||||||
|
|
||||||
if result == QDialog.DialogCode.Accepted:
|
if result == QDialog.DialogCode.Accepted:
|
||||||
@ -42,10 +46,10 @@ if __name__ == "__main__":
|
|||||||
None, "Database Error", "\n".join(traceback.format_exception(e))
|
None, "Database Error", "\n".join(traceback.format_exception(e))
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
else:
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
window = MainWindow()
|
window = MainWindow()
|
||||||
window.setWindowIcon(QIcon(":/images/icon.png"))
|
window.setWindowIcon(QIcon(":/images/icon.png"))
|
||||||
window.show()
|
window.show()
|
||||||
sys.exit(app.exec())
|
sys.exit(app.exec())
|
||||||
else:
|
|
||||||
sys.exit(1)
|
|
||||||
|
@ -89,14 +89,14 @@
|
|||||||
<translation>Continue</translation>
|
<translation>Continue</translation>
|
||||||
</message>
|
</message>
|
||||||
<message>
|
<message>
|
||||||
<location filename="../../startup/databaseChecker.py" line="80"/>
|
<location filename="../../startup/databaseChecker.py" line="117"/>
|
||||||
<source>dialog.confirmNewDatabase</source>
|
<source>dialog.tryInitExistingDatabase</source>
|
||||||
<translation>Database file does not exist. Create now?</translation>
|
<translation>The existing database doesn't seem to be initialized properly, try initialize again?</translation>
|
||||||
</message>
|
</message>
|
||||||
<message>
|
<message>
|
||||||
<location filename="../../startup/databaseChecker.py" line="100"/>
|
<location filename="../../startup/databaseChecker.py" line="133"/>
|
||||||
<source>dialog.tryInit</source>
|
<source>dialog.confirmNewDatabase</source>
|
||||||
<translation>The existing database doesn't seem to be initialized, try initialize it again?</translation>
|
<translation>Database file does not exist. Create now?</translation>
|
||||||
</message>
|
</message>
|
||||||
</context>
|
</context>
|
||||||
<context>
|
<context>
|
||||||
@ -567,6 +567,11 @@ validation</translation>
|
|||||||
<source>About Qt</source>
|
<source>About Qt</source>
|
||||||
<translation>About Qt</translation>
|
<translation>About Qt</translation>
|
||||||
</message>
|
</message>
|
||||||
|
<message>
|
||||||
|
<location filename="../../designer/tabs/tabAbout.ui" line="86"/>
|
||||||
|
<source>Version Info</source>
|
||||||
|
<translation type="unfinished"></translation>
|
||||||
|
</message>
|
||||||
</context>
|
</context>
|
||||||
<context>
|
<context>
|
||||||
<name>TabDbEntry</name>
|
<name>TabDbEntry</name>
|
||||||
|
@ -89,14 +89,14 @@
|
|||||||
<translation>继续</translation>
|
<translation>继续</translation>
|
||||||
</message>
|
</message>
|
||||||
<message>
|
<message>
|
||||||
<location filename="../../startup/databaseChecker.py" line="80"/>
|
<location filename="../../startup/databaseChecker.py" line="117"/>
|
||||||
<source>dialog.confirmNewDatabase</source>
|
<source>dialog.tryInitExistingDatabase</source>
|
||||||
<translation>数据库文件不存在,是否创建?</translation>
|
<translation>现有的数据库似乎没有正确初始化,是否尝试再次初始化?</translation>
|
||||||
</message>
|
</message>
|
||||||
<message>
|
<message>
|
||||||
<location filename="../../startup/databaseChecker.py" line="100"/>
|
<location filename="../../startup/databaseChecker.py" line="133"/>
|
||||||
<source>dialog.tryInit</source>
|
<source>dialog.confirmNewDatabase</source>
|
||||||
<translation>现有的数据库似乎没有正确初始化,是否尝试再次初始化?</translation>
|
<translation>数据库文件不存在,是否创建?</translation>
|
||||||
</message>
|
</message>
|
||||||
</context>
|
</context>
|
||||||
<context>
|
<context>
|
||||||
@ -566,6 +566,11 @@
|
|||||||
<source>About Qt</source>
|
<source>About Qt</source>
|
||||||
<translation>关于 Qt</translation>
|
<translation>关于 Qt</translation>
|
||||||
</message>
|
</message>
|
||||||
|
<message>
|
||||||
|
<location filename="../../designer/tabs/tabAbout.ui" line="86"/>
|
||||||
|
<source>Version Info</source>
|
||||||
|
<translation type="unfinished"></translation>
|
||||||
|
</message>
|
||||||
</context>
|
</context>
|
||||||
<context>
|
<context>
|
||||||
<name>TabDbEntry</name>
|
<name>TabDbEntry</name>
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
from typing import Literal, Optional, Union
|
from enum import IntEnum
|
||||||
|
|
||||||
from arcaea_offline.database import Database
|
from arcaea_offline.database import Database
|
||||||
from PySide6.QtCore import QCoreApplication, QDir, QFileInfo, Qt, QTimer, QUrl, Slot
|
from PySide6.QtCore import QCoreApplication, QDir, QFileInfo, Qt, QUrl, Slot
|
||||||
from PySide6.QtWidgets import QDialog, QMessageBox
|
from PySide6.QtWidgets import QDialog, QMessageBox
|
||||||
|
|
||||||
from ui.extends.shared.database import create_engine
|
from ui.extends.shared.database import create_engine
|
||||||
@ -14,6 +14,11 @@ from .databaseChecker_ui import Ui_DatabaseChecker
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseCheckerResult(IntEnum):
|
||||||
|
FileExist = 0x001
|
||||||
|
Initted = 0x002
|
||||||
|
|
||||||
|
|
||||||
class DatabaseChecker(Ui_DatabaseChecker, QDialog):
|
class DatabaseChecker(Ui_DatabaseChecker, QDialog):
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
@ -25,9 +30,7 @@ class DatabaseChecker(Ui_DatabaseChecker, QDialog):
|
|||||||
|
|
||||||
self.confirmDbByExistingSettings = False
|
self.confirmDbByExistingSettings = False
|
||||||
self.settings = Settings(self)
|
self.settings = Settings(self)
|
||||||
dbUrlString = self.settings.databaseUrl()
|
if dbUrlString := self.settings.databaseUrl():
|
||||||
|
|
||||||
if dbUrlString:
|
|
||||||
dbFileUrl = QUrl(dbUrlString.replace("sqlite://", "file://"))
|
dbFileUrl = QUrl(dbUrlString.replace("sqlite://", "file://"))
|
||||||
dbFileInfo = QFileInfo(dbFileUrl.toLocalFile())
|
dbFileInfo = QFileInfo(dbFileUrl.toLocalFile())
|
||||||
if dbFileInfo.exists():
|
if dbFileInfo.exists():
|
||||||
@ -42,37 +45,85 @@ class DatabaseChecker(Ui_DatabaseChecker, QDialog):
|
|||||||
self.dbDirSelector.selectFile(QDir.currentPath())
|
self.dbDirSelector.selectFile(QDir.currentPath())
|
||||||
self.dbFilenameLineEdit.setText("arcaea_offline.db")
|
self.dbFilenameLineEdit.setText("arcaea_offline.db")
|
||||||
|
|
||||||
def updateLabels(
|
def dbPath(self):
|
||||||
self,
|
return QDir(self.dbDirSelector.selectedFiles()[0])
|
||||||
version: Union[Optional[int], Literal["reset"]],
|
|
||||||
init_status: Union[Optional[bool], Literal["reset"]],
|
|
||||||
):
|
|
||||||
if version is not None:
|
|
||||||
self.dbVersionLabel.setText(str(version))
|
|
||||||
elif version == "reset":
|
|
||||||
self.dbVersionLabel.setText("-")
|
|
||||||
|
|
||||||
if init_status is not None:
|
def dbFileInfo(self):
|
||||||
if init_status:
|
return QFileInfo(
|
||||||
self.dbCheckConnLabel.setText('<font color="green">OK</font>')
|
QDir.cleanPath(
|
||||||
self.continueButton.setEnabled(True)
|
self.dbPath().absoluteFilePath(self.dbFilenameLineEdit.text())
|
||||||
else:
|
)
|
||||||
self.dbCheckConnLabel.setText('<font color="red">Error</font>')
|
)
|
||||||
self.continueButton.setEnabled(False)
|
|
||||||
elif init_status == "reset":
|
def dbFileUrl(self):
|
||||||
self.dbCheckConnLabel.setText("-")
|
return QUrl.fromLocalFile(self.dbFileInfo().filePath())
|
||||||
|
|
||||||
|
def dbSqliteUrl(self):
|
||||||
|
# dbSqliteUrl.setScheme("sqlite")
|
||||||
|
return QUrl(self.dbFileUrl().toString().replace("file://", "sqlite://"))
|
||||||
|
|
||||||
|
def confirmDb(self) -> DatabaseCheckerResult:
|
||||||
|
flags = 0x000
|
||||||
|
|
||||||
|
dbFileInfo = self.dbFileInfo()
|
||||||
|
dbSqliteUrl = self.dbSqliteUrl()
|
||||||
|
if not dbFileInfo.exists():
|
||||||
|
return flags
|
||||||
|
|
||||||
|
flags |= DatabaseCheckerResult.FileExist
|
||||||
|
db = Database(create_engine(dbSqliteUrl))
|
||||||
|
if db.check_init():
|
||||||
|
flags |= DatabaseCheckerResult.Initted
|
||||||
|
|
||||||
|
return flags
|
||||||
|
|
||||||
|
def updateLabels(self):
|
||||||
|
result = self.confirmDb()
|
||||||
|
try:
|
||||||
|
db = Database()
|
||||||
|
version = db.version()
|
||||||
|
initted = db.check_init()
|
||||||
|
self.dbVersionLabel.setText(str(version))
|
||||||
|
self.dbCheckConnLabel.setText(
|
||||||
|
'<font color="green">OK</font>'
|
||||||
|
if initted
|
||||||
|
else '<font color="red">Not initted</font>'
|
||||||
|
)
|
||||||
|
self.continueButton.setEnabled(initted)
|
||||||
|
except Exception as e:
|
||||||
|
self.dbVersionLabel.setText("-")
|
||||||
|
self.dbCheckConnLabel.setText(
|
||||||
|
f'<font color="red">Error: {e}</font>'
|
||||||
|
if result & DatabaseCheckerResult.FileExist
|
||||||
|
else "-"
|
||||||
|
)
|
||||||
self.continueButton.setEnabled(False)
|
self.continueButton.setEnabled(False)
|
||||||
|
|
||||||
@Slot()
|
@Slot()
|
||||||
def on_confirmDbPathButton_clicked(self):
|
def on_confirmDbPathButton_clicked(self):
|
||||||
dbPath = QDir(self.dbDirSelector.selectedFiles()[0])
|
dbSqliteUrl = self.dbSqliteUrl()
|
||||||
dbFileInfo = QFileInfo(
|
|
||||||
QDir.cleanPath(dbPath.absoluteFilePath(self.dbFilenameLineEdit.text()))
|
result = self.confirmDb()
|
||||||
|
if result & DatabaseCheckerResult.Initted:
|
||||||
|
if not self.confirmDbByExistingSettings:
|
||||||
|
self.settings.setDatabaseUrl(dbSqliteUrl.toString())
|
||||||
|
elif result & DatabaseCheckerResult.FileExist:
|
||||||
|
confirm_try_init = QMessageBox.question(
|
||||||
|
self,
|
||||||
|
None,
|
||||||
|
# fmt: off
|
||||||
|
QCoreApplication.translate("DatabaseChecker", "dialog.tryInitExistingDatabase"),
|
||||||
|
# fmt: on
|
||||||
)
|
)
|
||||||
dbFileUrl = QUrl.fromLocalFile(dbFileInfo.filePath())
|
if confirm_try_init == QMessageBox.StandardButton.Yes:
|
||||||
# dbSqliteUrl.setScheme("sqlite")
|
try:
|
||||||
dbSqliteUrl = QUrl(dbFileUrl.toString().replace("file://", "sqlite://"))
|
Database().init(checkfirst=True)
|
||||||
if not dbFileInfo.exists():
|
except Exception as e:
|
||||||
|
logger.exception("Error while initializing an existing database")
|
||||||
|
QMessageBox.critical(
|
||||||
|
self, None, "\n".join(traceback.format_exception(e))
|
||||||
|
)
|
||||||
|
else:
|
||||||
confirm_new_database = QMessageBox.question(
|
confirm_new_database = QMessageBox.question(
|
||||||
self,
|
self,
|
||||||
None,
|
None,
|
||||||
@ -83,36 +134,7 @@ class DatabaseChecker(Ui_DatabaseChecker, QDialog):
|
|||||||
if confirm_new_database == QMessageBox.StandardButton.Yes:
|
if confirm_new_database == QMessageBox.StandardButton.Yes:
|
||||||
db = Database(create_engine(dbSqliteUrl))
|
db = Database(create_engine(dbSqliteUrl))
|
||||||
db.init()
|
db.init()
|
||||||
self.on_confirmDbPathButton_clicked()
|
self.updateLabels()
|
||||||
else:
|
|
||||||
db = Database(create_engine(dbSqliteUrl))
|
|
||||||
if db.check_init():
|
|
||||||
self.updateLabels(db.version(), True)
|
|
||||||
if self.confirmDbByExistingSettings:
|
|
||||||
QTimer.singleShot(25, self.accept)
|
|
||||||
else:
|
|
||||||
self.settings.setDatabaseUrl(dbSqliteUrl.toString())
|
|
||||||
else:
|
|
||||||
confirm_try_init = QMessageBox.question(
|
|
||||||
self,
|
|
||||||
None,
|
|
||||||
# fmt: off
|
|
||||||
QCoreApplication.translate("DatabaseChecker", "dialog.tryInit"),
|
|
||||||
# fmt: on
|
|
||||||
)
|
|
||||||
if confirm_try_init == QMessageBox.StandardButton.Yes:
|
|
||||||
try:
|
|
||||||
db.init(checkfirst=True)
|
|
||||||
except Exception as e:
|
|
||||||
logger.exception(
|
|
||||||
"Error while initializing an existing database"
|
|
||||||
)
|
|
||||||
QMessageBox.critical(
|
|
||||||
self, None, "\n".join(traceback.format_exception(e))
|
|
||||||
)
|
|
||||||
self.updateLabels("reset", False)
|
|
||||||
finally:
|
|
||||||
self.on_confirmDbPathButton_clicked()
|
|
||||||
|
|
||||||
@Slot()
|
@Slot()
|
||||||
def on_continueButton_clicked(self):
|
def on_continueButton_clicked(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user