<commit_before>from __future__ import print_function
import boto3
import botocore
import logging
from io import BytesIO

from botocore.utils import fix_s3_host
from cottoncandy.utils import *
from CCBackEnd import *

try:
    from urllib import unquote
except ImportError:
    from urllib.parse import unquote
try:
    import cPickle as pickle
except ImportError:
    import pickle


class S3Client(CCBackEnd):
    """
    S3 client interface refactored out of cotton candy interfaces to allow for switching between
    S3 and Google Drive
    """

    @staticmethod
    def connect(ACCESS_KEY, SECRET_KEY, url):
        """
        Connect to S3 using boto

        Parameters
        ----------
        ACCESS_KEY
        SECRET_KEY
        url

        Returns
        -------

        """
        s3 = boto3.resource('s3',
                            endpoint_url = url,
                            aws_access_key_id = ACCESS_KEY,
                            aws_secret_access_key = SECRET_KEY)
        s3.meta.client.meta.events.unregister('before-sign.s3', fix_s3_host)
        return s3

    def __init__(self, bucket, access_key, secret_key, s3url, force_bucket_creation = False):
        """
        Constructor

        Parameters
        ----------
        bucket
        access_key
        secret_key
        s3url
        force_bucket_creation
        """
        super(S3Client, self).__init__()

        self.connection = S3Client.connect(access_key, secret_key, s3url)
        self.url = s3url
        if self.check_bucket_exists(bucket):
            self.set_current_bucket(bucket)
        else:
            print('* Bucket "%s" does not exist' % bucket)
            if force_bucket_creation:
                print('* Creating "%s" bucket...\n' % bucket)
                self.create_bucket(bucket)
            else:
                print('* cottoncandy instantiated without bucket...\n' \
                      '* Use with caution!\n' \
                      '* Many features will not work!!!\n')
                self.bucket = None

        if string2bool(ISBOTO_VERBOSE) is False:
            logging.getLogger('boto3').setLevel(logging.WARNING)
            logging.getLogger('botocore').setLevel(logging.WARNING)

    def get_bucket_name(self, bucket_name):
        """

        Parameters
        ----------
        bucket_name

        Returns
        -------

        """
        bucket_name = self.bucket_name \
            if bucket_name is None \<commit_msg>accidentally bucket instead of bucket_name<commit_after>from __future__ import print_function
import boto3
import botocore
import logging
from io import BytesIO

from botocore.utils import fix_s3_host
from cottoncandy.utils import *
from CCBackEnd import *

try:
    from urllib import unquote
except ImportError:
    from urllib.parse import unquote
try:
    import cPickle as pickle
except ImportError:
    import pickle


class S3Client(CCBackEnd):
    """
    S3 client interface refactored out of cotton candy interfaces to allow for switching between
    S3 and Google Drive
    """

    @staticmethod
    def connect(ACCESS_KEY, SECRET_KEY, url):
        """
        Connect to S3 using boto

        Parameters
        ----------
        ACCESS_KEY
        SECRET_KEY
        url

        Returns
        -------

        """
        s3 = boto3.resource('s3',
                            endpoint_url = url,
                            aws_access_key_id = ACCESS_KEY,
                            aws_secret_access_key = SECRET_KEY)
        s3.meta.client.meta.events.unregister('before-sign.s3', fix_s3_host)
        return s3

    def __init__(self, bucket, access_key, secret_key, s3url, force_bucket_creation = False):
        """
        Constructor

        Parameters
        ----------
        bucket
        access_key
        secret_key
        s3url
        force_bucket_creation
        """
        super(S3Client, self).__init__()

        self.connection = S3Client.connect(access_key, secret_key, s3url)
        self.url = s3url
        if self.check_bucket_exists(bucket):
            self.set_current_bucket(bucket)
        else:
            print('* Bucket "%s" does not exist' % bucket)
            if force_bucket_creation:
                print('* Creating "%s" bucket...\n' % bucket)
                self.create_bucket(bucket)
            else:
                print('* cottoncandy instantiated without bucket...\n' \
                      '* Use with caution!\n' \
                      '* Many features will not work!!!\n')
                self.bucket_name = None

        if string2bool(ISBOTO_VERBOSE) is False:
            logging.getLogger('boto3').setLevel(logging.WARNING)
            logging.getLogger('botocore').setLevel(logging.WARNING)

    def get_bucket_name(self, bucket_name):
        """

        Parameters
        ----------
        bucket_name

        Returns
        -------

        """
        bucket_name = self.bucket_name \
            if bucket_name is None \
{'fraction_non_alphanumeric': 0.0904, 'fraction_numerical': 0.0084, 'mean_word_length': 1.7164584464964694, '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': '4476176', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1364, 'n_words': 338}