o
    h%                     @  s   d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	 d dl
mZmZmZ d dlmZ d dlmZ d dlmZ erNd d	lmZ d d
lmZmZ d dlmZ dddZedddZG dd deeef ZG dd deeef ZG dd dZ dS )    )annotations)IterableIteratorMapping)	lru_cache)MappingProxyType)TYPE_CHECKINGAnycast)runtime)gather_metrics)get_script_run_ctx)Morsel)HTTPHeadersHTTPServerRequest)RequestHandlerreturnHTTPServerRequest | Nonec                  C  s\   t  } | d u r	d S t | j}|d u rd S t|j dt|j dkr(d S td|j	S )N.zFstreamlit.web.server.browser_websocket_handler.BrowserWebSocketHandlerr   )
r   r   get_instance
get_client
session_idtype
__module____qualname__r
   request)ctxsession_client r   M/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/runtime/context.py_get_request!   s   r    namestrc                 C  s   d dd | dD S )ziMap a header name to Http-Header-Case.

    >>> _normalize_header("coNtent-TYPE")
    'Content-Type'
    -c                 s  s    | ]}|  V  qd S N)
capitalize).0wr   r   r   	<genexpr>>   s    z$_normalize_header.<locals>.<genexpr>)joinsplit)r!   r   r   r   _normalize_header7   s   r+   c                   @  sV   e Zd ZdddZeddd	ZdddZdddZdddZd ddZ	d!ddZ
dS )"StreamlitHeadersheadersIterable[tuple[str, str]]c                 C  s6   i }|D ]\}}| t|g }|| q|| _d S r$   )
setdefaultr+   append_headers)selfr-   dict_like_headerskeyvalueheader_valuer   r   r   __init__B   s
   
zStreamlitHeaders.__init__tornado_headersr   r   c                 C  s   | |  S r$   )get_all)clsr8   r   r   r   from_tornado_headersK   s   z%StreamlitHeaders.from_tornado_headersr4   r"   	list[str]c                 C  s   t | jt|g S r$   )listr1   getr+   r2   r4   r   r   r   r9   O   s   zStreamlitHeaders.get_allc                 C  s.   z
| j t| d W S  ty   t|d w )Nr   )r1   r+   LookupErrorKeyErrorr?   r   r   r   __getitem__R   s
   
zStreamlitHeaders.__getitem__intc                 C  
   t | jS z,Number of unique headers present in request.)lenr1   r2   r   r   r   __len__X      
zStreamlitHeaders.__len__Iterator[str]c                 C  rD   r$   )iterr1   rG   r   r   r   __iter__\      
zStreamlitHeaders.__iter__dict[str, str]c                   s    fdd D S )Nc                   s   i | ]}| | qS r   r   )r&   r4   rG   r   r   
<dictcomp>`   s    z,StreamlitHeaders.to_dict.<locals>.<dictcomp>r   rG   r   rG   r   to_dict_   s   zStreamlitHeaders.to_dictN)r-   r.   )r8   r   r   r,   )r4   r"   r   r<   r4   r"   r   r"   r   rC   r   rJ   r   rN   )__name__r   r   r7   classmethodr;   r9   rB   rH   rL   rP   r   r   r   r   r,   A   s    
	



r,   c                   @  sL   e Zd ZdddZeddd	ZdddZdddZdddZdddZ	dS )StreamlitCookiescookiesMapping[str, str]c                 C  s   t || _d S r$   )r   _cookies)r2   rX   r   r   r   r7   d   s   zStreamlitCookies.__init__tornado_cookiesdict[str, Morsel[Any]]r   c                 C  s(   i }|  D ]	\}}|j||< q| |S r$   )itemsr5   )r:   r[   dict_like_cookiesr4   morselr   r   r   from_tornado_cookiesg   s   z%StreamlitCookies.from_tornado_cookiesr4   r"   c                 C  s
   | j | S r$   )rZ   r?   r   r   r   rB   p   rM   zStreamlitCookies.__getitem__rC   c                 C  rD   rE   )rF   rZ   rG   r   r   r   rH   s   rI   zStreamlitCookies.__len__rJ   c                 C  rD   r$   )rK   rZ   rG   r   r   r   rL   w   rM   zStreamlitCookies.__iter__rN   c                 C  rD   r$   )dictrZ   rG   r   r   r   rP   z   rM   zStreamlitCookies.to_dictN)rX   rY   )r[   r\   r   rW   rQ   rR   rS   rT   )
rU   r   r   r7   rV   r`   rB   rH   rL   rP   r   r   r   r   rW   c   s    



