<s> """<STR_LIT>""" <EOL> import httplib <EOL> import logging <EOL> import re <EOL> from google . appengine . api import apiproxy_stub_map <EOL> from google . appengine . api import datastore <EOL> from google . appengine . api import datastore_errors <EOL> from google . appengine . api . images import images_service_pb <EOL> BLOBIMAGE_URL_PATTERN = '<STR_LIT>' <EOL> _BLOB_SERVING_URL_KIND = '<STR_LIT>' <EOL> _DEFAULT_SERVING_SIZE = <NUM_LIT> <EOL> _SIZE_LIMIT = <NUM_LIT> <EOL> _OPTIONS_RE = re . compile ( r'<STR_LIT>' ) <EOL> _PATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> _MIME_TYPE_MAP = { images_service_pb . OutputSettings . JPEG : '<STR_LIT>' , <EOL> images_service_pb . OutputSettings . PNG : '<STR_LIT>' , <EOL> images_service_pb . OutputSettings . WEBP : '<STR_LIT>' } <EOL> def _get_images_stub ( ) : <EOL> return apiproxy_stub_map . apiproxy . GetStub ( '<STR_LIT>' ) <EOL> class Error ( Exception ) : <EOL> pass <EOL> class InvalidRequestError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class Application ( object ) : <EOL> """<STR_LIT>""" <EOL> def _transform_image ( self , blob_key , options ) : <EOL> """<STR_LIT>""" <EOL> resize , crop = self . _parse_options ( options ) <EOL> image_data = images_service_pb . ImageData ( ) <EOL> image_data . set_blob_key ( blob_key ) <EOL> image = _get_images_stub ( ) . _OpenImageData ( image_data ) <EOL> original_mime_type = image . format <EOL> width , height = image . size <EOL> if crop : <EOL> crop_xform = None <EOL> if width > height : <EOL> crop_xform = images_service_pb . Transform ( ) <EOL> delta = ( width - height ) / ( width * <NUM_LIT> ) <EOL> crop_xform . set_crop_left_x ( delta ) <EOL> crop_xform . set_crop_right_x ( <NUM_LIT:1.0> - delta ) <EOL> elif width < height : <EOL> crop_xform = images_service_pb . Transform ( ) <EOL> delta = ( height - width ) / ( height * <NUM_LIT> ) <EOL> top_delta = max ( <NUM_LIT:0.0> , delta - <NUM_LIT> ) <EOL> bottom_delta = <NUM_LIT:1.0> - ( <NUM_LIT> * delta ) + top_delta <EOL> crop_xform . set_crop_top_y ( top_delta ) <EOL> crop_xform . set_crop_bottom_y ( bottom_delta ) <EOL> if crop_xform : <EOL> image = _get_images_stub ( ) . _Crop ( image , crop_xform ) <EOL> if resize is None : <EOL> if width > _DEFAULT_SERVING_SIZE or height > _DEFAULT_SERVING_SIZE : <EOL> resize = _DEFAULT_SERVING_SIZE <EOL> if resize : <EOL> resize_xform = images_service_pb . Transform ( ) <EOL> resize_xform . set_width ( resize ) <EOL> resize_xform . set_height ( resize ) <EOL> image = _get_images_stub ( ) . _Resize ( image , resize_xform ) <EOL> output_settings = images_service_pb . OutputSettings ( ) <EOL> output_mime_type = images_service_pb . OutputSettings . JPEG <EOL> if original_mime_type in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> output_mime_type = images_service_pb . OutputSettings . PNG <EOL> output_settings . set_mime_type ( output_mime_type ) <EOL> return ( _get_images_stub ( ) . _EncodeImage ( image , output_settings ) , <EOL> _MIME_TYPE_MAP [ output_mime_type ] ) <EOL> def _parse_options ( self , options ) : <EOL> """<STR_LIT>""" <EOL> match = _OPTIONS_RE . search ( options ) <EOL> resize = None <EOL> crop = False <EOL> if match : <EOL> if match . group ( <NUM_LIT:1> ) : <EOL> resize = int ( match . group ( <NUM_LIT:1> ) ) <EOL> if match . group ( <NUM_LIT:2> ) : <EOL> crop = True <EOL> if resize and ( resize > _SIZE_LIMIT or resize < <NUM_LIT:0> ) : <EOL> logging . error ( '<STR_LIT>' , resize ) <EOL> raise InvalidRequestError ( ) <EOL> return ( resize , crop ) <EOL> def _parse_path ( self , path ) : <EOL> """<STR_LIT>""" <EOL> match = _PATH_RE . search ( path ) <EOL> if not match or not match . group ( <NUM_LIT:1> ) : <EOL> logging . error ( '<STR_LIT>' , path ) <EOL> raise InvalidRequestError ( ) <EOL> options = '<STR_LIT>' <EOL> blobkey = match . group ( <NUM_LIT:1> ) <EOL> if match . group ( <NUM_LIT:3> ) : <EOL> if match . group ( <NUM_LIT:2> ) : <EOL> blobkey += match . group ( <NUM_LIT:2> ) [ <NUM_LIT:1> : ] <EOL> options = match . group ( <NUM_LIT:3> ) <EOL> elif match . group ( <NUM_LIT:2> ) : <EOL> blobkey += match . group ( <NUM_LIT:2> ) <EOL> return ( blobkey , options ) <EOL> def serve_image ( self , environ , start_response ) : <EOL> """<STR_LIT>""" <EOL> blobkey , options = self . _parse_path ( environ [ '<STR_LIT>' ] ) <EOL> key = datastore . Key . from_path ( _BLOB_SERVING_URL_KIND , blobkey , namespace = '<STR_LIT>' ) <EOL> try : <EOL> datastore . Get ( key ) <EOL> except datastore_errors . EntityNotFoundError : <EOL> logging . error ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , blobkey ) <EOL> start_response ( '<STR_LIT>' % httplib . responses [ <NUM_LIT> ] , [ ] ) <EOL> return [ ] <EOL> image , mime_type = self . _transform_image ( blobkey , options ) <EOL> start_response ( '<STR_LIT>' , [ <EOL> ( '<STR_LIT:Content-Type>' , mime_type ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> return [ image ] <EOL> def __call__ ( self , environ , start_response ) : <EOL> if environ [ '<STR_LIT>' ] != '<STR_LIT:GET>' : <EOL> start_response ( '<STR_LIT>' % httplib . responses [ <NUM_LIT> ] , [ ] ) <EOL> return [ ] <EOL> try : <EOL> return self . serve_image ( environ , start_response ) <EOL> except InvalidRequestError : <EOL> start_response ( '<STR_LIT>' % httplib . responses [ <NUM_LIT> ] , [ ] ) <EOL> return [ ] </s>
