
    ȯeiB                        U d Z ddlmZ ddlZddlZddlmZ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	rdd	lmZ  ee      Zd
ed<    ed       G d d             Z G d d      Zy)a  Component registry for Custom Components v2.

This module defines the data model and in-memory registry for Custom Components
v2. During development, component assets (JS/CSS/HTML) may change on disk as
build tools produce new outputs.

See Also
--------
- :class:`streamlit.components.v2.component_file_watcher.ComponentFileWatcher`
  for directory watching and change notifications.
    )annotationsN)	dataclassfield)TYPE_CHECKINGAnyFinal)ComponentPathUtils)StreamlitComponentRegistryError)
get_logger)MutableMappingr   _LOGGERT)frozenc                  h   e Zd ZU dZded<   dZded<   dZded<   dZded<    ed	d	d	
      Z	ded<    ed	d	d	
      Z
ded<    eed	d	      Zded<   dZded<   dZded<   ddZddZedd       Zed d       Zed d       Z	 	 	 	 	 	 	 	 d!dZed d       Zed d       Zed d       Zed"d       Zy)#BidiComponentDefinitiona  Definition of a bidirectional component V2.

    The definition holds inline content or file references for HTML, CSS, and
    JavaScript, plus metadata used by the runtime to serve assets. When CSS/JS
    are provided as file paths, their asset-dir-relative URLs are exposed via
    ``css_url`` and ``js_url`` (or can be overridden with
    ``css_asset_relative_path``/``js_asset_relative_path``).

    Parameters
    ----------
    name : str
        A short, descriptive name for the component.
    html : str or None, optional
        HTML content as a string.
    css : str or None, optional
        Inline CSS content or an absolute/relative path to a ``.css`` file.
        Relative paths are interpreted as asset-dir-relative and validated to
        reside within the component's ``asset_dir``. Absolute paths are rejected
        by the API.
    js : str or None, optional
        Inline JavaScript content or an absolute/relative path to a ``.js``
        file. Relative paths are interpreted as asset-dir-relative and validated
        to reside within the component's ``asset_dir``. Absolute paths are
        rejected by the API.
    css_asset_relative_path : str or None, optional
        Asset-dir-relative URL path to use when serving the CSS file. If not
        provided, the filename from ``css`` is used when ``css`` is file-backed.
    js_asset_relative_path : str or None, optional
        Asset-dir-relative URL path to use when serving the JS file. If not
        provided, the filename from ``js`` is used when ``js`` is file-backed.
    strnameN
str | NonehtmlcssjsF)defaultinitreprbool_has_css_path_has_js_path)default_factoryr   r   dict[str, str]_source_pathscss_asset_relative_pathjs_asset_relative_pathc                   i }| j                  | j                        \  }}| j                  | j                        \  }}|r"t        j                  j                  |      |d<   |r"t        j                  j                  |      |d<   t        j                  | d|       t        j                  | d|       t        j                  | d|       y )Nr   r   r   r   r   )_is_file_pathr   r   ospathdirnameobject__setattr__)selfsource_pathsis_css_pathcss_path
is_js_pathjs_paths         l/var/www/html/glpi_dashboard/venv/lib/python3.12/site-packages/streamlit/components/v2/component_registry.py__post_init__z%BidiComponentDefinition.__post_init__^   s     !% 2 2488 <X"009
G"$''//(";L!#!9L4+>4<4,?    c                4   |yt        |t              r|j                         }t        j                  |       }|r]t
        j                  j                  |      r3|}t
        j                  j                  |      st        d|       d|fS t        d      y)ay  Determine whether ``content`` is a filesystem path and resolve it.

        For string inputs that look like paths (contain separators, prefixes, or
        have common asset extensions), values are normally provided by the v2
        public API, which resolves and validates asset-dir-relative inputs and
        passes absolute paths here. When this dataclass is constructed
        internally, callers must supply already-resolved absolute paths that
        have passed the same validation rules upstream. Relative paths are not
        accepted here.

        Parameters
        ----------
        content : str or None
            The potential inline content or path.

        Returns
        -------
        tuple[bool, str | None]
            ``(is_path, abs_path)`` where ``is_path`` indicates whether the
            input was treated as a path and ``abs_path`` is the resolved
            absolute path if a path, otherwise ``None``.

        Raises
        ------
        ValueError
            If ``content`` is treated as a path but the file does not exist, or
            if a non-absolute, path-like string is provided.
        )FNzFile does not exist: TztRelative file paths are not accepted in BidiComponentDefinition; pass absolute, pre-validated paths from the v2 API.)

