<s> from thumbor . context import Context , ServerParameters <EOL> from thumbor . config import Config <EOL> from thumbor . importer import Importer <EOL> from pyvows import Vows , expect <EOL> import thumbor . engines . pil as PIL <EOL> from os . path import join , abspath , dirname <EOL> ctx = Vows . Context <EOL> FIXTURES_FOLDER = join ( abspath ( dirname ( __file__ ) ) , '<STR_LIT>' ) <EOL> class MockImage : <EOL> def __init__ ( self , mode , width , height ) : <EOL> self . mode = mode <EOL> self . width = width <EOL> self . height = height <EOL> self . calls = [ ] <EOL> def convert ( self , mode ) : <EOL> self . calls . append ( '<STR_LIT>' ) <EOL> self . mode = mode <EOL> return self <EOL> def resize ( self , size , filter ) : <EOL> self . calls . append ( '<STR_LIT>' ) <EOL> self . width = size [ <NUM_LIT:0> ] <EOL> self . height = size [ <NUM_LIT:1> ] <EOL> return self <EOL> @ Vows . batch <EOL> class PilEngineVows ( ctx ) : <EOL> class ReadingJpegImage ( ctx ) : <EOL> def topic ( self ) : <EOL> config = Config ( ) <EOL> server = ServerParameters ( <EOL> <NUM_LIT> , '<STR_LIT:localhost>' , '<STR_LIT>' , None , '<STR_LIT:info>' , None <EOL> ) <EOL> context = Context ( server , config , Importer ( config ) ) <EOL> with open ( "<STR_LIT>" % FIXTURES_FOLDER , "<STR_LIT:rb>" ) as f : <EOL> buffer = f . read ( ) <EOL> engine = PIL . Engine ( context = context ) <EOL> engine . load ( buffer , '<STR_LIT>' ) <EOL> return engine <EOL> def should_store_subsampling_in_engine ( self , engine ) : <EOL> expect ( engine . subsampling ) . to_equal ( <NUM_LIT:0> ) <EOL> def should_store_quantization_in_engine ( self , engine ) : <EOL> expect ( len ( engine . qtables ) ) . to_equal ( <NUM_LIT:2> ) <EOL> class WritingJpegImageWithOriginalJpegSettings ( ctx ) : <EOL> def topic ( self ) : <EOL> config = Config ( ) <EOL> server = ServerParameters ( <EOL> <NUM_LIT> , '<STR_LIT:localhost>' , '<STR_LIT>' , None , '<STR_LIT:info>' , None <EOL> ) <EOL> config . PILLOW_JPEG_SUBSAMPLING = '<STR_LIT>' <EOL> config . PILLOW_JPEG_QTABLES = '<STR_LIT>' <EOL> context = Context ( server , config , Importer ( config ) ) <EOL> with open ( "<STR_LIT>" % FIXTURES_FOLDER , "<STR_LIT:rb>" ) as f : <EOL> buffer = f . read ( ) <EOL> engine = PIL . Engine ( context = context ) <EOL> engine . load ( buffer , '<STR_LIT>' ) <EOL> engine . read ( '<STR_LIT>' , None ) <EOL> return engine . image <EOL> def should_save_with_stored_subsampling ( self , image ) : <EOL> expect ( image . encoderinfo . get ( '<STR_LIT>' ) ) . to_equal ( <NUM_LIT:0> ) <EOL> def should_save_with_stored_qtables ( self , image ) : <EOL> expect ( len ( image . encoderinfo . get ( '<STR_LIT>' ) ) ) . to_equal ( <NUM_LIT:2> ) <EOL> class WritingJpegImageWithExplicitJpegSettings ( ctx ) : <EOL> def topic ( self ) : <EOL> config = Config ( ) <EOL> server = ServerParameters ( <EOL> <NUM_LIT> , '<STR_LIT:localhost>' , '<STR_LIT>' , None , '<STR_LIT:info>' , None <EOL> ) <EOL> config . PILLOW_JPEG_SUBSAMPLING = <NUM_LIT:2> <EOL> config . PILLOW_JPEG_QTABLES = '<STR_LIT>' <EOL> context = Context ( server , config , Importer ( config ) ) <EOL> with open ( "<STR_LIT>" % FIXTURES_FOLDER , "<STR_LIT:rb>" ) as f : <EOL> buffer = f . read ( ) <EOL> engine = PIL . Engine ( context = context ) <EOL> engine . load ( buffer , '<STR_LIT>' ) <EOL> engine . read ( '<STR_LIT>' , None ) <EOL> return engine . image <EOL> def should_save_with_explicit_subsampling ( self , image ) : <EOL> expect ( image . encoderinfo . get ( '<STR_LIT>' ) ) . to_equal ( <NUM_LIT:2> ) <EOL> def should_save_with_explicit_qtables ( self , image ) : <EOL> expect ( image . encoderinfo . get ( '<STR_LIT>' ) ) . to_equal ( '<STR_LIT>' ) <EOL> class ResizedPaletteImage ( ctx ) : <EOL> def topic ( self ) : <EOL> engine = PIL . Engine ( None ) <EOL> engine . image = MockImage ( '<STR_LIT:P>' , <NUM_LIT> , <NUM_LIT> ) <EOL> engine . resize ( <NUM_LIT> , <NUM_LIT> ) <EOL> return engine . image <EOL> def should_convert_p_to_rgba ( self , image ) : <EOL> expect ( image . mode ) . to_equal ( '<STR_LIT>' ) <EOL> expect ( ( image . width , image . height ) ) . to_equal ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> expect ( image . calls . index ( '<STR_LIT>' ) < image . calls . index ( '<STR_LIT>' ) ) . to_be_true ( ) <EOL> class ResizedNonPaletteImage ( ctx ) : <EOL> def topic ( self ) : <EOL> engine = PIL . Engine ( None ) <EOL> engine . image = MockImage ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ) <EOL> engine . resize ( <NUM_LIT> , <NUM_LIT> ) <EOL> return engine . image <EOL> def should_not_convert_non_palette_images ( self , image ) : <EOL> expect ( image . mode ) . to_equal ( '<STR_LIT>' ) <EOL> expect ( ( image . width , image . height ) ) . to_equal ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> expect ( image . calls ) . Not . to_include ( [ '<STR_LIT>' ] ) <EOL> class ShouldRaiseIfFiltersNotAvailable ( ctx ) : <EOL> @ Vows . capture_error <EOL> def topic ( self ) : <EOL> FILTERS_AVAILABLE_BAK = PIL . FILTERS_AVAILABLE <EOL> PIL . FILTERS_AVAILABLE = False <EOL> engine = PIL . Engine ( None ) <EOL> try : <EOL> return engine . paste ( None , None , True ) <EOL> finally : <EOL> PIL . FILTERS_AVAILABLE = FILTERS_AVAILABLE_BAK <EOL> def should_be_an_error ( self , topic ) : <EOL> expect ( topic ) . to_be_an_error ( ) <EOL> expect ( topic ) . to_be_an_error_like ( RuntimeError ) <EOL> expected = '<STR_LIT>' + '<STR_LIT>' <EOL> expect ( topic ) . to_have_an_error_message_of ( expected ) </s>
