<commit_before>import os
from dateutil.parser import parse

from boto.s3.connection import S3Connection, Key
from boto.s3.connection import OrdinaryCallingFormat
from boto.s3.cors import CORSConfiguration
from boto.exception import S3ResponseError

from hurry.filesize import size, alternative


#Note: (from boto docs) this function is in beta
def enable_versioning(settings):
    wrapper = S3Wrapper.from_addon(settings)
    wrapper.bucket.configure_versioning(True)


def has_access(access_key, secret_key):
    try:
        c = S3Connection(access_key, secret_key)
        c.get_all_buckets()
        return True
    except Exception:
        return False


def get_bucket_list(user_settings):
        return S3Connection(user_settings.access_key, user_settings.secret_key).get_all_buckets()


def create_bucket(user_settings, bucket_name):
    try:
        connect = S3Connection(
            user_settings.access_key, user_settings.secret_key)
        return connect.create_bucket(bucket_name)
    except Exception:
        return False


def does_bucket_exist(accessKey, secretKey, bucketName):
    try:
        c = S3Connection(accessKey, secretKey)
        c.get_bucket(bucketName, validate=False)
        return True
    except Exception:
        return False


class S3Wrapper(object):

    @classmethod
    def from_addon(cls, s3):
        if s3 is None or s3.user_settings is None:
            return None
        if not s3.is_registration:
            return cls(S3Connection(s3.user_settings.access_key, s3.user_settings.secret_key, calling_format=OrdinaryCallingFormat()), s3.bucket)
        else:
            return RegistrationWrapper(s3)

    @classmethod
    def bucket_exist(cls, s3, bucketName):
        m = cls.fromAddon(s3)
        return not m.connection.lookup(bucketName.lower(), validate=False)

    "S3 Bucket management"

    def __init__(self, connect, bucketName):
        self.connection = connect
        self.bucket = self.connection.get_bucket(bucketName, validate=False)

    def create_key(self, key):
        self.bucket.new_key(key)

    def get_file_list(self, prefix=None):
        if not prefix:
            return self.bucket.list()
        else:
            return self.bucket.list(prefix=prefix)

    def create_folder(self, name, pathToFolder=""):
        if not name.endswith('/'):
            name.append("/")
        k = self.bucket.new_key(pathToFolder + name)
        return k.set_contents_from_string("")

    def delete_file(self, keyName):
        return self.bucket.delete_key(keyName)

    def download_file_URL(self, keyName, vid=None):
        return self.bucket.get_key(keyName, version_id=vid, headers={'Content-Disposition': 'attachment'}).generate_url(5)

    def get_wrapped_keys(self, prefix=None):
        return [S3Key(x) for x in self.get_file_list()]<commit_msg>Fixed the issue with some buckets being unusable.<commit_after>import os
from dateutil.parser import parse

from boto.s3.connection import S3Connection, Key
from boto.s3.connection import OrdinaryCallingFormat
from boto.s3.cors import CORSConfiguration
from boto.exception import S3ResponseError

from hurry.filesize import size, alternative


#Note: (from boto docs) this function is in beta
def enable_versioning(settings):
    wrapper = S3Wrapper.from_addon(settings)
    wrapper.bucket.configure_versioning(True)


def has_access(access_key, secret_key):
    try:
        c = S3Connection(access_key, secret_key)
        c.get_all_buckets()
        return True
    except Exception:
        return False


def get_bucket_list(user_settings):
        return S3Connection(user_settings.access_key, user_settings.secret_key).get_all_buckets()


def create_bucket(user_settings, bucket_name):
    try:
        connect = S3Connection(
            user_settings.access_key, user_settings.secret_key)
        return connect.create_bucket(bucket_name)
    except Exception:
        return False


def does_bucket_exist(accessKey, secretKey, bucketName):
    try:
        c = S3Connection(accessKey, secretKey)
        c.get_bucket(bucketName, validate=False)
        return True
    except Exception:
        return False


class S3Wrapper(object):

    @classmethod
    def from_addon(cls, s3):
        if s3 is None or s3.user_settings is None:
            return None
        if not s3.is_registration:
            return cls(S3Connection(s3.user_settings.access_key, s3.user_settings.secret_key), s3.bucket)
        else:
            return RegistrationWrapper(s3)

    @classmethod
    def bucket_exist(cls, s3, bucketName):
        m = cls.fromAddon(s3)
        return not m.connection.lookup(bucketName.lower(), validate=False)

    "S3 Bucket management"

    def __init__(self, connect, bucketName):
        self.connection = connect
        if bucketName != bucketName.lower():
            self.connection.calling_format = OrdinaryCallingFormat()
        self.bucket = self.connection.get_bucket(bucketName, validate=False)

    def create_key(self, key):
        self.bucket.new_key(key)

    def get_file_list(self, prefix=None):
        if not prefix:
            return self.bucket.list()
        else:
            return self.bucket.list(prefix=prefix)

    def create_folder(self, name, pathToFolder=""):
        if not name.endswith('/'):
            name.append("/")
        k = self.bucket.new_key(pathToFolder + name)
        return k.set_contents_from_string("")

    def delete_file(self, keyName):
        return self.bucket.delete_key(keyName)

    def download_file_URL(self, keyName, vid=None):
        return self.bucket.get_key(keyName, version_id=vid, headers={'Content-Disposition': 'attachment'}).generate_url(5)

    def get_wrapped_keys(self, prefix=None):
        return [S3Key(x) for x in self.get_file_list()]
{'fraction_non_alphanumeric': 0.08835758835758836, 'fraction_numerical': 0.008662508662508662, 'mean_word_length': 3.207725947521866, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2066966', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1721, 'n_words': 394}