First try to adopt Python3 and PyQt5

This commit is contained in:
Hugo Posnic 2017-11-18 14:44:11 +01:00
parent e47888e067
commit 8eca530275
8 changed files with 109 additions and 235 deletions

View file

@ -8,7 +8,7 @@ ThreadPool Implementation
from __future__ import with_statement from __future__ import with_statement
from threading import Thread, RLock from threading import Thread, RLock
from time import sleep from time import sleep
from Queue import Queue, Empty from queue import Queue, Empty
import logging import logging
import sys import sys
@ -86,7 +86,7 @@ class ThreadPool:
''' '''
try: try:
return_value = self.callable(*self.arguments) #IGNORE:W0142 return_value = self.callable(*self.arguments) #IGNORE:W0142
except Exception, excep: #IGNORE:W0703 except Exception as excep: #IGNORE:W0703
logger = logging.getLogger("threadpool.worker") logger = logging.getLogger("threadpool.worker")
logger.warning("A job in the ThreadPool raised an exception: " + excep) logger.warning("A job in the ThreadPool raised an exception: " + excep)
#else do nothing cause we don't know what to do... #else do nothing cause we don't know what to do...
@ -95,7 +95,7 @@ class ThreadPool:
try: try:
if (self.return_callback != None): if (self.return_callback != None):
self.return_callback(return_value) self.return_callback(return_value)
except Exception, _: #IGNORE:W0703 everything could go wrong... except Exception as _: #IGNORE:W0703 everything could go wrong...
logger = logging.getLogger('threadpool') logger = logging.getLogger('threadpool')
logger.warning('Error while delivering return value to callback function') logger.warning('Error while delivering return value to callback function')

View file

@ -1 +0,0 @@
from ThreadPool import ThreadPool, ThreadPoolMixIn

View file

@ -1,71 +0,0 @@
Metadata-Version: 1.0
Name: hurry.filesize
Version: 0.9
Summary: A simple Python library for human readable file sizes (or anything sized in bytes).
Home-page: UNKNOWN
Author: Martijn Faassen, Startifact
Author-email: faassen@startifact.com
License: ZPL 2.1
Description: hurry.filesize
==============
hurry.filesize a simple Python library that can take a number of bytes and
returns a human-readable string with the size in it, in kilobytes (K),
megabytes (M), etc.
The default system it uses is "traditional", where multipliers of 1024
increase the unit size::
>>> from hurry.filesize import size
>>> size(1024)
'1K'
An alternative, slightly more verbose system::
>>> from hurry.filesize import alternative
>>> size(1, system=alternative)
'1 byte'
>>> size(10, system=alternative)
'10 bytes'
>>> size(1024, system=alternative)
'1 KB'
A verbose system::
>>> from hurry.filesize import verbose
>>> size(10, system=verbose)
'10 bytes'
>>> size(1024, system=verbose)
'1 kilobyte'
>>> size(2000, system=verbose)
'1 kilobyte'
>>> size(3000, system=verbose)
'2 kilobytes'
>>> size(1024 * 1024, system=verbose)
'1 megabyte'
>>> size(1024 * 1024 * 3, system=verbose)
'3 megabytes'
You can also use the SI system, where multipliers of 1000 increase the unit
size::
>>> from hurry.filesize import si
>>> size(1000, system=si)
'1K'
Changes
=======
0.9 (2009-03-11)
----------------
* Initial public release.
Download
========
Keywords: file size bytes
Platform: UNKNOWN
Classifier: Programming Language :: Python
Classifier: Topic :: Software Development :: Libraries :: Python Modules

View file

@ -1,50 +0,0 @@
hurry.filesize
==============
hurry.filesize a simple Python library that can take a number of bytes and
returns a human-readable string with the size in it, in kilobytes (K),
megabytes (M), etc.
The default system it uses is "traditional", where multipliers of 1024
increase the unit size::
>>> from hurry.filesize import size
>>> size(1024)
'1K'
An alternative, slightly more verbose system::
>>> from hurry.filesize import alternative
>>> size(1, system=alternative)
'1 byte'
>>> size(10, system=alternative)
'10 bytes'
>>> size(1024, system=alternative)
'1 KB'
A verbose system::
>>> from hurry.filesize import verbose
>>> size(10, system=verbose)
'10 bytes'
>>> size(1024, system=verbose)
'1 kilobyte'
>>> size(2000, system=verbose)
'1 kilobyte'
>>> size(3000, system=verbose)
'2 kilobytes'
>>> size(1024 * 1024, system=verbose)
'1 megabyte'
>>> size(1024 * 1024 * 3, system=verbose)
'3 megabytes'
You can also use the SI system, where multipliers of 1000 increase the unit
size::
>>> from hurry.filesize import si
>>> size(1000, system=si)
'1K'
http://pypi.python.org/pypi/hurry.filesize

