o
    h:                     @  s  U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZ d dlmZmZmZmZmZmZ d dlmZmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ d dl m!Z! ee"Z#de$d< dZ%de$d< dZ&de$d< ddddddddddd
Z'de$d< g dZ(de$d< d Z)d!Z*dRd$d%Z+G d&d' d'Z,dSd*d+Z-dTd.d/Z.dUd2d3Z/dVd7d8Z0dWd<d=Z1ed>ed?ef d@Z2edXdBdCZ3e	dYdZdFdCZ3dYd[dIdCZ3	dYd\dPdQZ4dS )]    )annotationsN)Sized)wraps)AnyCallableFinalTypeVarcastoverload)configutil)
get_logger)
ForwardMsg)ArgumentCommand)RerunException)get_script_run_ctxr   _LOGGER   _MAX_TRACKED_COMMANDS   _MAX_TRACKED_PER_COMMANDDG	DataFramePlotlyFigureBokehFigureMatplotlibFigurePandasStylerPandasIndexPandasSeriesSnowparkConnectionSQLConnection)
z(streamlit.delta_generator.DeltaGeneratorzpandas.core.frame.DataFramez plotly.graph_objs._figure.Figurezbokeh.plotting.figure.Figurezmatplotlib.figure.Figurezpandas.io.formats.style.Stylerzpandas.core.indexes.base.Indexzpandas.core.series.Seriesz<streamlit.connections.snowpark_connection.SnowparkConnectionz2streamlit.connections.sql_connection.SQLConnection_OBJECT_NAME_MAPPING)ZpymysqlMySQLdbmysqlpymongoibisboto3psycopg2psycopg3
sqlalchemyelasticsearchpyodbcpymssql	cassandraazureredissqlite3neo4jduckdbopensearchpysupabasepolarsdaskvaexmodinpysparkcudfxarrayray	geopandasmarstableszarrdatasets	mistralaiopenai	langchainllama_index	llama_cpp	anthropic
pyllamacppcoheretransformersnomic	diffuserssemantic_kernel	replicatehuggingface_hubwandbtorch
tensorflowtrubricscomet_mlclarifairekahegelfastchat
assemblyaiopenllm
embedchainhaystackvllmalpajinaaiguidancelitellm	comet_llm
instructorxgboostlightgbmcatboostsklearnprefectluigiairflowdagsterpgvectorfaissannoypineconechromadbweaviateqdrant_clientpymilvuslancedb	snowflakestreamlit_extrasstreamlit_pydanticpydanticplostauthlib_ATTRIBUTIONS_TO_CHECKz/etc/machine-idz/var/lib/dbus/machine-idreturnstrc                  C  s   t t } tjtr'tt}| } W d   | S 1 s w   Y  | S tjt	rFtt	}| } W d   | S 1 sAw   Y  | S )aD  Get the machine ID.

    This is a unique identifier for a user for tracking metrics,
    that is broken in different ways in some Linux distros and Docker images.
    - at times just a hash of '', which means many machines map to the same ID
    - at times a hash of the same string, when running in a Docker container
    N)
r   uuidgetnodeospathisfile_ETC_MACHINE_ID_PATHopenread_DBUS_MACHINE_ID_PATH)
machine_idf r   R/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/runtime/metrics_util.py_get_machine_id_v3   s   	





r   c                   @  sN   e Zd ZU e ZdZded< edddZ	dd Z
dd
dZedd ZdS )InstallationNzInstallation | None	_instancer~   c                 C  s^   | j du r,| j | j du rt | _ W d   | j S W d   | j S 1 s'w   Y  | j S )z#Returns the singleton Installation.N)r   _instance_lockr   )clsr   r   r   instance   s   




zInstallation.instancec                 C  s   t ttjt | _d S N)r   r   uuid5NAMESPACE_DNSr   installation_id_v3selfr   r   r   __init__   s   
zInstallation.__init__r   c                 C  s
   t | S r   )r   repr_r   r   r   r   __repr__   s   
zInstallation.__repr__c                 C  s   | j S r   )r   r   r   r   r   installation_id   s   zInstallation.installation_id)r~   r   r~   r   )__name__
__module____qualname__	threadingLockr   r   __annotations__classmethodr   r   r   propertyr   r   r   r   r   r      s   
 
r   objobjectc                 C  s   t t= t| r| nt| }d}t|dr|j}nt|dr$|j}|j	dkr1|j	 d| }|t
v r9t
| }|W  d   S 1 sEw   Y  dS )z7Get a simplified name for the type of the given object.unknownr   r   builtins.Nfailed)
contextlibsuppress	Exceptioninspectisclasstypehasattrr   r   r   r"   )r   obj_type	type_namer   r   r   _get_type_name   s   


 r   funcCallable[..., Any]c                 C  s,   t | }|du s|jsdS |jdd S )z0Get the top level module for the given function.Nr   r   r   )r   	getmoduler   split)r   moduler   r   r   _get_top_level_module   s   
r   arg
str | Nonec                 C  sx   t t- t| trd|  W  d   S t| tr*dt|  W  d   S W d   dS 1 s5w   Y  dS )zBGet metadata information related to the value of the given object.zval:Nzlen:)r   r   r   
isinstanceboolr   len)r   r   r   r   _get_arg_metadata   s   


