
    BTh4                         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Zddlm	Z	 ddl
mZ ddlmZmZmZmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ ddlmZ  ej:                  e      Z G d d      Z  G d d      Z!y)    N)iscoroutinefunction   )handle_coroutine)UnhandledHTTPRequestError)get_matchers_resultsmethodrequests_matchuri)CassettePatcherBuilder)CassetteDecodeErrorCassetteNotFoundErrorFilesystemPersister)
RecordMode)yamlserializer)partition_dictc                       e Zd ZdZdZed        Zd Zd Zd Z	d Z
ej                  d        Zd	 Zd
 Zd Zed        Zd Zy)CassetteContextDecoratora  Context manager/decorator that handles installing the cassette and
    removing cassettes.

    This class defers the creation of a new cassette instance until
    the point at which it is installed by context manager or
    decorator. The fact that a new cassette is used with each
    application prevents the state of any cassette from interfering
    with another.

    Instances of this class are NOT reentrant as context managers.
    However, functions that are decorated by
    ``CassetteContextDecorator`` instances ARE reentrant. See the
    implementation of ``__call__`` on this class for more details.
    There is also a guard against attempts to reenter instances of
    this class as a context manager in ``__exit__``.
    )path_transformerfunc_path_generatorrecord_on_exceptionc                      | |fd      S )Nc                      t               S N)dict)kwargss   ^/var/www/catia.catastroantioquia-mas.com/valormas/lib/python3.12/site-packages/vcr/cassette.py<lambda>z4CassetteContextDecorator.from_args.<locals>.<lambda>0   s    4<      )clscassette_classr   s     `r   	from_argsz"CassetteContextDecorator.from_args.   s    >#788r   c                 <    || _         || _        d | _        d | _        y r   )r    _args_getter!_CassetteContextDecorator__finish#_CassetteContextDecorator__cassette)selfr    args_getters      r   __init__z!CassetteContextDecorator.__init__2   s    'r   c              #     K   t        j                         5 }t        |      j                         D ]  }|j	                  |        d}t
        j                  |j                  d|j                               | t
        j                  |j                  d|j                               d d d        y # 1 sw Y   y xY ww)Nz({action} context for cassette at {path}.Entering)actionpathExiting)	
contextlib	ExitStackr   buildenter_contextlogdebugformat_path)r'   cassette
exit_stackpatcher
log_formats        r   _patch_generatorz)CassetteContextDecorator._patch_generator8   s     !!# 	Pz1(;AAC 2((12CJIIj''z'OPNIIj''yx~~'NO	P 	P 	Ps   CBB6-	C6B?;Cc                 ^     j                   J d       t         fd j                               \  }}|j                  d      r|d   } ||d         |d<     j                  j
                  di | _         j                   j                         _         t         j                         S )NzCassette already open.c                      | j                   v S r   )_non_cassette_arguments)key_r'   s     r   r   z4CassetteContextDecorator.__enter__.<locals>.<lambda>L   s    3$">">> r   r   r-   r   )	r%   r   r$   getr    loadr&   r;   next)r'   other_kwargscassette_kwargstransformers   `   r   	__enter__z"CassetteContextDecorator.__enter__A   s     }}$>&>>$(6>)
%o ./&'9:K&1/&2I&JOF#'$((--:/:--doo>DMM""r   c                     t        |      }| j                         j                  dd      }|s|s!| j                  j	                          d | _        t        | j                  d        d | _        y )Nr   T)anyr$   rA   r&   _saverC   r%   )r'   exc_infoexception_was_raisedr   s       r   __exit__z!CassetteContextDecorator.__exit__V   s]    "8}"//1556KTR&:OO!!#"DO 	T]]D!r   c                 ~    | j                  |      } t        |       | j                  |      j                  |||      S r   ) _build_args_getter_for_decoratortyper    _execute_function)r'   functioninstanceargsr   r(   s         r   __call__z!CassetteContextDecorator.__call__e   s;    
 ;;HEtDz$((K0BB8TSYZZr   c                     fd}t              rt        | |      S t        j                        r| j	                  |      S | j                  |      S )Nc                 D    | j                   r | gi S  i S r   )inject)r7   rT   rR   r   s    r   handle_functionzCCassetteContextDecorator._execute_function.<locals>.handle_functionn   s/    :4:6::000r   )vcrfn)r[   )r   r   inspectisgeneratorfunction_handle_generator_handle_function)r'   rR   rT   r   rY   s    ``` r   rQ   z*CassetteContextDecorator._execute_functionm   sU    	1 x(#AA&&x0))_)==$$$88r   c              #   ^   K   | 5 } ||      E d{   cddd       S 7 # 1 sw Y   yxY ww)zoWraps a generator so that we're inside the cassette context for the
        duration of the generator.
        Nr   r'   r[   r7   s      r   r^   z*CassetteContextDecorator._handle_generator{   s4       	-X!(|+	- 	-+	- 	-s    -!!
