ó
    Ð]j¿  ã                  ó*  • S SK Jr  S SKrS SKrS SKJr  S SKJr  S SKJ	r	  S SK
Jr  S SKJrJrJrJr  S SKrSS	KJrJr  \(       a  SS
KJr              SS jr                SS jr\	 " S S\5      5       r\" S5      rSS jrSS jrg)é    )ÚannotationsN)Údeque)ÚAbstractContextManager)Ú	dataclass)Úpartial)ÚTYPE_CHECKINGÚAnyÚCallableÚTypeVaré   )ÚBaseTransformerÚLogfireArgs)ÚLogfirec           	     óÞ   ^^^• S[         R                  " 5       R                   3m/ m[        XTX#TU5      n [	        U [
        R                  5      (       d   e[        XSSS9mSUUU4S jjnU$ )aù  Compile a modified AST of the module's source code in the module's namespace.

Returns a function which accepts module globals and executes the compiled code.

The modified AST wraps the body of every function definition in `with context_factories[index]():`.
`context_factories` is added to the module's namespace as `logfire_<uuid>`.
`index` is a different constant number for each function definition.
`context_factories[index]` is one of these:
    - `partial(logfire_instance._fast_span, name, attributes)` where the name and attributes
        are constructed from `filename`, `module_name`, attributes of `logfire_instance`,
        and the qualified name and line number of the current function.
    - `MeasureTime`, a class that measures the time elapsed. If it exceeds `min_duration`,
        then `context_factories[index]` is replaced with the `partial` above.
If `min_duration` is greater than 0, then `context_factories[index]` is initially `MeasureTime`.
Otherwise, it's initially the `partial` above.
Úlogfire_ÚexecT)Údont_inheritc                ó(   >• TU T'   [        TX 5        g ©N)r   )ÚglobsÚcodeÚcontext_factoriesÚlogfire_names    €€€Úe/home/mande/repo/quber/.venv/lib/python3.13/site-packages/logfire/_internal/auto_trace/rewrite_ast.pyÚexecuteÚcompile_source.<locals>.execute-   s   ø€ Ø/ˆˆlÑÜˆT�5Õ ó    )r   zdict[str, Any])ÚuuidÚuuid4ÚhexÚrewrite_astÚ
isinstanceÚastÚModuleÚcompile)	ÚtreeÚfilenameÚmodule_nameÚlogfire_instanceÚmin_durationr   r   r   r   s	         @@@r   Úcompile_sourcer+      sn   ú€ ð& œdŸjšj›l×.Ñ.Ð/Ð0€LØIKÐÜ�t |°[ÐTeÐgsÓt€DÜ�dœCŸJ™J×'Ñ'Ð'Ð'ä�4 6¸Ñ=€D÷!ñ !ð €Nr   c           	     ó€   • [        UR                  UR                  5      n[        XrXXEU5      nUR	                  U 5      $ r   )r   Ú_tagsÚ_sample_rateÚAutoTraceTransformerÚvisit)	r&   r'   r   r(   r)   r   r*   Úlogfire_argsÚtransformers	            r   r!   r!   4   sF   € ô Ð/×5Ñ5Ð7G×7TÑ7TÓU€LÜ&Ø HÐ;KÐ`ló€Kð ×Ñ˜TÓ"Ð"r   c                  ó„   ^ • \ rS rSr% SrS\S'   S\S'   S\S'   SS	 jrSU 4S
 jjrSU 4S jjrSU 4S jjr	SS jr
SrU =r$ )r/   éD   zUTrace all encountered functions except those explicitly marked with `@no_auto_trace`.r   r)   ú/list[Callable[[], AbstractContextManager[Any]]]r   Úintr*   c                ó:   • [        S UR                   5       5      $ )zUReturn true if the node has a `@no_auto_trace` or `@logfire.no_auto_trace` decorator.c              3  óÚ  #   • U  Há  n[        U[        R                  5      =(       a    UR                  [        R
                  :H  =(       d—    [        U[        R                  5      =(       av    UR                  [        R
                  :H  =(       aR    [        UR                  [        R                  5      =(       a'    UR                  R                  [        R
                  :H  v •  Mã     g 7fr   )
r"   r#   ÚNameÚidÚno_auto_traceÚ__name__Ú	AttributeÚattrÚvalueÚlogfire)Ú.0Únodes     r   Ú	<genexpr>Ú;AutoTraceTransformer.check_no_auto_trace.<locals>.<genexpr>N   s¢   é € ð 
ò ,�ô ˜4¤§¡Ó*÷ 6Ø—G‘Gœ}×5Ñ5Ñ5÷ô ˜t¤S§]¡]Ó3÷ :ØŸ	™	¤]×%;Ñ%;Ñ;÷:ä" 4§:¡:¬s¯x©xÓ8÷:ð Ÿ
™
Ÿ™¬×)9Ñ)9Ñ9ôò ,ùs   ‚C)C+)ÚanyÚdecorator_list)ÚselfrB   s     r   Úcheck_no_auto_traceÚ(AutoTraceTransformer.check_no_auto_traceL   s$   € äñ 
ð ×+Ò+ó
ó 
ð 	
r   c                óR   >• U R                  U5      (       a  U$ [        TU ]	  U5      $ r   )rH   ÚsuperÚvisit_ClassDef©rG   rB   Ú	__class__s     €r   rL   Ú#AutoTraceTransformer.visit_ClassDef\   s)   ø€ Ø×#Ñ# D×)Ñ)ØˆKä‰wÑ% dÓ+Ð+r   c                óR   >• U R                  U5      (       a  U$ [        TU ]	  U5      $ r   )rH   rK   Úvisit_FunctionDefrM   s     €r   rQ   Ú&AutoTraceTransformer.visit_FunctionDefb   s)   ø€ Ø×#Ñ# D×)Ñ)ØˆKä‰wÑ(¨Ó.Ð.r   c                óF   >• [        U5      (       a  U$ [        TU ]	  X5      $ r   )Ú	has_yieldrK   Úrewrite_function)rG   rB   ÚqualnamerN   s      €r   rU   Ú%AutoTraceTransformer.rewrite_functionh   s!   ø€ Ü�T�?‰?ØˆKä‰wÑ'¨Ó7Ð7r   c                óê  ^ ^^^^• [        T R                  5      m[        T R                  R                  /T R                  X!R                  5      Q76 mT R                  S:”  ae  T R                  R                  nUR                  R                  mT R                  m " UUU UU4S jS5      nT R                  R                  U5        OT R                  R                  T5        [        R                  " [        R                  " [        R                  " T R                   [        R"                  " 5       S9[        R$                  " [        R&                  " TS9S9[        R"                  " 5       S9/ / S9$ )Nr   c                  ó:   >• \ rS rSrSrU4S jrU UUUU4S jrSrg)ÚBAutoTraceTransformer.logfire_method_call_node.<locals>.MeasureTimeé~   Ústartc                ó   >• T" 5       U l         g r   )r\   )Ú_selfÚtimers    €r   Ú	__enter__ÚLAutoTraceTransformer.logfire_method_call_node.<locals>.MeasureTime.__enter__�   s   ø€ Ù"'£'�E•Kr   c                óV   >• T" 5       U R                   -
  T:¼  a  TTR                  T'   g g r   )r\   r   )r^   Ú_Úindexr*   rG   Úspan_factoryr_   s     €€€€€r   Ú__exit__ÚKAutoTraceTransformer.logfire_method_call_node.<locals>.MeasureTime.__exit__„   s+   ø€ Ù“w §¡Ñ,°Ó<Ø8D˜×.Ñ.¨uÒ5ð =r   © N)r<   Ú
__module__Ú__qualname__Ú__firstlineno__Ú	__slots__r`   rf   Ú__static_attributes__)rd   r*   rG   re   r_   s   €€€€€r   ÚMeasureTimerZ   ~   s   ø† Ø#�	õ*÷Eô Er   rn   )r:   Úctx)r?   )r?   Úslicero   )ÚfuncÚargsÚkeywords)Úlenr   r   r)   Ú
_fast_spanÚlogfire_method_arg_valuesÚlinenor*   Ú_configÚadvancedÚns_timestamp_generatorÚappendr#   ÚCallÚ	Subscriptr9   Úlogfire_method_nameÚLoadÚIndexÚConstant)	rG   rB   rV   Úconfigrn   rd   r*   re   r_   s	   `    @@@@r   Úlogfire_method_call_nodeÚ-AutoTraceTransformer.logfire_method_call_noden   s  ü€ ä�D×*Ñ*Ó+ˆÜØ×!Ñ!×,Ñ,ð
à×+Ñ+¨H·k±kÓBò
ˆð ×Ñ˜qÓ Ø×*Ñ*×2Ñ2ˆFð —O‘O×:Ñ:ˆEØ×,Ñ,ˆL÷E÷ Eð Eð ×"Ñ"×)Ñ)¨+Õ6à×"Ñ"×)Ñ)¨,Ô7ô �xŠxÜ—’Ü—h’h $×":Ñ":ÄÇÂÃ
ÑKÜ—i’i¤c§l¢l¸Ñ&?Ñ@Ü—H’H“Jñð
 Øñ
