<commit_before>import os
import config

from flask import Flask
from raven.contrib.flask import Sentry

from skylines.api.middleware import HTTPMethodOverrideMiddleware


class SkyLines(Flask):
    def __init__(self, name='skylines', config_file=None, *args, **kw):
        # Create Flask instance
        super(SkyLines, self).__init__(name, *args, **kw)

        # Load default settings and from environment variable
        self.config.from_pyfile(config.DEFAULT_CONF_PATH)

        if 'SKYLINES_CONFIG' in os.environ:
            self.config.from_pyfile(os.environ['SKYLINES_CONFIG'])

        if config_file:
            self.config.from_pyfile(config_file)

    def add_sqlalchemy(self):
        """ Create and configure SQLAlchemy extension """
        from skylines.database import db, migrate
        db.init_app(self)
        migrate.init_app(self, db)

    def add_cache(self):
        """ Create and attach Cache extension """
        from flask.ext.cache import Cache
        self.cache = Cache(self, with_jinja2_ext=False)

    def add_login_manager(self):
        """ Create and attach Login extension """
        from flask.ext.login import LoginManager
        self.login_manager = LoginManager()
        self.login_manager.init_app(self)

    def add_logging_handlers(self):
        if self.debug: return

        import logging
        from logging import Formatter
        from logging.handlers import RotatingFileHandler

        # Set general log level
        self.logger.setLevel(logging.INFO)

        # Add log file handler (if configured)
        path = self.config.get('LOGFILE')
        if path:
            file_handler = RotatingFileHandler(path, 'a', 10000, 4)
            file_handler.setLevel(logging.INFO)

            file_formatter = Formatter(
                '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
            file_handler.setFormatter(file_formatter)

            self.logger.addHandler(file_handler)

    def add_sentry(self):
        sentry_dsn = self.config.get('SENTRY_DSN')
        if sentry_dsn:
            Sentry(self, dsn=sentry_dsn)

    def add_debug_toolbar(self):
        try:
            from flask_debugtoolbar import DebugToolbarExtension
            DebugToolbarExtension(self)
        except ImportError:
            pass

    def add_celery(self):
        from skylines.worker.celery import celery
        celery.init_app(self)
        return celery

    def initialize_lib(self):
        from skylines.lib.geoid import load_geoid
        load_geoid(self)


def create_app(*args, **kw):
    app = SkyLines(*args, **kw)
    app.add_sqlalchemy()
    app.add_cache()
    app.initialize_lib()
    return app


def create_http_app(*args, **kw):
    app = create_app(*args, **kw)

    app.add_logging_handlers()
    app.add_sentry()
    app.add_celery()

    return app


def create_frontend_app(*args, **kw):
    app = create_http_app('skylines.frontend', *args, **kw)

    app.add_debug_toolbar()

    app.add_login_manager()

    import skylines.frontend.views
    skylines.frontend.views.register(app)

    return app<commit_msg>app: Remove unused "debug_toolbar" code<commit_after>import os
import config

from flask import Flask
from raven.contrib.flask import Sentry

from skylines.api.middleware import HTTPMethodOverrideMiddleware


class SkyLines(Flask):
    def __init__(self, name='skylines', config_file=None, *args, **kw):
        # Create Flask instance
        super(SkyLines, self).__init__(name, *args, **kw)

        # Load default settings and from environment variable
        self.config.from_pyfile(config.DEFAULT_CONF_PATH)

        if 'SKYLINES_CONFIG' in os.environ:
            self.config.from_pyfile(os.environ['SKYLINES_CONFIG'])

        if config_file:
            self.config.from_pyfile(config_file)

    def add_sqlalchemy(self):
        """ Create and configure SQLAlchemy extension """
        from skylines.database import db, migrate
        db.init_app(self)
        migrate.init_app(self, db)

    def add_cache(self):
        """ Create and attach Cache extension """
        from flask.ext.cache import Cache
        self.cache = Cache(self, with_jinja2_ext=False)

    def add_login_manager(self):
        """ Create and attach Login extension """
        from flask.ext.login import LoginManager
        self.login_manager = LoginManager()
        self.login_manager.init_app(self)

    def add_logging_handlers(self):
        if self.debug: return

        import logging
        from logging import Formatter
        from logging.handlers import RotatingFileHandler

        # Set general log level
        self.logger.setLevel(logging.INFO)

        # Add log file handler (if configured)
        path = self.config.get('LOGFILE')
        if path:
            file_handler = RotatingFileHandler(path, 'a', 10000, 4)
            file_handler.setLevel(logging.INFO)

            file_formatter = Formatter(
                '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
            file_handler.setFormatter(file_formatter)

            self.logger.addHandler(file_handler)

    def add_sentry(self):
        sentry_dsn = self.config.get('SENTRY_DSN')
        if sentry_dsn:
            Sentry(self, dsn=sentry_dsn)

    def add_celery(self):
        from skylines.worker.celery import celery
        celery.init_app(self)
        return celery

    def initialize_lib(self):
        from skylines.lib.geoid import load_geoid
        load_geoid(self)


def create_app(*args, **kw):
    app = SkyLines(*args, **kw)
    app.add_sqlalchemy()
    app.add_cache()
    app.initialize_lib()
    return app


def create_http_app(*args, **kw):
    app = create_app(*args, **kw)

    app.add_logging_handlers()
    app.add_sentry()
    app.add_celery()

    return app


def create_frontend_app(*args, **kw):
    app = create_http_app('skylines.frontend', *args, **kw)

    app.add_login_manager()

    import skylines.frontend.views
    skylines.frontend.views.register(app)

    return app
{'fraction_non_alphanumeric': 0.09625579854208084, 'fraction_numerical': 0.0023194168323392977, 'mean_word_length': 2.7403965303593556, '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': '3898155', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1826, 'n_words': 403}