<commit_before>from urlparse import parse_qs, urlparse
import re

from jinja2 import Template

from .models import s3_backend
from .utils import bucket_name_from_url


def parse_key_name(pth):
    return pth.lstrip("/")


class ResponseObject(object):
    def __init__(self, backend, bucket_name_from_url, parse_key_name):
        self.backend = backend
        self.bucket_name_from_url = bucket_name_from_url
        self.parse_key_name = parse_key_name

    def all_buckets(self):
        # No bucket specified. Listing all buckets
        all_buckets = self.backend.get_all_buckets()
        template = Template(S3_ALL_BUCKETS)
        return template.render(buckets=all_buckets)

    def bucket_response(self, request, full_url, headers):
        response = self._bucket_response(request, full_url, headers)
        if isinstance(response, basestring):
            return 200, headers, response

        else:
            status_code, headers, response_content = response
            return status_code, headers, response_content

    def _bucket_response(self, request, full_url, headers):
        parsed_url = urlparse(full_url)
        querystring = parse_qs(parsed_url.query)
        method = request.method

        bucket_name = self.bucket_name_from_url(full_url)
        if not bucket_name:
            # If no bucket specified, list all buckets
            return self.all_buckets()

        if method == 'GET':
            bucket = self.backend.get_bucket(bucket_name)
            if bucket:
                prefix = querystring.get('prefix', [None])[0]
                delimiter = querystring.get('delimiter', [None])[0]
                result_keys, result_folders = self.backend.prefix_query(bucket, prefix, delimiter)
                template = Template(S3_BUCKET_GET_RESPONSE)
                return template.render(
                    bucket=bucket,
                    prefix=prefix,
                    delimiter=delimiter,
                    result_keys=result_keys,
                    result_folders=result_folders
                )
            else:
                return 404, headers, ""
        elif method == 'PUT':
            new_bucket = self.backend.create_bucket(bucket_name)
            template = Template(S3_BUCKET_CREATE_RESPONSE)
            return template.render(bucket=new_bucket)
        elif method == 'DELETE':
            removed_bucket = self.backend.delete_bucket(bucket_name)
            if removed_bucket is None:
                # Non-existant bucket
                template = Template(S3_DELETE_NON_EXISTING_BUCKET)
                return 404, headers, template.render(bucket_name=bucket_name)
            elif removed_bucket:
                # Bucket exists
                template = Template(S3_DELETE_BUCKET_SUCCESS)
                return 204, headers, template.render(bucket=removed_bucket)
            else:
                # Tried to delete a bucket that still has keys
                template = Template(S3_DELETE_BUCKET_WITH_ITEMS_ERROR)
                return 409, headers, template.render(bucket=removed_bucket)
        elif method == 'POST':
            #POST to bucket-url should create file from form
            if hasattr(request, 'form'):
                #Not HTTPretty
                form = request.form
            else:
                #HTTPretty, build new form object
                form = {}
                for kv in request.body.split('&'):
                    k, v = kv.split('=')
                    form[k] = v

            key = form['key']
            f = form['file']

            new_key = self.backend.set_key(bucket_name, key, f)

            #Metadata
            meta_regex = re.compile('^x-amz-meta-([a-zA-Z0-9\-_]+)$', flags=re.IGNORECASE)
            for form_id in form:
                result = meta_regex.match(form_id)<commit_msg>when getting a key (HEAD request) return key.value instead of empty string.  This is will mirror boto's method.<commit_after>from urlparse import parse_qs, urlparse
import re

from jinja2 import Template

from .models import s3_backend
from .utils import bucket_name_from_url


def parse_key_name(pth):
    return pth.lstrip("/")


class ResponseObject(object):
    def __init__(self, backend, bucket_name_from_url, parse_key_name):
        self.backend = backend
        self.bucket_name_from_url = bucket_name_from_url
        self.parse_key_name = parse_key_name

    def all_buckets(self):
        # No bucket specified. Listing all buckets
        all_buckets = self.backend.get_all_buckets()
        template = Template(S3_ALL_BUCKETS)
        return template.render(buckets=all_buckets)

    def bucket_response(self, request, full_url, headers):
        response = self._bucket_response(request, full_url, headers)
        if isinstance(response, basestring):
            return 200, headers, response

        else:
            status_code, headers, response_content = response
            return status_code, headers, response_content

    def _bucket_response(self, request, full_url, headers):
        parsed_url = urlparse(full_url)
        querystring = parse_qs(parsed_url.query)
        method = request.method

        bucket_name = self.bucket_name_from_url(full_url)
        if not bucket_name:
            # If no bucket specified, list all buckets
            return self.all_buckets()

        if method == 'GET':
            bucket = self.backend.get_bucket(bucket_name)
            if bucket:
                prefix = querystring.get('prefix', [None])[0]
                delimiter = querystring.get('delimiter', [None])[0]
                result_keys, result_folders = self.backend.prefix_query(bucket, prefix, delimiter)
                template = Template(S3_BUCKET_GET_RESPONSE)
                return template.render(
                    bucket=bucket,
                    prefix=prefix,
                    delimiter=delimiter,
                    result_keys=result_keys,
                    result_folders=result_folders
                )
            else:
                return 404, headers, ""
        elif method == 'PUT':
            new_bucket = self.backend.create_bucket(bucket_name)
            template = Template(S3_BUCKET_CREATE_RESPONSE)
            return template.render(bucket=new_bucket)
        elif method == 'DELETE':
            removed_bucket = self.backend.delete_bucket(bucket_name)
            if removed_bucket is None:
                # Non-existant bucket
                template = Template(S3_DELETE_NON_EXISTING_BUCKET)
                return 404, headers, template.render(bucket_name=bucket_name)
            elif removed_bucket:
                # Bucket exists
                template = Template(S3_DELETE_BUCKET_SUCCESS)
                return 204, headers, template.render(bucket=removed_bucket)
            else:
                # Tried to delete a bucket that still has keys
                template = Template(S3_DELETE_BUCKET_WITH_ITEMS_ERROR)
                return 409, headers, template.render(bucket=removed_bucket)
        elif method == 'POST':
            #POST to bucket-url should create file from form
            if hasattr(request, 'form'):
                #Not HTTPretty
                form = request.form
            else:
                #HTTPretty, build new form object
                form = {}
                for kv in request.body.split('&'):
                    k, v = kv.split('=')
                    form[k] = v

            key = form['key']
            f = form['file']

            new_key = self.backend.set_key(bucket_name, key, f)

            #Metadata
            meta_regex = re.compile('^x-amz-meta-([a-zA-Z0-9\-_]+)$', flags=re.IGNORECASE)

            for form_id in form:
                result = meta_regex.match(form_id)
{'fraction_non_alphanumeric': 0.07958789439793947, 'fraction_numerical': 0.006954282034771411, 'mean_word_length': 1.9743393335886634, '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': '5176137', 'n_tokens_mistral': 2206, 'n_tokens_neox': 2082, 'n_words': 486}