-!*-c                 B    | 5 } ||      cd d d        S # 1 sw Y   y xY wr   r   ra   s      r   r_   z)CassetteContextDecorator._handle_function   s#     	 Xh<	  	  	 s   c                     | j                   S r   )__name__)rR   s    r   get_function_namez*CassetteContextDecorator.get_function_name   s       r   c                       fd}|S )Nc                      j                         } d| vr,| j                  d      xs j                  } |      }|| d<   | S )Nr-   r   )r$   rA   re   )r   name_generatorr-   rR   r'   s      r   new_args_getterzRCassetteContextDecorator._build_args_getter_for_decorator.<locals>.new_args_getter   sK    &&(FV#!',A!B!\dF\F\%h/!%vMr   r   )r'   rR   ri   s   `` r   rO   z9CassetteContextDecorator._build_args_getter_for_decorator   s    	 r   N)rd   
__module____qualname____doc__r>   classmethodr"   r)   r;   rG   rM   wrapt	decoratorrU   rQ   r^   r_   staticmethodre   rO   r   r   r   r   r      sz    " 9 9P#* __[ [9-  ! !	r   r   c            	          e Zd ZdZed        Zed        Zed        Zddej                  e
efdddddf	dZed	        Zed
        Zed        Zed        Zed        Zd Zd Zd Zd Zd Zd Zd Zd Zd ZddZd Zd Zd Zd Z y)Cassettez/A container for recorded requests and responsesc                 6     | di |}|j                          |S )z?Instantiate and load the cassette stored at the specified path.r   )_load)r    r   new_cassettes      r   rB   zCassette.load   s!     }V}r   c                     t        | |      S r   )r   )r    
arg_getters     r   use_arg_getterzCassette.use_arg_getter   s    'Z88r   c                 .    t        j                  | fi |S r   )r   r"   )r    r   s     r   usezCassette.use   s    '11#@@@r   Nr   Fc                 ^   |xs t         | _        || _        |xs t        | _        || _        |xs d | _        t        j                  | j                         |xs d | _	        |	| _
        || _        || _        |
| _        g | _        t        j                          | _        d| _        d| _        y )Nc                     | S r   r   xs    r   r   z#Cassette.__init__.<locals>.<lambda>   s    ! r   c                     | S r   r   r}   s    r   r   z#Cassette.__init__.<locals>.<lambda>   s    A r   F)r   
_persisterr6   r   _serializer	_match_on_before_record_requestr3   info_before_record_responserX   record_modecustom_patchesallow_playback_repeatsdatacollectionsCounterplay_countsdirtyrewound)r'   r-   
serializer	persisterr   match_onbefore_record_requestbefore_record_responser   rX   r   s              r   r)   zCassette.__init__   s     $:':
%7!&;&L#,,-'='N+$&,&<# 	&..0
r   c                 H    t        | j                  j                               S r   )sumr   valuesr'   s    r   
play_countzCassette.play_count   s    4##**,--r   c                 `    t        | j                  j                               t        |       k(  S )z@Returns True if all responses have been played, False otherwise.)lenr   r   r   s    r   
all_playedzCassette.all_played   s'     4##**,-T::r   c                 L    | j                   D cg c]  \  }}|	 c}}S c c}}w r   r   r'   requestresponses      r   requestszCassette.requests   s    3799=/Wh===    c                 L    | j                   D cg c]  \  }}|	 c}}S c c}}w r   r   r   s      r   	responseszCassette.responses   s    48II>0gx>>>r   c                     | j                   xr | j                  t        j                  k(  xs | j                  t        j                  k(  S r   )r   r   r   ONCENONEr   s    r   write_protectedzCassette.write_protected   s7    D!1!1Z__!DlIYIY]g]l]lIllr   c                     t         j                  d||       | j                  |      }|syt        j                  |      }| j                  |      }|y| j                  j                  ||f       d| _        y)z-Add a request, response pair to this cassettez$Appending request %s and response %sNT)	r3   r   r   copydeepcopyr   r   appendr   r   s      r   r   zCassette.append   sp    7(K--g6 ==*//9		'8,-
r   c                 $    | j                  |      S r   )r   r'   r   s     r   filter_requestzCassette.filter_request   s    **733r   c              #      K   | j                  |      }t        | j                        D ]&  \  }\  }}t        ||| j                        s!||f ( yw)zd
        internal API, returns an iterator with all responses matching
        the request.
        N)r   	enumerater   r	   r   )r'   r   indexstored_requestr   s        r   