View file

@ -1,2 +0,0 @@
from filesize import size
from filesize import traditional, alternative, verbose, iec, si

View file

@ -11,13 +11,14 @@ from shutil import copy
from subprocess import call, PIPE from subprocess import call, PIPE
from optparse import OptionParser from optparse import OptionParser
from PyQt4.QtCore import * from PyQt5.QtCore import *
from PyQt4.QtGui import * from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from filesize import * from filesize import *
from imghdr import what as determinetype from imghdr import what as determinetype
from Queue import Queue from queue import Queue
from ThreadPool import ThreadPool from ThreadPool import ThreadPool, ThreadPoolMixIn
from multiprocessing import cpu_count from multiprocessing import cpu_count
from ui import Ui_trimage from ui import Ui_trimage
@ -25,7 +26,7 @@ from ui import Ui_trimage
VERSION = "1.0.5" VERSION = "1.0.5"
class StartQT4(QMainWindow): class StartQT5(QMainWindow):
def __init__(self, parent=None): def __init__(self, parent=None):
QWidget.__init__(self, parent) QWidget.__init__(self, parent)
@ -40,7 +41,7 @@ class StartQT4(QMainWindow):
QCoreApplication.setOrganizationDomain("trimage.org") QCoreApplication.setOrganizationDomain("trimage.org")
QCoreApplication.setApplicationName("Trimage") QCoreApplication.setApplicationName("Trimage")
self.settings = QSettings() self.settings = QSettings()
self.restoreGeometry(self.settings.value("geometry").toByteArray()) self.restoreGeometry(self.settings.value("geometry"))
# check if apps are installed # check if apps are installed
if self.checkapps(): if self.checkapps():
@ -61,17 +62,15 @@ class StartQT4(QMainWindow):
self.thread = Worker() self.thread = Worker()
# connect signals with slots # connect signals with slots
QObject.connect(self.ui.addfiles, SIGNAL("clicked()"), self.ui.addfiles.clicked.connect(self.file_dialog)
self.file_dialog) self.ui.recompress.clicked.connect(self.recompress_files)
QObject.connect(self.ui.recompress, SIGNAL("clicked()"), # QObject.connect(self.quit_shortcut, SIGNAL("activated()"),
self.recompress_files) # qApp, SLOT('quit()'))
QObject.connect(self.quit_shortcut, SIGNAL("activated()"), # QObject.connect(self.ui.processedfiles, SIGNAL("fileDropEvent"),
qApp, SLOT('quit()')) # self.file_drop)
QObject.connect(self.ui.processedfiles, SIGNAL("fileDropEvent"), # QObject.connect(self.thread, SIGNAL("finished()"), self.update_table)
self.file_drop) # QObject.connect(self.thread, SIGNAL("terminated()"), self.update_table)
QObject.connect(self.thread, SIGNAL("finished()"), self.update_table) # QObject.connect(self.thread, SIGNAL("updateUi"), self.update_table)
QObject.connect(self.thread, SIGNAL("terminated()"), self.update_table)
QObject.connect(self.thread, SIGNAL("updateUi"), self.update_table)
self.compressing_icon = QIcon(QPixmap(self.ui.get_image("pixmaps/compressing.gif"))) self.compressing_icon = QIcon(QPixmap(self.ui.get_image("pixmaps/compressing.gif")))
@ -143,8 +142,8 @@ class StartQT4(QMainWindow):
def file_dialog(self): def file_dialog(self):
"""Open a file dialog and send the selected images to compress_file.""" """Open a file dialog and send the selected images to compress_file."""
fd = QFileDialog(self) fd = QFileDialog(self)
fd.restoreState(self.settings.value("fdstate").toByteArray()) fd.restoreState(self.settings.value("fdstate"))
directory = self.settings.value("directory", QVariant("")).toString() directory = self.settings.value("directory", QVariant(""))
fd.setDirectory(directory) fd.setDirectory(directory)
images = fd.getOpenFileNames(self, images = fd.getOpenFileNames(self,
@ -155,8 +154,8 @@ class StartQT4(QMainWindow):
self.settings.setValue("fdstate", QVariant(fd.saveState())) self.settings.setValue("fdstate", QVariant(fd.saveState()))
if images: if images:
self.settings.setValue("directory", QVariant(path.dirname(unicode(images[0])))) self.settings.setValue("directory", QVariant(path.dirname(images[0][0])))
self.delegator([unicode(fullpath) for fullpath in images]) self.delegator([fullpath for fullpath in images])
def recompress_files(self): def recompress_files(self):
"""Send each file in the current file list to compress_file again.""" """Send each file in the current file list to compress_file again."""
@ -174,16 +173,16 @@ class StartQT4(QMainWindow):
for fullpath in images: for fullpath in images:
try: # recompress images already in the list try: # recompress images already in the list
image = (i.image for i in self.imagelist image = (i.image for i in self.imagelist
if i.image.fullpath == fullpath).next() if i.image.fullpath == fullpath[0]).__next__()
if image.compressed: if image.compressed:
image.reset() image.reset()
image.recompression = True image.recompression = True
delegatorlist.append(image) delegatorlist.append(image)
except StopIteration: except StopIteration:
if not path.isdir(fullpath): if not path.isdir(fullpath[0]):
self. add_image(fullpath, delegatorlist) self.add_image(fullpath[0], delegatorlist)
else: else:
self.walk(fullpath, delegatorlist) self.walk(fullpath[0], delegatorlist)
self.update_table() self.update_table()
self.thread.compress_file(delegatorlist, self.showapp, self.verbose, self.thread.compress_file(delegatorlist, self.showapp, self.verbose,
@ -214,7 +213,7 @@ class StartQT4(QMainWindow):
self.systemtray.trayIcon.setToolTip("Trimage image compressor (" + str(len(self.imagelist)) + " files)") self.systemtray.trayIcon.setToolTip("Trimage image compressor (" + str(len(self.imagelist)) + " files)")
self.setWindowTitle("Trimage image compressor (" + str(len(self.imagelist)) + " files)") self.setWindowTitle("Trimage image compressor (" + str(len(self.imagelist)) + " files)")
else: else:
print >> sys.stderr, u"[error] %s not a supported image file and/or not writeable" % image.fullpath print(str(sys.stderr) + "[error] {} not a supported image file and/or not writeable".format(image.fullpath))
""" """
UI Functions UI Functions
@ -287,7 +286,7 @@ class StartQT4(QMainWindow):
while True: while True:
try: try:
return call(command, shell=True, stdout=PIPE) return call(command, shell=True, stdout=PIPE)
except OSError, e: except OSError as e:
if e.errno == errno.EINTR: if e.errno == errno.EINTR:
continue continue
else: else:
@ -488,7 +487,7 @@ class Worker(QThread):
tp._ThreadPool__jobs.empty()): tp._ThreadPool__jobs.empty()):
image = self.toDisplay.get() image = self.toDisplay.get()
self.emit(SIGNAL("updateUi")) ##self.emit(SIGNAL("updateUi"))
if not self.showapp and self.verbose: # we work via the commandline if not self.showapp and self.verbose: # we work via the commandline
if image.retcode == 0: if image.retcode == 0:
@ -497,7 +496,7 @@ class Worker(QThread):
+ ir['oldfilesizestr'] + ", New Size: " + ir['oldfilesizestr'] + ", New Size: "
+ ir['newfilesizestr'] + ", Ratio: " + ir['ratiostr']) + ir['newfilesizestr'] + ", Ratio: " + ir['ratiostr'])
else: else:
print >> sys.stderr, u"[error] %s could not be compressed" % image.fullpath print(str(sys.stderr) + "[error] {} could not be compressed".format(image.fullpath))
class Systray(QWidget): class Systray(QWidget):
@ -550,7 +549,7 @@ class Systray(QWidget):
if __name__ == "__main__": if __name__ == "__main__":
app = QApplication(sys.argv) app = QApplication(sys.argv)
myapp = StartQT4() myapp = StartQT5()
if myapp.showapp: if myapp.showapp:
myapp.show() myapp.show()

