try to improve readability

This commit is contained in:
dries.k
2023-05-13 19:03:26 +02:00
parent fdacfa23c2
commit 8ea89b1df9
6 changed files with 44 additions and 136 deletions

View File

@@ -49,9 +49,7 @@ def init_logger(logger: logging.Logger):
else: else:
logging.disable() logging.disable()
logdir = QtCore.QStandardPaths.standardLocations( logdir = QtCore.QStandardPaths.standardLocations(QtCore.QStandardPaths.StandardLocation.AppDataLocation)[0]
QtCore.QStandardPaths.StandardLocation.AppDataLocation
)[0]
if not os.path.exists(logdir): if not os.path.exists(logdir):
os.mkdir(logdir) os.mkdir(logdir)
logging.basicConfig( logging.basicConfig(
@@ -106,29 +104,17 @@ class MainApplication(QtWidgets.QApplication):
self.application_model.setItem(0, 0, ApplicationAllMessagesItem()) self.application_model.setItem(0, 0, ApplicationAllMessagesItem())
self.get_applications_task = GetApplicationsTask(self.gotify_client) self.get_applications_task = GetApplicationsTask(self.gotify_client)
self.get_applications_task.success.connect( self.get_applications_task.success.connect(self.get_applications_success_callback)
self.get_applications_success_callback self.get_applications_task.started.connect(self.main_window.disable_applications)
) self.get_applications_task.finished.connect(self.main_window.enable_applications)
self.get_applications_task.started.connect(
self.main_window.disable_applications
)
self.get_applications_task.finished.connect(
self.main_window.enable_applications
)
self.get_applications_task.start() self.get_applications_task.start()
def get_applications_success_callback( def get_applications_success_callback(
self, applications: list[gotify.GotifyApplicationModel], self, applications: list[gotify.GotifyApplicationModel],
): ):
for i, application in enumerate(applications): for i, application in enumerate(applications):
icon = QtGui.QIcon( icon = QtGui.QIcon(self.downloader.get_filename(f"{self.gotify_client.url}/{application.image}"))
self.downloader.get_filename( self.application_model.setItem(i + 1, 0, ApplicationModelItem(application, icon))
f"{self.gotify_client.url}/{application.image}"
)
)
self.application_model.setItem(
i + 1, 0, ApplicationModelItem(application, icon),
)
def update_last_id(self, i: int): def update_last_id(self, i: int):
if i > settings.value("message/last", type=int): if i > settings.value("message/last", type=int):
@@ -167,9 +153,7 @@ class MainApplication(QtWidgets.QApplication):
self.main_window.set_connecting() self.main_window.set_connecting()
self.tray.set_icon_error() self.tray.set_icon_error()
self.gotify_client.increase_wait_time() self.gotify_client.increase_wait_time()
QtCore.QTimer.singleShot( QtCore.QTimer.singleShot(self.gotify_client.get_wait_time() * 1000, self.gotify_client.reconnect)
self.gotify_client.get_wait_time() * 1000, self.gotify_client.reconnect
)
def listener_error_callback(self, exception: Exception): def listener_error_callback(self, exception: Exception):
self.main_window.set_connecting() self.main_window.set_connecting()
@@ -308,17 +292,13 @@ class MainApplication(QtWidgets.QApplication):
# Update the message widget icons # Update the message widget icons
for r in range(self.messages_model.rowCount()): for r in range(self.messages_model.rowCount()):
message_widget: MessageWidget = self.main_window.listView_messages.indexWidget( message_widget: MessageWidget = self.main_window.listView_messages.indexWidget(self.messages_model.index(r, 0))
self.messages_model.index(r, 0)
)
message_widget.set_icons() message_widget.set_icons()
def settings_callback(self): def settings_callback(self):
settings_dialog = SettingsDialog(self) settings_dialog = SettingsDialog(self)
settings_dialog.quit_requested.connect(self.quit) settings_dialog.quit_requested.connect(self.quit)
settings_dialog.theme_change_requested.connect( settings_dialog.theme_change_requested.connect(self.theme_change_requested_callback)
self.theme_change_requested_callback
)
accepted = settings_dialog.exec() accepted = settings_dialog.exec()
if accepted and settings_dialog.settings_changed: if accepted and settings_dialog.settings_changed:
@@ -361,9 +341,7 @@ class MainApplication(QtWidgets.QApplication):
self.main_window.refresh.connect(self.refresh_applications) self.main_window.refresh.connect(self.refresh_applications)
self.main_window.delete_all.connect(self.delete_all_messages_callback) self.main_window.delete_all.connect(self.delete_all_messages_callback)
self.main_window.application_selection_changed.connect( self.main_window.application_selection_changed.connect(self.application_selection_changed_callback)
self.application_selection_changed_callback
)
self.main_window.delete_message.connect(self.delete_message_callback) self.main_window.delete_message.connect(self.delete_message_callback)
self.main_window.image_popup.connect(self.image_popup_callback) self.main_window.image_popup.connect(self.image_popup_callback)
self.main_window.hidden.connect(self.main_window_hidden_callback) self.main_window.hidden.connect(self.main_window_hidden_callback)
@@ -384,9 +362,7 @@ class MainApplication(QtWidgets.QApplication):
def acquire_lock(self) -> bool: def acquire_lock(self) -> bool:
temp_dir = tempfile.gettempdir() temp_dir = tempfile.gettempdir()
lock_filename = os.path.join( lock_filename = os.path.join(temp_dir, __title__ + "-" + getpass.getuser() + ".lock")
temp_dir, __title__ + "-" + getpass.getuser() + ".lock"
)
self.lock_file = QtCore.QLockFile(lock_filename) self.lock_file = QtCore.QLockFile(lock_filename)
self.lock_file.setStaleLockTime(0) self.lock_file.setStaleLockTime(0)
return self.lock_file.tryLock() return self.lock_file.tryLock()

View File

@@ -54,9 +54,7 @@ class ApplicationAllMessagesItem(QtGui.QStandardItem):
class ApplicationModel(QtGui.QStandardItemModel): class ApplicationModel(QtGui.QStandardItemModel):
def __init__(self): def __init__(self):
super(ApplicationModel, self).__init__() super(ApplicationModel, self).__init__()
self.setItemPrototype( self.setItemPrototype(ApplicationModelItem(gotify.GotifyApplicationModel({"name": ""}), None))
ApplicationModelItem(gotify.GotifyApplicationModel({"name": ""}), None)
)
def setItem( def setItem(
self, self,

View File

@@ -43,10 +43,7 @@ class MessageWidget(QtWidgets.QWidget, Ui_Form):
self.label_title.setText(message.title) self.label_title.setText(message.title)
self.label_date.setText(message.date.strftime("%Y-%m-%d, %H:%M")) self.label_date.setText(message.date.strftime("%Y-%m-%d, %H:%M"))
if markdown := ( if message.get("extras", {}).get("client::display", {}).get("contentType") == "text/markdown":
message.get("extras", {}).get("client::display", {}).get("contentType")
== "text/markdown"
):
self.label_message.setTextFormat(QtCore.Qt.TextFormat.MarkdownText) self.label_message.setTextFormat(QtCore.Qt.TextFormat.MarkdownText)
# If the message is only an image URL, then instead of showing the message, # If the message is only an image URL, then instead of showing the message,
@@ -107,13 +104,10 @@ class MessageWidget(QtWidgets.QWidget, Ui_Form):
pixmap = QtGui.QPixmap(filename) pixmap = QtGui.QPixmap(filename)
# Make sure the image fits within the listView # Make sure the image fits within the listView
W = settings.value("MessageWidget/content_image/W_percentage", type=float) * ( W = settings.value("MessageWidget/content_image/W_percentage", type=float)
self._parent.width() - self.label_image.width() H = settings.value("MessageWidget/content_image/H_percentage", type=float)
) W *= self._parent.width() - self.label_image.width()
H = ( H *= self._parent.height()
settings.value("MessageWidget/content_image/H_percentage", type=float)
* self._parent.height()
)
if pixmap.width() > W or pixmap.height() > H: if pixmap.width() > W or pixmap.height() > H:
pixmap = pixmap.scaled( pixmap = pixmap.scaled(
@@ -144,7 +138,5 @@ class MessageWidget(QtWidgets.QWidget, Ui_Form):
self.image_popup.emit(link, QtGui.QCursor.pos()) self.image_popup.emit(link, QtGui.QCursor.pos())
def link_callbacks(self): def link_callbacks(self):
self.pb_delete.clicked.connect( self.pb_delete.clicked.connect(lambda: self.deletion_requested.emit(self.message_item))
lambda: self.deletion_requested.emit(self.message_item)
)
self.label_message.linkHovered.connect(self.link_hovered_callback) self.label_message.linkHovered.connect(self.link_hovered_callback)

View File

@@ -19,9 +19,7 @@ class ServerInfoDialog(QtWidgets.QDialog, Ui_Dialog):
self.line_url.setPlaceholderText("https://gotify.example.com") self.line_url.setPlaceholderText("https://gotify.example.com")
self.line_url.setText(url) self.line_url.setText(url)
self.line_token.setText(token) self.line_token.setText(token)
self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setDisabled( self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setDisabled(True)
True
)
self.pb_import.setVisible(enable_import) self.pb_import.setVisible(enable_import)
self.link_callbacks() self.link_callbacks()
@@ -37,9 +35,7 @@ class ServerInfoDialog(QtWidgets.QDialog, Ui_Dialog):
return return
self.pb_test.setDisabled(True) self.pb_test.setDisabled(True)
self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setDisabled( self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setDisabled(True)
True
)
self.task = VerifyServerInfoTask(url, client_token) self.task = VerifyServerInfoTask(url, client_token)
self.task.success.connect(self.server_info_success) self.task.success.connect(self.server_info_success)
@@ -59,9 +55,7 @@ class ServerInfoDialog(QtWidgets.QDialog, Ui_Dialog):
self.update_widget_state(self.pb_test, "success") self.update_widget_state(self.pb_test, "success")
self.update_widget_state(self.line_token, "success") self.update_widget_state(self.line_token, "success")
self.update_widget_state(self.line_url, "success") self.update_widget_state(self.line_url, "success")
self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setEnabled( self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setEnabled(True)
True
)
self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setFocus() self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setFocus()
def incorrect_token_callback(self, version: GotifyVersionModel): def incorrect_token_callback(self, version: GotifyVersionModel):
@@ -95,14 +89,6 @@ class ServerInfoDialog(QtWidgets.QDialog, Ui_Dialog):
def link_callbacks(self): def link_callbacks(self):
self.pb_test.clicked.connect(self.test_server_info) self.pb_test.clicked.connect(self.test_server_info)
self.line_url.textChanged.connect( self.line_url.textChanged.connect(lambda: self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setDisabled(True))
lambda: self.buttonBox.button( self.line_token.textChanged.connect(lambda: self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setDisabled(True))
QtWidgets.QDialogButtonBox.StandardButton.Ok
).setDisabled(True)
)
self.line_token.textChanged.connect(
lambda: self.buttonBox.button(
QtWidgets.QDialogButtonBox.StandardButton.Ok
).setDisabled(True)
)
self.pb_import.clicked.connect(self.import_callback) self.pb_import.clicked.connect(self.import_callback)

View File

@@ -43,42 +43,28 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
self.link_callbacks() self.link_callbacks()
def initUI(self): def initUI(self):
self.buttonBox.button( self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Apply).setEnabled(False)
QtWidgets.QDialogButtonBox.StandardButton.Apply
).setEnabled(False)
# Notifications # Notifications
self.spin_priority.setValue( self.spin_priority.setValue(settings.value("tray/notifications/priority", type=int))
settings.value("tray/notifications/priority", type=int)
)
self.spin_duration.setValue( self.spin_duration.setValue(settings.value("tray/notifications/duration_ms", type=int))
settings.value("tray/notifications/duration_ms", type=int)
)
if platform.system() == "Windows": if platform.system() == "Windows":
# The notification duration setting is ignored by windows # The notification duration setting is ignored by windows
self.label_notification_duration.hide() self.label_notification_duration.hide()
self.spin_duration.hide() self.spin_duration.hide()
self.label_notification_duration_ms.hide() self.label_notification_duration_ms.hide()
self.cb_notify.setChecked( self.cb_notify.setChecked(settings.value("message/check_missed/notify", type=bool))
settings.value("message/check_missed/notify", type=bool)
)
self.cb_notification_click.setChecked( self.cb_notification_click.setChecked(settings.value("tray/notifications/click", type=bool))
settings.value("tray/notifications/click", type=bool)
)
self.cb_tray_icon_unread.setChecked( self.cb_tray_icon_unread.setChecked(settings.value("tray/icon/unread", type=bool))
settings.value("tray/icon/unread", type=bool)
)
# Interface # Interface
self.combo_theme.addItems(get_themes()) self.combo_theme.addItems(get_themes())
self.combo_theme.setCurrentText(settings.value("theme", type=str)) self.combo_theme.setCurrentText(settings.value("theme", type=str))
self.cb_priority_colors.setChecked( self.cb_priority_colors.setChecked(settings.value("MessageWidget/priority_color", type=bool))
settings.value("MessageWidget/priority_color", type=bool)
)
# Logging # Logging
self.combo_logging.addItems( self.combo_logging.addItems(
@@ -96,9 +82,7 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
self.add_message_widget() self.add_message_widget()
# Advanced # Advanced
self.groupbox_image_popup.setChecked( self.groupbox_image_popup.setChecked(settings.value("ImagePopup/enabled", type=bool))
settings.value("ImagePopup/enabled", type=bool)
)
self.spin_popup_w.setValue(settings.value("ImagePopup/w", type=int)) self.spin_popup_w.setValue(settings.value("ImagePopup/w", type=int))
self.spin_popup_h.setValue(settings.value("ImagePopup/h", type=int)) self.spin_popup_h.setValue(settings.value("ImagePopup/h", type=int))
self.label_cache.setText("0 MB") self.label_cache.setText("0 MB")
@@ -132,16 +116,12 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
def settings_changed_callback(self, *args, **kwargs): def settings_changed_callback(self, *args, **kwargs):
self.settings_changed = True self.settings_changed = True
self.buttonBox.button( self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Apply).setEnabled(True)
QtWidgets.QDialogButtonBox.StandardButton.Apply
).setEnabled(True)
def change_font_callback(self, name: str): def change_font_callback(self, name: str):
label: QtWidgets.QLabel = getattr(self.message_widget, "label_" + name) label: QtWidgets.QLabel = getattr(self.message_widget, "label_" + name)
font, accepted = QtWidgets.QFontDialog.getFont( font, accepted = QtWidgets.QFontDialog.getFont(label.font(), self, f"Select a {name} font")
label.font(), self, f"Select a {name} font"
)
if accepted: if accepted:
self.settings_changed_callback() self.settings_changed_callback()
@@ -205,9 +185,7 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
self.label_cache.setText("0 MB") self.label_cache.setText("0 MB")
def link_callbacks(self): def link_callbacks(self):
self.buttonBox.button( self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Apply).clicked.connect(self.apply_settings)
QtWidgets.QDialogButtonBox.StandardButton.Apply
).clicked.connect(self.apply_settings)
# Notifications # Notifications
self.spin_priority.valueChanged.connect(self.settings_changed_callback) self.spin_priority.valueChanged.connect(self.settings_changed_callback)
@@ -225,22 +203,14 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
# Logging # Logging
self.combo_logging.currentTextChanged.connect(self.settings_changed_callback) self.combo_logging.currentTextChanged.connect(self.settings_changed_callback)
self.pb_open_log.clicked.connect( self.pb_open_log.clicked.connect(lambda: open_file(logger.root.handlers[0].baseFilename))
lambda: open_file(logger.root.handlers[0].baseFilename)
)
# Fonts # Fonts
self.pb_reset_fonts.clicked.connect(self.reset_fonts_callback) self.pb_reset_fonts.clicked.connect(self.reset_fonts_callback)
self.pb_font_message_title.clicked.connect( self.pb_font_message_title.clicked.connect(lambda: self.change_font_callback("title"))
lambda: self.change_font_callback("title") self.pb_font_message_date.clicked.connect(lambda: self.change_font_callback("date"))
) self.pb_font_message_content.clicked.connect(lambda: self.change_font_callback("message"))
self.pb_font_message_date.clicked.connect(
lambda: self.change_font_callback("date")
)
self.pb_font_message_content.clicked.connect(
lambda: self.change_font_callback("message")
)
# Advanced # Advanced
self.pb_export.clicked.connect(self.export_callback) self.pb_export.clicked.connect(self.export_callback)
@@ -257,9 +227,7 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
settings.setValue("tray/notifications/priority", self.spin_priority.value()) settings.setValue("tray/notifications/priority", self.spin_priority.value())
settings.setValue("tray/notifications/duration_ms", self.spin_duration.value()) settings.setValue("tray/notifications/duration_ms", self.spin_duration.value())
settings.setValue("message/check_missed/notify", self.cb_notify.isChecked()) settings.setValue("message/check_missed/notify", self.cb_notify.isChecked())
settings.setValue( settings.setValue("tray/notifications/click", self.cb_notification_click.isChecked())
"tray/notifications/click", self.cb_notification_click.isChecked()
)
settings.setValue("tray/icon/unread", self.cb_tray_icon_unread.isChecked()) settings.setValue("tray/icon/unread", self.cb_tray_icon_unread.isChecked())
# Interface # Interface
@@ -269,9 +237,7 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
settings.setValue("theme", selected_theme) settings.setValue("theme", selected_theme)
self.theme_change_requested.emit(selected_theme) self.theme_change_requested.emit(selected_theme)
settings.setValue( settings.setValue("MessageWidget/priority_color", self.cb_priority_colors.isChecked())
"MessageWidget/priority_color", self.cb_priority_colors.isChecked()
)
# Logging # Logging
selected_level = self.combo_logging.currentText() selected_level = self.combo_logging.currentText()
@@ -283,17 +249,9 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
logger.setLevel(selected_level) logger.setLevel(selected_level)
# Fonts # Fonts
settings.setValue( settings.setValue("MessageWidget/font/title", self.message_widget.label_title.font().toString())
"MessageWidget/font/title", settings.setValue("MessageWidget/font/date", self.message_widget.label_date.font().toString())
self.message_widget.label_title.font().toString(), settings.setValue("MessageWidget/font/message", self.message_widget.label_message.font().toString())
)
settings.setValue(
"MessageWidget/font/date", self.message_widget.label_date.font().toString()
)
settings.setValue(
"MessageWidget/font/message",
self.message_widget.label_message.font().toString(),
)
# Advanced # Advanced
settings.setValue("ImagePopup/enabled", self.groupbox_image_popup.isChecked()) settings.setValue("ImagePopup/enabled", self.groupbox_image_popup.isChecked())

View File

@@ -195,9 +195,7 @@ class ServerConnectionWatchdogTask(BaseTask):
time.sleep(settings.value("watchdog/interval/s", type=int)) time.sleep(settings.value("watchdog/interval/s", type=int))
if not self.gotify_client.is_listening(): if not self.gotify_client.is_listening():
self.closed.emit() self.closed.emit()
logger.debug( logger.debug("ServerConnectionWatchdogTask: gotify_client is not listening")
"ServerConnectionWatchdogTask: gotify_client is not listening"
)
class ExportSettingsTask(BaseTask): class ExportSettingsTask(BaseTask):