_responseszCassette._responses   sT     
 --g61:4991E 	&-E-NHg~t~~FXo%	&s   AA
	Ac                     | j                  |      }|xr1 || v xr+ | j                  t        j                  k7  xr | j                  S r   )r   r   r   ALLr   r   s     r   can_play_response_forzCassette.can_play_response_for   sA    --g6b7d?bt/?/?:>>/QbVZVbVbbr   c                     | j                  |      D ]>  \  }}| j                  |   dk(  s| j                  s%| j                  |xx   dz  cc<   |c S  t        d| j                  d|d      )z
        Get the response corresponding to a request, but only if it
        hasn't been played back before, and mark it as played
        r   r   The cassette () doesn't contain the request () asked for)r   r   r   r   r6   )r'   r   r   r   s       r   play_responsezCassette.play_response   s}    
  $w7 	 OE8&!+t/J/J  '1,'	 
 (TZZN*I'T_`
 	
r   c                     | j                  |      D cg c]  \  }}|	 }}}|r|S t        d| j                  d|d      c c}}w )z
        Find the responses corresponding to a request.
        This function isn't actually used by VCR internally, but is
        provided as an external API.
        r   r   r   )r   r   r6   )r'   r   r   r   r   s        r   responses_ofzCassette.responses_of
  sX     6:__W5MN/%XN	N'TZZN*I'T_`
 	
 Os   Ac                 6    t        j                         | _        y r   )r   r   r   r   s    r   rewindzCassette.rewind  s    &..0r   c                 ~   g }| j                  |      }t        | j                        D ]@  \  }\  }}t        ||| j                        \  }}|j                  t        |      |||f       B |j                  d d       g }|s|S |d   d   }|D ]*  }	|	d   }
|
dk  s||
k7  r |S |
}|j                  |	dd        , |S )a  
        Get the most similar request(s) stored in the cassette
        of a given request as a list of tuples like this:
        - the request object
        - the successful matchers as string
        - the failed matchers and the related assertion message with the difference details as strings tuple

        This is useful when a request failed to be found,
        we can get the similar request(s) in order to know what have changed in the request parts.
        c                     | d   S )Nr   r   )ts    r   r   z:Cassette.find_requests_with_most_matches.<locals>.<lambda>,  s
    ! r   T)r?   reverser   r   N)r   r   r   r   r   r   r   sort)r'   r   best_matchesr@   r   	successesfailsfinal_best_matchesprevious_nb_success
best_match
nb_successs              r   find_requests_with_most_matchesz(Cassette.find_requests_with_most_matches  s     --g6&/		&: 	T"A"3G^T^^\IuYE RS	T 	nd;%%*1oa0& 	6J#AJ A~!4
!B "! #-%%jn5	6 "!r   c                 4    | j                   | j                  dS )Nr   r   r   r   s    r   _as_dictzCassette._as_dict@  s     MMGGr   c                     |s| j                   rH| j                  j                  | j                  | j	                         | j
                         d| _         y y )Nr   F)r   r   save_cassetter6   r   r   )r'   forces     r   rJ   zCassette._saveC  sA    DJJOO))$**dmmoRVRbRb)cDJ r   c                     	 | j                   j                  | j                  | j                        \  }}t	        ||      D ]  \  }}| j                  ||        d| _        d| _        y # t        t        f$ r Y y w xY w)Nr   FT)
r   load_cassetter6   r   zipr   r   r   r   r   )r'   r   r   r   r   s        r   rt   zCassette._loadH  s    	"&//"?"?

W[WgWg"?"hHi%(9%= /!GX./DJDL#%:; 		s   A(A+ +A=<A=c                      dt        |        dS )Nz<Cassette containing z recorded response(s)>)r   r   s    r   __str__zCassette.__str__R  s    &s4yk1GHHr   c                 ,    t        | j                        S )z:Return the number of request,response pairs stored in here)r   r   r   s    r   __len__zCassette.__len__U  s    499~r   c                 v    | j                  |      D ]%  \  }}| j                  |   dk(  s| j                  s% y y)z/Return whether or not a request has been storedr   TF)r   r   r   )r'   r   r   r@   s       r   __contains__zCassette.__contains__Y  sA    0 	HE1&!+t/J/J	 r   )F)!rd   rj   rk   rl   rm   rB   rx   rz   r   r   r
   r   r)   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r   rJ   rt   r   r   r   r   r   r   rr   rr      s   9  9 9 A A OOv"#$> . . ; ; > > ? ? m m4&c

1""HH
Ir   rr   )"r   r/   r   r\   loggingasyncior   rn   _handle_coroutiner   errorsr   matchersr   r   r	   r
   patchr   persisters.filesystemr   r   r   r   r   serializersr   utilr   	getLoggerrd   r3   r   rr   r   r   r   <module>r      sc         '  / - G G ) b b # '  g!} }@H Hr   