<commit_before>#!/usr/bin/env python
"""
Magpie additional typing definitions.
"""

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    import math
    import typing
    from typing import Any, AnyStr, Collection, Dict, Iterable, List, Optional, Tuple, Type, Union

    import six
    from pyramid.config import Configurator
    from pyramid.httpexceptions import HTTPException
    from pyramid.registry import Registry
    from pyramid.request import Request as PyramidRequest
    from pyramid.response import Response as PyramidResponse
    from requests.cookies import RequestsCookieJar
    from requests.models import Request as RequestsRequest
    from requests.structures import CaseInsensitiveDict
    from sqlalchemy.orm.session import Session
    from webob.headers import EnvironHeaders, ResponseHeaders
    from webob.request import Request as WebobRequest
    from webob.response import Response as WebobResponse
    from webtest.app import TestRequest
    from webtest.response import TestResponse
    from ziggurat_foundations.permissions import PermissionTuple  # noqa

    from magpie import models
    from magpie.api.webhooks import WEBHOOK_TEMPLATE_PARAMS, WebhookAction, WebhookActionNames
    from magpie.permissions import Permission, PermissionSet

    if hasattr(typing, "TypeAlias"):
        from typing import TypeAlias  # pylint: disable=E0611,no-name-in-module  # Python >= 3.10
    else:
        from typing_extensions import TypeAlias
    if hasattr(typing, "TypedDict"):
        from typing import TypedDict  # pylint: disable=E0611,no-name-in-module
    else:
        from typing_extensions import TypedDict  # noqa
    if hasattr(typing, "Literal"):
        from typing import Literal  # pylint: disable=E0611,no-name-in-module
    else:
        from typing_extensions import Literal  # noqa

    # pylint: disable=W0611,unused-import  # following definitions provided to be employed elsewhere in the code

    if six.PY2:
        # pylint: disable=E0602,undefined-variable  # unicode not recognized by python 3
        Str = Union[AnyStr, unicode]  # noqa: E0602,F405,F821
    else:
        Str = str

    Number = Union[int, float]
    SettingValue = Union[Str, Number, bool, None]
    SettingsType = Dict[Str, SettingValue]
    AnySettingsContainer = Union[Configurator, Registry, PyramidRequest, PyramidResponse, SettingsType]

    ParamsType = Dict[Str, Any]
    CookiesType = Union[Dict[Str, Str], List[Tuple[Str, Str]]]
    HeadersType = Union[Dict[Str, Str], List[Tuple[Str, Str]]]
    AnyHeadersType = Union[HeadersType, ResponseHeaders, EnvironHeaders, CaseInsensitiveDict]
    AnyCookiesType = Union[CookiesType, RequestsCookieJar]
    AnyRequestType = Union[WebobRequest, PyramidRequest, RequestsRequest, TestRequest]
    AnyResponseType = Union[WebobResponse, PyramidResponse, HTTPException, TestResponse]
    CookiesOrSessionType = Union[RequestsCookieJar, Session]

    AnyKey = Union[Str, int]
    AnyValue = Union[Str, Number, bool, None]
    _JSONType = "JSON"  # type: TypeAlias   # pylint: disable=C0103
    BaseJSON = Union[AnyValue, List[_JSONType], Dict[AnyKey, _JSONType]]
    JSON = Union[Dict[Str, Union[_JSONType]], List[_JSONType]]

    GroupPriority = Union[int, Type[math.inf]]
    UserServicesType = Union[Dict[Str, Dict[Str, Any]], List[Dict[Str, Any]]]
    ServiceOrResourceType = Union[models.Service, models.Resource]
    PermissionDict = TypedDict("PermissionDict", {
        "name": Str,
        "access": Optional[Str],
        "scope": Optional[Str],
        "type": Optional[Str],
        "reason": Optional[Str]
    }, total=False)
    # recursive nodes structure employed by functions for listing children resources hierarchy
    # {<res-id>: {"node": <res>, "children": {<res-id>: ... }}
    NestedResourceNodes = Dict[int, "ResourceNode"]
    ResourceNode = TypedDict("ResourceNode", {
        "node": ServiceOrResourceType,
        "children": NestedResourceNodes
    }, total=True)
    ResourcePermissionMap = Dict[int, List[PermissionSet]]  # raw mapping of permission-names applied per resource ID<commit_msg>improve recursive typing of children resources<commit_after>#!/usr/bin/env python
