From 206ff99de9888302d9e6f5fcc8f4a62b8e94a9e2 Mon Sep 17 00:00:00 2001 From: Landon Epps Date: Sat, 12 Aug 2023 23:55:47 -0400 Subject: [PATCH] Use aqt for PyQt5 backwards compatiblity (#293) (#297) * When possible use aqt instead of directly using PyQt. * This will make morphman compatible with anki-qt5 and anki-qt6. Motivation: #291 #286 Co-authored-by: rameauv --- .github/workflows/build.yml | 3 ++- .pylintrc | 2 +- __init__.py | 1 - morph/UI/morphemizerComboBox.py | 4 +--- morph/customTableWidget.py | 3 +-- morph/manager.py | 4 +--- morph/preferencesDialog.py | 4 +--- morph/readability.py | 22 +++++++++++++++------- test/fake_aqt.py | 1 + test/test_MorphemizerComboBox.py | 2 +- 10 files changed, 24 insertions(+), 22 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9115dceb..c54b95dd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -33,7 +33,7 @@ jobs: fi source pyenv/bin/activate fi - python -m pip install aqt==${{ matrix.anki }} anki==${{ matrix.anki }} PyQt6-WebEngine pylint + python -m pip install aqt[qt5]==${{ matrix.anki }} aqt[qt6]==${{ matrix.anki }} anki==${{ matrix.anki }} PyQt6-WebEngine pylint - name: Lint shell: bash run: | @@ -55,3 +55,4 @@ jobs: export QT_QPA_PLATFORM=minimal export PYTHONPATH=./ python test.py + diff --git a/.pylintrc b/.pylintrc index 5772b3ed..e5bb0511 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,6 +1,6 @@ [MASTER] ignore=deps -extension-pkg-whitelist=PyQt6 +extension-pkg-whitelist=PyQt5,PyQt6 [MESSAGES CONTROL] disable=C,R, diff --git a/__init__.py b/__init__.py index a793c806..b15da051 100644 --- a/__init__.py +++ b/__init__.py @@ -1,4 +1,3 @@ -from PyQt6.QtWidgets import * from aqt.reviewer import Reviewer from aqt.utils import tooltip from aqt import gui_hooks diff --git a/morph/UI/morphemizerComboBox.py b/morph/UI/morphemizerComboBox.py index 6a1117cf..aa7b0f48 100644 --- a/morph/UI/morphemizerComboBox.py +++ b/morph/UI/morphemizerComboBox.py @@ -1,6 +1,4 @@ - -from PyQt6.QtWidgets import QComboBox - +from aqt.qt import QComboBox class MorphemizerComboBox(QComboBox): diff --git a/morph/customTableWidget.py b/morph/customTableWidget.py index 3db35afd..9ee59a30 100644 --- a/morph/customTableWidget.py +++ b/morph/customTableWidget.py @@ -1,5 +1,4 @@ -from PyQt6.QtWidgets import QApplication, QTableWidget -from PyQt6.QtGui import QKeySequence +from aqt.qt import QApplication, QTableWidget, QKeySequence class CustomTableWidget(QTableWidget): diff --git a/morph/manager.py b/morph/manager.py index f96a94fa..aa029bd2 100644 --- a/morph/manager.py +++ b/morph/manager.py @@ -1,9 +1,7 @@ # -*- coding: utf-8 -*- import os -from PyQt6.QtCore import * -from PyQt6.QtGui import * -from PyQt6.QtWidgets import * +from aqt.qt import * from anki.utils import is_mac from .UI import MorphemizerComboBox diff --git a/morph/preferencesDialog.py b/morph/preferencesDialog.py index 394a007f..202541fe 100644 --- a/morph/preferencesDialog.py +++ b/morph/preferencesDialog.py @@ -1,6 +1,4 @@ -from PyQt6.QtCore import * -from PyQt6.QtWidgets import * -from PyQt6.QtGui import * +from aqt.qt import * from anki.lang import _ from aqt.utils import tooltip diff --git a/morph/readability.py b/morph/readability.py index e293f6c7..df799e87 100644 --- a/morph/readability.py +++ b/morph/readability.py @@ -17,14 +17,18 @@ from contextlib import redirect_stdout, redirect_stderr from functools import partial -from PyQt6.QtCore import * -from PyQt6.QtGui import * -from PyQt6.QtWidgets import * +from aqt.qt import * +isSupportingWebSockets = False try: - from PyQt6 import QtWebSockets, QtNetwork + from PyQt6 import QtWebSockets, QtNetwork + isSupportingWebSockets = True except: - pass + try: + from PyQt5 import QtWebSockets, QtNetwork + isSupportingWebSockets = True + except: + pass from .morphemes import Morpheme, MorphDb, getMorphemes, altIncludesMorpheme from .morphemizer import getAllMorphemizers @@ -361,7 +365,11 @@ def __init__(self, parent=None): self.ui.migakuDictionaryTagsCheckBox.setChecked(False) self.ui.migakuDictionaryTagsCheckBox.setEnabled(False) - self.ui.webServiceCheckBox.setChecked(cfg('Option_EnableWebService')) + if isSupportingWebSockets: + self.ui.webServiceCheckBox.setChecked(cfg('Option_EnableWebService')) + else: + self.ui.webServiceCheckBox.setChecked(False) + self.ui.webServiceCheckBox.setEnabled(False) self.ui.buttonBox.accepted.connect(self.onAccept) self.ui.buttonBox.rejected.connect(self.onReject) @@ -451,7 +459,7 @@ def __init__(self, parent=None): self.server = None self.clients = set() - if cfg('Option_EnableWebService'): + if cfg('Option_EnableWebService') and isSupportingWebSockets: self.server = QtWebSockets.QWebSocketServer('MorphMan Service', QtWebSockets.QWebSocketServer.SslMode.NonSecureMode) if self.server.listen(QtNetwork.QHostAddress.SpecialAddress.LocalHost, 9779): self.write('Web Service Created: '+self.server.serverName()+' : '+self.server.serverAddress().toString()+':'+str(self.server.serverPort()) + '\n') diff --git a/test/fake_aqt.py b/test/fake_aqt.py index c5ba09a0..520842c8 100644 --- a/test/fake_aqt.py +++ b/test/fake_aqt.py @@ -1,4 +1,5 @@ from unittest.mock import MagicMock +from aqt import * class FakeCollection: def __init__(self, config): diff --git a/test/test_MorphemizerComboBox.py b/test/test_MorphemizerComboBox.py index 190daa50..0a770e41 100644 --- a/test/test_MorphemizerComboBox.py +++ b/test/test_MorphemizerComboBox.py @@ -1,6 +1,6 @@ import unittest -from PyQt6.QtWidgets import QApplication +from aqt.qt import QApplication from morph.UI import MorphemizerComboBox from morph.morphemizer import getAllMorphemizers