Errors now go to stderr, and PEP-8 is back.

This commit is contained in:
Paul Chaplin 2010-02-08 17:19:41 +00:00
parent 245fde934b
commit cac5e6ff1f
2 changed files with 40 additions and 44 deletions

4
todo
View file

@ -1,7 +1,6 @@
========================================== ==========================================
todo app wise todo app wise
- errors need to go to standarderror - check for optipng, advpng and jpegoptim on load
- check for optipng, advpng en jpegoptim on load
todo else todo else
- figure out dependencies for a .deb/how to make a .deb - figure out dependencies for a .deb/how to make a .deb
@ -28,4 +27,3 @@ later versions:
thanks to: thanks to:
Neil Wallace Neil Wallace
Jeroen Goudsmit Jeroen Goudsmit

View file

@ -1,8 +1,7 @@
import sys import sys
from os import system
from os import listdir from os import listdir
from os import path from os import path
from subprocess import * from subprocess import call, PIPE
from optparse import OptionParser from optparse import OptionParser
from PyQt4.QtCore import * from PyQt4.QtCore import *
@ -11,7 +10,7 @@ from hurry.filesize import *
from ui import Ui_trimage from ui import Ui_trimage
VERSION = "1.0" VERSION = "1.0.0"
#init imagelist #init imagelist
imagelist = [] imagelist = []
@ -33,9 +32,9 @@ class StartQT4(QMainWindow):
quit() quit()
#add quit shortcut #add quit shortcut
if hasattr(QKeySequence, 'Quit'): if hasattr(QKeySequence, "Quit"):
self.quit_shortcut = QShortcut(QKeySequence(QKeySequence.Quit), self.quit_shortcut = QShortcut(QKeySequence(QKeySequence.Quit),
self) self)
else: else:
self.quit_shortcut = QShortcut(QKeySequence("Ctrl+Q"), self) self.quit_shortcut = QShortcut(QKeySequence("Ctrl+Q"), self)
@ -76,7 +75,7 @@ class StartQT4(QMainWindow):
parser.add_option("-f", "--file", action="store", type="string", parser.add_option("-f", "--file", action="store", type="string",
dest="filename", help="compresses image and exit") dest="filename", help="compresses image and exit")
parser.add_option("-d", "--directory", action="store", type="string", parser.add_option("-d", "--directory", action="store", type="string",
dest="directory", help="compresses images in directory and exit", ) dest="directory", help="compresses images in directory and exit")
options, args = parser.parse_args() options, args = parser.parse_args()
@ -94,8 +93,9 @@ class StartQT4(QMainWindow):
""" """
def dir_from_cmd(self, directory): def dir_from_cmd(self, directory):
"""Read the files in the directory and send all files to """
compress_file.""" Read the files in the directory and send all files to compress_file.
"""
global showapp global showapp
showapp = False showapp = False
dirpath = path.abspath(path.dirname(directory)) dirpath = path.abspath(path.dirname(directory))
@ -116,8 +116,9 @@ class StartQT4(QMainWindow):
self.delegator(filecmdlist) self.delegator(filecmdlist)
def file_drop(self, images): def file_drop(self, images):
"""Get a file from the drag and drop handler and send it to """
compress_file.""" Get a file from the drag and drop handler and send it to compress_file.
"""
self.delegator(images) self.delegator(images)
def file_dialog(self): def file_dialog(self):
@ -126,7 +127,7 @@ class StartQT4(QMainWindow):
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 differenciating 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.delegator(images) self.delegator(images)
@ -144,16 +145,17 @@ class StartQT4(QMainWindow):
""" """
def delegator(self, images): def delegator(self, images):
"""Recieve all images, check them and send them to the worker """
thread.""" Recieve all images, check them and send them to the worker thread.
"""
delegatorlist = [] delegatorlist = []
for image in images: for image in images:
if self.checkname(image): if self.checkname(image):
delegatorlist.append((image, QIcon(image))) delegatorlist.append((image, QIcon(image)))
imagelist.append(("Compressing...", "", "", "", image, imagelist.append(("Compressing...", "", "", "", image,
QIcon(QPixmap("compressing.gif")))) QIcon(QPixmap("compressing.gif"))))
else: else:
print('[error] %s not an image file' % image) sys.stderr.write("[error] %s not an image file" % image)
self.thread.compress_file(delegatorlist) self.thread.compress_file(delegatorlist)
""" """
@ -205,17 +207,17 @@ class StartQT4(QMainWindow):
retcode = call("jpegoptim --version", shell=True, stdout=PIPE) retcode = call("jpegoptim --version", shell=True, stdout=PIPE)
if retcode != 0: if retcode != 0:
status = True status = True
print("[error] please install jpegoptim") sys.stderr.write("[error] please install jpegoptim")
retcode = call("optipng -v", shell=True, stdout=PIPE) retcode = call("optipng -v", shell=True, stdout=PIPE)
if retcode != 0: if retcode != 0:
status = True status = True
print("[error] please install optipng") sys.stderr.write("[error] please install optipng")
retcode = call("advpng --version", shell=True, stdout=PIPE) retcode = call("advpng --version", shell=True, stdout=PIPE)
if retcode != 0: if retcode != 0:
status = True status = True
print("[error] please install advancecomp") sys.stderr.write("[error] please install advancecomp")
return status return status
@ -223,9 +225,9 @@ class TriTableModel(QAbstractTableModel):
def __init__(self, parent, imagelist, header, *args): def __init__(self, parent, imagelist, header, *args):
""" """
imagelist is list of tuples @param parent Qt parent object.
header is list of strings @param imagelist A list of tuples.
tuple length has to match header length @param header A list of strings.
""" """
QAbstractTableModel.__init__(self, parent, *args) QAbstractTableModel.__init__(self, parent, *args)
self.imagelist = imagelist self.imagelist = imagelist
@ -240,7 +242,7 @@ class TriTableModel(QAbstractTableModel):
return len(self.header) return len(self.header)
def data(self, index, role): def data(self, index, role):
"""fill the table with data""" """Fill the table with data."""
if not index.isValid(): if not index.isValid():
return QVariant() return QVariant()
elif role == Qt.DisplayRole: elif role == Qt.DisplayRole:
@ -278,9 +280,7 @@ 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 global imagelist
global verbose
for image in self.images: for image in self.images:
#gather old file data #gather old file data
filename = str(image[0]) filename = str(image[0])
@ -292,21 +292,20 @@ class Worker(QThread):
# get extention # get extention
extention = path.splitext(filename)[1] extention = path.splitext(filename)[1]
#decide with tool to use #decide with tool to use
if extention in ['.jpg', '.jpeg']: if extention in [".jpg", ".jpeg"]:
runString = 'jpegoptim -f --strip-all "%(file)s"' runString = "jpegoptim -f --strip-all '%(file)s'"
elif extention in ['.png']: elif extention in [".png"]:
runString = '''optipng -force -o7 "%(file)s"; runString = ("optipng -force -o7 '%(file)s';"
advpng -z4 "%(file)s"''' "advpng -z4 '%(file)s'")
else: else:
print('[error] %s not an image file' % filename) sys.stderr.write("[error] %s not an image file" % filename)
try: try:
retcode = call(runString % {'file': filename}, shell=True, retcode = call(runString % {"file": filename}, shell=True,
stdout=PIPE) stdout=PIPE)
runfile = retcode runfile = retcode
except OSError, e: except OSError as e:
runfile = e runfile = e
if runfile == 0: if runfile == 0:
@ -324,18 +323,17 @@ class Worker(QThread):
if image[4] == filename: if image[4] == filename:
imagelist.remove(image) imagelist.remove(image)
imagelist.insert(i, (name, oldfilesizestr, imagelist.insert(i, (name, oldfilesizestr,
newfilesizestr, ratiostr, newfilesizestr, ratiostr, filename, icon))
filename, icon))
self.emit(SIGNAL("updateUi")) self.emit(SIGNAL("updateUi"))
if not showapp and verbose: if not showapp and verbose:
# we work via the commandline # we work via the commandline
print("File:" + filename + ", Old Size:" + oldfilesizestr + print("File: " + filename + ", Old Size: "
", New Size:" + newfilesizestr + ", Ratio:" + + oldfilesizestr + ", New Size: " + newfilesizestr
ratiostr) + ", Ratio: " + ratiostr)
else: else:
print('[error] %s' % runfile) sys.stderr.write("[error] %s" % runfile)
if not showapp: if not showapp:
#make sure the app quits after all images are done #make sure the app quits after all images are done