<commit_before># -*- coding: utf-8 -*-

import inspect

from werkzeug.exceptions import HTTPException
from werkzeug.routing import Map, Rule
from werkzeug.wrappers import Request, Response
from werkzeug.wsgi import DispatcherMiddleware

from .errors import (
    DuplicatedExtension,
    UnrecognizedExtension,
    MissingEndpoint,
)
from .default_exts import (
    get_query,
    get_form,
    get_json,
    get_headers,
    get_method,
    get_body,
    get_request,
)

class Axe(object):

    DEFAULT_EXTS = {
        'query': get_query,
        'form': get_form,
        'json': get_json,
        'headers': get_headers,
        'method': get_method,
        'body': get_body,
        'request': get_request,
    }

    def __init__(self):
        self.urls = {}
        self.proxies = {}
        self.exts = dict(self.__class__.DEFAULT_EXTS)
        self.views = Map()
        self._wsgi_app = self.axe_wsgi_app

    def build(self, urls):
        self.urls = urls
        for key in urls:
            rule = Rule(key, endpoint=key)
            arg_spec = inspect.getargspec(urls[key])
            for ext in arg_spec.args:
                self.get_ext(ext)
            self.views.add(rule)

    def get_ext(self, name):
        try:
            return self.exts[name]
        except KeyError:
            raise UnrecognizedExtension(name)

    def ext(self, func):
        func_name = func.__name__
        if func_name in self.exts:
            raise DuplicatedExtension
        self.exts[func_name] = func
        return func

    def get_view(self, endpoint):
        try:
            return self.urls[endpoint]
        except KeyError:
            raise MissingEndpoint

    def get_view_args(self, view, request, cache):
        arg_spec = inspect.getargspec(view)
        if len(arg_spec.args) == 1 and 'request' in arg_spec.args:
            cache.update({'request': request})
            return cache

        args = {}
        for name in arg_spec.args:
            if name in cache:
                args[name] = cache[name]
            else:
                func = self.get_ext(name)
                func_args = self.get_view_args(func, request, cache)
                args[name] = func(**func_args)
                cache[name] = args[name]

        return args

    def gen_response(self, request):
        adapter = self.views.bind_to_environ(request.environ)
        endpoint, values = adapter.match()
        view = self.get_view(endpoint)
        args = self.get_view_args(view, request, cache={})
        resp = view(**args)
        return Response(resp)

    def wsgi_app(self, env, start_response):
        return self._wsgi_app(env, start_response)

    __call__ = wsgi_app<commit_msg>Bugfix for wrong recursive method `get_view_args`.<commit_after># -*- coding: utf-8 -*-

import inspect

from werkzeug.exceptions import HTTPException
from werkzeug.routing import Map, Rule
from werkzeug.wrappers import Request, Response
from werkzeug.wsgi import DispatcherMiddleware

from .errors import (
    DuplicatedExtension,
    UnrecognizedExtension,
    MissingEndpoint,
)
from .default_exts import (
    get_query,
    get_form,
    get_json,
    get_headers,
    get_method,
    get_body,
    get_request,
)

class Axe(object):

    DEFAULT_EXTS = {
        'query': get_query,
        'form': get_form,
        'json': get_json,
        'headers': get_headers,
        'method': get_method,
        'body': get_body,
        'request': get_request,
    }

    def __init__(self):
        self.urls = {}
        self.proxies = {}
        self.exts = dict(self.__class__.DEFAULT_EXTS)
        self.views = Map()
        self._wsgi_app = self.axe_wsgi_app

    def build(self, urls):
        self.urls = urls
        for key in urls:
            rule = Rule(key, endpoint=key)
            arg_spec = inspect.getargspec(urls[key])
            for ext in arg_spec.args:
                self.get_ext(ext)
            self.views.add(rule)

    def get_ext(self, name):
        try:
            return self.exts[name]
        except KeyError:
            raise UnrecognizedExtension(name)

    def ext(self, func):
        func_name = func.__name__
        if func_name in self.exts:
            raise DuplicatedExtension
        self.exts[func_name] = func
        return func

    def get_view(self, endpoint):
        try:
            return self.urls[endpoint]
        except KeyError:
            raise MissingEndpoint

    def get_view_args(self, view, request, cache):
        arg_spec = inspect.getargspec(view)
        if 'request' in arg_spec.args:
            cache.update({'request': request})

        args = {}
        for name in arg_spec.args:
            if name in cache:
                args[name] = cache[name]
            else:
                func = self.get_ext(name)
                func_args = self.get_view_args(func, request, cache)
                args[name] = func(**func_args)
                cache[name] = args[name]

        return args

    def gen_response(self, request):
        adapter = self.views.bind_to_environ(request.environ)
        endpoint, values = adapter.match()
        view = self.get_view(endpoint)
        args = self.get_view_args(view, request, cache={})
        resp = view(**args)
        return Response(resp)

    def wsgi_app(self, env, start_response):
        return self._wsgi_app(env, start_response)

    __call__ = wsgi_app
{'fraction_non_alphanumeric': 0.09474463360473723, 'fraction_numerical': 0.0005551443375277572, 'mean_word_length': 2.0467869222096957, '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': '3648626', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1633, 'n_words': 379}