U
    
W[³  ã                   @   s$   d dl mZmZ G dd„ deƒZdS )é    )ÚdivisionÚabsolute_importc                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Z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
   ú7/usr/lib/python3/dist-packages/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   Úrestore9   s    zMonkeyPatcher.restorec                 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   ÚrunWithPatchesB   s    zMonkeyPatcher.runWithPatchesN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r	   r   r   r
   r
   r
   r   r   	   s   

	r   N)Z
__future__r   r   Úobjectr   r
   r
   r
   r   Ú<module>   s   