U
    ª€ljë  ã                   @   s   G d d„ dƒZ dS )c                   @   sN   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ ZeZd
d„ Z	ddd„Z
dd„ ZdS )ÚMonkeyPatcherzn
    Cover up attributes with new objects. Neat for monkey-patching things for
    unit-testing purposes.
    c                 G   s$   g | _ g | _|D ]}| j|Ž  qd S ©N)Ú_patchesToApplyÚ
_originalsÚaddPatch)ÚselfZpatchesÚpatch© r   ú9/tmp/pip-unpacked-wheel-aghyhbke/twisted/python/monkey.pyÚ__init__   s    zMonkeyPatcher.__init__c                 C   s   | j  |||f¡ dS )zæ
        Add a patch so that the attribute C{name} on C{obj} will be assigned to
        C{value} when C{patch} is called or during C{runWithPatches}.

        You can restore the original values with a call to restore().
        N)r   Úappend©r   ÚobjÚnameÚvaluer   r   r	   r      s    zMonkeyPatcher.addPatchc                 C   s,   | j D ] \}}}||f||fkr dS qdS )zc
        Has the C{name} attribute of C{obj} already been patched by this
        patcher?
        TF)r   )r   r   r   ÚoÚnÚvr   r   r	   Ú_alreadyPatched   s    zMonkeyPatcher._alreadyPatchedc                 C   sF   | j D ]:\}}}|  ||¡s4| j ||t||ƒf¡ t|||ƒ qdS )z†
        Apply all of the patches that have been specified with L{addPatch}.
        Reverse this operation using L{restore}.
        N)r   r   r   r   ÚgetattrÚsetattrr   r   r   r	   r   )   s    zMonkeyPatcher.patchc                 C   s(   | j r$| j  ¡ \}}}t|||ƒ q dS )zE
        Restore all original values to any patched objects.
        N)r   Úpopr   r   r   r   r	   Úrestore5   s    zMonkeyPatcher.restoreNc                 C   s   |   ¡  d S r   )r   )r   ZexcTypeZexcValueZexcTracebackr   r   r	   Ú__exit__=   s    zMonkeyPatcher.__exit__c                 O   s&   |   ¡  z|||ŽW ¢S |  ¡  X dS )z“
        Apply each patch already specified. Then run the function f with the
        given args and kwargs. Restore everything when done.
        N)r   r   )r   ÚfÚargsÚkwr   r   r	   ÚrunWithPatches@   s    zMonkeyPatcher.runWithPatches)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r   Ú	__enter__r   r   r   r   r   r   r	   r      s   		


r   N)r   r   r   r   r	   Ú<module>   ó    