<commit_before>""" IMAPClient wrapper for Inbox.

Unfortunately, due to IMAP's statefulness, to implement connection pooling we
have to shunt off dealing with the connection pool to the caller or we'll end
up trying to execute calls with the wrong folder selected some amount of the
time. That's why functions take a connection argument.
"""
import os
import time

from .log import get_logger
from .pool import get_connection_pool

from ..util.misc import or_none
from ..util.cache import get_cache, set_cache

__all__ = ['CrispinClient', 'DummyCrispinClient']

### decorators

def timed(fn):
    """ A decorator for timing methods. """
    def timed_fn(self, *args, **kwargs):
        start_time = time.time()
        ret = fn(self, *args, **kwargs)
        self.log.info("\t\tTook {0} seconds".format(str(time.time() - start_time)))
        return ret
    return timed_fn

### main stuff

def new_crispin(account_id, provider, dummy=False):
    crispin_module_for = dict(Gmail=GmailCrispinClient, IMAP=CrispinClient,
        Yahoo=YahooCrispinClient)

    cls = DummyCrispinClient if dummy else crispin_module_for[provider]
    return cls(account_id, conn_pool_size=conn_pool_size)

class CrispinClientBase(object):
    """
    One thing to note about crispin clients is that *all* calls operate on
    the currently selected folder.

    Crispin will NEVER implicitly select a folder for you.

    This is very important! IMAP only guarantees that folder message UIDs
    are valid for a "session", which is defined as from the time you
    SELECT a folder until the connection is closed or another folder is
    selected.
    """
    def __init__(self, account_id, cache=False):
        self.log = get_logger(account_id)
        self.account_id = account_id
        # IMAP isn't stateless :(
        self.selected_folder = None
        self._folder_names = None
        self.cache = cache

    def set_cache(self, data, *keys):
        key = os.path.join('account.{0}'.format(self.account_id),
                *[str(key) for key in keys])
        return set_cache(key, data)
<commit_msg>define conn_pool_size default to 1<commit_after>""" IMAPClient wrapper for Inbox.

Unfortunately, due to IMAP's statefulness, to implement connection pooling we
have to shunt off dealing with the connection pool to the caller or we'll end
up trying to execute calls with the wrong folder selected some amount of the
time. That's why functions take a connection argument.
"""
import os
import time

from .log import get_logger
from .pool import get_connection_pool

from ..util.misc import or_none
from ..util.cache import get_cache, set_cache

__all__ = ['CrispinClient', 'DummyCrispinClient']

### decorators

def timed(fn):
    """ A decorator for timing methods. """
    def timed_fn(self, *args, **kwargs):
        start_time = time.time()
        ret = fn(self, *args, **kwargs)
        self.log.info("\t\tTook {0} seconds".format(str(time.time() - start_time)))
        return ret
    return timed_fn

### main stuff

def new_crispin(account_id, provider, dummy=False, conn_pool_size=1):
    crispin_module_for = dict(Gmail=GmailCrispinClient, IMAP=CrispinClient,
        Yahoo=YahooCrispinClient)

    cls = DummyCrispinClient if dummy else crispin_module_for[provider]
    return cls(account_id, conn_pool_size=conn_pool_size)

class CrispinClientBase(object):
    """
    One thing to note about crispin clients is that *all* calls operate on
    the currently selected folder.

    Crispin will NEVER implicitly select a folder for you.

    This is very important! IMAP only guarantees that folder message UIDs
    are valid for a "session", which is defined as from the time you
    SELECT a folder until the connection is closed or another folder is
    selected.
    """
    def __init__(self, account_id, cache=False):
        self.log = get_logger(account_id)
        self.account_id = account_id
        # IMAP isn't stateless :(
        self.selected_folder = None
        self._folder_names = None
        self.cache = cache

    def set_cache(self, data, *keys):
        key = os.path.join('account.{0}'.format(self.account_id),
                *[str(key) for key in keys])
        return set_cache(key, data)

{'fraction_non_alphanumeric': 0.08528784648187633, 'fraction_numerical': 0.0014214641080312722, 'mean_word_length': 3.4442105263157896, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6409468', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1234, 'n_words': 439}