<commit_before># -*- coding: utf-8 -*-
"""
    werkzeug_dispatch.application
    ~~~~~~~~~~~~~~~~~~~~~~~~~~

    :copyright: (c) 2014 by <NAME>.
    :license: BSD, see LICENSE for more details.
"""
from werkzeug import Request
from werkzeug.local import Local, LocalManager


class Application(object):
    """
    `url_map`
        werkzeug `Map` object that maps from urls to names

    `dispatcher`
        object to map from endpoint names to handler functions

    """
    def __init__(self, url_map, dispatcher, request_class=Request):
        """
        :param url_map:
            a werkzeug `Map` object`

        :param dispatcher:
            a `Dispatcher` object

        :param request_class:
            constructor applied to each wsgi environment to create the request
            object to be passed to the handler
        """
        self.url_map = url_map
        self.dispatcher = dispatcher
        self._request_class = request_class

        self._stack = self._dispatch_request

        self._local = Local()
        self._wsgi_env = self._local('wsgi_env')
        self._map_adapter = self._local('map_adapter')

        local_manager = LocalManager([self._local])
        self.add_middleware(local_manager.make_middleware)

    def add_middleware(self, middleware, *args, **kwargs):
        """
        :param middleware:
            a function which takes a wsgi application as it's first argument
            and returns a new wsgi application.  Any other args or kwargs are
            passed after.
        """
        self._stack = middleware(self._stack, *args, **kwargs)

    def _bind(self, wsgi_env):
        self._local.wsgi_env = wsgi_env
        self._local.map_adapter = self.url_map.bind_to_environ(wsgi_env)

    def _dispatch_request(self, wsgi_env, start_response):
        self._bind(wsgi_env)

        def call_view(name, kwargs):
            request = self._request_class(wsgi_env)

            endpoint = self.dispatcher.lookup(
                name, method=request.method,
                accept=wsgi_env.get('HTTP_ACCEPT'),
                accept_charset=wsgi_env.get('HTTP_ACCEPT_CHARSET'),
                accept_language=wsgi_env.get('HTTP_ACCEPT_LANGUAGE'))

            return endpoint(self, request, **kwargs)

        response = self._map_adapter.dispatch(call_view)

        return response(wsgi_env, start_response)
<commit_msg>read method from wsgi env in case request class doesn't have a method attribute<commit_after># -*- coding: utf-8 -*-
"""
    werkzeug_dispatch.application
    ~~~~~~~~~~~~~~~~~~~~~~~~~~

    :copyright: (c) 2014 by <NAME>.
    :license: BSD, see LICENSE for more details.
"""
from werkzeug import Request
from werkzeug.local import Local, LocalManager


class Application(object):
    """
    `url_map`
        werkzeug `Map` object that maps from urls to names

    `dispatcher`
        object to map from endpoint names to handler functions

    """
    def __init__(self, url_map, dispatcher, request_class=Request):
        """
        :param url_map:
            a werkzeug `Map` object`

        :param dispatcher:
            a `Dispatcher` object

        :param request_class:
            constructor applied to each wsgi environment to create the request
            object to be passed to the handler
        """
        self.url_map = url_map
        self.dispatcher = dispatcher
        self._request_class = request_class

        self._stack = self._dispatch_request

        self._local = Local()
        self._wsgi_env = self._local('wsgi_env')
        self._map_adapter = self._local('map_adapter')

        local_manager = LocalManager([self._local])
        self.add_middleware(local_manager.make_middleware)

    def add_middleware(self, middleware, *args, **kwargs):
        """
        :param middleware:
            a function which takes a wsgi application as it's first argument
            and returns a new wsgi application.  Any other args or kwargs are
            passed after.
        """
        self._stack = middleware(self._stack, *args, **kwargs)

    def _bind(self, wsgi_env):
        self._local.wsgi_env = wsgi_env
        self._local.map_adapter = self.url_map.bind_to_environ(wsgi_env)

    def _dispatch_request(self, wsgi_env, start_response):
        self._bind(wsgi_env)

        def call_view(name, kwargs):
            endpoint = self.dispatcher.lookup(
                name,
                method=wsgi_env.get('REQUEST_METHOD'),
                accept=wsgi_env.get('HTTP_ACCEPT'),
                accept_charset=wsgi_env.get('HTTP_ACCEPT_CHARSET'),
                accept_language=wsgi_env.get('HTTP_ACCEPT_LANGUAGE'))

            request = self._request_class(wsgi_env)

            return endpoint(self, request, **kwargs)

        response = self._map_adapter.dispatch(call_view)

        return response(wsgi_env, start_response)

{'fraction_non_alphanumeric': 0.09827656955272876, 'fraction_numerical': 0.002051702913418137, 'mean_word_length': 2.4379407616361073, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3376558', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1358, 'n_words': 349}