Merge pull request #53 from Huluti/master

A fix and some cosmetic changes
This commit is contained in:
Kilian Valkhof 2019-02-25 10:45:47 +01:00 committed by GitHub
commit 891bc9b73f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 55 deletions

View file

@ -1,9 +1,46 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys
import errno
from subprocess import call, PIPE
def human_readable_size(num, suffix='B'):
for unit in ['','K','M','G','T','P','E','Z']: def check_dependencies():
"""Check if the required command line apps exist."""
exe = ".exe" if (sys.platform == "win32") else ""
status = True
dependencies = {
"jpegoptim": "--version",
"optipng": "-v",
"advpng": "--version",
"pngcrush": "-version"
}
for elt in dependencies:
retcode = safe_call(elt + exe + " " + dependencies[elt])
if retcode != 0:
status = False
print("[error] please install {}".format(elt), file=sys.stderr)
return status
def safe_call(command):
"""Cross-platform command-line check."""
while True:
try:
return call(command, shell=True, stdout=PIPE)
except OSError as e:
if e.errno == errno.EINTR:
continue
else:
raise
def human_readable_size(num, suffix="B"):
"""Bytes to a readable size format"""
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
if abs(num) < 1024.0: if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix) return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0 num /= 1024.0
return "%.1f%s%s" % (num, 'Y', suffix) return "%.1f%s%s" % (num, "Y", suffix)

View file

