<s> import boto <EOL> from boto . connection import AWSAuthConnection <EOL> from boto . exception import DynamoDBResponseError <EOL> from boto . provider import Provider <EOL> from boto . dynamodb import exceptions as dynamodb_exceptions <EOL> import time <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> Debug = <NUM_LIT:0> <EOL> class Layer1 ( AWSAuthConnection ) : <EOL> """<STR_LIT>""" <EOL> DefaultRegionName = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> ServiceName = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> Version = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> ThruputError = "<STR_LIT>" <EOL> """<STR_LIT>""" <EOL> SessionExpiredError = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> ConditionalCheckFailedError = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> ValidationError = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> ResponseError = DynamoDBResponseError <EOL> def __init__ ( self , aws_access_key_id = None , aws_secret_access_key = None , <EOL> is_secure = True , port = None , proxy = None , proxy_port = None , <EOL> debug = <NUM_LIT:0> , session_token = None , region = None ) : <EOL> if not region : <EOL> region_name = boto . config . get ( '<STR_LIT>' , '<STR_LIT>' , <EOL> self . DefaultRegionName ) <EOL> for reg in boto . dynamodb . regions ( ) : <EOL> if reg . name == region_name : <EOL> region = reg <EOL> break <EOL> self . region = region <EOL> self . _passed_access_key = aws_access_key_id <EOL> self . _passed_secret_key = aws_secret_access_key <EOL> if not session_token : <EOL> session_token = self . _get_session_token ( ) <EOL> self . creds = session_token <EOL> self . throughput_exceeded_events = <NUM_LIT:0> <EOL> self . request_id = None <EOL> self . instrumentation = { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } <EOL> self . do_instrumentation = False <EOL> AWSAuthConnection . __init__ ( self , self . region . endpoint , <EOL> self . creds . access_key , <EOL> self . creds . secret_key , <EOL> is_secure , port , proxy , proxy_port , <EOL> debug = debug , <EOL> security_token = self . creds . session_token ) <EOL> def _update_provider ( self ) : <EOL> self . provider = Provider ( '<STR_LIT>' , <EOL> self . creds . access_key , <EOL> self . creds . secret_key , <EOL> self . creds . session_token ) <EOL> self . _auth_handler . update_provider ( self . provider ) <EOL> def _get_session_token ( self ) : <EOL> boto . log . debug ( '<STR_LIT>' ) <EOL> sts = boto . connect_sts ( self . _passed_access_key , <EOL> self . _passed_secret_key ) <EOL> return sts . get_session_token ( ) <EOL> def _required_auth_capability ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> def make_request ( self , action , body = '<STR_LIT>' , object_hook = None ) : <EOL> """<STR_LIT>""" <EOL> headers = { '<STR_LIT>' : '<STR_LIT>' % ( self . ServiceName , <EOL> self . Version , action ) , <EOL> '<STR_LIT>' : self . region . endpoint , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : str ( len ( body ) ) } <EOL> http_request = self . build_base_http_request ( '<STR_LIT:POST>' , '<STR_LIT:/>' , '<STR_LIT:/>' , <EOL> { } , headers , body , None ) <EOL> if self . do_instrumentation : <EOL> start = time . time ( ) <EOL> response = self . _mexe ( http_request , sender = None , <EOL> override_num_retries = <NUM_LIT:10> , <EOL> retry_handler = self . _retry_handler ) <EOL> self . request_id = response . getheader ( '<STR_LIT>' ) <EOL> boto . log . debug ( '<STR_LIT>' % self . request_id ) <EOL> if self . do_instrumentation : <EOL> self . instrumentation [ '<STR_LIT>' ] . append ( time . time ( ) - start ) <EOL> self . instrumentation [ '<STR_LIT>' ] . append ( self . request_id ) <EOL> response_body = response . read ( ) <EOL> boto . log . debug ( response_body ) <EOL> return json . loads ( response_body , object_hook = object_hook ) <EOL> def _retry_handler ( self , response , i , next_sleep ) : <EOL> status = None <EOL> if response . status == <NUM_LIT> : <EOL> response_body = response . read ( ) <EOL> boto . log . debug ( response_body ) <EOL> data = json . loads ( response_body ) <EOL> if self . ThruputError in data . get ( '<STR_LIT>' ) : <EOL> self . throughput_exceeded_events += <NUM_LIT:1> <EOL> msg = "<STR_LIT>" % ( self . ThruputError , i ) <EOL> if i == <NUM_LIT:0> : <EOL> next_sleep = <NUM_LIT:0> <EOL> else : <EOL> next_sleep = <NUM_LIT> * ( <NUM_LIT:2> ** i ) <EOL> i += <NUM_LIT:1> <EOL> status = ( msg , i , next_sleep ) <EOL> elif self . SessionExpiredError in data . get ( '<STR_LIT>' ) : <EOL> msg = '<STR_LIT>' <EOL> self . creds = self . _get_session_token ( ) <EOL> self . _update_provider ( ) <EOL> status = ( msg , i + self . num_retries - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> elif self . ConditionalCheckFailedError in data . get ( '<STR_LIT>' ) : <EOL> raise dynamodb_exceptions . DynamoDBConditionalCheckFailedError ( <EOL> response . status , response . reason , data ) <EOL> elif self . ValidationError in data . get ( '<STR_LIT>' ) : <EOL> raise dynamodb_exceptions . DynamoDBValidationError ( <EOL> response . status , response . reason , data ) <EOL> else : <EOL> raise self . ResponseError ( response . status , response . reason , <EOL> data ) <EOL> return status <EOL> def list_tables ( self , limit = None , start_table = None ) : <EOL> """<STR_LIT>""" <EOL> data = { } <EOL> if limit : <EOL> data [ '<STR_LIT>' ] = limit <EOL> if start_table : <EOL> data [ '<STR_LIT>' ] = start_table <EOL> json_input = json . dumps ( data ) <EOL> return self . make_request ( '<STR_LIT>' , json_input ) <EOL> def describe_table ( self , table_name ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT>' : table_name } <EOL> json_input = json . dumps ( data ) <EOL> return self . make_request ( '<STR_LIT>' , json_input ) <EOL> def create_table ( self , table_name , schema , provisioned_throughput ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT>' : table_name , <EOL> '<STR_LIT>' : schema , <EOL> '<STR_LIT>' : provisioned_throughput } <EOL> json_input = json . dumps ( data ) <EOL> response_dict = self . make_request ( '<STR_LIT>' , json_input ) <EOL> return response_dict <EOL> def update_table ( self , table_name , provisioned_throughput ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT>' : table_name , <EOL> '<STR_LIT>' : provisioned_throughput } <EOL> json_input = json . dumps ( data ) <EOL> return self . make_request ( '<STR_LIT>' , json_input ) <EOL> def delete_table ( self , table_name ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT>' : table_name } <EOL> json_input = json . dumps ( data ) <EOL> return self . make_request ( '<STR_LIT>' , json_input ) <EOL> def get_item ( self , table_name , key , attributes_to_get = None , <EOL> consistent_read = False , object_hook = None ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT>' : table_name , <EOL> '<STR_LIT>' : key } <EOL> if attributes_to_get : <EOL> data [ '<STR_LIT>' ] = attributes_to_get <EOL> if consistent_read : <EOL> data [ '<STR_LIT>' ] = True <EOL> json_input = json . dumps ( data ) <EOL> response = self . make_request ( '<STR_LIT>' , json_input , <EOL> object_hook = object_hook ) <EOL> if '<STR_LIT>' not in response : <EOL> raise dynamodb_exceptions . DynamoDBKeyNotFoundError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> return response <EOL> def batch_get_item ( self , request_items , object_hook = None ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT>' : request_items } <EOL> json_input = json . dumps ( data ) <EOL> return self . make_request ( '<STR_LIT>' , json_input , <EOL> object_hook = object_hook ) <EOL> def batch_write_item ( self , request_items , object_hook = None ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT>' : request_items } <EOL> json_input = json . dumps ( data ) <EOL> return self . make_request ( '<STR_LIT>' , json_input , <EOL> object_hook = object_hook ) <EOL> def put_item ( self , table_name , item , <EOL> expected = None , return_values = None , <EOL> object_hook = None ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT>' : table_name , <EOL> '<STR_LIT>' : item } <EOL> if expected : <EOL> data [ '<STR_LIT>' ] = expected <EOL> if return_values : <EOL> data [ '<STR_LIT>' ] = return_values <EOL> json_input = json . dumps ( data ) <EOL> return self . make_request ( '<STR_LIT>' , json_input , <EOL> object_hook = object_hook ) <EOL> def update_item ( self , table_name , key , attribute_updates , <EOL> expected = None , return_values = None , <EOL> object_hook = None ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT>' : table_name , <EOL> '<STR_LIT>' : key , <EOL> '<STR_LIT>' : attribute_updates } <EOL> if expected : <EOL> data [ '<STR_LIT>' ] = expected <EOL> if return_values : <EOL> data [ '<STR_LIT>' ] = return_values <EOL> json_input = json . dumps ( data ) <EOL> return self . make_request ( '<STR_LIT>' , json_input , <EOL> object_hook = object_hook ) <EOL> def delete_item ( self , table_name , key , <EOL> expected = None , return_values = None , <EOL> object_hook = None ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT>' : table_name , <EOL> '<STR_LIT>' : key } <EOL> if expected : <EOL> data [ '<STR_LIT>' ] = expected <EOL> if return_values : <EOL> data [ '<STR_LIT>' ] = return_values <EOL> json_input = json . dumps ( data ) <EOL> return self . make_request ( '<STR_LIT>' , json_input , <EOL> object_hook = object_hook ) <EOL> def query ( self , table_name , hash_key_value , range_key_conditions = None , <EOL> attributes_to_get = None , limit = None , consistent_read = False , <EOL> scan_index_forward = True , exclusive_start_key = None , <EOL> object_hook = None ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT>' : table_name , <EOL> '<STR_LIT>' : hash_key_value } <EOL> if range_key_conditions : <EOL> data [ '<STR_LIT>' ] = range_key_conditions <EOL> if attributes_to_get : <EOL> data [ '<STR_LIT>' ] = attributes_to_get <EOL> if limit : <EOL> data [ '<STR_LIT>' ] = limit <EOL> if consistent_read : <EOL> data [ '<STR_LIT>' ] = True <EOL> if scan_index_forward : <EOL> data [ '<STR_LIT>' ] = True <EOL> else : <EOL> data [ '<STR_LIT>' ] = False <EOL> if exclusive_start_key : <EOL> data [ '<STR_LIT>' ] = exclusive_start_key <EOL> json_input = json . dumps ( data ) <EOL> return self . make_request ( '<STR_LIT>' , json_input , <EOL> object_hook = object_hook ) <EOL> def scan ( self , table_name , scan_filter = None , <EOL> attributes_to_get = None , limit = None , <EOL> count = False , exclusive_start_key = None , <EOL> object_hook = None ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT>' : table_name } <EOL> if scan_filter : <EOL> data [ '<STR_LIT>' ] = scan_filter <EOL> if attributes_to_get : <EOL> data [ '<STR_LIT>' ] = attributes_to_get <EOL> if limit : <EOL> data [ '<STR_LIT>' ] = limit <EOL> if count : <EOL> data [ '<STR_LIT>' ] = True <EOL> if exclusive_start_key : <EOL> data [ '<STR_LIT>' ] = exclusive_start_key <EOL> json_input = json . dumps ( data ) <EOL> return self . make_request ( '<STR_LIT>' , json_input , object_hook = object_hook ) </s>