View file

@ -1,5 +1,6 @@
from PyQt4.QtCore import * from PyQt5.QtCore import *
from PyQt4.QtGui import * from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from os import path from os import path
class TrimageTableView(QTableView): class TrimageTableView(QTableView):
@ -44,7 +45,7 @@ class Ui_trimage(object):
self.centralwidget.setObjectName("centralwidget") self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QGridLayout(self.centralwidget) self.gridLayout_2 = QGridLayout(self.centralwidget)
self.gridLayout_2.setMargin(0) #self.gridLayout_2.setMargin(0)
self.gridLayout_2.setSpacing(0) self.gridLayout_2.setSpacing(0)
self.gridLayout_2.setObjectName("gridLayout_2") self.gridLayout_2.setObjectName("gridLayout_2")
@ -60,7 +61,7 @@ class Ui_trimage(object):
self.verticalLayout = QVBoxLayout(self.widget) self.verticalLayout = QVBoxLayout(self.widget)
self.verticalLayout.setSpacing(0) self.verticalLayout.setSpacing(0)
self.verticalLayout.setMargin(0) #self.verticalLayout.setMargin(0)
self.verticalLayout.setObjectName("verticalLayout") self.verticalLayout.setObjectName("verticalLayout")
self.frame = QFrame(self.widget) self.frame = QFrame(self.widget)
@ -68,12 +69,12 @@ class Ui_trimage(object):
self.verticalLayout_2 = QVBoxLayout(self.frame) self.verticalLayout_2 = QVBoxLayout(self.frame)
self.verticalLayout_2.setSpacing(0) self.verticalLayout_2.setSpacing(0)
self.verticalLayout_2.setMargin(0) #self.verticalLayout_2.setMargin(0)
self.verticalLayout_2.setObjectName("verticalLayout_2") self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout = QHBoxLayout() self.horizontalLayout = QHBoxLayout()
self.horizontalLayout.setSpacing(0) self.horizontalLayout.setSpacing(0)
self.horizontalLayout.setMargin(10) #self.horizontalLayout.setMargin(10)
self.horizontalLayout.setObjectName("horizontalLayout") self.horizontalLayout.setObjectName("horizontalLayout")
self.addfiles = QPushButton(self.frame) self.addfiles = QPushButton(self.frame)
@ -93,7 +94,7 @@ class Ui_trimage(object):
font.setPointSize(8) font.setPointSize(8)
self.label.setFont(font) self.label.setFont(font)
self.label.setFrameShadow(QFrame.Plain) self.label.setFrameShadow(QFrame.Plain)
self.label.setMargin(1) #self.label.setMargin(1)
self.label.setIndent(10) self.label.setIndent(10)
self.label.setObjectName("label") self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label) self.horizontalLayout.addWidget(self.label)
@ -143,24 +144,22 @@ class Ui_trimage(object):
def retranslateUi(self, trimage): def retranslateUi(self, trimage):
""" Fill in the texts for all UI elements """ """ Fill in the texts for all UI elements """
trimage.setWindowTitle(QApplication.translate("trimage", trimage.setWindowTitle(QApplication.translate("trimage",
"Trimage image compressor", None, QApplication.UnicodeUTF8)) "Trimage image compressor", None))
self.addfiles.setToolTip(QApplication.translate("trimage", self.addfiles.setToolTip(QApplication.translate("trimage",
"Add file to the compression list", None, "Add file to the compression list", None))
QApplication.UnicodeUTF8))
self.addfiles.setText(QApplication.translate("trimage", self.addfiles.setText(QApplication.translate("trimage",
"&Add and compress", None, QApplication.UnicodeUTF8)) "&Add and compress", None))
self.addfiles.setShortcut(QApplication.translate("trimage", self.addfiles.setShortcut(QApplication.translate("trimage",
"Alt+A", None, QApplication.UnicodeUTF8)) "Alt+A", None))
self.label.setText(QApplication.translate("trimage", self.label.setText(QApplication.translate("trimage",
"Drag and drop images onto the table", None, "Drag and drop images onto the table", None))
QApplication.UnicodeUTF8))
self.recompress.setToolTip(QApplication.translate("trimage", self.recompress.setToolTip(QApplication.translate("trimage",
"Recompress all images", None, QApplication.UnicodeUTF8)) "Recompress all images", None))
self.recompress.setText(QApplication.translate("trimage", self.recompress.setText(QApplication.translate("trimage",
"&Recompress", None, QApplication.UnicodeUTF8)) "&Recompress", None))
self.recompress.setShortcut(QApplication.translate("trimage", self.recompress.setShortcut(QApplication.translate("trimage",
"Alt+R", None, QApplication.UnicodeUTF8)) "Alt+R", None))
self.processedfiles.setToolTip(QApplication.translate("trimage", self.processedfiles.setToolTip(QApplication.translate("trimage",
"Drag files in here", None, QApplication.UnicodeUTF8)) "Drag files in here", None))
self.processedfiles.setWhatsThis(QApplication.translate("trimage", self.processedfiles.setWhatsThis(QApplication.translate("trimage",
"Drag files in here", None, QApplication.UnicodeUTF8)) "Drag files in here", None))