ð 	
r   rh   )rB   z5ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDefÚreturnÚbool)rB   zast.ClassDef)rB   ú&ast.FunctionDef | ast.AsyncFunctionDef)rB   r‡   rV   Ústrr…   úast.AST)rB   r‡   rV   rˆ   r…   zast.Call)r<   ri   rj   rk   Ú__doc__Ú__annotations__rH   rL   rQ   rU   rƒ   rm   Ú__classcell__)rN   s   @r   r/   r/   D   s6   ø‡ á_àÓØFÓFØÓô
÷ ,÷/÷8÷*
ò *
r   r/   ÚTc                ó   • U $ )aš  Decorator to prevent a function/class from being traced by `logfire.install_auto_tracing`.

This is useful for small functions that are called very frequently and would generate too much noise.

The decorator is detected at import time.
Only `@no_auto_trace` or `@logfire.no_auto_trace` are supported.
Renaming/aliasing either the function or module won't work.
Neither will calling this indirectly via another function.

Any decorated function, or any function defined anywhere inside a decorated function/class,
will be completely ignored by `logfire.install_auto_tracing`.

This decorator simply returns the argument unchanged, so there is zero runtime overhead.
rh   )Úxs    r   r;   r;   ž   s	   € ð €Hr   c                óœ  • [        U /5      nU(       a¹  UR                  5       n [        R                  " U 5       H…  n[	        U[        R
                  [        R                  45      (       a    g[	        U[        R                  [        R                  [        R                  45      (       a  Mt  UR                  U5        M‡     U(       a  M¸  g g )NT)r   Úpopleftr#   Úiter_child_nodesr"   ÚYieldÚ	YieldFromÚFunctionDefÚAsyncFunctionDefÚLambdar{   )rB   ÚqueueÚchilds      r   rT   rT   °   s‚   € Ü�4�&‹M€EÞ
Ø�}‰}‹ˆÜ×)Ò)¨$Ö/ˆEÜ˜%¤#§)¡)¬S¯]©]Ð!;×<Ñ<ÙÜ˜e¤c§o¡o´s×7KÑ7KÌSÏZÉZÐ%X×YÓYØ—‘˜UÖ#ñ	 0÷ Š%r   )r&   r‰   r'   rˆ   r(   rˆ   r)   r   r*   r6   r…   z Callable[[dict[str, Any]], None])r&   r‰   r'   rˆ   r   rˆ   r(   rˆ   r)   r   r   r5   r*   r6   r…   r‰   )r�   r�   r…   r�   )rB   r‰   )Ú
__future__r   r#   r   Úcollectionsr   Ú
contextlibr   Údataclassesr   Ú	functoolsr   Útypingr   r	   r
   r   r@   Ú	ast_utilsr   r   Úmainr   r+   r!   r/   r�   r;   rT   rh   r   r   Ú<module>r¢      sè   ðÝ "ã 
Û Ý Ý -Ý !Ý ß 8Ó 8ã ç 4æÝðØ
ðØ ðØ/2ðØFMðØ]`ðà%ôðB#Ø
ð#àð#ð ð#ð ð	#ð
 ð#ð Gð#ð ð#ð ô#ð  ôS
˜?ó S
ó ðS
ñl ˆCƒL€ôõ$$r   