<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from twisted.web import resource, server
import json

class JsonObjectEncoder(json.JSONEncoder):
    """
    Serialize using object.__json__ if it has one.
    """
    def default(self, obj):
        if hasattr(obj, '__json__') and callable(getattr(obj, '__json__')):
            return obj.__json__()
        raise TypeError, '%s is not JSON serializable' % obj


class JsonResource(resource.Resource):
    """
    Renders something as a JSON object.
    """
    def __init__(self, obj):
        resource.Resource.__init__(self)
        self.obj = obj
        self.j = JsonObjectEncoder()

    def render_GET(self, request):
        request.setHeader("Cache-Control", "no-cache, must-revalidate")
        request.setHeader("Expires", "Sat, 26 Jul 1997 05:00:00 GMT")
        request.write(self.j.encode(self.obj))
        request.finish()
        return server.NOT_DONE_YET
        # ^ seems deceptive, but is the correct way of handling this case.
        # Later versions of twisted will likely return a deferred instead.
        # render() is supposed to either return a string or NOT_DONE_YET
        # immediately, twisted doesn't know that we'll save the request object
        # and directly write to it later even though it's completely supported


class ErrorResource(resource.Resource):
    def __init__(self, errmessage):
        resource.Resource.__init__(self)
        self.err_message = errmessage

    def render_GET(self, request):
        JsonResource({'client_error': self.err_message}).render(request)
        return server.NOT_DONE_YET<commit_msg>every request now gets jsonp if it likes<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from twisted.web import resource, server
import json

class JsonObjectEncoder(json.JSONEncoder):
    """
    Serialize using object.__json__ if it has one.
    """
    def default(self, obj):
        if hasattr(obj, '__json__') and callable(getattr(obj, '__json__')):
            return obj.__json__()
        raise TypeError, '%s is not JSON serializable' % obj


class JsonResource(resource.Resource):
    """
    Renders something as a JSON object.
    """
    def __init__(self, obj):
        resource.Resource.__init__(self)
        self.obj = obj
        self.j = JsonObjectEncoder()

    def render_GET(self, request):
        request.setHeader("Cache-Control", "no-cache, must-revalidate")
        request.setHeader("Expires", "Sat, 26 Jul 1997 05:00:00 GMT")
        json = self.j.encode(self.obj)
        if 'jsonp' in request.args:
            json = "%s(%s)" % (request.args['jsonp'][0], json)
        request.write(json)
        request.finish()
        return server.NOT_DONE_YET
        # ^ seems deceptive, but is the correct way of handling this case.
        # Later versions of twisted will likely return a deferred instead.
        # render() is supposed to either return a string or NOT_DONE_YET
        # immediately, twisted doesn't know that we'll save the request object
        # and directly write to it later even though it's completely supported


class ErrorResource(resource.Resource):
    def __init__(self, errmessage):
        resource.Resource.__init__(self)
        self.err_message = errmessage

    def render_GET(self, request):
        JsonResource({'client_error': self.err_message}).render(request)
        return server.NOT_DONE_YET
{'fraction_non_alphanumeric': 0.10364063417498531, 'fraction_numerical': 0.007927187316500293, 'mean_word_length': 2.9115958668197472, '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': '3359487', 'n_tokens_mistral': 993, 'n_tokens_neox': 947, 'n_words': 294}