Error when attempting to generate image w/o source

This commit is contained in:
Matthew Tretter 2013-02-25 22:34:24 -05:00
parent 61aa1c32e7
commit 92a3c2688c
3 changed files with 24 additions and 1 deletions

View file

@ -13,6 +13,10 @@ class MissingGeneratorId(Exception):
pass
class MissingSource(ValueError):
pass
# Aliases for backwards compatibility
UnknownExtensionError = UnknownExtension
UnknownFormatError = UnknownFormat

View file

@ -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)

View file

@ -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()