<commit_before>from werkzeug.exceptions import MethodNotAllowed

from spa.wrappers import JSONResponse

class Handler(object):
    """Baseclass for our handlers."""

    allowed_methods = ('GET', 'HEAD', 'POST', 'DELETE', 'PUT')

    def __init__(self, app, req, params, **kwargs):
        self.app = app
        self.request = req
        self.params = params
        self.kwargs = kwargs

    def get(self, *args, **kwargs):
        return MethodNotAllowed()
    post = delete = put = websocket = get

    def head(self, *args, **kwargs):
        return self.get()

    def websocket_close(self):
        pass

    def __call__(self, environ, start_response):
        if self.request.method not in self.allowed_methods:
            return MethodNotAllowed()(environ, start_response)

        if self.request.method == 'GET' and 'wsgi.websocket' in environ:
            self.ws = environ['wsgi.websocket']
            self.ws.add_close_callback(self.websocket_close)

            handler = self.websocket
        else:
            handler = getattr(self, self.request.method.lower())

        resp = handler(**self.params)
        return resp(environ, start_response)


class JSONHandler(Handler):
    """Works the same as Handler, but allows you to reply with just a dict that
    will then be turned into a JSONResponse for you."""

    def _get_response(self):
        environ = self.request.environ

        if self.request.method == 'GET' and 'wsgi.websocket' in environ:
            self.ws = environ['wsgi.websocket']
            self.ws.add_close_callback(self.websocket_close)

            handler = self.websocket
        else:
            handler = getattr(self, self.request.method.lower())

        resp = handler(**self.params)<commit_msg>slightly nicer UX for specifying unimplemented methods<commit_after>from werkzeug.exceptions import MethodNotAllowed

from spa.wrappers import JSONResponse

class Handler(object):
    """Baseclass for our handlers."""

    allowed_methods = ('GET', 'HEAD', 'POST', 'DELETE', 'PUT', 'PATCH')
    get = post = delete = put = patch = NotImplemented

    def __init__(self, app, req, params, **kwargs):
        self.app = app
        self.request = req
        self.params = params
        self.kwargs = kwargs

    def head(self, *args, **kwargs):
        return self.get()

    def websocket_close(self):
        pass

    def __call__(self, environ, start_response):
        if self.request.method not in self.allowed_methods:
            return MethodNotAllowed()(environ, start_response)

        if self.request.method == 'GET' and 'wsgi.websocket' in environ:
            self.ws = environ['wsgi.websocket']
            self.ws.add_close_callback(self.websocket_close)

            handler = self.websocket
        else:
            handler = getattr(self, self.request.method.lower())

        if handler == NotImplemented:
            return MethodNotAllowed()

        resp = handler(**self.params)
        return resp(environ, start_response)


class JSONHandler(Handler):
    """Works the same as Handler, but allows you to reply with just a dict that
    will then be turned into a JSONResponse for you."""

    def _get_response(self):
        environ = self.request.environ

        if self.request.method == 'GET' and 'wsgi.websocket' in environ:
            self.ws = environ['wsgi.websocket']
            self.ws.add_close_callback(self.websocket_close)

            handler = self.websocket
        else:
            handler = getattr(self, self.request.method.lower())

        resp = handler(**self.params)
{'fraction_non_alphanumeric': 0.10651383841207716, 'fraction_numerical': 0.0, 'mean_word_length': 2.578, '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': '1147470', 'n_tokens_mistral': 1043, 'n_tokens_neox': 1001, 'n_words': 276}