L i(<UdZddlmZddlZddlmZddlmZmZddl m Z ddl m Z ddl mZdd lmZmZdd lmZer dd lmZdd lmZeeZd ed<eGddZGddZy)aCustom 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_CHECKINGFinal build_definition_with_validation)ComponentFileWatcher)ComponentManifestHandler)BidiComponentDefinitionBidiComponentRegistry) get_logger)Path)ComponentManifestr_LOGGERc&eZdZUdZded<ded<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 | NonecssjsN)__name__ __module__ __qualname____doc____annotations__o/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/streamlit/components/v2/component_manager.pyrr1s ONrrceZdZdZ d ddZ ddZ ddZddZddZ ddZ dd Z dd Z dd Z dd Z dd ZddZddZddZd dZ d!dZed"dZy)#BidiComponentManagera9Manager 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|xs t|_|xs t|_i|_t j |_|xst|j|_ y)aInitialize 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 threadingLock_api_inputs_lockr _on_components_changed _file_watcher)selfregistrymanifest_handler file_watchers r__init__zBidiComponentManager.__init__lsZ$"<%:%<!1!O5M5O24 ) 0) -A  ' '. rcz|j5t|||j|<dddy#1swYyxYw)aRecord 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``. )rrN)r&rr#)r) component_keyrrs rrecord_api_inputsz&BidiComponentManager.record_api_inputss=  " " I.8SR.HD  ] + I I Is1:c|jj||}|jj|tj dt |y)aRegister 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_definitionsrdebuglen)r)manifest package_rootcomponent_definitionss rregister_from_manifestz+BidiComponentManager.register_from_manifestsL !% 6 6 G G l!  ;;zBidiComponentManager.gets~~!!$''rc"t|||||S)agBuild 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/htmlrrr)r)r/rCrrs rrz5BidiComponentManager.build_definition_with_validations 40'   rc8|jj|S)aLGet 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 rget_component_asset_rootz-BidiComponentManager.get_component_asset_roots%%44T::rc:|jj|y)zUnregister a component by name. Parameters ---------- name : str The name of the component to unregister. N)r! unregisterr?s rrHzBidiComponentManager.unregisters !!$'rc8|jjy)z Clear all registered components.N)r!clearr)s rrJzBidiComponentManager.clear s rcT|jj|}| t|Sy)a5Get 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"rEstr)r)r@ asset_roots rget_component_pathz'BidiComponentManager.get_component_paths-++::4@  !z? "rcH|jjrtjdy|jj }|jj ||jjrtjdytjdy)z*Start file watching for component changes.z File watching is already startedNz+Started file watching for component changeszFile watching not started)r(is_watching_activerwarningr"get_asset_watch_rootsstart_file_watchingr4)r) asset_rootss rrTz(BidiComponentManager.start_file_watching"sv    0 0 OO> ? ,,BBD  ..{;    0 0 MMG H MM5 6rc& ddlm}|}|D]B\}}|j||tj d|j |j D|jy#t$r }tjd|Yd}~yd}~wwxYw)zDiscover installed v2 components and register them. This scans installed distributions for manifests, registers all discovered components, and starts file watching for development workflows. r)scan_component_manifestsz1Registered components from pyproject.toml: %s v%sz&Failed to scan component manifests: %sN) (streamlit.components.v2.manifest_scannerrWr9rinfor@versionrT ExceptionrR)r)rW manifestsr6r7es r discover_and_register_componentsz5BidiComponentManager.discover_and_register_components3s  I 12I*3 &,++HlC GMM$$   $ $ & I OODa H H IsA$A'' B0B  Bc|jjstjdy|jj tj dy)zStop file watching.zFile watching is not startedNzStopped file watching)r(rQrrRstop_file_watchingr4rKs rr`z'BidiComponentManager.stop_file_watchingMs@!!44 OO: ;  --/ -.rc8|jj|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 rrbz!BidiComponentManager.get_metadataWs%%22>BBrc|D]1} |j|}||jj|3y#t$rtj d|YVwxYw)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_componentr[r exception)r)component_namesr@ updated_defs rr'z+BidiComponentManager._on_components_changedfsi$ WD W"AA$G *NN33K@  W  W!!"OQUV Ws.8AAc|j5|jj|}| dddy|jj|}|r |jnd} |j |||j |j} ddd|S#t$r)}tjd||Yd}~dddyd}~wwxYw#1swYSxYw)aRecompute 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/rCrrz5Skipping update for %s due to re-resolution error: %s) r&r#r>r!rCrrrr[rr4)r)rcinputs existing_def html_valuenew_defr]s rrez3BidiComponentManager._recompute_definition_from_apiys" " " %%)).9F~    >>--n=L.:**J ??"0# yy @ 4  K" 1  $ % 4s4C +C !)B CC5C CC  Cc.|jjS)zCheck if file watching is currently active. Returns ------- bool True if file watching is started, False otherwise )r(rQrKs ris_file_watching_startedz-BidiComponentManager.is_file_watching_starteds!!444r)NNN)r*zBidiComponentRegistry | Noner+zComponentManifestHandler | Noner,zComponentFileWatcher | NonereturnNone)r/rMrrrrrqrr)r6rr7rrqrr)r<r rqrr)r@rMrqBidiComponentDefinition | None) r/rMrCrrrrrrqr )r@rMrqz Path | None)r@rMrqrr)rqrr)r@rMrqr)rcrMrqzComponentManifest | None)rhz list[str]rqrr)rcrMrqrs)rqbool)rrrrr-r0r9r;r>rrFrHrJrOrTr^r`rbr'repropertyrprrrrrAs!(X26<@48  . : 2    6I I'1I7AI I& ) 9=  6, (           !  D; ((7"I4/ CW&+!+ '+Z55rr)r __future__rr$ dataclassesrtypingrr5streamlit.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 pathlibrrXrrrrrrrrrrsm#!'PW(JH%%     n5n5r