r   _command_func_command_namer   c                 O  s,  t | j}d}g }t | }|}t|D ]7\}	}
|	}|r!|	d }	t||	kr+||	 n|	 }|dkr5|
}qt|t|
|d}t|
}|rG||_	|
| q| D ]\}}t|t|d}t|}|rf||_	|
| qQt| }|dkr|d| d| }|d	kr|rt|d
r|jrd|j }t||dS )zCGet telemetry information for the given callable and its arguments.N   r   )ktp)r   r   	streamlitz	external::create_instancenamez
component:)r   args)r   getfullargspecr   ismethod	enumerater   r   r   r   mappenditemsr   r   r   r   )r   r   r   kwargsarg_keywordsself_arg	arguments	is_methodr   ir   poskeywordargumentarg_metadatakwargkwarg_valuetop_level_moduler   r   r   _get_command_telemetry   sH   
r   secondsfloatintc                 C  s   t | d S )z"Convert seconds into microseconds.i@B )r   )r   r   r   r   to_microseconds/  s   r   F.)boundr   c                 C     d S r   r   r   r   r   r   r   gather_metrics7     r   NoneCallable[[F], F]c                 C  r   r   r   r   r   r   r   r   >  r   F | NoneCallable[[F], F] | Fc                   s    s	t d d |du rd fdd}|S |t fd	d
}tt |jj t	|_
W d   n1 s@w   Y  td|S )a  Function decorator to add telemetry tracking to commands.

    Parameters
    ----------
    func : callable
    The function to track for telemetry.

    name : str or None
    Overwrite the function name with a custom name that is used for telemetry tracking.

    Example
    -------
    >>> @st.gather_metrics
    ... def my_command(url):
    ...     return url

    >>> @st.gather_metrics(name="custom_name")
    ... def my_command(url):
    ...     return url
    zgather_metrics: name is empty	undefinedNr   r   r~   c                   s   t  | dS )Nr   )r   )r   )r   r   r   wrappera  s   zgather_metrics.<locals>.wrapperc            
   
     sj  ddl m} | }tdd}|d uo|jo|j ot|jtk }d }d}|rq|rqz/t g| R i |}|j	|j
vsC|j
|j	 tk rI|j| |j
|j	g d|_d}W n typ } ztjd|d W Y d }~nd }~ww z-z	| i |}	W n ty } z|r|rt| | |_|d }~ww W |r|rd|_n|r|rd|_w |r|rt| | |_|	S )Nr   )default_timerT)suppress_warningFz#Failed to collect command telemetry)exc_info)timeitr   r   gather_usage_statscommand_tracking_deactivatedr   tracked_commandsr   r   r   tracked_commands_counterr   r   updater   r   debugr   r   time)
r   r   timer
exec_startctxtracking_activatedcommand_telemetry$has_set_command_tracking_deactivatedexresultr   non_optional_funcr   r   wrapped_funcl  sh   

z$gather_metrics.<locals>.wrapped_func)r   r   r~   r   )r   warningr   r   r   AttributeError__dict__r   r   	signature__signature__r	   )r   r   r   r  r   r  r   r   E  s   
A
commandslist[Command]	exec_time	prep_timeuncaught_exceptionr   c                 C  s   t  }|j}|j|  ||_||_td|_t	 }tj
r>tj
 D ]}t|s+q#tj
| }|jr8| d}|| q#|j| dd tD }	ttj|_ttj|_|j|	 |rb||_t  }
rmt|
j|_|S )z2Create and return the full PageProfile ForwardMsg.zserver.headlessz:defaultc                 S  s   h | ]	}|t jv r|qS r   )sysmodules).0attributionr   r   r   	<setcomp>  s
    
z.create_page_profile_message.<locals>.<setcomp>)r   page_profiler  extendr  r  r   
get_optionheadlessset_config_optionskeysis_manually_set
is_defaultaddr}   r   r  platformr   r   tznametimezoneattributionsr  r   r   fragment_ids_this_runis_fragment_run)r  r  r  r  msgr  config_optionsoption_nameconfig_optionr#  r   r   r   r   create_page_profile_message  s6   



r*  r   )r   r   r~   r   )r   r   r~   r   )r   r   r~   r   )r   r   r   r   r~   r   )r   r   r~   r   )r   r   r   r   r~   r   r   )r   r   r   r   r~   r   )r   r   r   r   r~   r   )
r  r  r  r   r  r   r  r   r~   r   )5
__future__r   r   r   r   r  r   r   r   collections.abcr   	functoolsr   typingr   r   r   r   r	   r
   r   r   r   streamlit.loggerr   streamlit.proto.ForwardMsg_pb2r   streamlit.proto.PageProfile_pb2r   r   /streamlit.runtime.scriptrunner_utils.exceptionsr   7streamlit.runtime.scriptrunner_utils.script_run_contextr   r   r   r   r   r   r"   r}   r   r   r   r   r   r   r   r   r   r   r   r*  r   r   r   r   <module>   sd    c





3u