# Path: newsApp/doc.py
# class Doc:
#   """
#   Represents a document.
# 
#   Each doc consists of an auto-generated id, original, processed content
#   and some tags(which are key-value pairs to help in clustering).
#   """
# 
#   def __init__(self, key, content, tags):
#     """
#     Instantiates a new document object.
# 
#     """
# 
#     self.key = key.upper();
#     self.content = content;
#     self.tags = tags;
# 
# Path: newsApp/docManager.py
# class DocManager:
#     """
#     Manage documents stored in cloud.
# 
#     Contains functions for CRUD operations on documents
#     """
# 
#     def __init__(self):
#         """
#         Instantiates a new instance of DocManager class
# 
#         'bucketConnString' : connection string of s3 bucket in which docs
#         are stored.
#         """
# 
#         self.bucketConnString = os.environ['DOCSBUCKET_CONNECTIONSTRING']
#         self.cache = getCache()
#         self.__cacheExpiry= 900
# 
#     def __getBucket(self):
#         bucketConnParams = parseConnectionString(self.bucketConnString)
#         conn = getS3Connection(self.bucketConnString)
# 
#         return conn.get_bucket(bucketConnParams['bucketName'], validate=False)
# 
#     def __isDocNew(self, key, timeLimit):
#         if _getEpochSecs(key.last_modified) < timeLimit:
#             return False
# 
#         doc = self.get(key.name)
#         return (doc.tags[LINKTAG_PUBTIME] > timeLimit) and \
#             (FEEDTAG_DO_NOT_CLUSTER not in doc.tags)
# 
#     def put(self, doc):
#         k = Key(self.__getBucket())
#         k.key = doc.key
# 
#         # not storing tags directly in blob's metadata as the maximum size
#         # allowed there is only 2kb.
#         tags = dict(doc.tags)
#         tags['content'] = doc.content
#         keyContents = json.dumps(tags)
#         k.set_contents_from_string(keyContents)
#         self.cache.set(k.key, keyContents, self.__cacheExpiry)
# 
#     def get(self, docKey):
#         keyContents = self.cache.get(docKey)
#         if not keyContents:
#             k = Key(self.__getBucket())
#             k.key = docKey
#             keyContents = k.get_contents_as_string()
#             self.cache.set(docKey, keyContents, self.__cacheExpiry)
# 
#         storedTags = json.loads(keyContents)
#         content = storedTags.pop('content', None)
#         tags = storedTags
# 
#         return Doc(docKey, content, tags)
# 
#     def delete(self, docKey):
#         k = Key(self.__getBucket())
#         k.key = docKey
#         k.delete()
#         self.cache.delete(docKey)


import unittest
from newsApp.doc import Doc
from newsApp.docManager import DocManager


class DocManagerTests(unittest.TestCase):

    def testPutGetDeleteDoc(self):
        testDocManager = DocManager()

# Path: newsApp/doc.py
# class Doc:
#   """
#   Represents a document.
# 
#   Each doc consists of an auto-generated id, original, processed content
#   and some tags(which are key-value pairs to help in clustering).
#   """
# 
#   def __init__(self, key, content, tags):
#     """
#     Instantiates a new document object.
# 
#     """
# 
#     self.key = key.upper();
#     self.content = content;
#     self.tags = tags;
# 
# Path: newsApp/docManager.py
# class DocManager:
#     """
#     Manage documents stored in cloud.
# 
#     Contains functions for CRUD operations on documents
#     """
# 
#     def __init__(self):
#         """
#         Instantiates a new instance of DocManager class
# 
#         'bucketConnString' : connection string of s3 bucket in which docs
#         are stored.
#         """
# 
#         self.bucketConnString = os.environ['DOCSBUCKET_CONNECTIONSTRING']
#         self.cache = getCache()
#         self.__cacheExpiry= 900
# 
#     def __getBucket(self):
#         bucketConnParams = parseConnectionString(self.bucketConnString)
#         conn = getS3Connection(self.bucketConnString)
# 
#         return conn.get_bucket(bucketConnParams['bucketName'], validate=False)
# 
#     def __isDocNew(self, key, timeLimit):
#         if _getEpochSecs(key.last_modified) < timeLimit:
#             return False
# 
#         doc = self.get(key.name)
#         return (doc.tags[LINKTAG_PUBTIME] > timeLimit) and \
#             (FEEDTAG_DO_NOT_CLUSTER not in doc.tags)
# 
#     def put(self, doc):
#         k = Key(self.__getBucket())
#         k.key = doc.key
# 
#         # not storing tags directly in blob's metadata as the maximum size
#         # allowed there is only 2kb.
#         tags = dict(doc.tags)
#         tags['content'] = doc.content
#         keyContents = json.dumps(tags)
#         k.set_contents_from_string(keyContents)
#         self.cache.set(k.key, keyContents, self.__cacheExpiry)
# 
#     def get(self, docKey):
#         keyContents = self.cache.get(docKey)
#         if not keyContents:
#             k = Key(self.__getBucket())
#             k.key = docKey
#             keyContents = k.get_contents_as_string()
#             self.cache.set(docKey, keyContents, self.__cacheExpiry)
# 
#         storedTags = json.loads(keyContents)
#         content = storedTags.pop('content', None)
#         tags = storedTags
# 
#         return Doc(docKey, content, tags)
# 
#     def delete(self, docKey):
#         k = Key(self.__getBucket())
#         k.key = docKey
#         k.delete()
#         self.cache.delete(docKey)


# Path: tests/docManager_test.py
import unittest
from newsApp.doc import Doc
from newsApp.docManager import DocManager



class DocManagerTests(unittest.TestCase):

    def testPutGetDeleteDoc(self):
        testDocManager = DocManager()

        testDoc = Doc(