rW   c                   @  s~   e Zd ZdZeeddddZeeddd	d
ZeeddddZeeddddZ	eeddddZ
dS )ContextProxya  An interface to access user session context.

    ``st.context`` provides a read-only interface to access headers and cookies
    for the current user session.

    Each property (``st.context.headers`` and ``st.context.cookies``) returns
    a dictionary of named values.

    zcontext.headersr   r,   c                 C  s"   t  }|du rti S t|jS )a  A read-only, dict-like object containing headers sent in the initial request.

        Keys are case-insensitive and may be repeated. When keys are repeated,
        dict-like methods will only return the last instance of each key. Use
        ``.get_all(key="your_repeated_key")`` to see all values if the same
        header is set multiple times.

        Examples
        --------
        **Example 1: Access all available headers**

        Show a dictionary of headers (with only the last instance of any
        repeated key):

        >>> import streamlit as st
        >>>
        >>> st.context.headers

        **Example 2: Access a specific header**

        Show the value of a specific header (or the last instance if it's
        repeated):

        >>> import streamlit as st
        >>>
        >>> st.context.headers["host"]

        Show of list of all headers for a given key:

        >>> import streamlit as st
        >>>
        >>> st.context.headers.get_all("pragma")

        N)r    r,   r;   r-   )r2   session_client_requestr   r   r   r-      s   'zContextProxy.headerszcontext.cookiesrW   c                 C  s&   t  }|du rti S |j}t|S )a  A read-only, dict-like object containing cookies sent in the initial request.

        Examples
        --------
        **Example 1: Access all available cookies**

        Show a dictionary of cookies:

        >>> import streamlit as st
        >>>
        >>> st.context.cookies

        **Example 2: Access a specific cookie**

        Show the value of a specific cookie:

        >>> import streamlit as st
        >>>
        >>> st.context.cookies["_ga"]

        N)r    rW   rX   r`   )r2   rc   rX   r   r   r   rX      s
   
zContextProxy.cookieszcontext.timezone
str | Nonec                 C  $   t  }|du s|jdu rdS |jjS )aF  The read-only timezone of the user's browser.

        Example
        -------
        Access the user's timezone, and format a datetime to display locally:

        >>> import streamlit as st
        >>> from datetime import datetime, timezone
        >>> import pytz
        >>>
        >>> tz = st.context.timezone
        >>> tz_obj = pytz.timezone(tz)
        >>>
        >>> now = datetime.now(timezone.utc)
        >>>
        >>> f"The user's timezone is {tz}."
        >>> f"The UTC time is {now}."
        >>> f"The user's local time is {now.astimezone(tz_obj)}"

        N)r   context_infotimezoner2   r   r   r   r   rg      s   zContextProxy.timezonezcontext.timezone_offset
int | Nonec                 C  re   )ac  The read-only timezone offset of the user's browser.

        Example
        -------
        Access the user's timezone offset, and format a datetime to display locally:

        >>> import streamlit as st
        >>> from datetime import datetime, timezone, timedelta
        >>>
        >>> tzoff = st.context.timezone_offset
        >>> tz_obj = timezone(-timedelta(minutes=tzoff))
        >>>
        >>> now = datetime.now(timezone.utc)
        >>>
        >>> f"The user's timezone is {tz}."
        >>> f"The UTC time is {now}."
        >>> f"The user's local time is {now.astimezone(tz_obj)}"

        N)r   rf   timezone_offsetrh   r   r   r   rj      s   zContextProxy.timezone_offsetzcontext.localec                 C  re   )a  The read-only locale of the user's browser.

        ``st.context.locale`` returns the value of |navigator.language|_ from
        the user's DOM. This is a string representing the user's preferred
        language (e.g. "en-US").

        .. |navigator.language| replace:: ``navigator.language``
        .. _navigator.language: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/language

        Example
        -------
        Access the user's locale to display locally:

        >>> import streamlit as st
        >>>
        >>> if st.context.locale == "fr-FR":
        >>>     st.write("Bonjour!")
        >>> else:
        >>>     st.write("Hello!")

        N)r   rf   localerh   r   r   r   rk     s   zContextProxy.localeN)r   r,   )r   rW   )r   rd   )r   ri   )rU   r   r   __doc__propertyr   r-   rX   rg   rj   rk   r   r   r   r   rb   ~   s"    
, rb   N)r   r   )r!   r"   r   r"   )!
__future__r   collections.abcr   r   r   	functoolsr   typesr   typingr   r	   r
   	streamlitr   streamlit.runtime.metrics_utilr   7streamlit.runtime.scriptrunner_utils.script_run_contextr   http.cookiesr   tornado.httputilr   r   tornado.webr   r    r+   r"   r,   rW   rb   r   r   r   r   <module>   s$   
	"