Ë
    úmxi=  ã                  óâ   — U d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZmZ dd
lmZ erddlmZ ddlmZ  ee«      Zded<   e G d„ d«      «       Z G d„ d«      Zy)a†  Custom Components v2 manager and supporting orchestration.

This module composes the registry, manifest handling, and file watching
capabilities for Streamlit's Custom Components v2. It provides a unified
interface to register components from manifests or individual definitions, query
component metadata and asset paths, and react to on-disk changes by re-resolving
component definitions.
é    )ÚannotationsN)Ú	dataclass)ÚTYPE_CHECKINGÚFinal©Ú build_definition_with_validation)ÚComponentFileWatcher)ÚComponentManifestHandler)ÚBidiComponentDefinitionÚBidiComponentRegistry)Ú
get_logger)ÚPath)ÚComponentManifestr   Ú_LOGGERc                  ó&   — e Zd ZU dZded<   ded<   y)Ú
_ApiInputsa.  Inputs provided via the Python API to resolve a component definition.

    Attributes
    ----------
    css : str | None
        Inline CSS content or a path/glob to a CSS asset within ``asset_dir``.
    js : str | None
        Inline JS content or a path/glob to a JS asset within ``asset_dir``.
    ú
str | NoneÚcssÚjsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__© ó    ú`/home/htdocs/ttos/venv/lib/python3.12/site-packages/streamlit/components/v2/component_manager.pyr   r   1   s   … ñð 
ƒOØ„Nr   r   c                  óü   — e Zd ZdZ	 	 	 d	 	 	 	 	 	 	 dd„Z	 	 	 	 	 	 	 	 dd„Z	 	 	 	 	 	 dd„Zdd„Zdd„Z	 	 	 	 	 	 	 	 	 	 dd„Z	dd	„Z
dd
„Zdd„Zd d„Zdd„Zddœ	 	 	 d!d„Zdd„Zd"d„Zd#d„Z	 	 	 	 d$d„Zed%d„«       Zy)&ÚBidiComponentManagera9  Manager class that composes component registry, manifest handler, and
    file watcher.

    This class provides a unified interface for working with bidirectional
    components while maintaining clean separation of concerns through
    composition. It handles the coordination and lifecycle management of all
    component-related functionality.

    Component Lifecycle
    -------------------
    The lifecycle of a component managed by this class involves four key stages:

    1.  **Discovery**: On startup, ``discover_and_register_components`` scans
        for installed packages with component manifests (``pyproject.toml``).
        For each component found, a placeholder definition containing only its
        name and ``asset_dir`` is registered. This makes the system aware of all
        available installed components from the outset.

    2.  **Definition & Validation**: When a user's script calls the public API
        (e.g., ``st.components.v2.component(...)``), the manager invokes
        ``build_definition_with_validation``. This function is the single,
        centralized point for all validation. It resolves file paths, performs
        security checks against the component's ``asset_dir``, and produces a
        complete, validated ``BidiComponentDefinition``.

    3.  **Registration**: The validated definition is then passed to the
        registry's ``register`` method. This adds the complete definition,
        overwriting the placeholder if one existed from the discovery phase.

    4.  **Updating**: The ``ComponentFileWatcher`` monitors the ``asset_dir``
        for changes. On a change, it triggers a re-computation of the definition
        using the original API inputs, runs it through the same validation
        logic, and updates the registry with the new definition via the stricter
        ``update_component`` method.

    Notes
    -----
    This manager intentionally favors composition over inheritance and delegates
    specialized responsibilities to ``BidiComponentRegistry``,
    ``ComponentManifestHandler``, and ``ComponentFileWatcher``.
    Nc                óÌ   — |xs
 t        «       | _        |xs
 t        «       | _        i | _        t        j                  «       | _        |xs t        | j                  «      | _
        y)aï  Initialize the component manager.

        Parameters
        ----------
        registry : BidiComponentRegistry, optional
            Component registry instance. If not provided, a new one will be created.
        manifest_handler : ComponentManifestHandler, optional
            Manifest handler instance. If not provided, a new one will be created.
        file_watcher : ComponentFileWatcher, optional
            File watcher instance. If not provided, a new one will be created.
        N)r   Ú	_registryr
   Ú_manifest_handlerÚ_api_inputsÚ	threadingÚLockÚ_api_inputs_lockr	   Ú_on_components_changedÚ_file_watcher)ÚselfÚregistryÚmanifest_handlerÚfile_watchers       r   Ú__init__zBidiComponentManager.__init__l   sZ   € ð$ "Ò<Ô%:Ó%<ˆŒØ!1Ò!OÔ5MÓ5OˆÔà24ˆÔÜ )§¡Ó 0ˆÔØ)ò 
Ô-AØ×'Ñ'ó.
ˆÕr   c                óz   — | j                   5  t        ||¬«      | j                  |<   ddd«       y# 1 sw Y   yxY w)a¾  Record original API inputs for later re-resolution on file changes.

        Parameters
        ----------
        component_key : str
            Fully-qualified component name.
        css : str | None
            Inline CSS or a path/glob to a CSS file within the component's
            ``asset_dir``.
        js : str | None
            Inline JavaScript or a path/glob to a JS file within the component's
            ``asset_dir``.
        )r   r   N)r&   r   r#   )r)   Úcomponent_keyr   r   s       r   Úrecord_api_inputsz&BidiComponentManager.record_api_inputs‡   s=   € ð  ×"Ñ"ñ 	IÜ.8¸SÀRÔ.HˆD×Ñ˜]Ñ+÷	I÷ 	Iñ 	Iús   �1±:c                ó°   — | j                   j                  ||«      }| j                  j                  |«       t        j                  dt        |«      «       y)a€  Register components from a manifest file.

        This is a high-level method that processes the manifest and registers
        all components found within it.

        Parameters
        ----------
        manifest : ComponentManifest
            The component manifest to process.
        package_root : Path
            Root path of the package containing the components.
        z&Registered %d components from manifestN)r"   Úprocess_manifestr!   Ú$register_components_from_definitionsr   ÚdebugÚlen)r)   ÚmanifestÚpackage_rootÚcomponent_definitionss       r   Úregister_from_manifestz+BidiComponentManager.register_from_manifestš   sL   € ð  !%× 6Ñ 6× GÑ GØ�ló!
Ðð
 	�‰×;Ñ;Ð<QÔRä�‰Ø4´cÐ:OÓ6Põ	
r   c                ó:   — | j                   j                  |«       y)z¶Register a single component definition.

        Parameters
        ----------
        definition : BidiComponentDefinition
            The component definition to register.
        N)r!   Úregister)r)   Ú
definitions     r   r;   zBidiComponentManager.registerµ   s   € ð 	�‰×Ñ 
Õ+r   c                ó8   — | j                   j                  |«      S )a%  Get a component definition by name.

        Parameters
        ----------
        name : str
            The name of the component to retrieve.

        Returns
        -------
        BidiComponentDefinition or None
            The component definition if found; otherwise ``None``.
        )r!   Úget©r)   Únames     r   r>   zBidiComponentManager.get¿   s   € ð �~‰~×!Ñ! $Ó'Ð'r   c               ó"   — t        | ||||¬«      S )ag  Build a validated component definition for the given inputs.

        Parameters
        ----------
        component_key : str
            Fully-qualified component name the definition is for.
        html : str | None
            Inline HTML content to include in the definition.
        css : str | None
            Inline CSS content or a path/glob under the component's asset_dir.
        js : str | None
            Inline JS content or a path/glob under the component's asset_dir.

        Returns
        -------
        BidiComponentDefinition
            The fully validated component definition.
        )Úmanagerr/   Úhtmlr   r   r   )r)   r/   rC   r   r   s        r   r   z5BidiComponentManager.build_definition_with_validationÎ   s    € ô4 0ØØ'ØØØô
ð 	
r   c                ó8   — | j                   j                  |«      S )aL  Get the asset root for a manifest-backed component.

        Parameters
        ----------
        name : str
            The name of the component to get the asset root for.

        Returns
        -------
        Path or None
            The component's ``asset_root`` directory if found; otherwise
            ``None``.
        )r"   Úget_asset_rootr?   s     r   Úget_component_asset_rootz-BidiComponentManager.get_component_asset_rootð   s   € ð ×%Ñ%×4Ñ4°TÓ:Ð:r   c                ó:   — | j                   j                  |«       y)z—Unregister a component by name.

        Parameters
        ----------
        name : str
            The name of the component to unregister.
        N)r!   Ú
unregisterr?   s     r   rH   zBidiComponentManager.unregister   s   € ð 	�‰×!Ñ! $Õ'r   c                ó8   — | j                   j                  «        y)z Clear all registered components.N)r!   Úclear©r)   s    r   rJ   zBidiComponentManager.clear
  s   € à�‰×ÑÕr   c                óT   — | j                   j                  |«      }|�t        |«      S y)a5  Get the filesystem path for a manifest-backed component.

        Parameters
        ----------
        name : str
            The name of the component.

        Returns
        -------
        str or None
            The component's ``asset_dir`` directory if found; otherwise
            ``None``.
        N)r"   rE   Ústr)r)   r@   Ú
asset_roots      r   Úget_component_pathz'BidiComponentManager.get_component_path  s-   € ð ×+Ñ+×:Ñ:¸4Ó@ˆ
ØÐ!Ü�z“?Ð"àr   c                óH  — | j                   j                  rt        j                  d«       y| j                  j                  «       }| j                   j                  |«       | j                   j                  rt        j                  d«       yt        j                  d«       y)z*Start file watching for component changes.z File watching is already startedNz+Started file watching for component changeszFile watching not started)r(   Úis_watching_activer   Úwarningr"   Úget_asset_watch_rootsÚstart_file_watchingr4   )r)   Úasset_rootss     r   rT   z(BidiComponentManager.start_file_watching"  sv   € à×Ñ×0Ò0Ü�O‰OÐ>Ô?Øð ×,Ñ,×BÑBÓDˆð 	×Ñ×.Ñ.¨{Ô;à×Ñ×0Ò0Ü�M‰MÐGÕHä�M‰MÐ5Õ6r   T)rT   c               ó,  — 	 ddl m}  |«       }|D ]B  \  }}| j                  ||«       t        j	                  d|j
                  |j                  «       ŒD |r| j                  «        yy# t        $ r }t        j                  d|«       Y d}~yd}~ww xY w)an  Discover installed v2 components and register them.

        This scans installed distributions for manifests, registers all discovered
        components, and starts file watching for development workflows.

        Parameters
        ----------
        start_file_watching : bool
            Whether to start file watching after components are registered.
        r   )Úscan_component_manifestsz1Registered components from pyproject.toml: %s v%sz&Failed to scan component manifests: %sN)
Ú(streamlit.components.v2.manifest_scannerrW   r9   r   r4   r@   ÚversionrT   Ú	ExceptionrR   )r)   rT   rW   Ú	manifestsr6   r7   Úes          r   Ú discover_and_register_componentsz5BidiComponentManager.discover_and_register_components3  s–   € ð	Iõñ 1Ó2ˆIØ*3ò Ñ&�˜,Ø×+Ñ+¨H°lÔCÜ—‘ØGØ—M‘MØ×$Ñ$õðñ #Ø×(Ñ(Õ*ð #øô ò 	IÜ�O‰OÐDÀa×HÑHûð	Iús   ‚A&A* Á*	BÁ3BÂBc                óº   — | j                   j                  st        j                  d«       y| j                   j	                  «        t        j                  d«       y)zStop file watching.zFile watching is not startedNzStopped file watching)r(   rQ   r   rR   Ústop_file_watchingr4   rK   s    r   r_   z'BidiComponentManager.stop_file_watchingU  s@   € à×!Ñ!×4Ò4Ü�O‰OÐ:Ô;Øà×Ñ×-Ñ-Ô/ä�‰Ð-Õ.r   c                ó8   — | j                   j                  |«      S )a)  Get metadata for a component.

        Parameters
        ----------
        component_name : str
            The name of the component to get metadata for.

        Returns
        -------
        ComponentManifest or None
            The component metadata if found; otherwise ``None``.
        )r"   Úget_metadata)r)   Úcomponent_names     r   ra   z!BidiComponentManager.get_metadata_  s   € ð ×%Ñ%×2Ñ2°>ÓBÐBr   c                óº   — |D ]1  }	 | j                  |«      }|�| j                  j                  |«       Œ3 y# t        $ r t        j                  d|«       Y ŒVw xY w)a]  Handle change events for components' asset roots.

        For each component, re-resolve from stored API inputs and update the
        registry with the new definition if resolution succeeds.

        Parameters
        ----------
        component_names : list[str]
            Fully-qualified component names whose watched files changed.
        Nz+Failed to update component after change: %s)Ú_recompute_definition_from_apir!   Úupdate_componentrZ   r   Ú	exception)r)   Úcomponent_namesr@   Úupdated_defs       r   r'   z+BidiComponentManager._on_components_changedn  si   € ð $ò 	WˆDðWØ"×AÑAÀ$ÓG�ØÐ*Ø—N‘N×3Ñ3°KÔ@øñ		Wøô
 ò WÜ×!Ñ!Ð"OÐQUÖVðWús   ‡.8¸AÁAc                ó®  — | j                   5  | j                  j                  |«      }|€
	 ddd«       y| j                  j                  |«      }|r|j                  nd}	 | j                  |||j                  |j                  ¬«      }	 ddd«       |S # t        $ r)}t        j                  d||«       Y d}~ddd«       yd}~ww xY w# 1 sw Y   S xY w)aé  Recompute a component's definition using previously recorded API inputs.

        Parameters
        ----------
        component_name : str
            Fully-qualified component name to recompute.

        Returns
        -------
        BidiComponentDefinition | None
            A fully validated component definition suitable for replacing the
            stored entry in the registry, or ``None`` if recomputation failed
            or no API inputs were previously recorded.
        N)r/   rC   r   r   z5Skipping update for %s due to re-resolution error: %s)r&   r#   r>   r!   rC   r   r   r   rZ   r   r4   )r)   rb   ÚinputsÚexisting_defÚ
html_valueÚnew_defr\   s          r   rd   z3BidiComponentManager._recompute_definition_from_api�  sß   € ð" ×"Ñ"ñ 	Ø×%Ñ%×)Ñ)¨.Ó9ˆFØˆ~Ø÷	ð 	ð  Ÿ>™>×-Ñ-¨nÓ=ˆLÙ.:˜×*Ò*ÀˆJðð ×?Ñ?Ø"0Ø#ØŸ
™
Ø—y‘yð	 @ó ‘÷	ð4 ˆøô ò Ü—‘ØKØ"Øôó
 ÷1	ð 	ûð$ú÷%	ð4 ˆús4   �C
µ+C
Á!)BÂ	CÂCÂ5C
ÃCÃC
Ã
Cc                ó.   — | j                   j                  S )z Check if file watching is currently active.

        Returns
        -------
        bool
            True if file watching is started, False otherwise
        )r(   rQ   rK   s    r   Úis_file_watching_startedz-BidiComponentManager.is_file_watching_started®  s   € ð ×!Ñ!×4Ñ4Ð4r   )NNN)r*   zBidiComponentRegistry | Noner+   zComponentManifestHandler | Noner,   zComponentFileWatcher | NoneÚreturnÚNone)r/   rM   r   r   r   r   rp   rq   )r6   r   r7   r   rp   rq   )r<   r   rp   rq   )r@   rM   rp   úBidiComponentDefinition | None)
r/   rM   rC   r   r   r   r   r   rp   r   )r@   rM   rp   zPath | None)r@   rM   rp   rq   )rp   rq   )r@   rM   rp   r   )rT   Úboolrp   rq   )rb   rM   rp   zComponentManifest | None)rg   z	list[str]rp   rq   )rb   rM   rp   rr   )rp   rs   )r   r   r   r   r-   r0   r9   r;   r>   r   rF   rH   rJ   rO   rT   r]   r_   ra   r'   rd   Úpropertyro   r   r   r   r   r   A   s9  „ ñ(ðX 26Ø<@Ø48ð	
à.ð
ð :ð
ð 2ð	
ð
 
ó
ð6IØ ðIØ'1ðIØ7AðIà	óIð&
Ø)ð
Ø9=ð
à	ó
ó6,ó(ð 
ð ð 
ð ð	 
ð
 ð 
ð ð 
ð 
!ó 
óD;ó (óóó(7ð$ .2ñ IØ&*ð Ià	ó IóD/óCóWð&+Ø!ð+à	'ó+ðZ ò5ó ñ5r   r   )r   Ú
__future__r   r$   Údataclassesr   Útypingr   r   Ú5streamlit.components.v2.component_definition_resolverr   Ú.streamlit.components.v2.component_file_watcherr	   Ú2streamlit.components.v2.component_manifest_handlerr
   Ú*streamlit.components.v2.component_registryr   r   Ústreamlit.loggerr   Úpathlibr   rX   r   r   r   r   r   r   r   r   r   ú<module>r~      sm   ðòõ #ã Ý !ß 'õõ PÝ W÷õ (áÝåJá˜HÓ%€ˆÓ %ð ÷ð ó ð÷v5ò v5r   