<s> from __future__ import unicode_literals <EOL> import json <EOL> import six <EOL> from moto . core . responses import BaseResponse <EOL> from moto . core . utils import camelcase_to_underscores <EOL> from . models import dynamodb_backend2 , dynamo_json_dump <EOL> GET_SESSION_TOKEN_RESULT = """<STR_LIT>""" <EOL> def sts_handler ( ) : <EOL> return GET_SESSION_TOKEN_RESULT <EOL> class DynamoHandler ( BaseResponse ) : <EOL> def get_endpoint_name ( self , headers ) : <EOL> """<STR_LIT>""" <EOL> match = headers . get ( '<STR_LIT>' ) or headers . get ( '<STR_LIT>' ) <EOL> if match : <EOL> return match . split ( "<STR_LIT:.>" ) [ <NUM_LIT:1> ] <EOL> def error ( self , type_ , status = <NUM_LIT> ) : <EOL> return status , self . response_headers , dynamo_json_dump ( { '<STR_LIT>' : type_ } ) <EOL> def call_action ( self ) : <EOL> body = self . body . decode ( '<STR_LIT:utf-8>' ) <EOL> if '<STR_LIT>' in body : <EOL> return <NUM_LIT:200> , self . response_headers , sts_handler ( ) <EOL> self . body = json . loads ( body or '<STR_LIT:{}>' ) <EOL> endpoint = self . get_endpoint_name ( self . headers ) <EOL> if endpoint : <EOL> endpoint = camelcase_to_underscores ( endpoint ) <EOL> response = getattr ( self , endpoint ) ( ) <EOL> if isinstance ( response , six . string_types ) : <EOL> return <NUM_LIT:200> , self . response_headers , response <EOL> else : <EOL> status_code , new_headers , response_content = response <EOL> self . response_headers . update ( new_headers ) <EOL> return status_code , self . response_headers , response_content <EOL> else : <EOL> return <NUM_LIT> , self . response_headers , "<STR_LIT>" <EOL> def list_tables ( self ) : <EOL> body = self . body <EOL> limit = body . get ( '<STR_LIT>' ) <EOL> if body . get ( "<STR_LIT>" ) : <EOL> last = body . get ( "<STR_LIT>" ) <EOL> start = list ( dynamodb_backend2 . tables . keys ( ) ) . index ( last ) + <NUM_LIT:1> <EOL> else : <EOL> start = <NUM_LIT:0> <EOL> all_tables = list ( dynamodb_backend2 . tables . keys ( ) ) <EOL> if limit : <EOL> tables = all_tables [ start : start + limit ] <EOL> else : <EOL> tables = all_tables [ start : ] <EOL> response = { "<STR_LIT>" : tables } <EOL> if limit and len ( all_tables ) > start + limit : <EOL> response [ "<STR_LIT>" ] = tables [ - <NUM_LIT:1> ] <EOL> return dynamo_json_dump ( response ) <EOL> def create_table ( self ) : <EOL> body = self . body <EOL> table_name = body [ '<STR_LIT>' ] <EOL> throughput = body [ "<STR_LIT>" ] <EOL> key_schema = body [ '<STR_LIT>' ] <EOL> attr = body [ "<STR_LIT>" ] <EOL> global_indexes = body . get ( "<STR_LIT>" , [ ] ) <EOL> local_secondary_indexes = body . get ( "<STR_LIT>" , [ ] ) <EOL> table = dynamodb_backend2 . create_table ( table_name , <EOL> schema = key_schema , <EOL> throughput = throughput , <EOL> attr = attr , <EOL> global_indexes = global_indexes , <EOL> indexes = local_secondary_indexes ) <EOL> if table is not None : <EOL> return dynamo_json_dump ( table . describe ) <EOL> else : <EOL> er = '<STR_LIT>' <EOL> return self . error ( er ) <EOL> def delete_table ( self ) : <EOL> name = self . body [ '<STR_LIT>' ] <EOL> table = dynamodb_backend2 . delete_table ( name ) <EOL> if table is not None : <EOL> return dynamo_json_dump ( table . describe ) <EOL> else : <EOL> er = '<STR_LIT>' <EOL> return self . error ( er ) <EOL> def update_table ( self ) : <EOL> name = self . body [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in self . body : <EOL> table = dynamodb_backend2 . update_table_global_indexes ( name , self . body [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in self . body : <EOL> throughput = self . body [ "<STR_LIT>" ] <EOL> table = dynamodb_backend2 . update_table_throughput ( name , throughput ) <EOL> return dynamo_json_dump ( table . describe ) <EOL> def describe_table ( self ) : <EOL> name = self . body [ '<STR_LIT>' ] <EOL> try : <EOL> table = dynamodb_backend2 . tables [ name ] <EOL> except KeyError : <EOL> er = '<STR_LIT>' <EOL> return self . error ( er ) <EOL> return dynamo_json_dump ( table . describe ) <EOL> def put_item ( self ) : <EOL> name = self . body [ '<STR_LIT>' ] <EOL> item = self . body [ '<STR_LIT>' ] <EOL> overwrite = '<STR_LIT>' not in self . body <EOL> if not overwrite : <EOL> expected = self . body [ '<STR_LIT>' ] <EOL> else : <EOL> expected = None <EOL> try : <EOL> result = dynamodb_backend2 . put_item ( name , item , expected , overwrite ) <EOL> except Exception : <EOL> er = '<STR_LIT>' <EOL> return self . error ( er ) <EOL> if result : <EOL> item_dict = result . to_json ( ) <EOL> item_dict [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> return dynamo_json_dump ( item_dict ) <EOL> else : <EOL> er = '<STR_LIT>' <EOL> return self . error ( er ) <EOL> def batch_write_item ( self ) : <EOL> table_batches = self . body [ '<STR_LIT>' ] <EOL> for table_name , table_requests in table_batches . items ( ) : <EOL> for table_request in table_requests : <EOL> request_type = list ( table_request . keys ( ) ) [ <NUM_LIT:0> ] <EOL> request = list ( table_request . values ( ) ) [ <NUM_LIT:0> ] <EOL> if request_type == '<STR_LIT>' : <EOL> item = request [ '<STR_LIT>' ] <EOL> dynamodb_backend2 . put_item ( table_name , item ) <EOL> elif request_type == '<STR_LIT>' : <EOL> keys = request [ '<STR_LIT>' ] <EOL> item = dynamodb_backend2 . delete_item ( table_name , keys ) <EOL> response = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : <NUM_LIT:1.0> <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : <NUM_LIT:1.0> <EOL> } <EOL> } , <EOL> "<STR_LIT>" : { } <EOL> } <EOL> return dynamo_json_dump ( response ) <EOL> def get_item ( self ) : <EOL> name = self . body [ '<STR_LIT>' ] <EOL> key = self . body [ '<STR_LIT>' ] <EOL> try : <EOL> item = dynamodb_backend2 . get_item ( name , key ) <EOL> except ValueError : <EOL> er = '<STR_LIT>' <EOL> return self . error ( er , status = <NUM_LIT> ) <EOL> if item : <EOL> item_dict = item . describe_attrs ( attributes = None ) <EOL> item_dict [ '<STR_LIT>' ] = <NUM_LIT:0.5> <EOL> return dynamo_json_dump ( item_dict ) <EOL> else : <EOL> er = '<STR_LIT:{}>' <EOL> return self . error ( er , status = <NUM_LIT:200> ) <EOL> def batch_get_item ( self ) : <EOL> table_batches = self . body [ '<STR_LIT>' ] <EOL> results = { <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : { <EOL> } , <EOL> "<STR_LIT>" : { <EOL> } <EOL> } <EOL> for table_name , table_request in table_batches . items ( ) : <EOL> keys = table_request [ '<STR_LIT>' ] <EOL> attributes_to_get = table_request . get ( '<STR_LIT>' ) <EOL> results [ "<STR_LIT>" ] [ table_name ] = [ ] <EOL> for key in keys : <EOL> item = dynamodb_backend2 . get_item ( table_name , key ) <EOL> if item : <EOL> item_describe = item . describe_attrs ( attributes_to_get ) <EOL> results [ "<STR_LIT>" ] [ table_name ] . append ( item_describe [ "<STR_LIT>" ] ) <EOL> results [ "<STR_LIT>" ] . append ( { <EOL> "<STR_LIT>" : len ( keys ) , <EOL> "<STR_LIT>" : table_name <EOL> } ) <EOL> return dynamo_json_dump ( results ) <EOL> def query ( self ) : <EOL> name = self . body [ '<STR_LIT>' ] <EOL> key_condition_expression = self . body . get ( '<STR_LIT>' ) <EOL> filter_kwargs = { } <EOL> if key_condition_expression : <EOL> value_alias_map = self . body [ '<STR_LIT>' ] <EOL> table = dynamodb_backend2 . get_table ( name ) <EOL> index_name = self . body . get ( '<STR_LIT>' ) <EOL> if index_name : <EOL> all_indexes = ( table . global_indexes or [ ] ) + ( table . indexes or [ ] ) <EOL> indexes_by_name = dict ( ( i [ '<STR_LIT>' ] , i ) for i in all_indexes ) <EOL> if index_name not in indexes_by_name : <EOL> raise ValueError ( '<STR_LIT>' % ( <EOL> index_name , name , '<STR_LIT:U+002CU+0020>' . join ( indexes_by_name . keys ( ) ) <EOL> ) ) <EOL> index = indexes_by_name [ index_name ] [ '<STR_LIT>' ] <EOL> else : <EOL> index = table . schema <EOL> key_map = [ column for _ , column in sorted ( ( k , v ) for k , v in self . body [ '<STR_LIT>' ] . items ( ) ) ] <EOL> if "<STR_LIT>" in key_condition_expression : <EOL> expressions = key_condition_expression . split ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> index_hash_key = [ key for key in index if key [ '<STR_LIT>' ] == '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> hash_key_index_in_key_map = key_map . index ( index_hash_key [ '<STR_LIT>' ] ) <EOL> hash_key_expression = expressions . pop ( hash_key_index_in_key_map ) . strip ( '<STR_LIT>' ) <EOL> range_key_expression = expressions [ <NUM_LIT:0> ] . strip ( '<STR_LIT>' ) <EOL> range_key_expression_components = range_key_expression . split ( ) <EOL> range_comparison = range_key_expression_components [ <NUM_LIT:1> ] <EOL> if '<STR_LIT>' in range_key_expression : <EOL> range_comparison = '<STR_LIT>' <EOL> range_values = [ <EOL> value_alias_map [ range_key_expression_components [ <NUM_LIT:2> ] ] , <EOL> value_alias_map [ range_key_expression_components [ <NUM_LIT:4> ] ] , <EOL> ] <EOL> elif '<STR_LIT>' in range_key_expression : <EOL> range_comparison = '<STR_LIT>' <EOL> range_values = [ <EOL> value_alias_map [ range_key_expression_components [ <NUM_LIT:1> ] ] , <EOL> ] <EOL> else : <EOL> range_values = [ value_alias_map [ range_key_expression_components [ <NUM_LIT:2> ] ] ] <EOL> else : <EOL> hash_key_expression = key_condition_expression <EOL> range_comparison = None <EOL> range_values = [ ] <EOL> hash_key_value_alias = hash_key_expression . split ( "<STR_LIT:=>" ) [ <NUM_LIT:1> ] . strip ( ) <EOL> hash_key = value_alias_map [ hash_key_value_alias ] <EOL> else : <EOL> key_conditions = self . body . get ( '<STR_LIT>' ) <EOL> if key_conditions : <EOL> hash_key_name , range_key_name = dynamodb_backend2 . get_table_keys_name ( name , key_conditions . keys ( ) ) <EOL> for key , value in key_conditions . items ( ) : <EOL> if key not in ( hash_key_name , range_key_name ) : <EOL> filter_kwargs [ key ] = value <EOL> if hash_key_name is None : <EOL> er = "<STR_LIT>" <EOL> return self . error ( er ) <EOL> hash_key = key_conditions [ hash_key_name ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> if len ( key_conditions ) == <NUM_LIT:1> : <EOL> range_comparison = None <EOL> range_values = [ ] <EOL> else : <EOL> if range_key_name is None and not filter_kwargs : <EOL> er = "<STR_LIT>" <EOL> return self . error ( er ) <EOL> else : <EOL> range_condition = key_conditions . get ( range_key_name ) <EOL> if range_condition : <EOL> range_comparison = range_condition [ '<STR_LIT>' ] <EOL> range_values = range_condition [ '<STR_LIT>' ] <EOL> else : <EOL> range_comparison = None <EOL> range_values = [ ] <EOL> index_name = self . body . get ( '<STR_LIT>' ) <EOL> exclusive_start_key = self . body . get ( '<STR_LIT>' ) <EOL> limit = self . body . get ( "<STR_LIT>" ) <EOL> scan_index_forward = self . body . get ( "<STR_LIT>" ) <EOL> items , scanned_count , last_evaluated_key = dynamodb_backend2 . query ( <EOL> name , hash_key , range_comparison , range_values , limit , <EOL> exclusive_start_key , scan_index_forward , index_name = index_name , ** filter_kwargs ) <EOL> if items is None : <EOL> er = '<STR_LIT>' <EOL> return self . error ( er ) <EOL> result = { <EOL> "<STR_LIT>" : len ( items ) , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : scanned_count <EOL> } <EOL> if self . body . get ( '<STR_LIT>' , '<STR_LIT>' ) . upper ( ) != '<STR_LIT>' : <EOL> result [ "<STR_LIT>" ] = [ item . attrs for item in items ] <EOL> if last_evaluated_key is not None : <EOL> result [ "<STR_LIT>" ] = last_evaluated_key <EOL> return dynamo_json_dump ( result ) <EOL> def scan ( self ) : <EOL> name = self . body [ '<STR_LIT>' ] <EOL> filters = { } <EOL> scan_filters = self . body . get ( '<STR_LIT>' , { } ) <EOL> for attribute_name , scan_filter in scan_filters . items ( ) : <EOL> comparison_operator = scan_filter [ "<STR_LIT>" ] <EOL> comparison_values = scan_filter . get ( "<STR_LIT>" , [ ] ) <EOL> filters [ attribute_name ] = ( comparison_operator , comparison_values ) <EOL> exclusive_start_key = self . body . get ( '<STR_LIT>' ) <EOL> limit = self . body . get ( "<STR_LIT>" ) <EOL> items , scanned_count , last_evaluated_key = dynamodb_backend2 . scan ( name , filters , <EOL> limit , <EOL> exclusive_start_key ) <EOL> if items is None : <EOL> er = '<STR_LIT>' <EOL> return self . error ( er ) <EOL> result = { <EOL> "<STR_LIT>" : len ( items ) , <EOL> "<STR_LIT>" : [ item . attrs for item in items ] , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : scanned_count <EOL> } <EOL> if last_evaluated_key is not None : <EOL> result [ "<STR_LIT>" ] = last_evaluated_key <EOL> return dynamo_json_dump ( result ) <EOL> def delete_item ( self ) : <EOL> name = self . body [ '<STR_LIT>' ] <EOL> keys = self . body [ '<STR_LIT>' ] <EOL> return_values = self . body . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> item = dynamodb_backend2 . delete_item ( name , keys ) <EOL> if item : <EOL> if return_values == '<STR_LIT>' : <EOL> item_dict = item . to_json ( ) <EOL> else : <EOL> item_dict = { '<STR_LIT>' : { } } <EOL> item_dict [ '<STR_LIT>' ] = <NUM_LIT:0.5> <EOL> return dynamo_json_dump ( item_dict ) <EOL> else : <EOL> er = '<STR_LIT>' <EOL> return self . error ( er ) <EOL> def update_item ( self ) : <EOL> name = self . body [ '<STR_LIT>' ] <EOL> key = self . body [ '<STR_LIT>' ] <EOL> update_expression = self . body . get ( '<STR_LIT>' ) <EOL> attribute_updates = self . body . get ( '<STR_LIT>' ) <EOL> existing_item = dynamodb_backend2 . get_item ( name , key ) <EOL> item = dynamodb_backend2 . update_item ( name , key , update_expression , attribute_updates ) <EOL> item_dict = item . to_json ( ) <EOL> item_dict [ '<STR_LIT>' ] = <NUM_LIT:0.5> <EOL> if not existing_item : <EOL> item_dict [ '<STR_LIT>' ] = { } <EOL> return dynamo_json_dump ( item_dict ) </s>
