def __call__(self, *args, **kw): if len(args) < 1: cherrypy.response.status = 404 return "not found" path = "/".join(args) image = jpeg(path=path) cache_full_path = None if self.should_cache: cache_full_path = self.get_cache_path(path) if self.fs.exists(cache_full_path): return static.serve_file(cache_full_path, 'image/jpeg') if len(args) >= 3 and args[0] == 'crop': proportion = re.match(r'(?P<width>\d+)x(?P<height>\d+)', args[1]) if proportion: width = int(proportion.group('width')) height = int(proportion.group('height')) image = picture(path="/".join(args[2:]), width=width, height=height) if self.should_cache: dir_path = self.fs.dirname(cache_full_path) self.fs.mkdir(dir_path) img_file = self.fs.open_raw(cache_full_path, 'w') img_file.write(image) img_file.close() return image
def test_jpeg_success(): mox = Mox() path = '/path/to/mocked/img.jpg' mox.StubOutWithMock(image, 'Image') mox.StubOutWithMock(image, 'StringIO') stringio_mock = mox.CreateMockAnything() return_mock = mox.CreateMockAnything() img_mock = mox.CreateMockAnything() stringio_mock.getvalue().AndReturn(return_mock) image.StringIO.StringIO().AndReturn(stringio_mock) image.Image.open(path).AndReturn(img_mock) img_mock.save(stringio_mock, "JPEG", quality=100) cherrypy.config['image.dir'] = path mox.ReplayAll() return_got = image.jpeg(path) assert return_got == return_mock, 'The return of image.jpeg() should be %r, got %r' % (return_mock, return_got) mime = cherrypy.response.headers['Content-type'] assert mime == 'image/jpeg', 'The response header "Content-type" should be image/jpeg, but got %r' % mime mox.VerifyAll() del cherrypy.config['image.dir']
def test_jpeg_success(): mox = Mox() path = '/path/to/mocked/img.jpg' mox.StubOutWithMock(image, 'Image') mox.StubOutWithMock(image, 'StringIO') stringio_mock = mox.CreateMockAnything() return_mock = mox.CreateMockAnything() img_mock = mox.CreateMockAnything() stringio_mock.getvalue().AndReturn(return_mock) image.StringIO.StringIO().AndReturn(stringio_mock) image.Image.open(path).AndReturn(img_mock) img_mock.save(stringio_mock, "JPEG", quality=100) cherrypy.config['image.dir'] = path mox.ReplayAll() return_got = image.jpeg(path) assert return_got == return_mock, 'The return of image.jpeg() should be %r, got %r' % ( return_mock, return_got) mime = cherrypy.response.headers['Content-type'] assert mime == 'image/jpeg', 'The response header "Content-type" should be image/jpeg, but got %r' % mime mox.VerifyAll() del cherrypy.config['image.dir']
def test_jpeg_return_string_when_file_not_found(): filename = 'foo-file.jpg' path = join('bazbar', filename) mox = Mox() mox.StubOutWithMock(image, 'Image') image.Image.open(path).AndRaise(IOError('File not found: foo-file.jpg')) mox.ReplayAll() ret = image.jpeg(filename, base_path='bazbar') assert isinstance(ret, unicode), 'The return value should be unicode, but is %r' % type(ret) assert ret == 'File not found: foo-file.jpg', 'Wrong error description: %r' % ret mox.VerifyAll()
def test_jpeg_return_string_when_file_not_found(): filename = 'foo-file.jpg' path = join('bazbar', filename) mox = Mox() mox.StubOutWithMock(image, 'Image') image.Image.open(path).AndRaise(IOError('File not found: foo-file.jpg')) mox.ReplayAll() ret = image.jpeg(filename, base_path='bazbar') assert isinstance( ret, unicode), 'The return value should be unicode, but is %r' % type(ret) assert ret == 'File not found: foo-file.jpg', 'Wrong error description: %r' % ret mox.VerifyAll()
def test_jpeg(): cherrypy.config['image.dir'] = images got = image.jpeg('2823371.jpg') assert isinstance(got, basestring), 'Expected a string, got %r' % got