Support Python 3 using six

This commit is contained in:
David Baumgold 2014-08-26 13:25:50 -04:00
commit eedb4c4b73
67 changed files with 455 additions and 255 deletions

View file

@ -31,8 +31,8 @@ class DynamoType(object):
"""
def __init__(self, type_as_dict):
self.type = type_as_dict.keys()[0]
self.value = type_as_dict.values()[0]
self.type = list(type_as_dict.keys())[0]
self.value = list(type_as_dict.values())[0]
def __hash__(self):
return hash((self.type, self.value))
@ -66,7 +66,7 @@ class Item(object):
self.range_key_type = range_key_type
self.attrs = {}
for key, value in attrs.iteritems():
for key, value in attrs.items():
self.attrs[key] = DynamoType(value)
def __repr__(self):
@ -74,7 +74,7 @@ class Item(object):
def to_json(self):
attributes = {}
for attribute_key, attribute in self.attrs.iteritems():
for attribute_key, attribute in self.attrs.items():
attributes[attribute_key] = attribute.value
return {
@ -84,7 +84,7 @@ class Item(object):
def describe_attrs(self, attributes):
if attributes:
included = {}
for key, value in self.attrs.iteritems():
for key, value in self.attrs.items():
if key in attributes:
included[key] = value
else:
@ -143,7 +143,7 @@ class Table(object):
def __len__(self):
count = 0
for key, value in self.items.iteritems():
for key, value in self.items.items():
if self.has_range_key:
count += len(value)
else:
@ -213,7 +213,7 @@ class Table(object):
for result in self.all_items():
scanned_count += 1
passes_all_conditions = True
for attribute_name, (comparison_operator, comparison_objs) in filters.iteritems():
for attribute_name, (comparison_operator, comparison_objs) in filters.items():
attribute = result.attrs.get(attribute_name)
if attribute:
@ -296,7 +296,7 @@ class DynamoDBBackend(BaseBackend):
return None, None, None
scan_filters = {}
for key, (comparison_operator, comparison_values) in filters.iteritems():
for key, (comparison_operator, comparison_values) in filters.items():
dynamo_types = [DynamoType(value) for value in comparison_values]
scan_filters[key] = (comparison_operator, dynamo_types)

View file

@ -1,5 +1,6 @@
from __future__ import unicode_literals
import json
import six
from moto.core.responses import BaseResponse
from moto.core.utils import camelcase_to_underscores
@ -50,15 +51,16 @@ class DynamoHandler(BaseResponse):
return status, self.response_headers, dynamo_json_dump({'__type': type_})
def call_action(self):
if 'GetSessionToken' in self.body:
body = self.body.decode('utf-8')
if 'GetSessionToken' in body:
return 200, self.response_headers, sts_handler()
self.body = json.loads(self.body or '{}')
self.body = json.loads(body or '{}')
endpoint = self.get_endpoint_name(self.headers)
if endpoint:
endpoint = camelcase_to_underscores(endpoint)
response = getattr(self, endpoint)()
if isinstance(response, basestring):
if isinstance(response, six.string_types):
return 200, self.response_headers, response
else:
@ -73,10 +75,10 @@ class DynamoHandler(BaseResponse):
limit = body.get('Limit')
if body.get("ExclusiveStartTableName"):
last = body.get("ExclusiveStartTableName")
start = dynamodb_backend.tables.keys().index(last) + 1
start = list(dynamodb_backend.tables.keys()).index(last) + 1
else:
start = 0
all_tables = dynamodb_backend.tables.keys()
all_tables = list(dynamodb_backend.tables.keys())
if limit:
tables = all_tables[start:start + limit]
else:
@ -155,7 +157,7 @@ class DynamoHandler(BaseResponse):
def batch_write_item(self):
table_batches = self.body['RequestItems']
for table_name, table_requests in table_batches.iteritems():
for table_name, table_requests in table_batches.items():
for table_request in table_requests:
request_type = table_request.keys()[0]
request = table_request.values()[0]
@ -212,7 +214,7 @@ class DynamoHandler(BaseResponse):
}
}
for table_name, table_request in table_batches.iteritems():
for table_name, table_request in table_batches.items():
items = []
keys = table_request['Keys']
attributes_to_get = table_request.get('AttributesToGet')
@ -262,7 +264,7 @@ class DynamoHandler(BaseResponse):
filters = {}
scan_filters = self.body.get('ScanFilter', {})
for attribute_name, scan_filter in scan_filters.iteritems():
for attribute_name, scan_filter in scan_filters.items():
# Keys are attribute names. Values are tuples of (comparison, comparison_value)
comparison_operator = scan_filter["ComparisonOperator"]
comparison_values = scan_filter.get("AttributeValueList", [])