Rename to playtypus, modularise it

This commit is contained in:
Raoul Snyman 2021-06-17 08:24:48 -07:00
parent b9666a0f61
commit 8ab9cafa24
9 changed files with 73 additions and 74 deletions

View File

@ -1,3 +1,3 @@
# pyfunkwhale # Playtypus
(working name) Playtpus is a desktop Funkwhale client.

0
playtypus/__init__.py Normal file
View File

View File

@ -1,72 +1,10 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import requests
from funksnake import Funkwhale from funksnake import Funkwhale
from PyQt5 import QtCore, QtGui, QtWidgets, QtNetwork from PyQt5 import QtCore, QtWidgets, QtNetwork
from ui_mainwindow import UiMainWindow from playtypus.ui_mainwindow import UiMainWindow
from settingsdialog import SettingsDialog from playtypus.settingsdialog import SettingsDialog
from playtypus.threads import AlbumWorker
class ThreadWorker(QtCore.QObject):
"""
The :class:`~threads.ThreadWorker` class provides a base class for thread workers.
In Qt5, all work is done in workers, rather than actual QThread classes, and this case class provides
some utility signals and slots to make it easier to use threads and thread workers.
"""
quit = QtCore.pyqtSignal()
error = QtCore.pyqtSignal(str, str)
def start(self):
"""
The start method is how the worker runs. Basically, put your code here.
"""
raise NotImplementedError('Your class needs to override this method and run self.quit.emit() when complete')
class AlbumArtWorker(ThreadWorker):
album_item = None
artwork_url = None
def start(self):
if not self.album_item:
self.error.emit('No album QListWidgetItem set', 'The album_item attribute was not set')
self.quit.emit()
return
if not self.artwork_url:
self.error.emit('No artwork URL set', 'The artwork_url attribute was not set')
self.quit.emit()
return
response = requests.get(self.artwork_url)
if response.status_code == 200:
pixmap = QtGui.QPixmap()
pixmap.loadFromData(response.content)
self.album_item.setIcon(QtGui.QIcon(pixmap))
else:
print(response.text)
self.quit.emit()
class AlbumWorker(ThreadWorker):
"""
A thread worker to fetch the album art
"""
window = None
def start(self):
if not self.window:
self.error.emit('No window set', 'The window attribute was not set')
self.quit.emit()
return
self.window.albumListWidget.clear()
for album in self.window.funkwhale.albums.list()['results']:
album_item = QtWidgets.QListWidgetItem(album['title'])
self.window.albumListWidget.addItem(album_item)
artwork_worker = AlbumArtWorker()
artwork_worker.album_item = album_item
artwork_worker.artwork_url = album['cover']['urls']['medium_square_crop']
self.window.run_thread(artwork_worker, 'album-{}'.format(album['title']))
self.quit.emit()
class MainWindow(QtWidgets.QMainWindow, UiMainWindow): class MainWindow(QtWidgets.QMainWindow, UiMainWindow):

67
playtypus/threads.py Normal file
View File

@ -0,0 +1,67 @@
"""
The :mod:`threads` module contains functions to make working with QThreads easier
"""
import requests
from PyQt5 import QtCore, QtGui, QtWidgets
class ThreadWorker(QtCore.QObject):
"""
The :class:`~threads.ThreadWorker` class provides a base class for thread workers.
In Qt5, all work is done in workers, rather than actual QThread classes, and this case class provides
some utility signals and slots to make it easier to use threads and thread workers.
"""
quit = QtCore.pyqtSignal()
error = QtCore.pyqtSignal(str, str)
def start(self):
"""
The start method is how the worker runs. Basically, put your code here.
"""
raise NotImplementedError('Your class needs to override this method and run self.quit.emit() when complete')
class AlbumArtWorker(ThreadWorker):
album_item = None
artwork_url = None
def start(self):
if not self.album_item:
self.error.emit('No album QListWidgetItem set', 'The album_item attribute was not set')
self.quit.emit()
return
if not self.artwork_url:
self.error.emit('No artwork URL set', 'The artwork_url attribute was not set')
self.quit.emit()
return
response = requests.get(self.artwork_url)
if response.status_code == 200:
pixmap = QtGui.QPixmap()
pixmap.loadFromData(response.content)
self.album_item.setIcon(QtGui.QIcon(pixmap))
else:
print(response.text)
self.quit.emit()
class AlbumWorker(ThreadWorker):
"""
A thread worker to fetch the album art
"""
window = None
def start(self):
if not self.window:
self.error.emit('No window set', 'The window attribute was not set')
self.quit.emit()
return
self.window.albumListWidget.clear()
for album in self.window.funkwhale.albums.list()['results']:
album_item = QtWidgets.QListWidgetItem(album['title'])
self.window.albumListWidget.addItem(album_item)
artwork_worker = AlbumArtWorker()
artwork_worker.album_item = album_item
artwork_worker.artwork_url = album['cover']['urls']['medium_square_crop']
self.window.run_thread(artwork_worker, 'album-{}'.format(album['title']))
self.quit.emit()

View File

@ -1,6 +0,0 @@
"""
The :mod:`threads` module contains functions to make working with QThreads easier
"""
from PyQt5 import QtCore