# Path: newsApp/cachingHelper.py
# def getCache():
#   servers = os.environ.get('MEMCACHIER_SERVERS', '').split(',')
#   user = os.environ.get('MEMCACHIER_USERNAME', '')
#   passw = os.environ.get('MEMCACHIER_PASSWORD', '')
# 
#   return bmemcached.Client(servers, user, passw)
# 
# Path: newsApp/constants.py
# LINKTAG_PUBTIME = 'pubtime'
# 
# FEEDTAG_DO_NOT_CLUSTER = 'doNotCluster'
# 
# Path: newsApp/dbhelper.py
# def parseConnectionString(connectionString):
#     """
#     Parses the table connection string. Returns a dict of parameters found.
#     """
# 
#     return dict(item.split('=') for item in connectionString.split(';'))
# 
# def getS3Connection(connectionString):
#     connectionParams = parseConnectionString(connectionString)
# 
#     connection = S3Connection(
#         connectionParams['accessKeyId'],
#         connectionParams['secretAccessKey'])
# 
#     return connection
# 
# 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;


import os
import json
import calendar
import time
from boto.s3.connection import S3Connection
from boto.s3.key import Key
from .cachingHelper import getCache
from .constants import LINKTAG_PUBTIME, FEEDTAG_DO_NOT_CLUSTER
from .dbhelper import parseConnectionString, getS3Connection
from .doc import Doc
            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


# Path: newsApp/cachingHelper.py
# def getCache():
#   servers = os.environ.get('MEMCACHIER_SERVERS', '').split(',')
#   user = os.environ.get('MEMCACHIER_USERNAME', '')
#   passw = os.environ.get('MEMCACHIER_PASSWORD', '')
# 
#   return bmemcached.Client(servers, user, passw)
# 
# Path: newsApp/constants.py
# LINKTAG_PUBTIME = 'pubtime'
# 
# FEEDTAG_DO_NOT_CLUSTER = 'doNotCluster'
# 
# Path: newsApp/dbhelper.py
# def parseConnectionString(connectionString):
#     """
#     Parses the table connection string. Returns a dict of parameters found.
#     """
# 
#     return dict(item.split('=') for item in connectionString.split(';'))
# 
# def getS3Connection(connectionString):
#     connectionParams = parseConnectionString(connectionString)
# 
#     connection = S3Connection(
#         connectionParams['accessKeyId'],
#         connectionParams['secretAccessKey'])
# 
#     return connection
# 
# 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
import os
import json
import calendar
import time
from boto.s3.connection import S3Connection
from boto.s3.key import Key
from .cachingHelper import getCache
from .constants import LINKTAG_PUBTIME, FEEDTAG_DO_NOT_CLUSTER
from .dbhelper import parseConnectionString, getS3Connection
from .doc import Doc

            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)