"""
Magpie additional typing definitions.
"""

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    import math
    import typing
    from typing import Any, AnyStr, Collection, Dict, Iterable, List, Optional, Tuple, Type, Union

    import six
    from pyramid.config import Configurator
    from pyramid.httpexceptions import HTTPException
    from pyramid.registry import Registry
    from pyramid.request import Request as PyramidRequest
    from pyramid.response import Response as PyramidResponse
    from requests.cookies import RequestsCookieJar
    from requests.models import Request as RequestsRequest
    from requests.structures import CaseInsensitiveDict
    from sqlalchemy.orm.session import Session
    from webob.headers import EnvironHeaders, ResponseHeaders
    from webob.request import Request as WebobRequest
    from webob.response import Response as WebobResponse
    from webtest.app import TestRequest
    from webtest.response import TestResponse
    from ziggurat_foundations.permissions import PermissionTuple  # noqa

    from magpie import models
    from magpie.api.webhooks import WEBHOOK_TEMPLATE_PARAMS, WebhookAction, WebhookActionNames
    from magpie.permissions import Permission, PermissionSet

    if hasattr(typing, "TypeAlias"):
        from typing import TypeAlias  # pylint: disable=E0611,no-name-in-module  # Python >= 3.10
    else:
        from typing_extensions import TypeAlias
    if hasattr(typing, "TypedDict"):
        from typing import TypedDict  # pylint: disable=E0611,no-name-in-module
    else:
        from typing_extensions import TypedDict  # noqa
    if hasattr(typing, "Literal"):
        from typing import Literal  # pylint: disable=E0611,no-name-in-module
    else:
        from typing_extensions import Literal  # noqa

    # pylint: disable=W0611,unused-import  # following definitions provided to be employed elsewhere in the code

    if six.PY2:
        # pylint: disable=E0602,undefined-variable  # unicode not recognized by python 3
        Str = Union[AnyStr, unicode]  # noqa: E0602,F405,F821
    else:
        Str = str

    Number = Union[int, float]
    SettingValue = Union[Str, Number, bool, None]
    SettingsType = Dict[Str, SettingValue]
    AnySettingsContainer = Union[Configurator, Registry, PyramidRequest, PyramidResponse, SettingsType]

    ParamsType = Dict[Str, Any]
    CookiesType = Union[Dict[Str, Str], List[Tuple[Str, Str]]]
    HeadersType = Union[Dict[Str, Str], List[Tuple[Str, Str]]]
    AnyHeadersType = Union[HeadersType, ResponseHeaders, EnvironHeaders, CaseInsensitiveDict]
    AnyCookiesType = Union[CookiesType, RequestsCookieJar]
    AnyRequestType = Union[WebobRequest, PyramidRequest, RequestsRequest, TestRequest]
    AnyResponseType = Union[WebobResponse, PyramidResponse, HTTPException, TestResponse]
    CookiesOrSessionType = Union[RequestsCookieJar, Session]

    AnyKey = Union[Str, int]
    AnyValue = Union[Str, Number, bool, None]
    _JSONType = "JSON"  # type: TypeAlias   # pylint: disable=C0103
    BaseJSON = Union[AnyValue, List[_JSONType], Dict[AnyKey, _JSONType]]
    JSON = Union[Dict[Str, Union[_JSONType]], List[_JSONType]]

    GroupPriority = Union[int, Type[math.inf]]
    UserServicesType = Union[Dict[Str, Dict[Str, Any]], List[Dict[Str, Any]]]
    ServiceOrResourceType = Union[models.Service, models.Resource]
    PermissionDict = TypedDict("PermissionDict", {
        "name": Str,
        "access": Optional[Str],
        "scope": Optional[Str],
        "type": Optional[Str],
        "reason": Optional[Str]
    }, total=False)
    # recursive nodes structure employed by functions for listing children resources hierarchy
    # {<res-id>: {"node": <res>, "children": {<res-id>: ... }}
    _ResourceNode = "ResourceNode"  # type: TypeAlias  # pylint: disable=C0103
    NestedResourceNodes = Dict[int, _ResourceNode]
    ResourceNode = TypedDict("ResourceNode", {
        "node": ServiceOrResourceType,
        "children": NestedResourceNodes
    }, total=True)
    ResourcePermissionMap = Dict[int, List[PermissionSet]]  # raw mapping of permission-names applied per resource ID
{'fraction_non_alphanumeric': 0.08699843354621038, 'fraction_numerical': 0.009880708519098686, 'mean_word_length': 4.027256208358571, 'pattern_counts': {'":': 18, '<': 9, '<?xml version=': 0, '>': 11, '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': '872940', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2343, 'n_words': 670}