Source code for kerno.web.pyramid.typing
"""Pyramid typing stubs so we can write annotated views."""
from typing import Any, List, Optional, Union
from kerno.kerno import Kerno
from kerno.repository.sqlalchemy import BaseSQLAlchemyRepository
from kerno.state import UIMessage
from kerno.typing import DictStr
JSON_primitives = Union[DictStr, List[Any], str, int, float]
[docs]class MultiDictStub(dict):
    """Typing stub for webob.multidict.MultiDict."""
[docs]    def getall(self, key: str) -> List[JSON_primitives]:
        """Return a list of all values matching key (may be an empty list).""" 
[docs]    def getone(self, key: str) -> JSON_primitives:
        """Get one value matching the key. May raise KeyError."""  
[docs]class RegistryStub:  # zope/interface/registry.py
    """Typing stub for a Pyramid registry object."""
    settings: DictStr
[docs]    def registerUtility(
        self,
        component=None,
        provided=None,
        name="",
        info="",
        event=True,
        factory=None,
    ):
        """Register a Pyramid utility.""" 
[docs]    def getUtility(self, provided, name=""):
        """Retrieve a Pyramid utility.""" 
[docs]    def queryUtility(self, provided, name="", default=None):
        """Retrieve a Pyramid utility.""" 
[docs]    def notify(self, *arg, **kw):  # TODO Fill arguments better
        """Broadcast a Pyramid event."""  
[docs]class PyramidSession:
    """Typing stub for a Pyramid session object."""
[docs]    def invalidate(self) -> None:  # noqa
        ... 
[docs]    def flash(self, msg, queue: str = "", allow_duplicate: bool = True) -> None:  # noqa
        ... 
[docs]    def pop_flash(self) -> List[Any]:  # noqa
        ...  
[docs]class PyramidResponse:
    """Typing stub for Pyramid response objects."""
    charset: str
    status: str
    status_int: int
    content_length: int
    content_type: str
    headers: DictStr
    body: bytes 
[docs]class PyramidRequest:
    """Typing stub for pure Pyramid request objects."""
    method: str
    path: str
    path_info: str
    path_qs: str
    path_url: str
    url: str
    context: Any
    identity: Any  # Pyramid 2.0+
    # unauthenticated_userid: Union[int, str]  # deprecated in Pyramid 2.0
    # authenticated_userid: Union[int, str]  # deprecated in Pyramid 2.0
    # effective_principals: List[str]  # deprecated in Pyramid 2.0
    client_addr: str
    exception: Optional[Exception]
    json_body: JSON_primitives
    GET: MultiDictStub
    POST: MultiDictStub
    params: MultiDictStub
    matchdict: DictStr
    response: PyramidResponse
    registry: RegistryStub
    body: bytes
    accept_language: Any
    cookies: DictStr
    session: PyramidSession
[docs]    def route_path(self, route_name: str, *elements, **kw) -> str:
        """Generate a relative URL for a named Pyramid route.""" 
[docs]    def static_path(self, path: str, **kw) -> str:
        """Generate a relative URL to a static resource."""  
[docs]class KRequest(PyramidRequest):
    """Typing stub for a Pyramid/kerno request object.
    It is recommended that you subclass with a more specific
    typing annotation for the ``user`` instance variable.
    """
    kerno: Kerno
    repo: BaseSQLAlchemyRepository
    user: Any
[docs]    def add_flash(self, **kw) -> UIMessage:
        """Add a flash message to the current Pyramid session."""