# -*- coding: utf-8 -*-

import boto
from boto.dynamodb.exceptions import DynamoDBKeyNotFoundError


class Table(object):
    def __init__(self, table=None, eager=False):
        self.table = table
        self.is_eager = eager

    def __repr__(self):
        return '<table \'{0}\'>'.format(self.name)

    @property
    def name(self):
        return self.table.__dict__['_dict']['TableName']

    def item(self, item):
        return Item(item, self)

    def delete(self):
        return self.table.delete()

    def scale(self, read=None, write=None):
        read = read or self.table.read_units
        write = write or self.table.read_units

        return self.table.update_throughput(read_units=read, write_units=write)

    def __getitem__(self, key):
        try:
            i = self.table.get_item(key)
            i = self.item(i)
        except DynamoDBKeyNotFoundError:
            return self.__magic_get(key)
            # i = self.item(i)

        return i

    def get(self, key, default=None):
        try:
            return self[key]
        except KeyError:
            return default

    def __setitem__(self, key, values):
        i = self.table.new_item(key, attrs=values)
        i = self.item(i)
        i.put()
        return i

    def __delitem__(self, key):
        return self[key].delete()

    def __magic_get(self, key):
        if self.is_eager:
            self[key] = {}
            return self.item(self[key])

    def __contains__(self, key):
        return not self.get(key) is None

    def new(self, name):

        table = self.table.layer2.create_table(
            name=name,
            schema=self.table._schema,
            read_units=self.table.read_units,
            write_units=self.table.write_units
        )

        return Table(table=table, eager=self.is_eager)


class Item(object):
    def __init__(self, item, table):
        self.item = item
        self.table = table

    @property
    def is_eager(self):
        return self.table.is_eager

    def __getattr__(self, key):
        if not key in ['item']:
            try:
                return getattr(object, key)
            except AttributeError:
                return getattr(self.item, key)

    def get(self, key, default=None):
        try:
            return self[key]
        except KeyError:
            return default

    def __repr__(self):
        return repr(self.item)

    def __getitem__(self, key):
        return self.item[key]

    def __setitem__(self, key, value):
        self.item[key] = value

        if self.is_eager:
            self.item.save()

    def __contains__(self, key):
        return key in self.item


def table(name, auth, eager=True):
    """Returns a given table for the given user."""

    dynamodb = boto.connect_dynamodb(*auth)
    table = dynamodb.get_table(name)

    return Table(table=table, eager=eager)


def tables(auth, eager=True):
    """Returns a list of tables for the given user."""

    dynamodb = boto.connect_dynamodb(*auth)
    return [table(t, auth, eager=eager) for t in dynamodb.list_tables()]
@@ -30,16 +30,43 @@
 rt boto%0A
+from numbers import Number%0A
 from bot
@@ -119,16 +119,16 @@
 ndError%0A
-
 %0A%0Aclass 
@@ -670,17 +670,16 @@
 d_units%0A
-%0A
         
@@ -788,32 +788,114 @@
 ):%0A        try:%0A
+            if isinstance(key, (basestring, Number)):%0A                key = %5Bkey%5D%0A
             i = 
@@ -914,16 +914,17 @@
 et_item(
+*
 key)%0A   
@@ -1035,40 +1035,8 @@
 ey)%0A
-            # i = self.item(i)%0A%0A
     
@@ -1222,16 +1222,90 @@
 alues):%0A
+        if isinstance(key, (basestring, Number)):%0A            key = %5Bkey%5D%0A
         
@@ -1328,16 +1328,17 @@
 ew_item(
+*
 key, att
@@ -1700,17 +1700,16 @@
  name):%0A
-%0A
         
@@ -1913,17 +1913,16 @@
       )%0A
-%0A
         
@@ -2684,17 +2684,16 @@
 = value%0A
-%0A
         
@@ -2886,33 +2886,32 @@
  given user.%22%22%22%0A
-%0A
     dynamodb = b
@@ -2971,25 +2971,24 @@
 table(name)%0A
-%0A
     return T
@@ -3105,17 +3105,16 @@
 ser.%22%22%22%0A
-%0A
     dyna
@@ -3145,24 +3145,24 @@
 modb(*auth)%0A
-
     return %5B
@@ -3221,8 +3221,9 @@
 ables()%5D
+%0A

add support for range key