o
    i(                     @  s   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m	Z	m
Z
 d dlmZ d dlmZ d dlmZmZ d dlZd dlmZ dd	lmZmZmZ dd
lmZmZ ddlmZ eddG dd dZeG dd dejZ G dd de	Z!G dd de"Z#ej$dddddZ%dS )    )annotationsN)ABCabstractmethod)Iterator)	dataclass)Anycast)types   )ATTRIBUTES_MESSAGE_TEMPLATE_KEYATTRIBUTES_SAMPLE_RATE_KEYATTRIBUTES_TAGS_KEY)	StackInfoget_filepath_attribute)uniquify_sequenceT)frozenc                   @  s:   e Zd ZU dZded< ded< dZded< dZded	< dS )
LogfireArgszValues passed to `logfire.instrument` and/or values stored in a logfire instance as basic configuration.

    These determine the arguments passed to the method calls added by the AST transformer.
    ztuple[str, ...]tagszfloat | Nonesample_rateNz
str | Nonemsg_template	span_name)__name__
__module____qualname____doc____annotations__r   r    r   r   a/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/logfire/_internal/ast_utils.pyr      s   
 r   c                   @  sv   e Zd ZU dZded< ded< ded< ded< dd	 Zd"ddZd#ddZd$ddZd%ddZ	d&ddZ
d'dd Zd!S )(BaseTransformerzXHelper for rewriting ASTs to wrap function bodies in `with {logfire_method_name}(...):`.r   logfire_argsstrlogfire_method_namefilenamemodule_namec                 C  s
   g | _ d S N)qualname_stackselfr   r   r   __post_init__0   s   
zBaseTransformer.__post_init__nodeast.ClassDefc                 C  s.   | j |j ttj| |}| j   |S r$   )r%   appendnamer   astClassDefgeneric_visitpopr'   r)   r   r   r   visit_ClassDef5   s   
zBaseTransformer.visit_ClassDef&ast.FunctionDef | ast.AsyncFunctionDefc                 C  sP   | j |j d| j }| j d | | | j   | j   | ||S )N.z<locals>)r%   r+   r,   joinr/   r0   rewrite_functionr'   r)   qualnamer   r   r   visit_FunctionDef<   s   


z!BaseTransformer.visit_FunctionDefast.AsyncFunctionDefc                 C  s
   |  |S r$   )r9   r1   r   r   r   visit_AsyncFunctionDefG   s   
z&BaseTransformer.visit_AsyncFunctionDefr8   returnast.ASTc                   s
   j  }g }|r+t|d tjr+t|d jtjr+t|d jjtr+||	d |rLt
|dkrNt|d tjsLt|d tjrNt|d jtjrN S tjtj|  |dg| jd}||  fdd jD }||d< t di |}tt| S )	Nr   r
   )context_expr)itemsbodytype_commentc                   s   i | ]	}|t  |d qS r$   )getattr).0r,   r)   r   r   
<dictcomp>s   s    z4BaseTransformer.rewrite_function.<locals>.<dictcomp>r@   r   )r@   copy
isinstancer-   ExprvalueConstantr    r+   r0   lenPassWithwithitemlogfire_method_call_noderA   _fieldstypefix_missing_locationscopy_location)r'   r)   r8   r@   new_bodyspankwargsnew_noder   rD   r   r6   J   sB   
	

	z BaseTransformer.rewrite_functionast.Callc                 C  s   t  r$   )NotImplementedErrorr7   r   r   r   rO   x   s   z(BaseTransformer.logfire_method_call_nodelinenoint0tuple[str, dict[str, otel_types.AttributeValue]]c           	      C  s   i t | j||d}i |}| j}|jpd| j d| }||t< |jp'|}|jr2t|j|t	< |j
}|dvr=||t< ||fS )N)zcode.linenozcode.functionzCalling r4   )Nr
   )r   r"   r   r   r#   r   r   r   r   r   r   r   )	r'   r8   rZ   
