<commit_before>from pyramid.events import NewRequest


def add_cors_callback(event):
    headers = "Origin, Content-Type, Accept, Authorization"

    def cors_headers(request, response):
        if 'static' not in request.url:
            print "CORS", request.url, request.client_addr
        response.headers.update({
            # In production you would be careful with this
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Headers": headers
        })

    event.request.add_response_callback(cors_headers)


def includeme(config):
    config.add_subscriber(add_cors_callback, NewRequest)
<commit_msg>Better logging of responses, Pyramid side.<commit_after>from pyramid.events import NewRequest


def add_cors_callback(event):
    headers = "Origin, Content-Type, Accept, Authorization"

    def cors_headers(request, response):
        # if 'static' not in request.url:
        #     print "CORS", request.url, request.client_addr
        response.headers.update({
            # In production you would be careful with this
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Headers": headers
        })

    event.request.add_response_callback(cors_headers)


class logger_tween_factory(object):
    def __init__(self, handler, registry):
        self.handler = handler
        self.registry = registry

    def __call__(self, request):
        response = self.handler(request)
        if 'static' not in request.url and '?oauth_token' in request.url:
            print "####\n", request.url, "\n####", response

        return response

def cache_callback(request, response):
    """Set the cache_control max_age for the response"""
    if request.exception is not None:
        response.cache_control.max_age = 360


def includeme(config):
    config.add_subscriber(add_cors_callback, NewRequest)
    # request.add_response_callback(cache_callback)
    config.add_tween('.subscribers.logger_tween_factory')

{'fraction_non_alphanumeric': 0.10101010101010101, 'fraction_numerical': 0.0015151515151515152, 'mean_word_length': 3.161764705882353, 'pattern_counts': {'":': 4, '<': 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': '1842420', 'n_tokens_mistral': 568, 'n_tokens_neox': 548, 'n_words': 135}