<commit_before>import importlib
import os
import traceback

from aiohttp.log import web_logger, access_logger
from aiohttp.web import Application as AioApp
from aioweb.util import awaitable
from yarl import URL

from aioweb.middleware import setup_middlewares
from aioweb.util.conf_reader import ConfigReader
from .core import router
from .conf import settings


class Application(AioApp):
    last_controller = None

    def __init__(self, *, logger=web_logger, loop=None, router=None, config=ConfigReader('config/config.yml'),
                 debug=...):
        super().__init__(logger=logger, loop=loop, router=router, middlewares=[], debug=debug)
        self.conf = config
        self.modules = set()

    async def setup(self):

        for mod_name in settings.MODULES:
            try:
                mod = importlib.import_module(".modules.%s" % mod_name, __name__)
                setup = getattr(mod, 'setup')
                if setup:
                    self.modules.add(mod_name)
                    await setup(self)
            except (ImportError, AttributeError) as e:
                traceback.print_exc()

        await setup_middlewares(self)
        router.setup_routes(self)

        try:
            mod = importlib.import_module("app")
            setup = getattr(mod, 'setup')
            await awaitable(setup(self))
        except (ImportError, AttributeError) as e:
            pass

    async def shutdown(self):
        for mod_name in settings.MODULES:
            try:
                mod = importlib.import_module(".modules.%s" % mod_name, __name__)
                shutdown = getattr(mod, 'shutdown')
                if shutdown:
                    await shutdown(self)
            except (ImportError, AttributeError) as e:
                pass
        try:
            mod = importlib.import_module("app")
            shutdown = getattr(mod, 'shutdown')
            await awaitable(shutdown(self))
        except (ImportError, AttributeError) as e:
            pass

    def has_module(self, module):
        return module in self.modules

    async def _handle(self, request):
        # data = await request.post()
        # http_method = request.headers.get('X-Http-Method-Override', '').upper()
        # http_method = data.get('X-Http-Method-Override', '').upper()
        overriden = request.clone(method=request.method,
                              rel_url="?".join((request.rel_url.path.rstrip(
                                  '/') if request.rel_url.path != '/' else request.rel_url.path,
                                                request.query_string))
                              )

        return await super()._handle(overriden)


def run_app(app, *,
            shutdown_timeout=60.0, ssl_context=None,
            print=print, backlog=128, access_log_format=None,
            access_log=access_logger, servers=1):
    """Run an app locally"""
    app['env'] = os.environ.get('AIOWEB_ENV', 'development')
    conf = {
        'host': '0.0.0.0',
        'port': 8000
    }
    conf_reader = ConfigReader('config/server.yml')
    conf.update(conf_reader.config.get(app['env']))

    host = conf['host']
    port = conf['port']
    servers = conf.get('servers', 1)
    unix_socket = conf.get('unix')

    if port is None:
        if not ssl_context:
            port = 8000
        else:
            port = 8443

    loop = app.loop

    make_handler_kwargs = dict()
    if access_log_format is not None:
        make_handler_kwargs['access_log_format'] = access_log_format
    handler = app.make_handler(access_log=access_log,<commit_msg>* do not crash if no config found<commit_after>import importlib
import os
import traceback

from aiohttp.log import web_logger, access_logger
from aiohttp.web import Application as AioApp
from aioweb.util import awaitable
from yarl import URL

from aioweb.middleware import setup_middlewares
from aioweb.util.conf_reader import ConfigReader
from .core import router
from .conf import settings


class Application(AioApp):
    last_controller = None

    def __init__(self, *, logger=web_logger, loop=None, router=None, config=ConfigReader('config/config.yml'),
                 debug=...):
        super().__init__(logger=logger, loop=loop, router=router, middlewares=[], debug=debug)
        self.conf = config
        self.modules = set()

    async def setup(self):

        for mod_name in settings.MODULES:
            try:
                mod = importlib.import_module(".modules.%s" % mod_name, __name__)
                setup = getattr(mod, 'setup')
                if setup:
                    self.modules.add(mod_name)
                    await setup(self)
            except (ImportError, AttributeError) as e:
                traceback.print_exc()

        await setup_middlewares(self)
        router.setup_routes(self)

        try:
            mod = importlib.import_module("app")
            setup = getattr(mod, 'setup')
            await awaitable(setup(self))
        except (ImportError, AttributeError) as e:
            pass

    async def shutdown(self):
        for mod_name in settings.MODULES:
            try:
                mod = importlib.import_module(".modules.%s" % mod_name, __name__)
                shutdown = getattr(mod, 'shutdown')
                if shutdown:
                    await shutdown(self)
            except (ImportError, AttributeError) as e:
                pass
        try:
            mod = importlib.import_module("app")
            shutdown = getattr(mod, 'shutdown')
            await awaitable(shutdown(self))
        except (ImportError, AttributeError) as e:
            pass

    def has_module(self, module):
        return module in self.modules

    async def _handle(self, request):
        # data = await request.post()
        # http_method = request.headers.get('X-Http-Method-Override', '').upper()
        # http_method = data.get('X-Http-Method-Override', '').upper()
        overriden = request.clone(method=request.method,
                              rel_url="?".join((request.rel_url.path.rstrip(
                                  '/') if request.rel_url.path != '/' else request.rel_url.path,
                                                request.query_string))
                              )

        return await super()._handle(overriden)


def run_app(app, *,
            shutdown_timeout=60.0, ssl_context=None,
            print=print, backlog=128, access_log_format=None,
            access_log=access_logger, servers=1):
    """Run an app locally"""
    app['env'] = os.environ.get('AIOWEB_ENV', 'development')
    conf = {
        'host': '0.0.0.0',
        'port': 8000
    }
    conf_reader = ConfigReader('config/server.yml')
    if conf_reader.config:
        conf.update(conf_reader.config.get(app['env']))

    host = conf['host']
    port = conf['port']
    servers = conf.get('servers', 1)
    unix_socket = conf.get('unix')

    if port is None:
        if not ssl_context:
            port = 8000
        else:
            port = 8443

    loop = app.loop

    make_handler_kwargs = dict()
    if access_log_format is not None:
        make_handler_kwargs['access_log_format'] = access_log_format
    handler = app.make_handler(access_log=access_log,
{'fraction_non_alphanumeric': 0.10516884906960716, 'fraction_numerical': 0.006616126809097175, 'mean_word_length': 2.0654837346852557, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4501494', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2034, 'n_words': 472}