<commit_before>import os
import logging
import importlib
import eve
import settings
import superdesk
from eve.io.mongo import MongoJSONEncoder
from eve.render import send_response
from superdesk import signals
from superdesk.auth import SuperdeskTokenAuth
from superdesk.celery_app import init_celery
from superdesk.desk_media_storage import SuperdeskGridFSMediaStorage
from superdesk.validator import SuperdeskValidator


def setup_amazon(config):
    config.setdefault('AMAZON_CONTAINER_NAME', os.environ.get('AMAZON_CONTAINER_NAME'))
    config.setdefault('AMAZON_ACCESS_KEY_ID', os.environ.get('AMAZON_ACCESS_KEY_ID'))
    config.setdefault('AMAZON_SECRET_ACCESS_KEY', os.environ.get('AMAZON_SECRET_ACCESS_KEY'))
    config.setdefault('AMAZON_REGION', os.environ.get('AMAZON_REGION'))


def get_app(config=None):
    """App factory.

    :param config: configuration that can override config from `settings.py`
    :return: a new SuperdeskEve app instance
    """
    if config is None:
        config = {}

    for key in dir(settings):
        if key.isupper():
            config.setdefault(key, getattr(settings, key))

    media_storage = SuperdeskGridFSMediaStorage

    setup_amazon(config)
    if config['AMAZON_CONTAINER_NAME']:
        from superdesk.amazon.amazon_media_storage import AmazonMediaStorage
        from superdesk.amazon.import_from_amazon import ImportFromAmazonCommand
        media_storage = AmazonMediaStorage
        superdesk.command('import:amazon', ImportFromAmazonCommand())

    app = eve.Eve(
        data=superdesk.SuperdeskDataLayer,
        auth=SuperdeskTokenAuth,
        media=media_storage,
        settings=config,
        json_encoder=MongoJSONEncoder,
        validator=SuperdeskValidator)

    app.on_fetched_resource = signals.proxy_resource_signal('read', app)
    app.on_fetched_item = signals.proxy_item_signal('read', app)
    app.on_inserted = signals.proxy_resource_signal('created', app)

    @app.errorhandler(superdesk.SuperdeskError)
    def error_handler(error):
        """Return json error response.

        :param error: an instance of :attr:`superdesk.SuperdeskError` class
        """
        return send_response(None, (error.to_dict(), None, None, error.status_code))

    init_celery(app)

    for module_name in app.config['INSTALLED_APPS']:
        app_module = importlib.import_module(module_name)
        try:
            app_module.init_app(app)
        except AttributeError:
            pass

    for resource in superdesk.DOMAIN:
        app.register_resource(resource, superdesk.DOMAIN[resource])

    for blueprint in superdesk.BLUEPRINTS:
        prefix = app.api_prefix or None
        app.register_blueprint(blueprint, url_prefix=prefix)

    return app

if __name__ == '__main__':

    if 'PORT' in os.environ:
        port = int(os.environ.get('PORT'))
        host = '0.0.0.0'
        debug = 'SUPERDESK_DEBUG' in os.environ
    else:
        port = 5000
        host = '127.0.0.1'
        debug = True<commit_msg>fix(elastic): put mapping after registering resources<commit_after>import os
import logging
import importlib
import eve
import settings
import superdesk
from eve.io.mongo import MongoJSONEncoder
from eve.render import send_response
from superdesk import signals
from superdesk.auth import SuperdeskTokenAuth
from superdesk.celery_app import init_celery
from superdesk.desk_media_storage import SuperdeskGridFSMediaStorage
from superdesk.validator import SuperdeskValidator


def setup_amazon(config):
    config.setdefault('AMAZON_CONTAINER_NAME', os.environ.get('AMAZON_CONTAINER_NAME'))
    config.setdefault('AMAZON_ACCESS_KEY_ID', os.environ.get('AMAZON_ACCESS_KEY_ID'))
    config.setdefault('AMAZON_SECRET_ACCESS_KEY', os.environ.get('AMAZON_SECRET_ACCESS_KEY'))
    config.setdefault('AMAZON_REGION', os.environ.get('AMAZON_REGION'))


def get_app(config=None):
    """App factory.

    :param config: configuration that can override config from `settings.py`
    :return: a new SuperdeskEve app instance
    """
    if config is None:
        config = {}

    for key in dir(settings):
        if key.isupper():
            config.setdefault(key, getattr(settings, key))

    media_storage = SuperdeskGridFSMediaStorage

    setup_amazon(config)
    if config['AMAZON_CONTAINER_NAME']:
        from superdesk.amazon.amazon_media_storage import AmazonMediaStorage
        from superdesk.amazon.import_from_amazon import ImportFromAmazonCommand
        media_storage = AmazonMediaStorage
        superdesk.command('import:amazon', ImportFromAmazonCommand())

    app = eve.Eve(
        data=superdesk.SuperdeskDataLayer,
        auth=SuperdeskTokenAuth,
        media=media_storage,
        settings=config,
        json_encoder=MongoJSONEncoder,
        validator=SuperdeskValidator)

    app.on_fetched_resource = signals.proxy_resource_signal('read', app)
    app.on_fetched_item = signals.proxy_item_signal('read', app)
    app.on_inserted = signals.proxy_resource_signal('created', app)

    @app.errorhandler(superdesk.SuperdeskError)
    def error_handler(error):
        """Return json error response.

        :param error: an instance of :attr:`superdesk.SuperdeskError` class
        """
        return send_response(None, (error.to_dict(), None, None, error.status_code))

    init_celery(app)

    for module_name in app.config['INSTALLED_APPS']:
        app_module = importlib.import_module(module_name)
        try:
            app_module.init_app(app)
        except AttributeError:
            pass

    for resource in superdesk.DOMAIN:
        app.register_resource(resource, superdesk.DOMAIN[resource])

    for blueprint in superdesk.BLUEPRINTS:
        prefix = app.api_prefix or None
        app.register_blueprint(blueprint, url_prefix=prefix)

    # we can only put mapping when all resources are registered
    app.data.elastic.put_mapping(app)

    return app

if __name__ == '__main__':

    if 'PORT' in os.environ:
        port = int(os.environ.get('PORT'))
        host = '0.0.0.0'
        debug = 'SUPERDESK_DEBUG' in os.environ
    else:
        port = 5000
        host = '127.0.0.1'
        debug = True
{'fraction_non_alphanumeric': 0.08771358828315703, 'fraction_numerical': 0.004556550040683482, 'mean_word_length': 3.614114114114114, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '5634898', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1881, 'n_words': 393}