<s> import re <EOL> import six <EOL> from six . moves import range <EOL> from . exception import ( <EOL> BadJson , BadUploadUrl , DuplicateBucketName , FileNotPresent , InvalidAuthToken , MissingPart , <EOL> NonExistentBucket <EOL> ) <EOL> from . raw_api import AbstractRawApi <EOL> class PartSimulator ( object ) : <EOL> def __init__ ( self , file_id , part_number , content_length , content_sha1 , part_data ) : <EOL> self . file_id = file_id <EOL> self . part_number = part_number <EOL> self . content_length = content_length <EOL> self . content_sha1 = content_sha1 <EOL> self . part_data = part_data <EOL> def as_list_parts_dict ( self ) : <EOL> return dict ( <EOL> fileId = self . file_id , <EOL> partNumber = self . part_number , <EOL> contentLength = self . content_length , <EOL> contentSha1 = self . content_sha1 <EOL> ) <EOL> class FileSimulator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( <EOL> self , account_id , bucket_id , file_id , action , name , content_type , content_sha1 , file_info , <EOL> data_bytes , upload_timestamp <EOL> ) : <EOL> self . account_id = account_id <EOL> self . bucket_id = bucket_id <EOL> self . file_id = file_id <EOL> self . action = action <EOL> self . name = name <EOL> if data_bytes is not None : <EOL> self . content_length = len ( data_bytes ) <EOL> self . content_type = content_type <EOL> self . content_sha1 = content_sha1 <EOL> self . file_info = file_info <EOL> self . data_bytes = data_bytes <EOL> self . upload_timestamp = upload_timestamp <EOL> if action == '<STR_LIT:start>' : <EOL> self . parts = [ ] <EOL> def sort_key ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . name , self . file_id ) <EOL> def as_upload_result ( self ) : <EOL> return dict ( <EOL> fileId = self . file_id , <EOL> fileName = self . name , <EOL> accountId = self . account_id , <EOL> bucketId = self . bucket_id , <EOL> contentLength = len ( self . data_bytes ) , <EOL> contentType = self . content_type , <EOL> contentSha1 = self . content_sha1 , <EOL> fileInfo = self . file_info , <EOL> uploadTimestamp = self . upload_timestamp <EOL> ) <EOL> def as_list_files_dict ( self ) : <EOL> return dict ( <EOL> fileId = self . file_id , <EOL> fileName = self . name , <EOL> size = len ( self . data_bytes ) , <EOL> contentType = self . content_type , <EOL> contentSha1 = self . content_sha1 , <EOL> fileInfo = self . file_info , <EOL> action = self . action , <EOL> uploadTimestamp = self . upload_timestamp <EOL> ) <EOL> def as_start_large_file_result ( self ) : <EOL> return dict ( <EOL> fileId = self . file_id , <EOL> fileName = self . name , <EOL> accountId = self . account_id , <EOL> bucketId = self . bucket_id , <EOL> contentType = self . content_type , <EOL> fileInfo = self . file_info , <EOL> uploadTimestamp = self . upload_timestamp <EOL> ) <EOL> def add_part ( self , part_number , part ) : <EOL> while len ( self . parts ) < part_number + <NUM_LIT:1> : <EOL> self . parts . append ( None ) <EOL> self . parts [ part_number ] = part <EOL> def finish ( self , part_sha1_array ) : <EOL> last_part_number = max ( part . part_number for part in self . parts if part is not None ) <EOL> for part_number in six . moves . range ( <NUM_LIT:1> , last_part_number + <NUM_LIT:1> ) : <EOL> if self . parts [ part_number ] is None : <EOL> raise MissingPart ( part_number ) <EOL> my_part_sha1_array = [ <EOL> self . parts [ part_number ] . content_sha1 <EOL> for part_number in six . moves . range ( <NUM_LIT:1> , last_part_number + <NUM_LIT:1> ) <EOL> ] <EOL> if part_sha1_array != my_part_sha1_array : <EOL> raise <EOL> self . data_bytes = six . b ( '<STR_LIT>' ) . join ( <EOL> self . parts [ part_number ] . part_data <EOL> for part_number in six . moves . range ( <NUM_LIT:1> , last_part_number + <NUM_LIT:1> ) <EOL> ) <EOL> self . content_length = len ( self . data_bytes ) <EOL> self . action = '<STR_LIT>' <EOL> def is_visible ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . action == '<STR_LIT>' <EOL> def list_parts ( self , start_part_number , max_part_count ) : <EOL> start_part_number = start_part_number or <NUM_LIT:1> <EOL> max_part_count = max_part_count or <NUM_LIT:100> <EOL> parts = [ <EOL> part . as_list_parts_dict ( ) <EOL> for part in self . parts if part is not None and start_part_number <= part . part_number <EOL> ] <EOL> if len ( parts ) <= max_part_count : <EOL> next_part_number = None <EOL> else : <EOL> next_part_number = parts [ max_part_count ] [ '<STR_LIT>' ] <EOL> parts = parts [ : max_part_count ] <EOL> return dict ( parts = parts , nextPartNumber = next_part_number ) <EOL> def mod_time_millis ( self ) : <EOL> return <NUM_LIT:0> <EOL> class BucketSimulator ( object ) : <EOL> FIRST_FILE_NUMBER = <NUM_LIT> <EOL> FIRST_FILE_ID = str ( FIRST_FILE_NUMBER ) <EOL> def __init__ ( self , account_id , bucket_id , bucket_name , bucket_type ) : <EOL> assert bucket_type in [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . account_id = account_id <EOL> self . bucket_name = bucket_name <EOL> self . bucket_id = bucket_id <EOL> self . bucket_type = bucket_type <EOL> self . upload_url_counter = iter ( range ( <NUM_LIT:200> ) ) <EOL> self . file_id_counter = iter ( range ( self . FIRST_FILE_NUMBER , <NUM_LIT:0> , - <NUM_LIT:1> ) ) <EOL> self . upload_timestamp_counter = iter ( range ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . file_id_to_file = dict ( ) <EOL> self . file_name_and_id_to_file = dict ( ) <EOL> def bucket_dict ( self ) : <EOL> return dict ( <EOL> accountId = self . account_id , <EOL> bucketName = self . bucket_name , <EOL> bucketId = self . bucket_id , <EOL> bucketType = self . bucket_type <EOL> ) <EOL> def cancel_large_file ( self , file_id ) : <EOL> file_sim = self . file_id_to_file [ file_id ] <EOL> key = ( file_sim . name , file_id ) <EOL> del self . file_name_and_id_to_file [ key ] <EOL> del self . file_id_to_file [ file_id ] <EOL> return dict ( <EOL> accountId = self . account_id , <EOL> bucketId = self . bucket_id , <EOL> fileId = file_id , <EOL> fileName = file_sim . name <EOL> ) <EOL> def delete_file_version ( self , file_id , file_name ) : <EOL> key = ( file_name , file_id ) <EOL> file_sim = self . file_name_and_id_to_file [ key ] <EOL> del self . file_name_and_id_to_file [ key ] <EOL> del self . file_id_to_file [ file_id ] <EOL> return dict ( fileId = file_id , fileName = file_name , uploadTimestamp = file_sim . upload_timestamp ) <EOL> def download_file_by_id ( self , file_id , download_dest ) : <EOL> file_sim = self . file_id_to_file [ file_id ] <EOL> self . _download_file_sim ( download_dest , file_sim ) <EOL> def download_file_by_name ( self , file_name , download_dest ) : <EOL> files = self . list_file_names ( file_name , <NUM_LIT:1> ) [ '<STR_LIT>' ] <EOL> if len ( files ) == <NUM_LIT:0> : <EOL> raise FileNotPresent ( file_name ) <EOL> file_dict = files [ <NUM_LIT:0> ] <EOL> if file_dict [ '<STR_LIT>' ] != file_name or file_dict [ '<STR_LIT:action>' ] != '<STR_LIT>' : <EOL> raise FileNotPresent ( file_name ) <EOL> file_sim = self . file_name_and_id_to_file [ ( file_name , file_dict [ '<STR_LIT>' ] ) ] <EOL> self . _download_file_sim ( download_dest , file_sim ) <EOL> def _download_file_sim ( self , download_dest , file_sim ) : <EOL> with download_dest . open ( <EOL> file_sim . file_id , file_sim . name , file_sim . content_length , file_sim . content_type , <EOL> file_sim . content_sha1 , file_sim . file_info , file_sim . mod_time_millis ( ) <EOL> ) as f : <EOL> f . write ( file_sim . data_bytes ) <EOL> def finish_large_file ( self , file_id , part_sha1_array ) : <EOL> file_sim = self . file_id_to_file [ file_id ] <EOL> file_sim . finish ( part_sha1_array ) <EOL> return file_sim . as_upload_result ( ) <EOL> def get_upload_url ( self ) : <EOL> upload_id = six . next ( self . upload_url_counter ) <EOL> upload_url = '<STR_LIT>' % ( self . bucket_id , upload_id ) <EOL> return dict ( bucketId = self . bucket_id , uploadUrl = upload_url , authorizationToken = upload_url ) <EOL> def get_upload_part_url ( self , file_id ) : <EOL> upload_url = '<STR_LIT>' % ( file_id , ) <EOL> return dict ( bucketId = self . bucket_id , uploadUrl = upload_url , authorizationToken = upload_url ) <EOL> def hide_file ( self , file_name ) : <EOL> file_id = self . _next_file_id ( ) <EOL> file_sim = FileSimulator ( <EOL> self . account_id , self . bucket_id , file_id , '<STR_LIT>' , file_name , None , "<STR_LIT:none>" , { } , <EOL> six . b ( '<STR_LIT>' ) , six . next ( self . upload_timestamp_counter ) <EOL> ) <EOL> self . file_id_to_file [ file_id ] = file_sim <EOL> self . file_name_and_id_to_file [ file_sim . sort_key ( ) ] = file_sim <EOL> return file_sim . as_upload_result ( ) <EOL> def list_file_names ( self , start_file_name = None , max_file_count = None ) : <EOL> start_file_name = start_file_name or '<STR_LIT>' <EOL> max_file_count = max_file_count or <NUM_LIT:100> <EOL> result_files = [ ] <EOL> next_file_name = None <EOL> prev_file_name = None <EOL> for key in sorted ( six . iterkeys ( self . file_name_and_id_to_file ) ) : <EOL> ( file_name , file_id ) = key <EOL> if start_file_name <= file_name and file_name != prev_file_name : <EOL> prev_file_name = file_name <EOL> file_sim = self . file_name_and_id_to_file [ key ] <EOL> if file_sim . is_visible ( ) : <EOL> result_files . append ( file_sim . as_list_files_dict ( ) ) <EOL> if len ( result_files ) == max_file_count : <EOL> next_file_name = file_sim . name + '<STR_LIT:U+0020>' <EOL> break <EOL> return dict ( files = result_files , nextFileName = next_file_name ) <EOL> def list_file_versions ( self , start_file_name = None , start_file_id = None , max_file_count = None ) : <EOL> start_file_name = start_file_name or '<STR_LIT>' <EOL> start_file_id = start_file_id or '<STR_LIT>' <EOL> max_file_count = max_file_count or <NUM_LIT:100> <EOL> result_files = [ ] <EOL> next_file_name = None <EOL> next_file_id = None <EOL> for key in sorted ( six . iterkeys ( self . file_name_and_id_to_file ) ) : <EOL> ( file_name , file_id ) = key <EOL> if ( start_file_name < file_name ) or ( <EOL> start_file_name == file_name and start_file_id <= file_id <EOL> ) : <EOL> file_sim = self . file_name_and_id_to_file [ key ] <EOL> result_files . append ( file_sim . as_list_files_dict ( ) ) <EOL> if len ( result_files ) == max_file_count : <EOL> next_file_name = file_sim . name <EOL> next_file_id = str ( int ( file_id ) + <NUM_LIT:1> ) <EOL> break <EOL> return dict ( files = result_files , nextFileName = next_file_name , nextFileId = next_file_id ) <EOL> def list_parts ( self , file_id , start_part_number , max_part_count ) : <EOL> file_sim = self . file_id_to_file [ file_id ] <EOL> return file_sim . list_parts ( start_part_number , max_part_count ) <EOL> def list_unfinished_large_files ( self , start_file_id = None , max_file_count = None ) : <EOL> start_file_id = start_file_id or self . FIRST_FILE_ID <EOL> max_file_count = max_file_count or <NUM_LIT:100> <EOL> all_unfinished_ids = set ( <EOL> k <EOL> for ( k , v ) in six . iteritems ( self . file_id_to_file ) <EOL> if v . action == '<STR_LIT:start>' and k <= start_file_id <EOL> ) <EOL> ids_in_order = sorted ( all_unfinished_ids , reverse = True ) <EOL> file_dict_list = [ <EOL> dict ( <EOL> fileId = file_sim . file_id , <EOL> fileName = file_sim . name , <EOL> accountId = file_sim . account_id , <EOL> bucketId = file_sim . bucket_id , <EOL> contentType = file_sim . content_type , <EOL> fileInfo = file_sim . file_info <EOL> ) <EOL> for file_sim in ( <EOL> self . file_id_to_file [ file_id ] for file_id in ids_in_order [ : max_file_count ] <EOL> ) <EOL> ] <EOL> next_file_id = None <EOL> if len ( file_dict_list ) == max_file_count : <EOL> next_file_id = str ( int ( file_dict_list [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] ) - <NUM_LIT:1> ) <EOL> return dict ( files = file_dict_list , nextFileId = next_file_id ) <EOL> def start_large_file ( self , file_name , content_type , file_info ) : <EOL> file_id = self . _next_file_id ( ) <EOL> file_sim = FileSimulator ( <EOL> self . account_id , self . bucket_id , file_id , '<STR_LIT:start>' , file_name , content_type , '<STR_LIT:none>' , <EOL> file_info , None , six . next ( self . upload_timestamp_counter ) <EOL> ) <EOL> self . file_id_to_file [ file_id ] = file_sim <EOL> self . file_name_and_id_to_file [ file_sim . sort_key ( ) ] = file_sim <EOL> return file_sim . as_start_large_file_result ( ) <EOL> def update_bucket ( self , bucket_type ) : <EOL> self . bucket_type = bucket_type <EOL> return self . bucket_dict ( ) <EOL> def upload_file ( <EOL> self , upload_id , upload_auth_token , file_name , content_length , content_type , content_sha1 , <EOL> file_infos , data_stream <EOL> ) : <EOL> data_bytes = data_stream . read ( ) <EOL> assert len ( data_bytes ) == content_length <EOL> file_id = self . _next_file_id ( ) <EOL> file_sim = FileSimulator ( <EOL> self . account_id , self . bucket_id , file_id , '<STR_LIT>' , file_name , content_type , <EOL> content_sha1 , file_infos , data_bytes , six . next ( self . upload_timestamp_counter ) <EOL> ) <EOL> self . file_id_to_file [ file_id ] = file_sim <EOL> self . file_name_and_id_to_file [ file_sim . sort_key ( ) ] = file_sim <EOL> return file_sim . as_upload_result ( ) <EOL> def upload_part ( self , file_id , part_number , content_length , sha1_sum , input_stream ) : <EOL> file_sim = self . file_id_to_file [ file_id ] <EOL> part_data = input_stream . read ( content_length ) <EOL> assert len ( part_data ) == content_length <EOL> part = PartSimulator ( file_sim . file_id , part_number , content_length , sha1_sum , part_data ) <EOL> file_sim . add_part ( part_number , part ) <EOL> return dict ( <EOL> fileId = file_id , <EOL> partNumber = part_number , <EOL> contentLength = content_length , <EOL> contentSha1 = sha1_sum <EOL> ) <EOL> def _next_file_id ( self ) : <EOL> return str ( six . next ( self . file_id_counter ) ) <EOL> class RawSimulator ( AbstractRawApi ) : <EOL> """<STR_LIT>""" <EOL> API_URL = '<STR_LIT>' <EOL> DOWNLOAD_URL = '<STR_LIT>' <EOL> MIN_PART_SIZE = <NUM_LIT:200> <EOL> def __init__ ( self ) : <EOL> self . authorized_accounts = set ( ) <EOL> self . bucket_name_to_bucket = dict ( ) <EOL> self . bucket_id_to_bucket = dict ( ) <EOL> self . bucket_id_counter = iter ( range ( <NUM_LIT:100> ) ) <EOL> self . file_id_to_bucket_id = { } <EOL> self . upload_errors = [ ] <EOL> def set_upload_errors ( self , errors ) : <EOL> """<STR_LIT>""" <EOL> assert len ( self . upload_errors ) == <NUM_LIT:0> <EOL> self . upload_errors = errors <EOL> def authorize_account ( self , realm_url , account_id , application_key ) : <EOL> assert realm_url == '<STR_LIT>' <EOL> if application_key != '<STR_LIT>' : <EOL> raise InvalidAuthToken ( <EOL> '<STR_LIT>' % ( application_key , ) , '<STR_LIT>' <EOL> ) <EOL> self . authorized_accounts . add ( account_id ) <EOL> return dict ( <EOL> accountId = account_id , <EOL> authorizationToken = '<STR_LIT>' + account_id , <EOL> apiUrl = self . API_URL , <EOL> downloadUrl = self . DOWNLOAD_URL , <EOL> minimumPartSize = self . MIN_PART_SIZE <EOL> ) <EOL> def cancel_large_file ( self , api_url , account_auth_token , file_id ) : <EOL> bucket_id = self . file_id_to_bucket_id [ file_id ] <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> self . _assert_account_auth ( api_url , account_auth_token , bucket . account_id ) <EOL> return bucket . cancel_large_file ( file_id ) <EOL> def create_bucket ( self , api_url , account_auth_token , account_id , bucket_name , bucket_type ) : <EOL> if not re . match ( r'<STR_LIT>' , bucket_name ) : <EOL> raise BadJson ( '<STR_LIT>' + bucket_name ) <EOL> self . _assert_account_auth ( api_url , account_auth_token , account_id ) <EOL> if bucket_name in self . bucket_name_to_bucket : <EOL> raise DuplicateBucketName ( bucket_name ) <EOL> bucket_id = '<STR_LIT>' + str ( six . next ( self . bucket_id_counter ) ) <EOL> bucket = BucketSimulator ( account_id , bucket_id , bucket_name , bucket_type ) <EOL> self . bucket_name_to_bucket [ bucket_name ] = bucket <EOL> self . bucket_id_to_bucket [ bucket_id ] = bucket <EOL> return bucket . bucket_dict ( ) <EOL> def delete_file_version ( self , api_url , account_auth_token , file_id , file_name ) : <EOL> bucket_id = self . file_id_to_bucket_id [ file_id ] <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> self . _assert_account_auth ( api_url , account_auth_token , bucket . account_id ) <EOL> return bucket . delete_file_version ( file_id , file_name ) <EOL> def delete_bucket ( self , api_url , account_auth_token , account_id , bucket_id ) : <EOL> self . _assert_account_auth ( api_url , account_auth_token , account_id ) <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> del self . bucket_name_to_bucket [ bucket . bucket_name ] <EOL> del self . bucket_id_to_bucket [ bucket_id ] <EOL> return bucket . bucket_dict ( ) <EOL> def download_file_by_id ( self , download_url , account_auth_token_or_none , file_id , download_dest ) : <EOL> bucket_id = self . file_id_to_bucket_id [ file_id ] <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> bucket . download_file_by_id ( file_id , download_dest ) <EOL> def download_file_by_name ( <EOL> self , download_url , account_auth_token_or_none , bucket_name , file_name , download_dest <EOL> ) : <EOL> assert download_url == self . DOWNLOAD_URL <EOL> bucket = self . _get_bucket_by_name ( bucket_name ) <EOL> bucket . download_file_by_name ( file_name , download_dest ) <EOL> def finish_large_file ( self , api_url , account_auth_token , file_id , part_sha1_array ) : <EOL> bucket_id = self . file_id_to_bucket_id [ file_id ] <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> self . _assert_account_auth ( api_url , account_auth_token , bucket . account_id ) <EOL> return bucket . finish_large_file ( file_id , part_sha1_array ) <EOL> def get_upload_url ( self , api_url , account_auth_token , bucket_id ) : <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> self . _assert_account_auth ( api_url , account_auth_token , bucket . account_id ) <EOL> return self . _get_bucket_by_id ( bucket_id ) . get_upload_url ( ) <EOL> def get_upload_part_url ( self , api_url , account_auth_token , file_id ) : <EOL> bucket_id = self . file_id_to_bucket_id [ file_id ] <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> self . _assert_account_auth ( api_url , account_auth_token , bucket . account_id ) <EOL> return self . _get_bucket_by_id ( bucket_id ) . get_upload_part_url ( file_id ) <EOL> def hide_file ( self , api_url , account_auth_token , bucket_id , file_name ) : <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> self . _assert_account_auth ( api_url , account_auth_token , bucket . account_id ) <EOL> response = bucket . hide_file ( file_name ) <EOL> self . file_id_to_bucket_id [ response [ '<STR_LIT>' ] ] = bucket_id <EOL> return response <EOL> def list_buckets ( self , api_url , account_auth_token , account_id ) : <EOL> self . _assert_account_auth ( api_url , account_auth_token , account_id ) <EOL> sorted_buckets = [ <EOL> self . bucket_name_to_bucket [ bucket_name ] <EOL> for bucket_name in sorted ( six . iterkeys ( self . bucket_name_to_bucket ) ) <EOL> ] <EOL> bucket_list = [ bucket . bucket_dict ( ) for bucket in sorted_buckets ] <EOL> return dict ( buckets = bucket_list ) <EOL> def list_file_names ( <EOL> self , api_url , <EOL> account_auth , <EOL> bucket_id , start_file_name = None , <EOL> max_file_count = None <EOL> ) : <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> self . _assert_account_auth ( api_url , account_auth , bucket . account_id ) <EOL> return bucket . list_file_names ( start_file_name , max_file_count ) <EOL> def list_file_versions ( <EOL> self , <EOL> api_url , <EOL> account_auth , <EOL> bucket_id , <EOL> start_file_name = None , <EOL> start_file_id = None , <EOL> max_file_count = None <EOL> ) : <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> self . _assert_account_auth ( api_url , account_auth , bucket . account_id ) <EOL> return bucket . list_file_versions ( start_file_name , start_file_id , max_file_count ) <EOL> def list_parts ( self , api_url , account_auth_token , file_id , start_part_number , max_part_count ) : <EOL> bucket_id = self . file_id_to_bucket_id [ file_id ] <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> self . _assert_account_auth ( api_url , account_auth_token , bucket . account_id ) <EOL> return bucket . list_parts ( file_id , start_part_number , max_part_count ) <EOL> def list_unfinished_large_files ( <EOL> self , api_url , <EOL> account_auth , <EOL> bucket_id , start_file_id = None , <EOL> max_file_count = None <EOL> ) : <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> self . _assert_account_auth ( api_url , account_auth , bucket . account_id ) <EOL> start_file_id = start_file_id or '<STR_LIT>' <EOL> max_file_count = max_file_count or <NUM_LIT:100> <EOL> return bucket . list_unfinished_large_files ( start_file_id , max_file_count ) <EOL> def start_large_file ( <EOL> self , api_url , account_auth_token , bucket_id , file_name , content_type , file_info <EOL> ) : <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> self . _assert_account_auth ( api_url , account_auth_token , bucket . account_id ) <EOL> result = bucket . start_large_file ( file_name , content_type , file_info ) <EOL> self . file_id_to_bucket_id [ result [ '<STR_LIT>' ] ] = bucket_id <EOL> return result <EOL> def update_bucket ( self , api_url , account_auth_token , account_id , bucket_id , bucket_type ) : <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> self . _assert_account_auth ( api_url , account_auth_token , bucket . account_id ) <EOL> return bucket . update_bucket ( bucket_type ) <EOL> def upload_file ( <EOL> self , upload_url , upload_auth_token , file_name , content_length , content_type , content_sha1 , <EOL> file_infos , data_stream <EOL> ) : <EOL> assert upload_url == upload_auth_token <EOL> url_match = re . match ( r'<STR_LIT>' , upload_url ) <EOL> if url_match is None : <EOL> raise BadUploadUrl ( upload_url ) <EOL> if len ( self . upload_errors ) != <NUM_LIT:0> : <EOL> raise self . upload_errors . pop ( <NUM_LIT:0> ) <EOL> bucket_id , upload_id = url_match . groups ( ) <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> response = bucket . upload_file ( <EOL> upload_id , upload_auth_token , file_name , content_length , content_type , content_sha1 , <EOL> file_infos , data_stream <EOL> ) <EOL> file_id = response [ '<STR_LIT>' ] <EOL> self . file_id_to_bucket_id [ file_id ] = bucket_id <EOL> return response <EOL> def upload_part ( <EOL> self , upload_url , upload_auth_token , part_number , content_length , sha1_sum , input_stream <EOL> ) : <EOL> re . compile ( '<STR_LIT>' ) <EOL> url_match = re . match ( '<STR_LIT>' , upload_url ) <EOL> if url_match is None : <EOL> raise BadUploadUrl ( upload_url ) <EOL> file_id = url_match . group ( <NUM_LIT:1> ) <EOL> bucket_id = self . file_id_to_bucket_id [ file_id ] <EOL> bucket = self . _get_bucket_by_id ( bucket_id ) <EOL> return bucket . upload_part ( file_id , part_number , content_length , sha1_sum , input_stream ) <EOL> def _assert_account_auth ( self , api_url , account_auth_token , account_id ) : <EOL> assert api_url == self . API_URL <EOL> assert account_auth_token == '<STR_LIT>' + account_id <EOL> assert account_id in self . authorized_accounts <EOL> def _get_bucket_by_id ( self , bucket_id ) : <EOL> if bucket_id not in self . bucket_id_to_bucket : <EOL> raise NonExistentBucket ( bucket_id ) <EOL> return self . bucket_id_to_bucket [ bucket_id ] <EOL> def _get_bucket_by_name ( self , bucket_name ) : <EOL> if bucket_name not in self . bucket_name_to_bucket : <EOL> raise NonExistentBucket ( bucket_name ) <EOL> return self . bucket_name_to_bucket [ bucket_name ] </s>
