<commit_before>from logging.handlers import SMTPHandler, RotatingFileHandler
import os

from flask import Flask, request, render_template
from flask import escape

import logging
import pprint
from markupsafe import Markup

import re


def relativize_paths(value, path):
    """
    THIS IS A FILTER.
    It should be moved somewhere else.
    It prepends the specified path to all src paths in the specified blocks so that the path can be relative
    to the static directory of the App (or to an arbitrary directory).
    :return:
    """
    expr = """(href|src)=["'](.+?)["']"""

    def repl(matchobj):
        """
        Function to be called in every match for replacing.
        :return: Replaced URI.
        """
        oldurl = matchobj.group(2)
        newurl = '%s="%s"' % (matchobj.group(1), os.path.join(path, oldurl))
        return newurl

    newvalue = re.sub(expr, repl, value)

    return Markup(newvalue)

app = Flask(__name__)
app.config["SECRET_KEY"] = os.urandom(32)
app.config['SESSION_COOKIE_NAME'] = 'appcompsession'
app.config['SQLALCHEMY_NATIVE_UNICODE'] = True
app.config['SQLALCHEMY_POOL_RECYCLE'] = 3600
app.config.from_object('config')

# Add an extension to jinja2
app.jinja_env.add_extension("jinja2.ext.i18n")

# Add custom filter to jinja2
app.jinja_env.filters['relativize_paths'] = relativize_paths

@app.template_filter('hash')
def hash_filter(url):
    return hash(url)

# Support old deployments
if not app.config.get('SQLALCHEMY_DATABASE_URI', False):
    if app.config.get('SQLALCHEMY_ENGINE_STR', False):
        app.config['SQLALCHEMY_DATABASE_URI'] = app.config['SQLALCHEMY_ENGINE_STR']


# Support CORS
# from flask.ext.cors import CORS
# cors = CORS(app)


from appcomposer.babel import Babel

if Babel is None:
    print "Not using Babel. Everything will be in English"
else:
    babel = Babel(app)

    supported_languages = ['en']
    supported_languages.extend([translation.language for translation in babel.list_translations()])

    @babel.localeselector
    def get_locale():
        locale = request.args.get('locale', None)
        if locale is None:
            locale = request.accept_languages.best_match(supported_languages)
        if locale is None:
            locale = 'en'
        return locale


# Initialize the logging mechanism to send error 500 mails to the administrators
if not app.debug and app.config.get("ADMINS") is not None and app.config.get("SMTP_SERVER") is not None:

    class MailLoggingFilter(logging.Filter):
        def filter(self, record):
            try:
                record.environ = pprint.pformat(request.environ)
            except RuntimeError:
                # This on production will raise an "out of request context" error. We ignore it.
                # TODO: Check if there is some way to detect that we are outside, rather than rely on an exception.
                return False
            return True

    app.logger.addFilter(MailLoggingFilter())

    smtp_server = app.config.get("SMTP_SERVER")
    from_addr = app.config.get("SENDER_ADDR")
    to_addrs = app.config.get("ADMINS")
    mail_handler = SMTPHandler(smtp_server,
                                from_addr,
                                to_addrs,
                                "AppComposer Application Error Report")
    formatter = logging.Formatter(
        '''
        Message type:       %(levelname)s
        Location:           %(pathname)s:%(lineno)d
        Module:             %(module)s
        Function:           %(funcName)s
        Time:               %(asctime)s

        Message:

        %(message)s<commit_msg>Add more logging information<commit_after>from logging.handlers import SMTPHandler, RotatingFileHandler
import os

from flask import Flask, request, render_template
from flask import escape

import logging
import pprint
from markupsafe import Markup

import re


def relativize_paths(value, path):
    """
    THIS IS A FILTER.
    It should be moved somewhere else.
    It prepends the specified path to all src paths in the specified blocks so that the path can be relative
    to the static directory of the App (or to an arbitrary directory).
    :return:
    """
    expr = """(href|src)=["'](.+?)["']"""

    def repl(matchobj):
        """
        Function to be called in every match for replacing.
        :return: Replaced URI.
        """
        oldurl = matchobj.group(2)
        newurl = '%s="%s"' % (matchobj.group(1), os.path.join(path, oldurl))
        return newurl

    newvalue = re.sub(expr, repl, value)

    return Markup(newvalue)

app = Flask(__name__)
app.config["SECRET_KEY"] = os.urandom(32)
app.config['SESSION_COOKIE_NAME'] = 'appcompsession'
app.config['SQLALCHEMY_NATIVE_UNICODE'] = True
app.config['SQLALCHEMY_POOL_RECYCLE'] = 3600
app.config.from_object('config')

# Add an extension to jinja2
app.jinja_env.add_extension("jinja2.ext.i18n")

# Add custom filter to jinja2
app.jinja_env.filters['relativize_paths'] = relativize_paths

@app.template_filter('hash')
def hash_filter(url):
    return hash(url)

# Support old deployments
if not app.config.get('SQLALCHEMY_DATABASE_URI', False):
    if app.config.get('SQLALCHEMY_ENGINE_STR', False):
        app.config['SQLALCHEMY_DATABASE_URI'] = app.config['SQLALCHEMY_ENGINE_STR']


# Support CORS
# from flask.ext.cors import CORS
# cors = CORS(app)


from appcomposer.babel import Babel

if Babel is None:
    print "Not using Babel. Everything will be in English"
else:
    babel = Babel(app)

    supported_languages = ['en']
    supported_languages.extend([translation.language for translation in babel.list_translations()])

    @babel.localeselector
    def get_locale():
        locale = request.args.get('locale', None)
        if locale is None:
            locale = request.accept_languages.best_match(supported_languages)
        if locale is None:
            locale = 'en'
        return locale


# Initialize the logging mechanism to send error 500 mails to the administrators
if not app.debug and app.config.get("ADMINS") is not None and app.config.get("SMTP_SERVER") is not None:

    class MailLoggingFilter(logging.Filter):
        def filter(self, record):
            try:
                record.environ = pprint.pformat(request.environ)
            except RuntimeError:
                # This on production will raise an "out of request context" error. We ignore it.
                # TODO: Check if there is some way to detect that we are outside, rather than rely on an exception.
                return False
            try:
                from appcomposer.login import current_golab_user
                user = current_golab_user()
                if user is None:
                    record.user = 'User not logged in'
                else:
                    record.user = user.email
            except:
                record.user = 'Error checking user'
            return True

    app.logger.addFilter(MailLoggingFilter())

    smtp_server = app.config.get("SMTP_SERVER")
    from_addr = app.config.get("SENDER_ADDR")
    to_addrs = app.config.get("ADMINS")
    mail_handler = SMTPHandler(smtp_server,
                                from_addr,
                                to_addrs,
                                "AppComposer Application Error Report")
    formatter = logging.Formatter(
        '''
        Message type:       %(levelname)s
        Location:           %(pathname)s:%(lineno)d
        Module:             %(module)s
        Function:           %(funcName)s
        Time:               %(asctime)s

        User: %(user)s

        Message:

        %(message)s
{'fraction_non_alphanumeric': 0.09505653431501446, 'fraction_numerical': 0.004207204838285564, 'mean_word_length': 2.5513538748832865, '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': '4642340', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2209, 'n_words': 632}