
    ȯei=                        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__     k/var/www/html/glpi_dashboard/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!O5M5O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.8SR.HD]+	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4c:O6P	
r   c                :    | j                   j                  |       y)zRegister 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4T::r   c                :    | j                   j                  |       y)zUnregister 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OO>? ,,BBD 	..{;00MMGHMM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2I*3 &,++HlCGMM$$ #((* #  	IOODa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OO:;--/-.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*NN33K@		W
  W!!"OQU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    " "" 	%%)).9F~	 	  >>--n=L.:**J ??"0#

yy	 @ 	4   K"
 1	 	$%	4 s4   C
+C
!)B	CC5C
CC

Cc                .    | j                   j                  S )zCheck 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   