# Path: partycrasher/api/report_threshold.py
# class ReportThreshold(Threshold):
#     def __init__(self, search, result, from_=None, size=None):
#         super(ReportThreshold, self).__init__(result)
#         search = BucketSearch(search=search, threshold=Threshold(self))
#         self.buckets = BucketSearch(
#             search=search,
#             from_=from_,
#             size=size
#             )
#     
#     def restify(self):
#         return self.buckets
# 
# Path: partycrasher/api/thresholds.py
# class Thresholds(Mapping):
#     """
#     Represents the thresholds available under a certain search.
#     """
#     def __init__(self, search):
#         self.search = search
#         # Lazy-load projects
#         self._d = self.get_thresholds()
#     
#     def get_thresholds(self):
#         thresholds = {
#             t: ReportThreshold(self.search, t) for t in self.search.thresholds
#             }
#         return thresholds
#     
#     def __getitem__(self, key):
#         return self._d.__getitem__(key)
#     
#     def __iter__(self):
#         return self._d.__iter__()
# 
#     def __len__(self):
#         return self._d.__len__()
#     
#     def restify(self):
#         d = {}
#         for k, v in self._d.items():
#             d[str(k)] = v
#         return d


import logging
import weakref
from partycrasher.api.report_threshold import ReportThreshold
from partycrasher.api.thresholds import Thresholds
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

logger = logging.getLogger(__name__)
ERROR = logger.error
WARN = logger.warn
INFO = logger.info
DEBUG = logger.debug



#cached_thresholds = weakref.WeakValueDictionary()
cached_thresholds = {}

def cached_threshold(search):
    global cached_thresholds
    if search in cached_thresholds:
        #DEBUG("HIT")
        return cached_thresholds[search]
    else:
        #DEBUG("MISS " + repr(search._d))
        if search['threshold'] is None:
            buckets = Thresholds(search)

# Path: partycrasher/api/report_threshold.py
# class ReportThreshold(Threshold):
#     def __init__(self, search, result, from_=None, size=None):
#         super(ReportThreshold, self).__init__(result)
#         search = BucketSearch(search=search, threshold=Threshold(self))
#         self.buckets = BucketSearch(
#             search=search,
#             from_=from_,
#             size=size
#             )
#     
#     def restify(self):
#         return self.buckets
# 
# Path: partycrasher/api/thresholds.py
# class Thresholds(Mapping):
#     """
#     Represents the thresholds available under a certain search.
#     """
#     def __init__(self, search):
#         self.search = search
#         # Lazy-load projects
#         self._d = self.get_thresholds()
#     
#     def get_thresholds(self):
#         thresholds = {
#             t: ReportThreshold(self.search, t) for t in self.search.thresholds
#             }
#         return thresholds
#     
#     def __getitem__(self, key):
#         return self._d.__getitem__(key)
#     
#     def __iter__(self):
#         return self._d.__iter__()
# 
#     def __len__(self):
#         return self._d.__len__()
#     
#     def restify(self):
#         d = {}
#         for k, v in self._d.items():
#             d[str(k)] = v
#         return d


# Path: partycrasher/api/cache.py
import logging
import weakref
from partycrasher.api.report_threshold import ReportThreshold
from partycrasher.api.thresholds import Thresholds

#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

logger = logging.getLogger(__name__)
ERROR = logger.error
WARN = logger.warn
INFO = logger.info
DEBUG = logger.debug



#cached_thresholds = weakref.WeakValueDictionary()
cached_thresholds = {}

def cached_threshold(search):
    global cached_thresholds
    if search in cached_thresholds:
        #DEBUG("HIT")
        return cached_thresholds[search]
    else:
        #DEBUG("MISS " + repr(search._d))
        if search['threshold'] is None:
            buckets = Thresholds(search)

        else: