mirror of
https://github.com/Kilian/Trimage.git
synced 2026-01-26 10:08:40 -05:00
fix command line options (was: app quitting before thread done)
This commit is contained in:
parent
0617a1dd5a
commit
5e39866f90
1 changed files with 48 additions and 47 deletions
51
trimage.py
51
trimage.py
|
|
@ -1,7 +1,7 @@
|
||||||
import sys
|
import sys
|
||||||
from os import system
|
from os import system
|
||||||
from os import listdir
|
from os import listdir
|
||||||
import os.path
|
from os import path
|
||||||
from subprocess import *
|
from subprocess import *
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
|
|
@ -84,11 +84,11 @@ class StartQT4(QMainWindow):
|
||||||
compress_file."""
|
compress_file."""
|
||||||
global showapp
|
global showapp
|
||||||
showapp = False
|
showapp = False
|
||||||
dirpath = os.path.abspath(os.path.dirname(directory))
|
dirpath = path.abspath(path.dirname(directory))
|
||||||
imagedir = listdir(directory)
|
imagedir = listdir(directory)
|
||||||
filelist = QStringList()
|
filelist = QStringList()
|
||||||
for image in imagedir:
|
for image in imagedir:
|
||||||
image = QString(os.path.join(dirpath, image))
|
image = QString(path.join(dirpath, image))
|
||||||
filelist.append(image)
|
filelist.append(image)
|
||||||
self.delegator(filelist)
|
self.delegator(filelist)
|
||||||
|
|
||||||
|
|
@ -96,7 +96,7 @@ class StartQT4(QMainWindow):
|
||||||
"""Get the file and send it to compress_file"""
|
"""Get the file and send it to compress_file"""
|
||||||
global showapp
|
global showapp
|
||||||
showapp = False
|
showapp = False
|
||||||
image = os.path.abspath(image)
|
image = path.abspath(image)
|
||||||
filecmdlist = QStringList()
|
filecmdlist = QStringList()
|
||||||
filecmdlist.append(image)
|
filecmdlist.append(image)
|
||||||
self.delegator(filecmdlist)
|
self.delegator(filecmdlist)
|
||||||
|
|
@ -181,7 +181,7 @@ class StartQT4(QMainWindow):
|
||||||
|
|
||||||
def checkname(self, name):
|
def checkname(self, name):
|
||||||
"""Check if the file is a jpg or png."""
|
"""Check if the file is a jpg or png."""
|
||||||
return os.path.splitext(str(name))[1].lower() in [".jpg", ".jpeg", ".png"]
|
return path.splitext(str(name))[1].lower() in [".jpg", ".jpeg", ".png"]
|
||||||
|
|
||||||
def enable_recompress(self):
|
def enable_recompress(self):
|
||||||
"""Enable the recompress button."""
|
"""Enable the recompress button."""
|
||||||
|
|
@ -246,17 +246,19 @@ class Worker(QThread):
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
"""Compress the given file, get data from it and call update_table."""
|
"""Compress the given file, get data from it and call update_table."""
|
||||||
|
global showapp
|
||||||
|
global imagelist
|
||||||
for image in self.images:
|
for image in self.images:
|
||||||
#gather old file data
|
#gather old file data
|
||||||
filename = image[0]
|
filename = str(image[0])
|
||||||
icon = image[1]
|
icon = image[1]
|
||||||
oldfile = QFileInfo(filename)
|
oldfile = QFileInfo(filename)
|
||||||
name = oldfile.fileName()
|
name = oldfile.fileName()
|
||||||
oldfilesize = oldfile.size()
|
oldfilesize = oldfile.size()
|
||||||
oldfilesizestr = size(oldfilesize, system=alternative)
|
oldfilesizestr = size(oldfilesize, system=alternative)
|
||||||
|
|
||||||
# get extention
|
# get extention
|
||||||
print os.path, type(filename), type(os.path), str(filename)
|
baseName, extention = path.splitext(filename)
|
||||||
baseName, extention = os.path.splitext(filename)
|
|
||||||
|
|
||||||
#decide with tool to use
|
#decide with tool to use
|
||||||
if extention in ['.jpg', '.jpeg']:
|
if extention in ['.jpg', '.jpeg']:
|
||||||
|
|
@ -264,21 +266,17 @@ class Worker(QThread):
|
||||||
elif extention in ['.png']:
|
elif extention in ['.png']:
|
||||||
runString = 'optipng -force -o7 "%(file)s"; advpng -z4 "%(file)s"'
|
runString = 'optipng -force -o7 "%(file)s"; advpng -z4 "%(file)s"'
|
||||||
else:
|
else:
|
||||||
# This probably should never happen...
|
raise Exception('File %s does not have the appropriate'
|
||||||
raise Exception('File %s does not have the appropriate extention' % filename)
|
'extention' % filename)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
retcode = call(runString % {'file' : filename}, shell = True, stdout = PIPE)
|
retcode = call(runString % {'file' : filename}, shell=True,
|
||||||
|
stdout=PIPE)
|
||||||
runfile = retcode
|
runfile = retcode
|
||||||
except OSError, e:
|
except OSError, e:
|
||||||
runfile = e
|
runfile = e
|
||||||
|
|
||||||
if runfile != 0:
|
if runfile == 0:
|
||||||
# TODO nice error recovery
|
|
||||||
raise Exception('Some error occurred!')
|
|
||||||
|
|
||||||
|
|
||||||
#gather new file data
|
#gather new file data
|
||||||
newfile = QFile(filename)
|
newfile = QFile(filename)
|
||||||
newfilesize = newfile.size()
|
newfilesize = newfile.size()
|
||||||
|
|
@ -289,27 +287,30 @@ class Worker(QThread):
|
||||||
ratiostr = "%.1f%%" % ratio
|
ratiostr = "%.1f%%" % ratio
|
||||||
|
|
||||||
# append current image to list
|
# append current image to list
|
||||||
global imagelist
|
|
||||||
for i, image in enumerate(imagelist):
|
for i, image in enumerate(imagelist):
|
||||||
if image[4] == filename:
|
if image[4] == filename:
|
||||||
imagelist.remove(image)
|
imagelist.remove(image)
|
||||||
imagelist.insert(i, (name, oldfilesizestr, newfilesizestr, ratiostr,
|
imagelist.insert(i, (name, oldfilesizestr,
|
||||||
filename, icon))
|
newfilesizestr,ratiostr, filename, icon))
|
||||||
|
|
||||||
self.emit(SIGNAL("updateUi"))
|
self.emit(SIGNAL("updateUi"))
|
||||||
|
|
||||||
global showapp
|
|
||||||
if not showapp:
|
if not showapp:
|
||||||
# we work via the commandline
|
# we work via the commandline
|
||||||
print("File:" + filename + ", Old Size:" + oldfilesizestr +
|
print("File:" + filename + ", Old Size:" + oldfilesizestr +
|
||||||
", New Size:" + newfilesizestr + ", Ratio:" + ratiostr)
|
", New Size:" + newfilesizestr + ", Ratio:" +
|
||||||
|
ratiostr)
|
||||||
|
else:
|
||||||
|
# TODO nice error recovery
|
||||||
|
raise Exception('Some error occurred!')
|
||||||
|
|
||||||
|
if not showapp:
|
||||||
|
quit()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app = QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
myapp = StartQT4()
|
myapp = StartQT4()
|
||||||
|
|
||||||
if showapp:
|
if showapp:
|
||||||
# no command line options called
|
|
||||||
myapp.show()
|
myapp.show()
|
||||||
else:
|
|
||||||
quit()
|
|
||||||
sys.exit(app.exec_())
|
sys.exit(app.exec_())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue