<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Interpreter version: python 2.7
#
# Imports =====================================================================
import os
import gzip
import time
import os.path
import StringIO

from bottle import request
from bottle import response
from bottle import parse_date
from bottle import HTTPResponse


# Variables ===================================================================
API_PATH = "/api_v1/"
RESPONSE_TYPE = "application/json; charset=utf-8"


# Functions ===================================================================
def gzipped(fn):
    def gzipped_wrapper(*args, **kwargs):
        accepted_encoding = request.get_header("Accept-Encoding")

        if not accepted_encoding or "gzip" not in accepted_encoding:
            return fn(*args, **kwargs)

        response.set_header("Content-Encoding", "gzip")

        out = StringIO.StringIO()
        with gzip.GzipFile(fileobj=out, mode="w") as f:
            f.write(
                fn(*args, **kwargs)
            )

        out.seek(0)
        return out

    return gzipped_wrapper


def gzip_cache(path):
    accept_enc = request.get_header("Accept-Encoding")

    if accept_enc and "gzip" in accept_enc and os.path.exists(path + ".gz"):
        path = path + ".gz"
        response.set_header("Content-Encoding", "gzip")

    headers = dict()
    stats = os.stat(path)
    headers['Content-Length'] = stats.st_size
    headers['Last-Modified'] = time.strftime(
        "%a, %d %b %Y %H:%M:%S GMT",
        time.gmtime(stats.st_mtime)
    )

    modified_since = request.environ.get('HTTP_IF_MODIFIED_SINCE')<commit_msg>Improved browser cacheing.<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Interpreter version: python 2.7
#
# Imports =====================================================================
import os
import gzip
import time
import os.path
import StringIO

from bottle import request
from bottle import response
from bottle import parse_date
from bottle import HTTPResponse


# Variables ===================================================================
API_PATH = "/api_v1/"
RESPONSE_TYPE = "application/json; charset=utf-8"


# Functions ===================================================================
def to_gzipped_stringio(data):
    out = StringIO.StringIO()
    with gzip.GzipFile(fileobj=out, mode="w") as f:
        f.write(data)

    out.seek(0)
    return out


def gzipped(fn):
    def gzipped_wrapper(*args, **kwargs):
        accepted_encoding = request.get_header("Accept-Encoding")

        if not accepted_encoding or "gzip" not in accepted_encoding:
            return fn(*args, **kwargs)

        response.set_header("Content-Encoding", "gzip")

        return to_gzipped_stringio(fn(*args, **kwargs))

    return gzipped_wrapper


def gzip_cache(path):
    accept_enc = request.get_header("Accept-Encoding")

    if accept_enc and "gzip" in accept_enc and os.path.exists(path + ".gz"):
        path = path + ".gz"
        response.set_header("Content-Encoding", "gzip")

    stats = os.stat(path)

    headers = dict()
    headers['Content-Length'] = stats.st_size
    headers['Last-Modified'] = time.strftime(
        "%a, %d %b %Y %H:%M:%S GMT",
        time.gmtime(stats.st_mtime)
    )

    # I need to set `headers` dict for optional  HTTPResponse use, but also
    # set hedears using `response.set_header()` for normal use
    for key, val in headers.iteritems():
        response.set_header(key, val)

    modified_since = request.environ.get('HTTP_IF_MODIFIED_SINCE')
{'fraction_non_alphanumeric': 0.22125874125874126, 'fraction_numerical': 0.0033566433566433566, 'mean_word_length': 3.65625, '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': '791724', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1026, 'n_words': 256}