From 92a3c2688cfa2a63687228765342422cd3ee362a Mon Sep 17 00:00:00 2001 From: Matthew Tretter Date: Mon, 25 Feb 2013 22:34:24 -0500 Subject: [PATCH] Error when attempting to generate image w/o source --- imagekit/exceptions.py | 4 ++++ imagekit/specs/__init__.py | 12 ++++++++++++ tests/test_specs.py | 9 ++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/imagekit/exceptions.py b/imagekit/exceptions.py index 0c46514..9aba018 100644 --- a/imagekit/exceptions.py +++ b/imagekit/exceptions.py @@ -13,6 +13,10 @@ class MissingGeneratorId(Exception): pass +class MissingSource(ValueError): + pass + + # Aliases for backwards compatibility UnknownExtensionError = UnknownExtension UnknownFormatError = UnknownFormat diff --git a/imagekit/specs/__init__.py b/imagekit/specs/__init__.py index a8fa742..c12acfa 100644 --- a/imagekit/specs/__init__.py +++ b/imagekit/specs/__init__.py @@ -4,6 +4,7 @@ from hashlib import md5 import pickle from ..cachefiles.backends import get_default_cachefile_backend from ..cachefiles.strategies import StrategyWrapper +from ..exceptions import MissingSource from ..processors import ProcessorPipeline from ..utils import open_image, img_to_fobj, get_by_qname from ..registry import generator_registry, register @@ -41,6 +42,13 @@ class BaseImageSpec(object): def generate(self): raise NotImplementedError + MissingSource = MissingSource + """ + Raised when an operation requiring a source is attempted on a spec that has + no source. + + """ + class ImageSpec(BaseImageSpec): """ @@ -116,6 +124,10 @@ class ImageSpec(BaseImageSpec): ])).hexdigest() def generate(self): + if not self.source: + raise MissingSource("The spec '%s' has no source file associated" + " with it." % self) + # TODO: Move into a generator base class # TODO: Factor out a generate_image function so you can create a generator and only override the PIL.Image creating part. (The tricky part is how to deal with original_format since generator base class won't have one.) img = open_image(self.source) diff --git a/tests/test_specs.py b/tests/test_specs.py index 6c8c4b9..5db081f 100644 --- a/tests/test_specs.py +++ b/tests/test_specs.py @@ -1,5 +1,5 @@ from imagekit.cachefiles import ImageCacheFile -from nose.tools import assert_false +from nose.tools import assert_false, raises from .imagegenerators import TestSpec @@ -10,3 +10,10 @@ def test_no_source(): spec = TestSpec(source=None) file = ImageCacheFile(spec) assert_false(bool(file)) + + +@raises(TestSpec.MissingSource) +def test_no_source_error(): + spec = TestSpec(source=None) + file = ImageCacheFile(spec) + file.generate()