<commit_before>##
# See the file COPYRIGHT for copyright information.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

"""
Incident Management System web service.
"""

from attr import Factory, attrib, attrs
from attr.validators import instance_of

from twisted.logger import ILogObserver, Logger, globalLogPublisher
from twisted.python.filepath import FilePath
from twisted.web.iweb import IRequest
from twisted.web.static import File

import ims.element
from ims.config import Configuration, URLs
from ims.dms import DutyManagementSystem
from ims.ext.json import jsonTextFromObject
from ims.ext.klein import ContentType, HeaderName, KleinRenderable, static

from ._api import APIApplication
from ._auth import AuthApplication
from ._eventsource import DataStoreEventSourceLogObserver
from ._external import ExternalApplication
from ._klein import redirect, router
from ._web import WebApplication


__all__ = (
    "MainApplication",
)


resourcesDirectory = FilePath(ims.element.__file__).parent().child("static")



@attrs(frozen=True)
class MainApplication(object):
    """
    Incident Management System main application.
    """

    log = _log = Logger()
    router = router


    config: Configuration = attrib(validator=instance_of(Configuration))

    storeObserver: ILogObserver = attrib(
        default=Factory(DataStoreEventSourceLogObserver), init=False
    )

    apiApplication: APIApplication = attrib(
        default=Factory(
            lambda self: APIApplication(
                config=self.config,
                storeObserver=self.storeObserver,
            ),
            takes_self=True,
        ),
        init=False,
    )

    authApplication: AuthApplication = attrib(
        default=Factory(
            lambda self: AuthApplication(config=self.config),
            takes_self=True,
        ),
        init=False,
    )

    externalApplication: ExternalApplication = attrib(
        default=Factory(
            lambda self: ExternalApplication(config=self.config),
            takes_self=True,
        ),
        init=False,
    )

    webApplication: WebApplication = attrib(
        default=Factory(
            lambda self: WebApplication(config=self.config),
            takes_self=True,
        ),
        init=False,
    )


    @property
    def dms(self) -> DutyManagementSystem:
        return self.config.dms


    def __attrs_post_init__(self) -> None:
        globalLogPublisher.addObserver(self.storeObserver)


    def __del__(self) -> None:
        globalLogPublisher.removeObserver(self.storeObserver)


    #
    # Static content
    #

    @router.route(URLs.root, methods=("HEAD", "GET"))<commit_msg>mypy doesn't like lambdas<commit_after>##
# See the file COPYRIGHT for copyright information.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

"""
Incident Management System web service.
"""

from typing import Callable, cast

from attr import Factory, attrib, attrs
from attr.validators import instance_of

from twisted.logger import ILogObserver, Logger, globalLogPublisher
from twisted.python.filepath import FilePath
from twisted.web.iweb import IRequest
from twisted.web.static import File

import ims.element
from ims.config import Configuration, URLs
from ims.dms import DutyManagementSystem
from ims.ext.json import jsonTextFromObject
from ims.ext.klein import ContentType, HeaderName, KleinRenderable, static

from ._api import APIApplication
from ._auth import AuthApplication
from ._eventsource import DataStoreEventSourceLogObserver
from ._external import ExternalApplication
from ._klein import redirect, router
from ._web import WebApplication


__all__ = (
    "MainApplication",
)


resourcesDirectory = FilePath(ims.element.__file__).parent().child("static")



def apiApplicationFactory(parent: 'MainApplication') -> APIApplication:
    return APIApplication(
        config=parent.config,
        storeObserver=parent.storeObserver,
    )


def authApplicationFactory(parent: 'MainApplication') -> AuthApplication:
    return AuthApplication(config=parent.config)


def externalApplicationFactory(parent: 'MainApplication') -> ExternalApplication:
    return ExternalApplication(config=parent.config)


def webApplicationFactory(parent: 'MainApplication') -> WebApplication:
    return WebApplication(config=parent.config)


@attrs(frozen=True)
class MainApplication(object):
    """
    Incident Management System main application.
    """

    log = _log = Logger()
    router = router


    config: Configuration = attrib(validator=instance_of(Configuration))

    storeObserver: ILogObserver = attrib(
        default=Factory(DataStoreEventSourceLogObserver), init=False
    )

    apiApplication: APIApplication = attrib(
        default=Factory(apiApplicationFactory, takes_self=True), init=False
    )

    authApplication: AuthApplication = attrib(
        default=Factory(authApplicationFactory, takes_self=True), init=False
    )

    externalApplication: ExternalApplication = attrib(
        default=Factory(externalApplicationFactory, takes_self=True),
        init=False,
    )

    webApplication: WebApplication = attrib(
        default=Factory(webApplicationFactory, takes_self=True), init=False
    )


    @property
    def dms(self) -> DutyManagementSystem:
        return self.config.dms


    def __attrs_post_init__(self) -> None:
        globalLogPublisher.addObserver(self.storeObserver)


    def __del__(self) -> None:
        globalLogPublisher.removeObserver(self.storeObserver)


    #
    # Static content
    #

    @router.route(URLs.root, methods=("HEAD", "GET"))
{'fraction_non_alphanumeric': 0.08105231143552312, 'fraction_numerical': 0.0012165450121654502, 'mean_word_length': 3.779796511627907, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3600295', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1692, 'n_words': 515}