Created
January 22, 2015 17:49
-
-
Save cynthia/6ca32793719984c8c2e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- appicon.py 2015-01-23 02:21:33.000000000 +0900 | |
+++ appicon2.py 2015-01-23 02:47:26.000000000 +0900 | |
@@ -4,6 +4,7 @@ | |
import os | |
import sys | |
from wand.image import Image | |
+from wand.exceptions import BlobError, CoderError, MissingDelegateError | |
DIMENSIONS = { | |
'OSX': [ | |
@@ -52,6 +53,8 @@ | |
], | |
} | |
+class PathReadOnlyError(Exception): | |
+ pass | |
def create(src_path): | |
src_path = os.path.abspath(src_path) | |
@@ -63,19 +66,38 @@ | |
else: | |
width, height = size | |
- dest_path = os.path.join('app-icons', t, name) | |
- dir_path = os.path.dirname(dest_path) | |
- if not os.path.exists(dir_path): | |
- os.makedirs(dir_path) | |
- | |
- with Image(filename=src_path) as src_im: | |
- src_im.strip() | |
- src_im.resize(width, height) | |
- src_im.save(filename=dest_path) | |
- | |
- print('created {}'.format(dest_path)) | |
- | |
- | |
+ try: | |
+ dest_path = os.path.join('app-icons', t, name) | |
+ dir_path = os.path.dirname(dest_path) | |
+ if not os.path.exists(dir_path): | |
+ os.makedirs(dir_path) | |
+ if not os.access(dir_path, os.W_OK): | |
+ raise PathReadOnlyError("Unable to write to path %s" % dir_path) | |
+ except OSError: | |
+ print("Unable to create output directory %s" % dir_path) | |
+ return 1 | |
+ except PathReadOnlyError: | |
+ print("Output directory %s is not writeable" % dir_path) | |
+ return 1 | |
+ | |
+ try: | |
+ with Image(filename=src_path) as src_im: | |
+ src_im.strip() | |
+ src_im.resize(width, height) | |
+ src_im.save(filename=dest_path) | |
+ print('Created {}'.format(dest_path)) | |
+ except BlobError: | |
+ print('Input file %s does not exist.' % src_path) | |
+ return 1 | |
+ except MissingDelegateError: | |
+ print('Input file %s is not a supported format.' % src_path) | |
+ return 1 | |
+ except CoderError as e: | |
+ print('Unable to write to destination file %s' % dest_path) | |
+ except Exception as e: | |
+ print(e) | |
+ | |
+ return 0 | |
if __name__ == '__main__': | |
try: | |
@@ -84,5 +106,5 @@ | |
print('usage> {} <image-path>'.format(sys.argv[0]), file=sys.stderr) | |
sys.exit(1) | |
- create(src_path) | |
+ sys.exit(create(src_path)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment