<commit_before># -*- coding: utf-8 -*-

# MemcachedUtility is loosely based on https://github.com/dlrust/python-memcached-stats

from django_clear_memcache.utility import MemcachedUtility
from django.conf import settings
from django.core.cache.backends.memcached import BaseMemcachedCache
from django.core.cache import cache, DEFAULT_CACHE_ALIAS
from django.core.exceptions import ImproperlyConfigured
from django.utils import six


########################################################################
class ClearMemcacheNoCacheFoundError(ImproperlyConfigured):
    pass


########################################################################
class ClearMemcacheController(object):

    #----------------------------------------------------------------------
    def __init__(self):
        self._servers = None
        self._host = None
        self._port = None
        self._cache_key_prefix = None
        self._keys = None
        self._init()

    #----------------------------------------------------------------------
    def _init(self):
        if not isinstance(cache, BaseMemcachedCache):
            raise ClearMemcacheNoCacheFoundError(u'Unknown memcached backend or no memcached backend found')

        # get cache key prefix
        self._cache_key_prefix = cache.key_prefix
        # LOCATION can be a string or a sequence
        server = settings.CACHES[DEFAULT_CACHE_ALIAS]['LOCATION']
        if isinstance(server, six.string_types):
            servers = server.split(';')
        else:
            servers = server

        self._servers = list()
        for server in servers:
            host, port = server.split(':')
            if self._host == u'unix':
                # for now, ignore unix domain sockets
                continue
            self._servers.append((host, port))

    #----------------------------------------------------------------------
    def keys(self, use_prefix=True, refresh=False):
        # cache keys per instance
        if self._keys is None or refresh:
            self._keys = list()<commit_msg>Django 1.7 support: check for _lib atribute on the cache class<commit_after># -*- coding: utf-8 -*-

# MemcachedUtility is loosely based on https://github.com/dlrust/python-memcached-stats

from django_clear_memcache.utility import MemcachedUtility
from django.conf import settings
from django.core.cache import cache, DEFAULT_CACHE_ALIAS
from django.core.exceptions import ImproperlyConfigured
from django.utils import six


########################################################################
class ClearMemcacheNoCacheFoundError(ImproperlyConfigured):
    pass


########################################################################
class ClearMemcacheController(object):

    #----------------------------------------------------------------------
    def __init__(self):
        self._servers = None
        self._host = None
        self._port = None
        self._cache_key_prefix = None
        self._keys = None
        self._init()

    #----------------------------------------------------------------------
    def _init(self):
        # support django-debug-toolbar which wraps the cache instance
        cache_ = getattr(cache, 'cache', cache)
        # check if we got a usable cache instance
        if not hasattr(cache_, '_lib'):
            raise ClearMemcacheNoCacheFoundError(u'Unknown memcached backend or no memcached backend found')

        # get cache key prefix
        self._cache_key_prefix = cache.key_prefix
        # LOCATION can be a string or a sequence
        server = settings.CACHES[DEFAULT_CACHE_ALIAS]['LOCATION']
        if isinstance(server, six.string_types):
            servers = server.split(';')
        else:
            servers = server

        self._servers = list()
        for server in servers:
            host, port = server.split(':')
            if self._host == u'unix':
                # for now, ignore unix domain sockets
                continue
            self._servers.append((host, port))

    #----------------------------------------------------------------------
    def keys(self, use_prefix=True, refresh=False):
        # cache keys per instance
        if self._keys is None or refresh:
            self._keys = list()
{'fraction_non_alphanumeric': 0.23340370631011026, 'fraction_numerical': 0.0009383063570255688, 'mean_word_length': 1.961111111111111, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5500482', 'n_tokens_mistral': 1001, 'n_tokens_neox': 977, 'n_words': 280}