stack_info
attributesr   r   r   r   r   r   r   logfire_method_arg_values{   s"   
z)BaseTransformer.logfire_method_arg_valuesN)r)   r*   )r)   r3   )r)   r:   )r)   r3   r8   r    r<   r=   )r)   r3   r8   r    r<   rX   )r8   r    rZ   r[   r<   r\   )r   r   r   r   r   r(   r2   r9   r;   r6   rO   r_   r   r   r   r   r   '   s   
 




.r   c                   @  s`   e Zd ZdZdddZddd	ZedddZedddZedddZ	d ddZ
dd ZdS )!CallNodeFindera  Base class for finding the `ast.Call` node corresponding to a function call in a given frame.

    Uses `executing`, then falls back to a heuristic if that fails.
    The heuristic is defined by subclasses which depends on what we're looking for,
    but in general `executing` is expected to work.
    Warns appropriately when things don't work.
    Only used when `inspect_arguments=True` in `logfire.configure()`.
    frametypes.FrameTypec                 C  s,   || _ tj|| _| jj| _|  | _d S r$   )ra   	executingSourceexsource_get_call_noder)   )r'   ra   r   r   r   __init__   s   
zCallNodeFinder.__init__r<   ast.Call | Nonec                   s   t  jjtjr jjS  jjs d d S d}tj	d d dk r&|d7 } fdd 
 D }t|dkr> | d S |d	 S )
NzNo source code available. This happens when running in an interactive shell, using exec(), or running .pyc files without the source .py files.z"`executing` failed to find a node.   )      zM This may be caused by a combination of using Python < 3.11 and auto-tracing.c                   s6   g | ]}t |D ]}t|t jr	 |r	|q	qS r   )r-   walkrG   Callheuristic_call_node_filter)rC   	main_noder)   r&   r   r   
<listcomp>   s    
z1CallNodeFinder._get_call_node.<locals>.<listcomp>r
   r   )rG   re   r)   r-   rn   rf   textwarn_inspect_argumentssysversion_infoheuristic_main_nodesrK   )r'   msg
call_nodesr   r&   r   rg      s"   

zCallNodeFinder._get_call_nodeIterator[ast.AST]c                 C     dS )zFAST nodes (e.g. statements) to search for potential call nodes inside.Nr   r&   r   r   r   rv          z#CallNodeFinder.heuristic_main_nodesr)   rX   boolc                 C  rz   )zKCondition that a potential call node must satisfy to be considered a match.Nr   r1   r   r   r   ro      r{   z)CallNodeFinder.heuristic_call_node_filterr    c                 C  rz   )zMiddle part of the warning message for `warn_inspect_arguments`.

        Should describe the consequences of the failure.
        Nr   r&   r   r   r   warn_inspect_arguments_middle   r{   z,CallNodeFinder.warn_inspect_arguments_middlerw   c                 C  s>   dd l }d|   d| }tj|t|  d || d S )Nr   zHFailed to introspect calling code. Please report this issue to Logfire. z_ Set inspect_arguments=False in logfire.configure() to suppress this warning. The problem was:
)
stacklevel)logfirer}   warningswarnInspectArgumentsFailedWarningget_stacklevel)r'   rw   r   r   r   r   rs      s   z%CallNodeFinder.warn_inspect_argumentsc                 C  s6   t  }d}|r|| jkr	 |S |d7 }|j}|s|S )Nr   r
   )inspectcurrentframera   f_back)r'   current_framer~   r   r   r   r      s   
zCallNodeFinder.get_stacklevelN)ra   rb   )r<   ri   )r<   ry   )r)   rX   r<   r|   )r<   r    )rw   r    )r   r   r   r   rh   rg   r   rv   ro   r}   rs   r   r   r   r   r   r`      s    
	
(
r`   c                   @  s   e Zd ZdS )r   N)r   r   r   r   r   r   r   r      s    r   i   )maxsizer)   r=   	ex_sourceexecuting.Sourcec                 C  s\   t | }t |j| pd}zt t j|dd}W n ty%   d}Y nw ||kr,|S |S )a]  Returns some Python source code representing `node`.

    Preferably the actual original code given by `ast.get_source_segment`,
    but falling back to `ast.unparse(node)` if the former is incorrect.
    This happens sometimes due to Python bugs (especially for older Python versions)
    in the source positions of AST nodes inside f-strings.
     eval)mode)r-   unparseget_source_segmentrr   parse	Exception)r)   r   source_unparsedsource_segmentsource_segment_unparsedr   r   r   get_node_source_text   s   
	r   )r)   r=   r   r   )&
__future__r   r-   	functoolsr   rt   r	   r   abcr   r   collections.abcr   dataclassesr   typingr   r   rc   opentelemetry.util
otel_types	constantsr   r   r   r]   r   r   utilsr   r   NodeTransformerr   r`   Warningr   	lru_cacher   r   r   r   r   <module>   s0    ka
