From b549645a50fc316e8d46aa3731ffa203ba47839a Mon Sep 17 00:00:00 2001 From: Dpeta Date: Thu, 1 Sep 2022 06:55:08 +0200 Subject: [PATCH] replaced sys.modules checks with try: PyQt6, except: PyQt5 --- console.py | 10 ++++++---- convo.py | 12 ++++++++---- logviewer.py | 12 ++++++++---- menus.py | 29 +++++++++++++++++++++++++---- pesterchum.py | 39 +++++++++++++++++++++++---------------- 5 files changed, 70 insertions(+), 32 deletions(-) diff --git a/console.py b/console.py index 18f23be..db4003b 100644 --- a/console.py +++ b/console.py @@ -454,10 +454,10 @@ class ConsoleText(QtWidgets.QTextEdit): def mousePressEvent(self, event): if event.button() == QtCore.Qt.MouseButton.LeftButton: - if 'PyQt6' in sys.modules: + try: # PyQt6 url = self.anchorAt(event.position().toPoint()) - elif 'PyQt5' in sys.modules: + except AttributeError: # PyQt5 url = self.anchorAt(event.pos()) if url != "": @@ -474,9 +474,11 @@ class ConsoleText(QtWidgets.QTextEdit): def mouseMoveEvent(self, event): # Change our cursor when we roll over links (anchors). super(ConsoleText, self).mouseMoveEvent(event) - if 'PyQt6' in sys.modules: + try: + # PyQt6 pos = event.position().toPoint() - elif 'PyQt5' in sys.modules: + except AttributeError: + # PyQt5 pos = event.pos() if self.anchorAt(pos): if self.viewport().cursor().shape != QtCore.Qt.CursorShape.PointingHandCursor: diff --git a/convo.py b/convo.py index f69bd75..72ad16f 100644 --- a/convo.py +++ b/convo.py @@ -530,9 +530,11 @@ class PesterText(QtWidgets.QTextEdit): def mousePressEvent(self, event): if event.button() == QtCore.Qt.MouseButton.LeftButton: - if 'PyQt6' in sys.modules: + try: + # PyQt6 url = self.anchorAt(event.position().toPoint()) - if 'PyQt5' in sys.modules: + except AttributeError: + # PyQt5 url = self.anchorAt(event.pos()) if url != "": if url[0] == "#" and url != "#pesterchum": @@ -549,9 +551,11 @@ class PesterText(QtWidgets.QTextEdit): QtWidgets.QTextEdit.mousePressEvent(self, event) def mouseMoveEvent(self, event): QtWidgets.QTextEdit.mouseMoveEvent(self, event) - if 'PyQt6' in sys.modules: + try: + # PyQt6 pos = event.position().toPoint() - if 'PyQt5' in sys.modules: + except AttributeError: + # PyQt5 pos = event.pos() if self.anchorAt(pos): if self.viewport().cursor().shape != QtCore.Qt.CursorShape.PointingHandCursor: diff --git a/logviewer.py b/logviewer.py index 155588b..e26c586 100644 --- a/logviewer.py +++ b/logviewer.py @@ -277,9 +277,11 @@ class PesterLogText(PesterText): def focusInEvent(self, event): QtWidgets.QTextEdit.focusInEvent(self, event) def mousePressEvent(self, event): - if 'PyQt6' in sys.modules: + try: + # PyQt6 url = self.anchorAt(event.position().toPoint()) - if 'PyQt5' in sys.modules: + except AttributeError: + # PyQt5 url = self.anchorAt(event.pos()) if url != "": if url[0] == "#" and url != "#pesterchum": @@ -292,9 +294,11 @@ class PesterLogText(PesterText): QtWidgets.QTextEdit.mousePressEvent(self, event) def mouseMoveEvent(self, event): QtWidgets.QTextEdit.mouseMoveEvent(self, event) - if 'PyQt6' in sys.modules: + try: + # PyQt6 pos = event.position().toPoint() - if 'PyQt5' in sys.modules: + except AttributeError: + # PyQt5 pos = event.pos() if self.anchorAt(pos): if self.viewport().cursor().shape != QtCore.Qt.CursorShape.PointingHandCursor: diff --git a/menus.py b/menus.py index 1b1702f..acfadf2 100644 --- a/menus.py +++ b/menus.py @@ -1,3 +1,4 @@ +import sys import re from os import remove @@ -723,20 +724,40 @@ class PesterChooseQuirks(QtWidgets.QDialog): elif vdict["type"] == "replace": vdict["from"] = str(page.itemAt(1).layout().itemAt(1).widget().text()) vdict["to"] = str(page.itemAt(2).layout().itemAt(1).widget().text()) - vdict["checkstate"] = str(page.itemAt(3).layout().itemAt(0).widget().checkState().value) + try: + # PyQt6 + vdict["checkstate"] = str(page.itemAt(3).layout().itemAt(0).widget().checkState().value) + except AttributeError: + # PyQt5 + vdict["checkstate"] = str(page.itemAt(3).layout().itemAt(0).widget().checkState()) elif vdict["type"] == "regexp": vdict["from"] = str(page.itemAt(2).layout().itemAt(1).layout().itemAt(1).widget().text()) vdict["to"] = str(page.itemAt(2).layout().itemAt(2).layout().itemAt(1).widget().text()) - vdict["checkstate"] = str(page.itemAt(2).layout().itemAt(3).layout().itemAt(0).widget().checkState().value) + try: + # PyQt6 + vdict["checkstate"] = str(page.itemAt(2).layout().itemAt(3).layout().itemAt(0).widget().checkState().value) + except AttributeError: + # PyQt5 + vdict["checkstate"] = str(page.itemAt(2).layout().itemAt(3).layout().itemAt(0).widget().checkState()) elif vdict["type"] == "random": vdict["from"] = str(self.quirkadd.regexp.text()) - vdict["checkstate"] = str(page.itemAt(2).layout().itemAt(2).layout().itemAt(0).widget().checkState().value) + try: + # PyQt6 + vdict["checkstate"] = str(page.itemAt(2).layout().itemAt(2).layout().itemAt(0).widget().checkState().value) + except AttributeError: + # PyQt5 + vdict["checkstate"] = str(page.itemAt(2).layout().itemAt(2).layout().itemAt(0).widget().checkState()) randomlist = [str(self.quirkadd.replacelist.item(i).text()) for i in range(0,self.quirkadd.replacelist.count())] vdict["randomlist"] = randomlist elif vdict["type"] == "spelling": vdict["percentage"] = self.quirkadd.slider.value() - vdict["checkstate"] = str(page.itemAt(3).layout().itemAt(0).widget().checkState().value) + try: + # PyQt6 + vdict["checkstate"] = str(page.itemAt(3).layout().itemAt(0).widget().checkState().value) + except AttributeError: + # PyQt5 + vdict["checkstate"] = str(page.itemAt(3).layout().itemAt(0).widget().checkState()) if vdict["type"] in ("regexp", "random"): try: re.compile(vdict["from"]) diff --git a/pesterchum.py b/pesterchum.py index f9f0148..f1538b3 100644 --- a/pesterchum.py +++ b/pesterchum.py @@ -515,9 +515,11 @@ class chumArea(RightClickTree): if thisitem.rfind(" (") != -1: thisitem = thisitem[0:thisitem.rfind(" (")] if thisitem == "Chums" or thisitem in self.groups: - if 'PyQt6' in sys.modules: + try: + # PyQt6 droppos = self.itemAt(event.position().toPoint()) - elif 'PyQt5' in sys.modules: + except AttributeError: + # PyQt5 droppos = self.itemAt(event.pos()) if not droppos: return droppos = str(droppos.text(0)) @@ -525,9 +527,11 @@ class chumArea(RightClickTree): droppos = droppos[0:droppos.rfind(" ")] if droppos == "Chums" or droppos in self.groups: saveOpen = event.source().currentItem().isExpanded() - if 'PyQt6' in sys.modules: + try: + # PyQt6 saveDrop = self.itemAt(event.position().toPoint()) - if 'PyQt5' in sys.modules: + except AttributeError: + # PyQt5 saveDrop = self.itemAt(event.pos()) saveItem = self.takeTopLevelItem(self.indexOfTopLevelItem(event.source().currentItem())) self.insertTopLevelItems(self.indexOfTopLevelItem(saveDrop)+1, [saveItem]) @@ -543,9 +547,11 @@ class chumArea(RightClickTree): self.mainwindow.config.saveGroups(gTemp) # Drop item is a chum else: - if 'PyQt6' in sys.modules: + try: + # PyQt6 eventpos = event.position().toPoint() - if 'PyQt5' in sys.modules: + except AttributeError: + # PyQt5 eventpos = event.pos() item = self.itemAt(eventpos) if item: @@ -2099,16 +2105,17 @@ class PesterWindow(MovingWindow): def _setup_sounds(self, soundclass=None): """Set up the event sounds for later use.""" # Set up the sounds we're using. - if 'pygame' in sys.modules: - # Pygame is imported (Linux) + try: + # Pygame soundclass = pygame.mixer.Sound - elif 'PyQt6.QtMultimedia' in sys.modules: - # QtMultimedia is imported (Windows, MacOS) - soundclass = QtMultimedia.QSoundEffect - else: - # death - soundclass = NoneSound - PchumLog.warning("No sound module loaded?") + except: + try: + # QtMultimedia + soundclass = QtMultimedia.QSoundEffect + except: + # death + soundclass = NoneSound + PchumLog.warning("No sound module loaded?") self.sound_type = soundclass @@ -3792,7 +3799,7 @@ class MainProgram(QtCore.QObject): # we could set the frequency higher but i love how cheesy it sounds try: pygame.mixer.init() - except pygame.error as err: + except (pygame.error, Exception) as err: print("Warning: No sound! (pygame error: %s)" % err) self.widget = PesterWindow(options, parent=self, app=self.app)