o
    iV=                     @  sl  d dl mZ d dlZd dlZd dl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Zd dlmZmZ d dlZd	d
lmZmZ d	dlmZmZmZ d	dlmZ d	dlmZ G dd deZG dd deZG dd de	Z e  Z!d6ddZ"	d7d8d d!Z#ed9d'd(Z$G d)d* d*e%Z&G d+d, d,e%Z'G d-d. d.e(Z)d:d0d1Z*d:d2d3Z+G d4d5 d5eZ,dS );    )annotationsN)Iterator)	lru_cache)	Formatter)CodeType)AnyLiteral)NotRequired	TypedDict   )CallNodeFinderget_node_source_text)NOOP_SCRUBBERBaseScrubberMessageValueCleaner)warn_at_user_stacklevel)log_internal_errorc                   @  s   e Zd ZU ded< ded< dS )LiteralChunkzLiteral['lit']tstrvN__name__
__module____qualname____annotations__ r   r   a/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/logfire/_internal/formatter.pyr      s   
 r   c                   @  s&   e Zd ZU ded< ded< ded< dS )ArgChunkzLiteral['arg']r   r   r   zNotRequired[str]specNr   r   r   r   r   r      s   
 r   c                   @  s6   e Zd ZdddddZdddZdddddZdS )ChunksFormatterN)fstring_frameformat_stringr   kwargsdict[str, Any]scrubberr   r!   types.FrameType | Nonereturn9tuple[list[LiteralChunk | ArgChunk], dict[str, Any], str]c                C  s8   |r|  |||}|r|S | j|||d\}}|||fS )N)r#   r%   )_fstring_chunks_vformat_chunks)selfr"   r#   r%   r!   resultchunksextra_attrsr   r   r   r-   "   s   

zChunksFormatter.chunksframetypes.FrameType@tuple[list[LiteralChunk | ArgChunk], dict[str, Any], str] | Nonec                 C  s  |j }|j}|d usJ t|}|j}|d u rd S |tjjjkrEt|j	dkr-|j	d }n(|j
D ]}|jdkr<|j} nq0|d d S n|j	rN|j	d }n|d d S t|tjs]d S |j}	i |j|}
g }d}i }t|dd}|jD ]U}t|tjr|j}||d	d
 ||7 }qvt|tjsJ t||j\}}}|d| d 7 }t||	|
}|||< t||	i |
d|i}|||}||dd
 qv||  |||fS )N   r   msg_templatez:Couldn't identify the `msg_template` argument in the call.r    F
check_keyslitr   r   {}@fvaluearg)f_codef_backFormattingCallNodeFindernodelogfireLogfirelog__code__lenargskeywordsr<   valuewarn_inspect_arguments
isinstanceast	JoinedStr	f_globalsf_localsr   valuesConstantappendFormattedValuecompile_formatted_valuesourceevalclean_valueupdater.   )r+   r#   r%   r/   called_codenode_finder	call_nodearg_nodekeywordglobal_vars
local_varsr,   new_templater.   value_cleaner
node_valuerH   rT   
value_codeformatted_code	formattedr   r   r   r)   =   sZ   






zChunksFormatter._fstring_chunksr2   )recursion_depthre   int4tuple[list[LiteralChunk | ArgChunk], dict[str, Any]]c                C  s  |dk rt dg }d}t|dd}| |D ](\}}	}
}|r(||dd |	dur@|	d	kr5t d
|	dr]|rO|d d dkrO|d d  |	7  < n||	dd |	dd }	z| |	||\}}W nb tys   t d ty } z6t|t	|	krt d|	 d|z||	 }W n ty } zt d| d| d|d}~ww W Y d}~nd}~w t
y } zt d|	 d| |d}~ww |durz| ||}W n t
y } zt d|	 d| |d}~ww | j|
pd	|t|d d\}}d	dd |D }
z| ||
}W n t
y( } zt d|	 d| |d}~ww ||	|}|dd}|
r;|
|d< || q|| fS )z|Copied from `string.Formatter._vformat` https://github.com/python/cpython/blob/v3.11.4/Lib/string.py#L198-L247 then altered.r   z"Max format spec recursion exceededr   Fr5   r7   r8   Nr4   zDEmpty curly brackets `{}` are not allowed. A field name is required.=r   r   z$Numeric field names are not allowed.zThe field {z} is not defined.zThe fields z and z are not defined.zError getting field {z}: zError converting field {r   )r%   re   c                 s      | ]}|d  V  qdS r   Nr   .0chunkr   r   r   	<genexpr>       z2ChunksFormatter._vformat_chunks.<locals>.<genexpr>zError formatting field {r<   r   )KnownFormattingErrorr   parserQ   endswith	get_field
IndexErrorKeyErrorr   repr	Exceptionconvert_fieldr*   r   joinformat_fieldrV   r.   )r+   r"   r#   r%   re   r,   rF   r`   literal_text
field_nameformat_spec
conversionobj	_arg_usedexc1exc2excformat_spec_chunks_rH   dr   r   r   r*      sv   	




