changeset 94:9790bc126ea6

Epic simplified error handling in true try/except/finally spirit.
author Edho Prima Arief <edho@myconan.net>
date Thu, 14 Jul 2011 00:49:33 +0700
parents 4f954c1e2fdc
children a5324d2cc0eb
files bin/recompress
diffstat 1 files changed, 8 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/bin/recompress	Thu Jul 14 00:39:09 2011 +0700
+++ b/bin/recompress	Thu Jul 14 00:49:33 2011 +0700
@@ -10,13 +10,11 @@
 # 2 = error saving file (no write permission)
 # 3 = not a png file
 def repng(filename):
-  retcode = 0
   im = Image.open(filename)
   if im.format == "PNG":
-    im.save(filename, optimize=1)
+    im.save(filename, "PNG", optimize=1)
   else:
-    retcode = 1
-  return retcode
+    raise Exception("Not a PNG image file")
 
 def hbytes(inbyte):
   units = ["B", "kB", "MB", "GB", "TB", "PB"] 
@@ -33,15 +31,14 @@
     sys.stdout.flush()
     try:
       byte_orig = os.path.getsize(filename)
-      re = repng(filename)
-      if re == 0:
-        byte_new = os.path.getsize(filename)
-        print "OK (%s => %s, %.2f%% saving)" % (hbytes(byte_orig), hbytes(byte_new), (byte_orig - byte_new)*100/float(byte_orig)),
-      elif re == 1:
-        print "Not a PNG image file",
+      repng(filename)
+      byte_new = os.path.getsize(filename)
+      print "OK (%s => %s, %.2f%% saving)" % (hbytes(byte_orig), hbytes(byte_new), (byte_orig - byte_new)*100/float(byte_orig)),
+      re = 0
     except:
       print "Failed",
       print "(%s: %s)" % (sys.exc_type, sys.exc_value),
       re = 1
-    print "[%s]" % re
+    finally:
+      print "[%s]" % re