From c3c571e73153574effcd0d014ae151a833974e61 Mon Sep 17 00:00:00 2001 From: RedDeadDepresso <94017243+RedDeadDepresso@users.noreply.github.com> Date: Wed, 25 Sep 2024 19:23:01 +0100 Subject: [PATCH] refactor(script): use qfluentwidgets dialog for password --- app/components/password_dialog.py | 54 +++++++++++++++++++++++++++++++ util/file_manager.py | 39 ++++++++++++---------- 2 files changed, 76 insertions(+), 17 deletions(-) create mode 100644 app/components/password_dialog.py diff --git a/app/components/password_dialog.py b/app/components/password_dialog.py new file mode 100644 index 0000000..e40df0c --- /dev/null +++ b/app/components/password_dialog.py @@ -0,0 +1,54 @@ +import sys +from PySide6.QtCore import Signal, Qt +from PySide6.QtWidgets import QApplication, QHBoxLayout +from qfluentwidgets import Dialog, FluentTitleBar, LineEdit, setTheme, Theme + + +app = QApplication(sys.argv) + + +class PasswordDialog(Dialog): + yesSignal = Signal() + cancelSignal = Signal() + + def __init__(self, title: str, content: str, parent=None): + super().__init__(title, content, parent) + self.passwordLineEdit = LineEdit(self) + self.password = '' + self.yesButtonPressed = False + self.__initLayout() + self.__connectSignalToSlot() + + def __initLayout(self): + passwordLayout = QHBoxLayout() + passwordLayout.setContentsMargins(12, 12, 12, 12) + passwordLayout.addWidget(self.passwordLineEdit, 1, Qt.AlignTop) + self.vBoxLayout.insertLayout(2, passwordLayout, 1) + self.setTitleBar(FluentTitleBar(self)) + + def __connectSignalToSlot(self): + self.passwordLineEdit.textChanged.connect(self.onTextChanged) + self.yesSignal.connect(self.onYesSignal) + + def onTextChanged(self, text: str): + self.password = text + + def onYesSignal(self): + self.yesButtonPressed = True + + def getPassword(self) -> str: + if self.yesButtonPressed: + print(self.password) + return self.password + return '' + +def password_dialog(title: str, content: str) -> str: + setTheme(Theme.AUTO) + dialog = PasswordDialog(title, content) + dialog.exec() + return dialog.getPassword() + + +if __name__ == "__main__": + password = password_dialog('test', 'test') + print(password) \ No newline at end of file diff --git a/util/file_manager.py b/util/file_manager.py index 73143dd..a52b272 100644 --- a/util/file_manager.py +++ b/util/file_manager.py @@ -1,7 +1,6 @@ import shutil import datetime import patoolib -import customtkinter import subprocess import time from pathlib import Path @@ -140,30 +139,36 @@ class FileManager: raise Exception() def extract_archive(self, archive_path: Union[Path, str]): + from app.components.password_dialog import password_dialog """Extract the archive.""" archive_path = Path(archive_path) archive_name = archive_path.name logger.info("ARCHIVE", f"Extracting {archive_name}") - extract_path = archive_path.with_stem(f"{archive_path.stem}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')}") + extract_path = archive_path.with_name(f"{archive_path.stem}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')}") try: patoolib.extract_archive(str(archive_path), outdir=str(extract_path)) return extract_path - - except patoolib.util.PatoolError as e: - text = f"There is an error with the archive {archive_name} but it is impossible to detect the cause. Maybe it requires a password?" - while self.config.install_chara["Password"] == "Request Password": + except: + + text = f"There is an error with the archive {archive_name}, but it is impossible to detect the cause. Maybe it requires a password?" + while True: try: - dialog = customtkinter.CTkInputDialog(text=text, title="Enter Password") - password = dialog.get_input() - - if password: - patoolib.extract_archive(str(archive_path), outdir=str(extract_path), password=password) - return extract_path - else: + password = password_dialog('Enter Password', text) + + if not password: break - except: - text = f"Wrong password or {archive_name} is corrupted. Please enter password again or click Cancel" - - logger.skipped("ARCHIVE", archive_name) + + patoolib.extract_archive(str(archive_path), outdir=str(extract_path), password=password) + return extract_path + + except patoolib.util.PatoolError as e: + text = f"Wrong password or {archive_name} is corrupted. Please enter password again or click Cancel." + print(f"Error: {str(e)}") + + except Exception as e: + print(f"An unexpected error occurred: {str(e)}") + break + + logger.skipped("ARCHIVE", archive_name)