<s> """<STR_LIT>""" <EOL> import base64 <EOL> import email <EOL> from . google_imports import api_blobstore as blobstore <EOL> from . google_imports import ext_blobstore <EOL> from . import model <EOL> from . import tasklets <EOL> __all__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> Error = blobstore . Error <EOL> InternalError = blobstore . InternalError <EOL> BlobFetchSizeTooLargeError = blobstore . BlobFetchSizeTooLargeError <EOL> BlobNotFoundError = blobstore . BlobNotFoundError <EOL> _CreationFormatError = blobstore . _CreationFormatError <EOL> DataIndexOutOfRangeError = blobstore . DataIndexOutOfRangeError <EOL> PermissionDeniedError = blobstore . PermissionDeniedError <EOL> BlobInfoParseError = ext_blobstore . BlobInfoParseError <EOL> BlobKey = blobstore . BlobKey <EOL> BLOB_INFO_KIND = blobstore . BLOB_INFO_KIND <EOL> BLOB_MIGRATION_KIND = blobstore . BLOB_MIGRATION_KIND <EOL> BLOB_KEY_HEADER = blobstore . BLOB_KEY_HEADER <EOL> BLOB_RANGE_HEADER = blobstore . BLOB_RANGE_HEADER <EOL> MAX_BLOB_FETCH_SIZE = blobstore . MAX_BLOB_FETCH_SIZE <EOL> UPLOAD_INFO_CREATION_HEADER = blobstore . UPLOAD_INFO_CREATION_HEADER <EOL> BlobKeyProperty = model . BlobKeyProperty <EOL> class BlobInfo ( model . Model ) : <EOL> """<STR_LIT>""" <EOL> _use_cache = False <EOL> _use_memcache = False <EOL> content_type = model . StringProperty ( ) <EOL> creation = model . DateTimeProperty ( ) <EOL> filename = model . StringProperty ( ) <EOL> size = model . IntegerProperty ( ) <EOL> md5_hash = model . StringProperty ( ) <EOL> @ classmethod <EOL> def _get_kind ( cls ) : <EOL> """<STR_LIT>""" <EOL> return BLOB_INFO_KIND <EOL> @ classmethod <EOL> def get ( cls , blob_key , ** ctx_options ) : <EOL> """<STR_LIT>""" <EOL> fut = cls . get_async ( blob_key , ** ctx_options ) <EOL> return fut . get_result ( ) <EOL> @ classmethod <EOL> def get_async ( cls , blob_key , ** ctx_options ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( blob_key , ( BlobKey , basestring ) ) : <EOL> raise TypeError ( '<STR_LIT>' % ( blob_key , ) ) <EOL> if '<STR_LIT>' in ctx_options : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return cls . get_by_id_async ( str ( blob_key ) , ** ctx_options ) <EOL> @ classmethod <EOL> def get_multi ( cls , blob_keys , ** ctx_options ) : <EOL> """<STR_LIT>""" <EOL> futs = cls . get_multi_async ( blob_keys , ** ctx_options ) <EOL> return [ fut . get_result ( ) for fut in futs ] <EOL> @ classmethod <EOL> def get_multi_async ( cls , blob_keys , ** ctx_options ) : <EOL> """<STR_LIT>""" <EOL> for blob_key in blob_keys : <EOL> if not isinstance ( blob_key , ( BlobKey , basestring ) ) : <EOL> raise TypeError ( '<STR_LIT>' % ( blob_key , ) ) <EOL> if '<STR_LIT>' in ctx_options : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> blob_key_strs = map ( str , blob_keys ) <EOL> keys = [ model . Key ( BLOB_INFO_KIND , id ) for id in blob_key_strs ] <EOL> return model . get_multi_async ( keys , ** ctx_options ) <EOL> def _put_async ( self , ** ctx_options ) : <EOL> """<STR_LIT>""" <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> put_async = _put_async <EOL> def key ( self ) : <EOL> """<STR_LIT>""" <EOL> return BlobKey ( self . _key . id ( ) ) <EOL> def delete ( self , ** options ) : <EOL> """<STR_LIT>""" <EOL> fut = delete_async ( self . key ( ) , ** options ) <EOL> fut . get_result ( ) <EOL> def delete_async ( self , ** options ) : <EOL> """<STR_LIT>""" <EOL> return delete_async ( self . key ( ) , ** options ) <EOL> def open ( self , * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> return BlobReader ( self , * args , ** kwds ) <EOL> get = BlobInfo . get <EOL> get_async = BlobInfo . get_async <EOL> get_multi = BlobInfo . get_multi <EOL> get_multi_async = BlobInfo . get_multi_async <EOL> def delete ( blob_key , ** options ) : <EOL> """<STR_LIT>""" <EOL> fut = delete_async ( blob_key , ** options ) <EOL> return fut . get_result ( ) <EOL> @ tasklets . tasklet <EOL> def delete_async ( blob_key , ** options ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( blob_key , ( basestring , BlobKey ) ) : <EOL> raise TypeError ( '<STR_LIT>' % ( blob_key , ) ) <EOL> rpc = blobstore . create_rpc ( ** options ) <EOL> yield blobstore . delete_async ( blob_key , rpc = rpc ) <EOL> def delete_multi ( blob_keys , ** options ) : <EOL> """<STR_LIT>""" <EOL> fut = delete_multi_async ( blob_keys , ** options ) <EOL> fut . get_result ( ) <EOL> @ tasklets . tasklet <EOL> def delete_multi_async ( blob_keys , ** options ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( blob_keys , ( basestring , BlobKey ) ) : <EOL> raise TypeError ( '<STR_LIT>' % ( blob_key , ) ) <EOL> rpc = blobstore . create_rpc ( ** options ) <EOL> yield blobstore . delete_async ( blob_keys , rpc = rpc ) <EOL> def create_upload_url ( success_path , <EOL> max_bytes_per_blob = None , <EOL> max_bytes_total = None , <EOL> ** options ) : <EOL> """<STR_LIT>""" <EOL> fut = create_upload_url_async ( success_path , <EOL> max_bytes_per_blob = max_bytes_per_blob , <EOL> max_bytes_total = max_bytes_total , <EOL> ** options ) <EOL> return fut . get_result ( ) <EOL> @ tasklets . tasklet <EOL> def create_upload_url_async ( success_path , <EOL> max_bytes_per_blob = None , <EOL> max_bytes_total = None , <EOL> ** options ) : <EOL> """<STR_LIT>""" <EOL> rpc = blobstore . create_rpc ( ** options ) <EOL> rpc = blobstore . create_upload_url_async ( success_path , <EOL> max_bytes_per_blob = max_bytes_per_blob , <EOL> max_bytes_total = max_bytes_total , <EOL> rpc = rpc ) <EOL> result = yield rpc <EOL> raise tasklets . Return ( result ) <EOL> def parse_blob_info ( field_storage ) : <EOL> """<STR_LIT>""" <EOL> if field_storage is None : <EOL> return None <EOL> field_name = field_storage . name <EOL> def get_value ( dct , name ) : <EOL> value = dct . get ( name , None ) <EOL> if value is None : <EOL> raise BlobInfoParseError ( <EOL> '<STR_LIT>' % ( field_name , name ) ) <EOL> return value <EOL> filename = get_value ( field_storage . disposition_options , '<STR_LIT:filename>' ) <EOL> blob_key_str = get_value ( field_storage . type_options , '<STR_LIT>' ) <EOL> blob_key = BlobKey ( blob_key_str ) <EOL> upload_content = email . message_from_file ( field_storage . file ) <EOL> content_type = get_value ( upload_content , '<STR_LIT>' ) <EOL> size = get_value ( upload_content , '<STR_LIT>' ) <EOL> creation_string = get_value ( upload_content , UPLOAD_INFO_CREATION_HEADER ) <EOL> md5_hash_encoded = get_value ( upload_content , '<STR_LIT>' ) <EOL> md5_hash = base64 . urlsafe_b64decode ( md5_hash_encoded ) <EOL> try : <EOL> size = int ( size ) <EOL> except ( TypeError , ValueError ) : <EOL> raise BlobInfoParseError ( <EOL> '<STR_LIT>' % ( size , field_name ) ) <EOL> try : <EOL> creation = blobstore . _parse_creation ( creation_string , field_name ) <EOL> except blobstore . _CreationFormatError , err : <EOL> raise BlobInfoParseError ( str ( err ) ) <EOL> return BlobInfo ( id = blob_key_str , <EOL> content_type = content_type , <EOL> creation = creation , <EOL> filename = filename , <EOL> size = size , <EOL> md5_hash = md5_hash , <EOL> ) <EOL> def fetch_data ( blob , start_index , end_index , ** options ) : <EOL> """<STR_LIT>""" <EOL> fut = fetch_data_async ( blob , start_index , end_index , ** options ) <EOL> return fut . get_result ( ) <EOL> @ tasklets . tasklet <EOL> def fetch_data_async ( blob , start_index , end_index , ** options ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( blob , BlobInfo ) : <EOL> blob = blob . key ( ) <EOL> rpc = blobstore . create_rpc ( ** options ) <EOL> rpc = blobstore . fetch_data_async ( blob , start_index , end_index , rpc = rpc ) <EOL> result = yield rpc <EOL> raise tasklets . Return ( result ) <EOL> class BlobReader ( ext_blobstore . BlobReader ) : <EOL> """<STR_LIT>""" <EOL> def __fill_buffer ( self , size = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> read_size = min ( max ( size , self . __buffer_size ) , MAX_BLOB_FETCH_SIZE ) <EOL> self . __buffer = fetch_data ( self . __blob_key , self . __position , <EOL> self . __position + read_size - <NUM_LIT:1> ) <EOL> self . __buffer_position = <NUM_LIT:0> <EOL> self . __eof = len ( self . __buffer ) < read_size <EOL> @ property <EOL> def blob_info ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . __blob_info : <EOL> self . __blob_info = BlobInfo . get ( self . __blob_key ) <EOL> return self . __blob_info </s>
