<s> """<STR_LIT>""" <EOL> import cgi <EOL> import email <EOL> import os <EOL> from google . appengine . api import datastore <EOL> from google . appengine . api import datastore_errors <EOL> from google . appengine . api import datastore_types <EOL> from google . appengine . api . blobstore import blobstore <EOL> from google . appengine . ext import db <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> Error = blobstore . Error <EOL> InternalError = blobstore . InternalError <EOL> BlobFetchSizeTooLargeError = blobstore . BlobFetchSizeTooLargeError <EOL> BlobNotFoundError = blobstore . BlobNotFoundError <EOL> _CreationFormatError = blobstore . _CreationFormatError <EOL> DataIndexOutOfRangeError = blobstore . DataIndexOutOfRangeError <EOL> BlobKey = blobstore . BlobKey <EOL> create_upload_url = blobstore . create_upload_url <EOL> delete = blobstore . delete <EOL> class BlobInfoParseError ( Error ) : <EOL> """<STR_LIT>""" <EOL> BLOB_INFO_KIND = blobstore . BLOB_INFO_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> class _GqlQuery ( db . GqlQuery ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , query_string , model_class , * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . ext import gql <EOL> app = kwds . pop ( '<STR_LIT>' , None ) <EOL> self . _proto_query = gql . GQL ( query_string , _app = app , namespace = '<STR_LIT>' ) <EOL> super ( db . GqlQuery , self ) . __init__ ( model_class , namespace = '<STR_LIT>' ) <EOL> self . bind ( * args , ** kwds ) <EOL> class BlobInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> _unindexed_properties = frozenset ( ) <EOL> @ property <EOL> def content_type ( self ) : <EOL> return self . __get_value ( '<STR_LIT>' ) <EOL> @ property <EOL> def creation ( self ) : <EOL> return self . __get_value ( '<STR_LIT>' ) <EOL> @ property <EOL> def filename ( self ) : <EOL> return self . __get_value ( '<STR_LIT:filename>' ) <EOL> @ property <EOL> def size ( self ) : <EOL> return self . __get_value ( '<STR_LIT:size>' ) <EOL> def __init__ ( self , entity_or_blob_key , _values = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( entity_or_blob_key , datastore . Entity ) : <EOL> self . __entity = entity_or_blob_key <EOL> self . __key = BlobKey ( entity_or_blob_key . key ( ) . name ( ) ) <EOL> elif isinstance ( entity_or_blob_key , BlobKey ) : <EOL> self . __entity = _values <EOL> self . __key = entity_or_blob_key <EOL> else : <EOL> TypeError ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def from_entity ( cls , entity ) : <EOL> """<STR_LIT>""" <EOL> return BlobInfo ( entity ) <EOL> @ classmethod <EOL> def properties ( cls ) : <EOL> """<STR_LIT>""" <EOL> return set ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:filename>' , '<STR_LIT:size>' ) ) <EOL> def __get_value ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if self . __entity is None : <EOL> self . __entity = datastore . Get ( <EOL> datastore_types . Key . from_path ( <EOL> self . kind ( ) , str ( self . __key ) , namespace = '<STR_LIT>' ) ) <EOL> try : <EOL> return self . __entity [ name ] <EOL> except KeyError : <EOL> raise AttributeError ( name ) <EOL> def key ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __key <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> delete ( self . key ( ) ) <EOL> @ classmethod <EOL> def get ( cls , blob_keys ) : <EOL> """<STR_LIT>""" <EOL> blob_keys = cls . __normalize_and_convert_keys ( blob_keys ) <EOL> try : <EOL> entities = datastore . Get ( blob_keys ) <EOL> except datastore_errors . EntityNotFoundError : <EOL> return None <EOL> if isinstance ( entities , datastore . Entity ) : <EOL> return BlobInfo ( entities ) <EOL> else : <EOL> references = [ ] <EOL> for entity in entities : <EOL> if entity is not None : <EOL> references . append ( BlobInfo ( entity ) ) <EOL> else : <EOL> references . append ( None ) <EOL> return references <EOL> @ classmethod <EOL> def all ( cls ) : <EOL> """<STR_LIT>""" <EOL> return db . Query ( model_class = cls , namespace = '<STR_LIT>' ) <EOL> @ classmethod <EOL> def __factory_for_kind ( cls , kind ) : <EOL> if kind == BLOB_INFO_KIND : <EOL> return BlobInfo <EOL> raise ValueError ( '<STR_LIT>' % kind ) <EOL> @ classmethod <EOL> def gql ( cls , query_string , * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> return _GqlQuery ( '<STR_LIT>' <EOL> % ( cls . kind ( ) , query_string ) , <EOL> cls , <EOL> * args , <EOL> ** kwds ) <EOL> @ classmethod <EOL> def kind ( self ) : <EOL> """<STR_LIT>""" <EOL> return BLOB_INFO_KIND <EOL> @ classmethod <EOL> def __normalize_and_convert_keys ( cls , keys ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( keys , ( list , tuple ) ) : <EOL> multiple = True <EOL> keys = list ( keys ) <EOL> else : <EOL> multiple = False <EOL> keys = [ keys ] <EOL> for index , key in enumerate ( keys ) : <EOL> if not isinstance ( key , ( basestring , BlobKey ) ) : <EOL> raise datastore_errors . BadArgumentError ( <EOL> '<STR_LIT>' % ( <EOL> key , <EOL> datastore . typename ( key ) ) ) <EOL> keys [ index ] = datastore . Key . from_path ( cls . kind ( ) , str ( key ) , namespace = '<STR_LIT>' ) <EOL> if multiple : <EOL> return keys <EOL> else : <EOL> return keys [ <NUM_LIT:0> ] <EOL> def get ( blob_key ) : <EOL> """<STR_LIT>""" <EOL> return BlobInfo . get ( blob_key ) <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 ( dict , name ) : <EOL> value = dict . 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 = BlobKey ( get_value ( field_storage . type_options , '<STR_LIT>' ) ) <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> 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 ( blob_key , <EOL> { '<STR_LIT>' : content_type , <EOL> '<STR_LIT>' : creation , <EOL> '<STR_LIT:filename>' : filename , <EOL> '<STR_LIT:size>' : size , <EOL> } ) <EOL> class BlobReferenceProperty ( db . Property ) : <EOL> """<STR_LIT>""" <EOL> data_type = BlobInfo <EOL> def get_value_for_datastore ( self , model_instance ) : <EOL> """<STR_LIT>""" <EOL> blob_info = getattr ( model_instance , self . name ) <EOL> if blob_info is None : <EOL> return None <EOL> return blob_info . key ( ) <EOL> def make_value_from_datastore ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return None <EOL> return BlobInfo ( value ) <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , ( basestring ) ) : <EOL> value = BlobInfo ( BlobKey ( value ) ) <EOL> elif isinstance ( value , BlobKey ) : <EOL> value = BlobInfo ( value ) <EOL> return super ( BlobReferenceProperty , self ) . validate ( value ) <EOL> def fetch_data ( blob , start_index , end_index ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( blob , BlobInfo ) : <EOL> blob = blob . key ( ) <EOL> return blobstore . fetch_data ( blob , start_index , end_index ) <EOL> class BlobReader ( object ) : <EOL> """<STR_LIT>""" <EOL> SEEK_SET = <NUM_LIT:0> <EOL> SEEK_CUR = <NUM_LIT:1> <EOL> SEEK_END = <NUM_LIT:2> <EOL> def __init__ ( self , blob_key , buffer_size = <NUM_LIT> , position = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . __blob_key = blob_key <EOL> self . __buffer_size = buffer_size <EOL> self . __buffer = "<STR_LIT>" <EOL> self . __position = position <EOL> self . __buffer_position = <NUM_LIT:0> <EOL> self . __eof = False <EOL> self . __blob_info = None <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . __blob_key , self . __buffer_size , self . __position ) <EOL> def __setstate__ ( self , state ) : <EOL> """<STR_LIT>""" <EOL> self . __init__ ( * state ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __blob_key = None <EOL> def flush ( self ) : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> line = self . readline ( ) <EOL> if not line : <EOL> raise StopIteration <EOL> return line <EOL> def __read_from_buffer ( self , size ) : <EOL> """<STR_LIT>""" <EOL> if not self . __blob_key : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if size < <NUM_LIT:0> : <EOL> end_pos = len ( self . __buffer ) <EOL> else : <EOL> end_pos = self . __buffer_position + size <EOL> data = self . __buffer [ self . __buffer_position : end_pos ] <EOL> data_length = len ( data ) <EOL> size -= data_length <EOL> self . __position += data_length <EOL> self . __buffer_position += data_length <EOL> if self . __buffer_position == len ( self . __buffer ) : <EOL> self . __buffer = "<STR_LIT>" <EOL> self . __buffer_position = <NUM_LIT:0> <EOL> return data , size <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> def read ( self , size = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> data_list = [ ] <EOL> while True : <EOL> data , size = self . __read_from_buffer ( size ) <EOL> data_list . append ( data ) <EOL> if size == <NUM_LIT:0> or self . __eof : <EOL> return '<STR_LIT>' . join ( data_list ) <EOL> self . __fill_buffer ( size ) <EOL> def readline ( self , size = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> data_list = [ ] <EOL> while True : <EOL> if size < <NUM_LIT:0> : <EOL> end_pos = len ( self . __buffer ) <EOL> else : <EOL> end_pos = self . __buffer_position + size <EOL> newline_pos = self . __buffer . find ( '<STR_LIT:\n>' , self . __buffer_position , end_pos ) <EOL> if newline_pos != - <NUM_LIT:1> : <EOL> data_list . append ( <EOL> self . __read_from_buffer ( newline_pos <EOL> - self . __buffer_position + <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) <EOL> break <EOL> else : <EOL> data , size = self . __read_from_buffer ( size ) <EOL> data_list . append ( data ) <EOL> if size == <NUM_LIT:0> or self . __eof : <EOL> break <EOL> self . __fill_buffer ( ) <EOL> return '<STR_LIT>' . join ( data_list ) <EOL> def readlines ( self , sizehint = None ) : <EOL> """<STR_LIT>""" <EOL> lines = [ ] <EOL> while sizehint is None or sizehint > <NUM_LIT:0> : <EOL> line = self . readline ( ) <EOL> if sizehint : <EOL> sizehint -= len ( line ) <EOL> if not line : <EOL> break <EOL> lines . append ( line ) <EOL> return lines <EOL> def seek ( self , offset , whence = SEEK_SET ) : <EOL> """<STR_LIT>""" <EOL> if whence == BlobReader . SEEK_CUR : <EOL> offset = self . __position + offset <EOL> elif whence == BlobReader . SEEK_END : <EOL> offset = self . blob_info . size + offset <EOL> self . __buffer = "<STR_LIT>" <EOL> self . __buffer_position = <NUM_LIT:0> <EOL> self . __position = offset <EOL> self . __eof = False <EOL> def tell ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __position <EOL> def truncate ( self , size ) : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> def write ( self , str ) : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> def writelines ( self , sequence ) : <EOL> raise IOError ( "<STR_LIT>" ) <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 <EOL> @ property <EOL> def closed ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __blob_key is None </s>