isinstancer   stripr	   looks_like_inline_contentr$   r%   isabsexists
ValueError)r)   contentstrippedis_likely_pathabs_paths        r/   r#   z%BidiComponentDefinition._is_file_paths   s    : ? gs#}}H!3!M!Mh!WWN77==)&H77>>(3(+@
)KLL>) J  r1   c                ^    | j                   du xr | j                  du xr | j                  du S )a  Return True if this definition is a placeholder (no content).

        Placeholders are typically created during the manifest scanning phase
        when we discover a component's existence but haven't yet loaded its
        content via the public API.
        N)r   r   r   r)   s    r/   is_placeholderz&BidiComponentDefinition.is_placeholder   s.     yyD ITXX%5I$''T/Ir1   c                f    | j                  | j                  | j                  | j                        S )a8  Return the asset-dir-relative URL path for CSS when file-backed.

        When present, servers construct
        ``/_stcore/bidi-components/<component>/<css_url>`` using this value. If
        ``css_asset_relative_path`` is specified, it takes precedence over the
        filename derived from ``css``.
        has_pathvalueoverride)_derive_asset_urlr   r   r    r>   s    r/   css_urlzBidiComponentDefinition.css_url   s5     %%''((11 & 
 	
r1   c                f    | j                  | j                  | j                  | j                        S )a4  Return the asset-dir-relative URL path for JS when file-backed.

        When present, servers construct
        ``/_stcore/bidi-components/<component>/<js_url>`` using this value. If
        ``js_asset_relative_path`` is specified, it takes precedence over the
        filename derived from ``js``.
        rA   )rE   r   r   r!   r>   s    r/   js_urlzBidiComponentDefinition.js_url   s5     %%&&''00 & 
 	
r1   c               .   |sy|r|S t        |      }t        j                  j                  |      rt        j                  j	                  |      S |j                  dd      j                  d      }d|v r|S t        j                  j	                  |      S )a  Compute asset-dir-relative URL for a file-backed asset.

        Parameters
        ----------
        has_path
            Whether the value refers to a file path.
        value
            The css/js field value (inline string or path).
        override
            Optional explicit asset-dir-relative override.

        Returns
        -------
        str or None
            The derived URL path or ``None`` if not file-backed.
        N\/z./)r   r$   r%   r6   basenamereplaceremoveprefix)r)   rB   rC   rD   path_strnorms         r/   rE   z)BidiComponentDefinition._derive_asset_url   s    & O u:77=="77##H--c*77=d{t>(8(8(>>r1   c                ^    | j                   s| j                  yt        | j                        S )z@Return inline CSS content or ``None`` if file-backed or missing.N)r   r   r   r>   s    r/   css_contentz#BidiComponentDefinition.css_content   s'     !1488}r1   c                ^    | j                   s| j                  yt        | j                        S )zGReturn inline JavaScript content or ``None`` if file-backed or missing.N)r   r   r   r>   s    r/   
js_contentz"BidiComponentDefinition.js_content   s&     477|r1   c                    | j                   S )z7Return inline HTML content or ``None`` if not provided.)r   r>   s    r/   html_contentz$BidiComponentDefinition.html_content  s     yyr1   c                    | j                   S )zReturn source directories for file-backed CSS/JS content.

        The returned mapping contains keys like ``"js"`` and ``"css"`` with the
        directory path from which each was loaded.
        )r   r>   s    r/   r*   z$BidiComponentDefinition.source_paths  s     !!!r1   returnNone)r9   r   rY   ztuple[bool, str | None])rY   r   )rY   r   )rB   r   rC   r   rD   r   rY   r   )rY   r   )__name__
__module____qualname____doc____annotations__r   r   r   r   r   r   dictr   r    r!   r0   r#   propertyr?   rF   rH   rE   rR   rT   rV   r*    r1   r/   r   r   .   s?   @ ID*CB
EFM4Fu5uEL$E$)$UQV$WM>W +/Z.)-J-@*2h J J 
 
 
 
 ? ?(2 ?>H ?	 ?D       " "r1   r   c                  P    e Zd ZdZd
dZ	 	 	 	 ddZddZddZddZd
dZ	ddZ
y	)BidiComponentRegistryzRegistry for bidirectional components V2.

    The registry stores and updates :class:`BidiComponentDefinition` instances in
    a thread-safe mapping guarded by a lock.
    c                D    i | _         t        j                         | _        y)zCInitialize the component registry with an empty, thread-safe store.N)_components	threadingLock_lockr>   s    r/   __init__zBidiComponentRegistry.__init__  s    IK^^%
r1   c                   | j                   5  |j                         D ]  \  }}|j                  d      }|st        d| d      t	        ||j                  d      |j                  d      |j                  d      |j                  d      |j                  d      	      }|| j
                  |<   t        j                  d
|        	 ddd       y# 1 sw Y   yxY w)zRegister components from processed definition data.

        Parameters
        ----------
        component_definitions : dict[str, dict[str, Any]]
            Mapping from component identifier to definition data.
        r   zComponent definition for key 'z"' is missing required 'name' fieldr   r   r   r    r!   )r   r   r   r   r    r!   z2Registered component %s from processed definitionsN)ri   itemsgetr8   r   rf   r   debug)r)   component_definitions	comp_namecomp_def_datar   