@ -2,10 +2,9 @@
import time import time
import sys import sys
import errno
from os import listdir, path, remove, access, W_OK from os import listdir, path, remove, access, W_OK
from shutil import copy from shutil import copy
from subprocess import call, PIPE
from optparse import OptionParser from optparse import OptionParser
from multiprocessing import cpu_count from multiprocessing import cpu_count
from queue import Queue from queue import Queue
@ -14,16 +13,15 @@ from PyQt5.QtCore import *
from PyQt5.QtGui import * from PyQt5.QtGui import *
from PyQt5.QtWidgets import * from PyQt5.QtWidgets import *
from tools import human_readable_size
from ThreadPool import ThreadPool from ThreadPool import ThreadPool
from ui import Ui_trimage from ui import Ui_trimage
from tools import *
VERSION = "1.0.5" VERSION = "1.0.5"
class StartQT5(QMainWindow): class StartQt(QMainWindow):
def __init__(self, parent=None): def __init__(self, parent=None):
QWidget.__init__(self, parent) QWidget.__init__(self, parent)
self.ui = Ui_trimage() self.ui = Ui_trimage()
@ -41,7 +39,7 @@ class StartQT5(QMainWindow):
self.restoreGeometry(self.settings.value("geometry")) self.restoreGeometry(self.settings.value("geometry"))
# check if dependencies are installed # check if dependencies are installed
if not self.check_dependencies(): if not check_dependencies():
quit() quit()
# add quit shortcut # add quit shortcut
@ -53,7 +51,6 @@ class StartQT5(QMainWindow):
# disable recompress # disable recompress
self.ui.recompress.setEnabled(False) self.ui.recompress.setEnabled(False)
#self.ui.recompress.hide()
# make a worker thread # make a worker thread
self.thread = Worker() self.thread = Worker()
@ -75,11 +72,11 @@ class StartQT5(QMainWindow):
self.systemtray = Systray(self) self.systemtray = Systray(self)
def commandline_options(self): def commandline_options(self):
self.cli = False
"""Set up the command line options.""" """Set up the command line options."""
self.cli = False
parser = OptionParser(version="%prog " + VERSION, parser = OptionParser(version="%prog " + VERSION,
description="GUI front-end to compress png and jpg images via " description="GUI front-end to compress png and jpg images via "
"optipng, advpng and jpegoptim") "advpng, jpegoptim, optipng and pngcrush")
parser.set_defaults(verbose=True) parser.set_defaults(verbose=True)
parser.add_option("-v", "--verbose", action="store_true", parser.add_option("-v", "--verbose", action="store_true",
@ -141,14 +138,13 @@ class StartQT5(QMainWindow):
directory = self.settings.value("directory", QVariant("")) directory = self.settings.value("directory", QVariant(""))
fd.setDirectory(directory) fd.setDirectory(directory)
images = fd.getOpenFileNames(self, images, _ = fd.getOpenFileNames(self,
"Select one or more image files to compress", "Select one or more image files to compress",
directory, directory,
# this is a fix for file dialog differentiating between cases # this is a fix for file dialog differentiating between cases
"Image files (*.png *.jpg *.jpeg *.PNG *.JPG *.JPEG)") "Image files (*.png *.jpg *.jpeg *.PNG *.JPG *.JPEG)")
self.settings.setValue("fdstate", QVariant(fd.saveState())) self.settings.setValue("fdstate", QVariant(fd.saveState()))
images = images[0]
if images: if images:
self.settings.setValue("directory", QVariant(path.dirname(images[0]))) self.settings.setValue("directory", QVariant(path.dirname(images[0])))
self.delegator([fullpath for fullpath in images]) self.delegator([fullpath for fullpath in images])
@ -242,46 +238,12 @@ class StartQT5(QMainWindow):
# enable recompress button # enable recompress button
self.enable_recompress() self.enable_recompress()
"""
Helper functions
"""
def enable_recompress(self): def enable_recompress(self):
"""Enable the recompress button.""" """Enable the recompress button."""
self.ui.recompress.setEnabled(True) self.ui.recompress.setEnabled(True)
if QSystemTrayIcon.isSystemTrayAvailable() and not self.cli: if QSystemTrayIcon.isSystemTrayAvailable() and not self.cli:
self.systemtray.recompress.setEnabled(True) self.systemtray.recompress.setEnabled(True)
def check_dependencies(self):
"""Check if the required command line apps exist."""
exe = ".exe" if (sys.platform == "win32") else ""
status = True
dependencies = {
"jpegoptim": "--version",
"optipng": "-v",
"advpng": "--version",
"pngcrush": "-version"
}
for elt in dependencies:
retcode = self.safe_call(elt + exe + " " + dependencies[elt])
if retcode != 0:
status = False
print("[error] please install {}".format(elt), file=sys.stderr)
return status
def safe_call(self, command):
"""Cross-platform command-line check."""
while True:
try:
return call(command, shell=True, stdout=PIPE)
except OSError as e:
if e.errno == errno.EINTR:
continue
else:
raise
def hide_main_window(self): def hide_main_window(self):
if self.isVisible(): if self.isVisible():
self.hide() self.hide()
@ -298,7 +260,6 @@ class StartQT5(QMainWindow):
class TriTableModel(QAbstractTableModel): class TriTableModel(QAbstractTableModel):
def __init__(self, parent, imagelist, header, *args): def __init__(self, parent, imagelist, header, *args):
""" """
@param parent Qt parent object. @param parent Qt parent object.
@ -340,7 +301,6 @@ class TriTableModel(QAbstractTableModel):
class ImageRow: class ImageRow:
def __init__(self, image, waitingIcon=None): def __init__(self, image, waitingIcon=None):
"""Build the information visible in the table image row.""" """Build the information visible in the table image row."""
self.image = image self.image = image
@ -381,14 +341,13 @@ class ImageRow:
class Image: class Image:
def __init__(self, fullpath): def __init__(self, fullpath):
"""Gather image information.""" """Gather image information."""
self.valid = False self.valid = False
self.reset() self.reset()
self.fullpath = fullpath self.fullpath = fullpath
if path.isfile(self.fullpath) and access(self.fullpath, W_OK): if path.isfile(self.fullpath) and access(self.fullpath, W_OK):
self.filetype = path.splitext(self.fullpath)[1][1:] self.filetype = path.splitext(self.fullpath)[1][1:].lower()
if self.filetype == "jpg": if self.filetype == "jpg":
self.filetype = "jpeg" self.filetype = "jpeg"
if self.filetype in ["jpeg", "png"]: if self.filetype in ["jpeg", "png"]:
@ -442,7 +401,6 @@ class Image:
class Worker(QThread): class Worker(QThread):
update_ui_signal = pyqtSignal() update_ui_signal = pyqtSignal()
def __init__(self, parent=None): def __init__(self, parent=None):
@ -485,7 +443,6 @@ class Worker(QThread):
class Systray(QWidget): class Systray(QWidget):
def __init__(self, parent): def __init__(self, parent):
QWidget.__init__(self) QWidget.__init__(self)
self.parent = parent self.parent = parent
@ -534,7 +491,7 @@ class Systray(QWidget):
if __name__ == "__main__": if __name__ == "__main__":
app = QApplication(sys.argv) app = QApplication(sys.argv)
myapp = StartQT5() myapp = StartQt()
if myapp.showapp: if myapp.showapp:
myapp.show() myapp.show()