# Path: newsApp/dbItemManagerV2.py
# class DbItemManagerV2:
#     """
#     Manage DbItems stored on AWS dynamo db database.
# 
#     Contains functions for CRUD operations on the DbItems stored
#     """
# 
#     def __init__(self, tableConnString):
#         """
#         Instantiates a new instance of DbItemManager class
# 
#         'TableConnString' : connection string of the table.
#         """
# 
#         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)
# 
#     def __queryTableRow(self, itemId):
# 
#         table = self.__getTable()
#         return table.get_item(itemId = itemId)
# 
#     @retry(stop_max_attempt_number=3)
#     def put(self, item):
#         """
#         Put a new item.
#         """
# 
#         tableData = {}
#         tableData['itemId'] = item.id
#         for tagName in item.tags:
#             tableData[tagName] = item.tags[tagName];
# 
#         table = self.__getTable();
#         table.put_item(data = tableData, overwrite = True)
# 
#     def get(self, itemId):
#         """
#         Get item with the specified Id.
#         """
# 
#         tableRow = self.__queryTableRow(itemId)
#         return self.__getItemFromTableRow(tableRow)
# 
#     def scan(self, **filters):
#         table = self.__getTable()
#         scanResults = table.scan(**filters);
#         return (self.__getItemFromTableRow(row) for row in scanResults)
# 
#     def query_2(self, **filters):
#         table = self.__getTable()
#         results = table.query_2(**filters);
#         return (row['itemId'] for row in results)
# 
#     def delete(self, itemId):
#         """
#         Delete item with the specified Id from the database.
#         """
# 
#         table = self.__getTable()
#         table.delete_item(itemId = itemId)
# 
# Path: newsApp/link.py
# class Link(DbItem):
#   """
#   Represents a link to a webPage.
# 
#   Each wlink consists of a unique identifier(the url)
#   and a set of tags(key-value pairs).
#   """
# 
#   def __init__(self, id, tags=None):
#     """
#     Instantiates a link object representing a link to a web page.
#     """
# 
#     DbItem.__init__(self, id, tags)
# 
#   def checkExistence(self):
#     try:
#       _openUrlWithRetries(self.id)
#     except:
#       return False
# 
#     return True
# 
#   def getFinalRedirect(self):
#     return getIdentifierUrl(self.id)
# 
#   def getHtmlDynamic(self):
#     return loadPageAndGetHtml(self.id)
# 
#   def getHtmlStatic(self):
#     return getHtmlStatic(self.id)


import os
import time
from .constants import *
from .dbhelper import *
from .dbItemManagerV2 import DbItemManagerV2
from .link import Link


LINK_EXPIRY_TIME_IN_DAYS = 80

class LinkManager(DbItemManagerV2):
    """
    Manage links stored on AWS dynamo db database.

    Contains functions for CRUD operations on the links stored

    Following environment variables need to be set -
    'LINKTAGSTABLE_CONNECTIONSTRING' : connection string of link tags table.
    """

    def __init__(self):
        """
        Instantiates the linkManager.
        """

        DbItemManagerV2.__init__(self,
            os.environ['LINKTAGSTABLE_CONNECTIONSTRING'])

    def get(self, linkId):
        """
        Put a new link.
        """

        dbItem = DbItemManagerV2.get(self, linkId)

# Path: newsApp/dbItemManagerV2.py
# class DbItemManagerV2:
#     """
#     Manage DbItems stored on AWS dynamo db database.
# 
#     Contains functions for CRUD operations on the DbItems stored
#     """
# 
#     def __init__(self, tableConnString):
#         """
#         Instantiates a new instance of DbItemManager class
# 
#         'TableConnString' : connection string of the table.
#         """
# 
#         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)
# 
#     def __queryTableRow(self, itemId):
# 
#         table = self.__getTable()
#         return table.get_item(itemId = itemId)
# 
#     @retry(stop_max_attempt_number=3)
#     def put(self, item):
#         """
#         Put a new item.
#         """
# 
#         tableData = {}
#         tableData['itemId'] = item.id
#         for tagName in item.tags:
#             tableData[tagName] = item.tags[tagName];
# 
#         table = self.__getTable();
#         table.put_item(data = tableData, overwrite = True)
# 
#     def get(self, itemId):
#         """
#         Get item with the specified Id.
#         """
# 
#         tableRow = self.__queryTableRow(itemId)
#         return self.__getItemFromTableRow(tableRow)
# 
#     def scan(self, **filters):
#         table = self.__getTable()
#         scanResults = table.scan(**filters);
#         return (self.__getItemFromTableRow(row) for row in scanResults)
# 
#     def query_2(self, **filters):
#         table = self.__getTable()
#         results = table.query_2(**filters);
#         return (row['itemId'] for row in results)
# 
#     def delete(self, itemId):
#         """
#         Delete item with the specified Id from the database.
#         """
# 
#         table = self.__getTable()
#         table.delete_item(itemId = itemId)
# 
# Path: newsApp/link.py
# class Link(DbItem):
#   """
#   Represents a link to a webPage.
# 
#   Each wlink consists of a unique identifier(the url)
#   and a set of tags(key-value pairs).
#   """
# 
#   def __init__(self, id, tags=None):
#     """
#     Instantiates a link object representing a link to a web page.
#     """
# 
#     DbItem.__init__(self, id, tags)
# 
#   def checkExistence(self):
#     try:
#       _openUrlWithRetries(self.id)
#     except:
#       return False
# 
#     return True
# 
#   def getFinalRedirect(self):
#     return getIdentifierUrl(self.id)
# 
#   def getHtmlDynamic(self):
#     return loadPageAndGetHtml(self.id)
# 
#   def getHtmlStatic(self):
#     return getHtmlStatic(self.id)


# Path: newsApp/linkManager.py
import os
import time
from .constants import *
from .dbhelper import *
from .dbItemManagerV2 import DbItemManagerV2
from .link import Link



LINK_EXPIRY_TIME_IN_DAYS = 80

class LinkManager(DbItemManagerV2):
    """
    Manage links stored on AWS dynamo db database.

    Contains functions for CRUD operations on the links stored

    Following environment variables need to be set -
    'LINKTAGSTABLE_CONNECTIONSTRING' : connection string of link tags table.
    """

    def __init__(self):
        """
        Instantiates the linkManager.
        """

        DbItemManagerV2.__init__(self,
            os.environ['LINKTAGSTABLE_CONNECTIONSTRING'])

    def get(self, linkId):
        """
        Put a new link.
        """

        dbItem = DbItemManagerV2.get(self, linkId)

        link = Link(linkId, dbItem.tags)