<commit_before>import asyncio
import json
import aiohttp_jinja2

from aiohttp import web

# from .tbtools.console import _ConsoleFrame
from .utils import TEMPLATE_KEY, APP_KEY, ROOT_ROUTE_NAME, STATIC_ROUTE_NAME


@aiohttp_jinja2.template('toolbar.jinja2', app_key=TEMPLATE_KEY)
def request_view(request):
    settings = request.app[APP_KEY]['settings']
    history = request.app[APP_KEY]['request_history']

    try:
        last_request_pair = history.last(1)[0]
    except IndexError:
        last_request_id = None
    else:
        last_request_id = last_request_pair[0]

    request_id = request.match_info.get('request_id', last_request_id)

    toolbar = history.get(request_id, None)

    panels = toolbar.panels if toolbar else []
    global_panels = toolbar.global_panels if toolbar else []

    static_path = request.app.router[STATIC_ROUTE_NAME].url(filename='')
    root_path = request.app.router[ROOT_ROUTE_NAME].url()

    button_style = settings.get('button_style', '')
    max_visible_requests = settings['max_visible_requests']

    hist_toolbars = history.last(max_visible_requests)

    return {'panels': panels,
            'static_path': static_path,
            'root_path': root_path,
            'button_style': button_style,
            'history': hist_toolbars,
            'global_panels': global_panels,
            'request_id': request_id,
            'request': toolbar.request if toolbar else None}


class ExceptionDebugView:

    def _validate_token(self, request):
        exc_history = self._exception_history(request)
        token = request.GET.get('token')

        if exc_history is None:
            raise web.HTTPBadRequest(text='No exception history')
        if not token:
            raise web.HTTPBadRequest(text='No token in request')
        if not (token == request.app[APP_KEY]['pdtb_token']):
            raise web.HTTPBadRequest(text='Bad token in request')

    def _exception_history(self, request):
        return request.app[APP_KEY]['exc_history']

    def _get_frame(self, request):
        frm = request.GET.get('frm')
        if frm is not None:
            frm = int(frm)
        return frm

    @asyncio.coroutine
    def _get_tb(self, request):
        yield from request.read()
        tb = request.GET.get('tb') or request.POST.get('tb')
        if tb is not None:
            tb = int(tb)
        return tb

    @asyncio.coroutine
    def _get_cmd(self, request):
        yield from request.read()
        cmd = request.GET.get('cmd') or request.POST.get('cmd')
        return cmd

    @asyncio.coroutine
    def exception(self, request):
        self._validate_token(request)
        tb_id = yield from self._get_tb(request)
        tb = self._exception_history(request).tracebacks[tb_id]
        body = tb.render_full(request).encode('utf-8', 'replace')
        response = web.Response(status=200)
        response.body = body
        return response

    @asyncio.coroutine
    def source(self, request):
        self._validate_token(request)
        exc_history = self._exception_history(request)
        _frame = self._get_frame(request)
        if _frame is not None:
            frame = exc_history.frames.get(_frame)
            if frame is not None:
                text = frame.render_source()
                return web.Response(text=text, content_type='text/html')
        return web.HTTPBadRequest()

    @asyncio.coroutine
    def execute(self, request):
        self._validate_token(request)

        _exc_history = self._exception_history(request)<commit_msg>fix code that never worked properly<commit_after>import asyncio
import json
import aiohttp_jinja2

from aiohttp import web

# from .tbtools.console import _ConsoleFrame
from .utils import TEMPLATE_KEY, APP_KEY, ROOT_ROUTE_NAME, STATIC_ROUTE_NAME


@aiohttp_jinja2.template('toolbar.jinja2', app_key=TEMPLATE_KEY)
def request_view(request):
    settings = request.app[APP_KEY]['settings']
    history = request.app[APP_KEY]['request_history']

    try:
        last_request_pair = history.last(1)[0]
    except IndexError:
        last_request_id = None
    else:
        last_request_id = last_request_pair[0]

    request_id = request.match_info.get('request_id', last_request_id)

    toolbar = history.get(request_id, None)

    panels = toolbar.panels if toolbar else []
    global_panels = toolbar.global_panels if toolbar else []

    static_path = request.app.router[STATIC_ROUTE_NAME].url(filename='')
    root_path = request.app.router[ROOT_ROUTE_NAME].url()

    button_style = settings.get('button_style', '')
    max_visible_requests = settings['max_visible_requests']

    hist_toolbars = history.last(max_visible_requests)

    return {'panels': panels,
            'static_path': static_path,
            'root_path': root_path,
            'button_style': button_style,
            'history': hist_toolbars,
            'global_panels': global_panels,
            'request_id': request_id,
            'request': toolbar.request if toolbar else None}


class ExceptionDebugView:

    def _validate_token(self, request):
        exc_history = self._exception_history(request)
        token = request.GET.get('token')

        if exc_history is None:
            raise web.HTTPBadRequest(text='No exception history')
        if not token:
            raise web.HTTPBadRequest(text='No token in request')
        if not (token == request.app[APP_KEY]['pdtb_token']):
            raise web.HTTPBadRequest(text='Bad token in request')

    def _exception_history(self, request):
        return request.app[APP_KEY]['exc_history']

    def _get_frame(self, request):
        frm = request.GET.get('frm')
        if frm is not None:
            frm = int(frm)
        return frm

    @asyncio.coroutine
    def _get_tb(self, request):
        yield from request.read()
        tb = request.GET.get('tb')
        if not tb:
            yield from request.post()
            tb = request.POST.get('tb')
        if tb is not None:
            tb = int(tb)
        return tb

    @asyncio.coroutine
    def _get_cmd(self, request):
        yield from request.read()
        cmd = request.GET.get('cmd')
        if not cmd:
            yield from request.post()
            cmd = request.POST.get('cmd')
        return cmd

    @asyncio.coroutine
    def exception(self, request):
        self._validate_token(request)
        tb_id = yield from self._get_tb(request)
        tb = self._exception_history(request).tracebacks[tb_id]
        body = tb.render_full(request).encode('utf-8', 'replace')
        response = web.Response(status=200)
        response.body = body
        return response

    @asyncio.coroutine
    def source(self, request):
        self._validate_token(request)
        exc_history = self._exception_history(request)
        _frame = self._get_frame(request)
        if _frame is not None:
            frame = exc_history.frames.get(_frame)
            if frame is not None:
                text = frame.render_source()
                return web.Response(text=text, content_type='text/html')
        return web.HTTPBadRequest()

    @asyncio.coroutine
    def execute(self, request):
        self._validate_token(request)

        _exc_history = self._exception_history(request)
{'fraction_non_alphanumeric': 0.10011031439602869, 'fraction_numerical': 0.0027578599007170436, 'mean_word_length': 2.7309670781893005, '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': '1854589', 'n_tokens_mistral': 2202, 'n_tokens_neox': 2148, 'n_words': 470}