# Path: newsApp/dbItem.py
# class DbItem:
#   """
#   Represents a generic item which can be added/updated/deleted/retrieved to a
#   database.
# 
#   Each dbItem consists of a unique identifier and a set of tags(which are simple key-value pairs).
#   """
# 
#   def __init__(self, id, tags=None):
#     """
#     Instantiates a new dbItem object.
# 
#     Requires a 'id' parameter which should be a simple representing a unique
#     identifier for the dbItem.
# 
#     Optionally accepts a 'tags' parameter which should be a dictionary of
#     key-value pairs. e.g. you can have a tag for the language of dbItem.
#     """
# 
#     self.id = id
#     self.tags = tags


import decimal
from boto.dynamodb2.table import Table
from boto.dynamodb2.fields import HashKey, RangeKey
from retrying import retry
from .dbItem import DbItem
from .dbhelper import *
        self.tableConnString = tableConnString;
        self.__table = None

    def __getTable(self):
        """
        Get the table.
        """

        if not self.__table:
            tableConnectionParams = parseConnectionString(
                self.tableConnString);

            self.__table = Table(
                tableConnectionParams['name'],
                connection = getDbConnection(tableConnectionParams))

        return self.__table

    def __getItemFromTableRow(self, tableRow):
        tags  = {}

        for field, value in list(tableRow.items()):
            if field != 'itemId':
                # boto retrieves numbers as decimals. Convert them to float
                # else we'll have json serialization issues down the pipeline
                if type(value) is decimal.Decimal:
                    tags[field] = float(value)
                else:
                    tags[field] = value


# Path: newsApp/dbItem.py
# class DbItem:
#   """
#   Represents a generic item which can be added/updated/deleted/retrieved to a
#   database.
# 
#   Each dbItem consists of a unique identifier and a set of tags(which are simple key-value pairs).
#   """
# 
#   def __init__(self, id, tags=None):
#     """
#     Instantiates a new dbItem object.
# 
#     Requires a 'id' parameter which should be a simple representing a unique
#     identifier for the dbItem.
# 
#     Optionally accepts a 'tags' parameter which should be a dictionary of
#     key-value pairs. e.g. you can have a tag for the language of dbItem.
#     """
# 
#     self.id = id
#     self.tags = tags


# Path: newsApp/dbItemManagerV2.py
import decimal
from boto.dynamodb2.table import Table
from boto.dynamodb2.fields import HashKey, RangeKey
from retrying import retry
from .dbItem import DbItem
from .dbhelper import *

        self.tableConnString = tableConnString;
        self.__table = None

    def __getTable(self):
        """
        Get the table.
        """

        if not self.__table:
            tableConnectionParams = parseConnectionString(
                self.tableConnString);

            self.__table = Table(
                tableConnectionParams['name'],
                connection = getDbConnection(tableConnectionParams))

        return self.__table

    def __getItemFromTableRow(self, tableRow):
        tags  = {}

        for field, value in list(tableRow.items()):
            if field != 'itemId':
                # boto retrieves numbers as decimals. Convert them to float
                # else we'll have json serialization issues down the pipeline
                if type(value) is decimal.Decimal:
                    tags[field] = float(value)
                else:
                    tags[field] = value


        return DbItem(tableRow['itemId'], tags)