definitions         r/   $register_components_from_definitionsz:BidiComponentRegistry.register_components_from_definitions  s     ZZ 	,A,G,G,I (	=$((0$8Cef  5$((.%))%0&**62,9,=,=1- ,9+<+<=U+V	
 /9  +H)'	 	 	s   B<CCc                0   | j                   5  |j                  }|| j                  v r6| j                  |   }||k7  r"|j                  st        j                  d|       || j                  |<   t        j                  d|       ddd       y# 1 sw Y   yxY w)a  Register or overwrite a component definition by name.

        This method is the primary entry point for adding a component to the
        registry. It is used when a component is first declared via the public
        API (e.g., ``st.components.v2.component``).

        If a component with the same name already exists (e.g., a placeholder
        from a manifest scan), it is overwritten. A warning is logged if the
        new definition differs from the old one to alert developers of
        potential conflicts.

        Parameters
        ----------
        definition : BidiComponentDefinition
            The component definition to store.
        zComponent %s is already registered. Overwriting previous definition. This may lead to unexpected behavior if different modules register the same component name with different definitions.zRegistered component %sN)ri   r   rf   r?   r   warningrn   )r)   rr   r   existing_definitions       r/   registerzBidiComponentRegistry.register?  s    & ZZ 	;??Dt'''&*&6&6t&<# (:5/>>OO1  &0DT"MM3T:)	; 	; 	;s   A6BBc                |    | j                   5  | j                  j                  |      cddd       S # 1 sw Y   yxY w)a9  Return a component definition by name, or ``None`` if not found.

        Parameters
        ----------
        name : str
            Component name to retrieve.

        Returns
        -------
        BidiComponentDefinition or None
            The component definition if present, otherwise ``None``.
        N)ri   rf   rm   r)   r   s     r/   rm   zBidiComponentRegistry.geth  s4     ZZ 	.##''-	. 	. 	.s   2;c                    | j                   5  || j                  v r#| j                  |= t        j                  d|       ddd       y# 1 sw Y   yxY w)zRemove a component definition from the registry.

        Primarily useful for tests and dynamic scenarios.

        Parameters
        ----------
        name : str
            Component name to unregister.
        zUnregistered component %sN)ri   rf   r   rn   ry   s     r/   
unregisterz BidiComponentRegistry.unregisterx  sO     ZZ 	At'''$$T*94@	A 	A 	As   2AAc                    | j                   5  | j                  j                          t        j	                  d       ddd       y# 1 sw Y   yxY w)z2Clear all component definitions from the registry.z$Cleared all components from registryN)ri   rf   clearr   rn   r>   s    r/   r}   zBidiComponentRegistry.clear  sA    ZZ 	B""$MM@A	B 	B 	Bs   0AAc                    | j                   5  |j                  }|| j                  vrt        d|       || j                  |<   t        j                  d|       ddd       y# 1 sw Y   yxY w)a  Update (replace) a stored component definition by name.

        This method provides a stricter way to update a component definition
        and is used for internal processes like file-watcher updates. Unlike
        ``register``, it will raise an error if the component is not already
        present in the registry.

        This ensures that background processes can only modify components that
        have been explicitly defined in the current session, preventing race
        conditions or unexpected behavior where a file-watcher event might try
        to update a component that has since been unregistered.

        Callers must supply a fully validated :class:`BidiComponentDefinition`.
        The registry replaces the stored definition under ``definition.name`` in
        a thread-safe manner.

        Parameters
        ----------
        definition : BidiComponentDefinition
            The fully-resolved component definition to store.
        z&Cannot update unregistered component: z#Updated component definition for %sN)ri   r   rf   r
   r   rn   )r)   rr   r   s      r/   update_componentz&BidiComponentRegistry.update_component  ss    , ZZ 	G??D4+++5<TFC  &0DT"MM?F	G 	G 	Gs   AA$$A-NrX   )ro   zdict[str, dict[str, Any]]rY   rZ   )rr   r   rY   rZ   )r   r   rY   zBidiComponentDefinition | None)r   r   rY   rZ   )r[   r\   r]   r^   rj   rs   rw   rm   r{   r}   r   rb   r1   r/   rd   rd     s?    &
!%>!	!F';R. ABGr1   rd   )r^   
__future__r   r$   rg   dataclassesr   r   typingr   r   r   ,streamlit.components.v2.component_path_utilsr	   streamlit.errorsr
   streamlit.loggerr   collections.abcr   r[   r   r_   r   rd   rb   r1   r/   <module>r      sn    
 # 	  ( , , K < '. H% % $^" ^" ^"BZG ZGr1   