<s> from os . path import splitext <EOL> from thumbor . ext . filters import _alpha <EOL> from thumbor . filters import BaseFilter , filter_method <EOL> from thumbor . loaders import LoaderResult <EOL> import tornado . gen <EOL> import math <EOL> class Filter ( BaseFilter ) : <EOL> regex = r'<STR_LIT>' <EOL> def on_image_ready ( self , buffer ) : <EOL> self . watermark_engine . load ( buffer , self . extension ) <EOL> self . watermark_engine . enable_alpha ( ) <EOL> mode , data = self . watermark_engine . image_data_as_rgb ( ) <EOL> imgdata = _alpha . apply ( mode , <EOL> self . alpha , <EOL> data ) <EOL> self . watermark_engine . set_image_data ( imgdata ) <EOL> mos_x = self . x == '<STR_LIT>' <EOL> mos_y = self . y == '<STR_LIT>' <EOL> center_x = self . x == '<STR_LIT>' <EOL> center_y = self . y == '<STR_LIT>' <EOL> if not center_x and not mos_x : <EOL> inv_x = self . x [ <NUM_LIT:0> ] == '<STR_LIT:->' <EOL> x = int ( self . x ) <EOL> if not center_y and not mos_y : <EOL> inv_y = self . y [ <NUM_LIT:0> ] == '<STR_LIT:->' <EOL> y = int ( self . y ) <EOL> sz = self . engine . size <EOL> watermark_sz = self . watermark_engine . size <EOL> if not mos_x : <EOL> repeat_x = ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> if center_x : <EOL> x = ( sz [ <NUM_LIT:0> ] - watermark_sz [ <NUM_LIT:0> ] ) / <NUM_LIT:2> <EOL> elif inv_x : <EOL> x = ( sz [ <NUM_LIT:0> ] - watermark_sz [ <NUM_LIT:0> ] ) + x <EOL> else : <EOL> repeat_x = divmod ( sz [ <NUM_LIT:0> ] , watermark_sz [ <NUM_LIT:0> ] ) <EOL> if sz [ <NUM_LIT:0> ] * <NUM_LIT:1.0> / watermark_sz [ <NUM_LIT:0> ] < <NUM_LIT:2> : <EOL> repeat_x = ( math . ceil ( sz [ <NUM_LIT:0> ] * <NUM_LIT:1.0> / watermark_sz [ <NUM_LIT:0> ] ) , <NUM_LIT:10> ) <EOL> space_x = <NUM_LIT:10> <EOL> if not mos_y : <EOL> repeat_y = ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> if center_y : <EOL> y = ( sz [ <NUM_LIT:1> ] - watermark_sz [ <NUM_LIT:1> ] ) / <NUM_LIT:2> <EOL> elif inv_y : <EOL> y = ( sz [ <NUM_LIT:1> ] - watermark_sz [ <NUM_LIT:1> ] ) + y <EOL> else : <EOL> repeat_y = divmod ( sz [ <NUM_LIT:1> ] , watermark_sz [ <NUM_LIT:1> ] ) <EOL> if sz [ <NUM_LIT:1> ] * <NUM_LIT:1.0> / watermark_sz [ <NUM_LIT:1> ] < <NUM_LIT:2> : <EOL> repeat_y = ( math . ceil ( sz [ <NUM_LIT:1> ] * <NUM_LIT:1.0> / watermark_sz [ <NUM_LIT:1> ] ) , <NUM_LIT:10> ) <EOL> space_y = <NUM_LIT:10> <EOL> if not mos_x and not mos_y : <EOL> self . engine . paste ( self . watermark_engine , ( x , y ) , merge = True ) <EOL> elif mos_x and mos_y : <EOL> if ( repeat_x [ <NUM_LIT:0> ] * repeat_y [ <NUM_LIT:0> ] ) > <NUM_LIT:100> : <EOL> tmpRepeatX = min ( <NUM_LIT:6> , repeat_x [ <NUM_LIT:0> ] ) <EOL> tmpRepeatY = min ( <NUM_LIT:6> , repeat_y [ <NUM_LIT:0> ] ) <EOL> repeat_x = ( tmpRepeatX , sz [ <NUM_LIT:0> ] - tmpRepeatX * watermark_sz [ <NUM_LIT:0> ] ) <EOL> repeat_y = ( tmpRepeatY , sz [ <NUM_LIT:1> ] - tmpRepeatY * watermark_sz [ <NUM_LIT:1> ] ) <EOL> space_x = repeat_x [ <NUM_LIT:1> ] / ( max ( repeat_x [ <NUM_LIT:0> ] , <NUM_LIT:2> ) - <NUM_LIT:1> ) <EOL> space_y = repeat_y [ <NUM_LIT:1> ] / ( max ( repeat_y [ <NUM_LIT:0> ] , <NUM_LIT:2> ) - <NUM_LIT:1> ) <EOL> for i in range ( int ( repeat_x [ <NUM_LIT:0> ] ) ) : <EOL> x = i * space_x + i * watermark_sz [ <NUM_LIT:0> ] <EOL> for j in range ( int ( repeat_y [ <NUM_LIT:0> ] ) ) : <EOL> y = j * space_y + j * watermark_sz [ <NUM_LIT:1> ] <EOL> self . engine . paste ( self . watermark_engine , ( x , y ) , merge = True ) <EOL> elif mos_x : <EOL> space_x = repeat_x [ <NUM_LIT:1> ] / ( max ( repeat_x [ <NUM_LIT:0> ] , <NUM_LIT:2> ) - <NUM_LIT:1> ) <EOL> for i in range ( int ( repeat_x [ <NUM_LIT:0> ] ) ) : <EOL> x = i * space_x + i * watermark_sz [ <NUM_LIT:0> ] <EOL> self . engine . paste ( self . watermark_engine , ( x , y ) , merge = True ) <EOL> else : <EOL> space_y = repeat_y [ <NUM_LIT:1> ] / ( max ( repeat_y [ <NUM_LIT:0> ] , <NUM_LIT:2> ) - <NUM_LIT:1> ) <EOL> for j in range ( int ( repeat_y [ <NUM_LIT:0> ] ) ) : <EOL> y = j * space_y + j * watermark_sz [ <NUM_LIT:1> ] <EOL> self . engine . paste ( self . watermark_engine , ( x , y ) , merge = True ) <EOL> self . callback ( ) <EOL> def on_fetch_done ( self , result ) : <EOL> if isinstance ( result , LoaderResult ) : <EOL> buffer = result . buffer <EOL> else : <EOL> buffer = result <EOL> self . watermark_engine . load ( buffer , self . extension ) <EOL> self . storage . put ( self . url , self . watermark_engine . read ( ) ) <EOL> self . storage . put_crypto ( self . url ) <EOL> self . on_image_ready ( buffer ) <EOL> @ filter_method ( <EOL> BaseFilter . String , <EOL> r'<STR_LIT>' , <EOL> r'<STR_LIT>' , <EOL> BaseFilter . PositiveNumber , <EOL> async = True <EOL> ) <EOL> @ tornado . gen . coroutine <EOL> def watermark ( self , callback , url , x , y , alpha ) : <EOL> self . url = url <EOL> self . x = x <EOL> self . y = y <EOL> self . alpha = alpha <EOL> self . callback = callback <EOL> self . extension = splitext ( self . url ) [ - <NUM_LIT:1> ] . lower ( ) <EOL> self . watermark_engine = self . context . modules . engine . __class__ ( self . context ) <EOL> self . storage = self . context . modules . storage <EOL> buffer = yield tornado . gen . maybe_future ( self . storage . get ( self . url ) ) <EOL> if buffer is not None : <EOL> self . on_image_ready ( buffer ) <EOL> else : <EOL> self . context . modules . loader . load ( self . context , self . url , self . on_fetch_done ) </s>
