<commit_before>import logging
import traceback

from tornado import httpclient
from xml.etree.ElementTree import Element, tostring


def _el_with_text(tag, text, **kwargs):
    el = Element(tag, **kwargs)
    el.text = text
    return el


def _traceback_line(filename, line, method):
    return Element("line",
                   dict(file=filename, method=method, number=str(line)))


def _cgi_data_element(req):
    cgi_data = Element("cgi-data")
    for key, val in [("HTTP_USER_AGENT", req.headers.get("User-Agent", ""))]:
        cgi_data.append(_el_with_text("var", val, key=key))
    return cgi_data


def _params_element(req):
    params = Element("params")
    for key, val in [("HTTP_METHOD", req.method)]:
        params.append(_el_with_text("var", val, key=key))
    return params


def _request_element(request, component=None):
    req = Element("request")
    req.append(_el_with_text("url", request.uri))
    req.append(_cgi_data_element(request))
    req.append(_params_element(request))
    if component is not None:
      req.append(_el_with_text("component", component))
    return req


def _backtrace_element(exc_info):
    backtrace = Element("backtrace")
    for line in traceback.extract_tb(exc_info[2]):
        backtrace.append(_traceback_line(line[0], line[1], line[2]))
    return backtrace


def notify(exc_info, request, name, api_key=None, environment=None, url=None,
    handler=None):

    if api_key is None or environment is None:
        return

    notice = Element("notice", version="2.3")
    notice.append(_el_with_text("api-key", api_key))

    notifier = Element("notifier")
    notifier.append(_el_with_text("name", name))
    notifier.append(_el_with_text("version", "1.00"))

    if url is not None:
        notifier.append(_el_with_text("url", url))

    notice.append(notifier)

    error = Element("error")
    error.append(_el_with_text("message", exc_info[1].message))
    error.append(_el_with_text("class", exc_info[1].__class__.__name__))

    error.append(_backtrace_element(exc_info))
    notice.append(error)
    
    if handler is not None:
      component = handler.__class__.__name__
    else:
      component = None
    notice.append(_request_element(request, component=component))

    server_environment = Element("server-environment")
    server_environment.append(_el_with_text("environment-name", environment))
    notice.append(server_environment)

    def handle_request(response):
        if response.error:<commit_msg>Add project-root from PWD<commit_after>import logging
import traceback
import os

from tornado import httpclient
from xml.etree.ElementTree import Element, tostring


def _el_with_text(tag, text, **kwargs):
    el = Element(tag, **kwargs)
    el.text = text
    return el


def _traceback_line(filename, line, method):
    return Element("line",
                   dict(file=filename, method=method, number=str(line)))


def _cgi_data_element(req):
    cgi_data = Element("cgi-data")
    for key, val in [("HTTP_USER_AGENT", req.headers.get("User-Agent", ""))]:
        cgi_data.append(_el_with_text("var", val, key=key))
    return cgi_data


def _params_element(req):
    params = Element("params")
    for key, val in [("HTTP_METHOD", req.method)]:
        params.append(_el_with_text("var", val, key=key))
    return params


def _request_element(request, component=None):
    req = Element("request")
    req.append(_el_with_text("url", request.uri))
    req.append(_cgi_data_element(request))
    req.append(_params_element(request))
    if component is not None:
      req.append(_el_with_text("component", component))
    return req


def _backtrace_element(exc_info):
    backtrace = Element("backtrace")
    for line in traceback.extract_tb(exc_info[2]):
        backtrace.append(_traceback_line(line[0], line[1], line[2]))
    return backtrace


def notify(exc_info, request, name, api_key=None, environment=None, url=None,
    handler=None):

    if api_key is None or environment is None:
        return

    notice = Element("notice", version="2.3")
    notice.append(_el_with_text("api-key", api_key))

    notifier = Element("notifier")
    notifier.append(_el_with_text("name", name))
    notifier.append(_el_with_text("version", "1.00"))

    if url is not None:
        notifier.append(_el_with_text("url", url))

    notice.append(notifier)

    error = Element("error")
    error.append(_el_with_text("message", exc_info[1].message))
    error.append(_el_with_text("class", exc_info[1].__class__.__name__))

    error.append(_backtrace_element(exc_info))
    notice.append(error)
    
    if handler is not None:
      component = handler.__class__.__name__
    else:
      component = None
    notice.append(_request_element(request, component=component))

    server_environment = Element("server-environment")
    pwd = os.<PASSWORD>('<PASSWORD>')
    if pwd:
      server_environment.append(_el_with_text("project-root", pwd))
    server_environment.append(_el_with_text("environment-name", environment))
    notice.append(server_environment)

    def handle_request(response):
        if response.error:
{'fraction_non_alphanumeric': 0.12602579132473624, 'fraction_numerical': 0.0042985541227041815, 'mean_word_length': 4.113886113886114, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '616709', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1587, 'n_words': 328}