zChunksFormatter._vformat_chunks)
r"   r   r#   r$   r%   r   r!   r&   r'   r(   )r#   r$   r%   r   r/   r0   r'   r1   )
r"   r   r#   r$   r%   r   re   rf   r'   rg   )r   r   r   r-   r)   r*   r   r   r   r   r    !   s    
ar    r"   r   r#   r$   r%   r   r'   c                 C  s   t | ||\}}}|S N)logfire_format_with_magic)r"   r#   r%   r,   _extra_attrs_new_templater   r   r   logfire_format   s   
r   r!   r&   tuple[str, dict[str, Any], str]c              
   C  s   zt j| |||d\}}}ddd |D ||fW S  ty7 } ztt|p+t|j W Y d }~n'd }~w tyN } ztt| W Y d }~nd }~w t	yY   t
  Y nw | i | fS )N)r%   r!   r4   c                 s  rj   rk   r   rl   r   r   r   ro     rp   z,logfire_format_with_magic.<locals>.<genexpr>)chunks_formatterr-   rz   rq   warn_formattingr   	__cause__FStringAwaitErrorwarn_fstring_awaitrx   r   )r"   r#   r%   r!   r-   r.   r_   er   r   r   r      s$   "

r   r@   ast.FormattedValue	ex_sourceexecuting.Sourcetuple[str, CodeType, CodeType]c                 C  s   t | j|}t| jD ]}t|tjrt|qt|dd}ttj	tj
tjdt d| j| jdgd}t| t|dd}|||fS )a  Returns three things that can be expensive to compute.

    1. Source code corresponding to the node value (excluding the format spec).
    2. A compiled code object which can be evaluated to calculate the value.
    3. Another code object which formats the value.
    z	<fvalue1>rU   r;   )idctx)rH   r   r~   )rO   z	<fvalue2>)r   rH   rK   walkrJ   Awaitr   compile
ExpressionrL   rR   NameLoadr   r~   fix_missing_locations)r@   r   rT   sub_noderb   exprrc   r   r   r   rS     s&   	

rS   c                   @     e Zd ZdZdS )rq   a.  An error raised when there's something wrong with a format string or the field values.

    In other words this should correspond to errors that would be raised when using `str.format`,
    and generally indicate a user error, most likely that they weren't trying to pass a template string at all.
    Nr   r   r   __doc__r   r   r   r   rq   =      rq   c                   @  r   )r   zAn error raised when an await expression is found in an f-string.

    This is a specific case that can't be handled by f-string introspection and requires
    pre-evaluating the await expression before logging.
    Nr   r   r   r   r   r   E  r   r   c                   @  s   e Zd ZdS )FormattingFailedWarningN)r   r   r   r   r   r   r   r   M  s    r   msgc                 C     t d|  td d S )NaO  
    Ensure you are either:
      (1) passing an f-string directly, with inspect_arguments enabled and working, or
      (2) passing a literal `str.format`-style template, not a preformatted string.
    See https://logfire.pydantic.dev/docs/guides/onboarding-checklist/add-manual-tracing/#messages-and-span-names.
    The problem was: categoryr   r   r   r   r   r   r   Q  s   
r   c                 C  r   )Na  
    Cannot evaluate await expression in f-string. Pre-evaluate the expression before logging.
    For example, change:
      logfire.info(f"{await get_value()}")
    To:
      value = await get_value()
      logfire.info(f"{value}")
    The problematic f-string value was: r   r   r   r   r   r   r   ]  s   
r   c                   @  s,   e Zd ZdZdddZdd	d
Zdd ZdS )r?   zRFinds the call node corresponding to a call like `logfire.span` or `logfire.info`.r'   Iterator[ast.AST]c                 c  s4    | j jD ]}t|tjr|jE d H  q|V  qd S r   )ex
statementsrJ   rK   Withitems)r+   	statementr   r   r   heuristic_main_nodesn  s   z-FormattingCallNodeFinder.heuristic_main_nodesr@   ast.Callboolc                 C  s   t |jp|jS r   )r   rF   rG   )r+   r@   r   r   r   heuristic_call_node_filterv  s   z3FormattingCallNodeFinder.heuristic_call_node_filterc                 C  s   dS )NzgFalling back to normal message formatting which may result in loss of information if using an f-string.r   )r+   r   r   r   warn_inspect_arguments_middlez  s   z6FormattingCallNodeFinder.warn_inspect_arguments_middleN)r'   r   )r@   r   r'   r   )r   r   r   r   r   r   r   r   r   r   r   r?   k  s
    

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   )-
__future__r   rK   typescollections.abcr   	functoolsr   stringr   r   typingr   r   	executingtyping_extensionsr	   r
   rA   	ast_utilsr   r   	scrubbingr   r   r   
stack_infor   utilsr   r   r   r    r   r   r   rS   rx   rq   r   UserWarningr   r   r   r?   r   r   r   r   <module>   s>     J
 &

