U
    ¶[Öe~  ã                   @   sh   d dl Z d dlZd dlZd dlZd dlZG dd„ dƒZe jdd„ ƒZdd„ Zej	ej
 Zefdd	„ZdS )
é    Nc                   @   s:   e Zd ZdZdd„ Zddd„Zdd„ Zd	d
„ Zdd„ ZdS )ÚImportKillerz@Context manager to make an import of a given name or names fail.c                 G   s
   || _ d S ©N©Únames)Úselfr   © r   úV/var/www/html/services/stratfitenv/lib/python3.8/site-packages/keyring/testing/util.pyÚ__init__   s    zImportKiller.__init__Nc                 C   s   || j kr| S d S r   r   )r   ÚfullnameÚpathr   r   r   Úfind_module   s    
zImportKiller.find_modulec                 C   s   || j kst‚t|ƒ‚d S r   )r   ÚAssertionErrorÚImportError)r   r
   r   r   r   Úload_module   s    zImportKiller.load_modulec                 C   s8   i | _ | jD ]}tj |d ¡| j |< qtj d| ¡ d S )Nr   )Úoriginalr   ÚsysÚmodulesÚpopÚ	meta_pathÚinsert)r   Únamer   r   r   Ú	__enter__   s    
zImportKiller.__enter__c                 G   s6   t j | ¡ | j ¡ D ]\}}|d k	r|t j|< qd S r   )r   r   Úremover   Úitemsr   )r   ÚargsÚkeyÚvaluer   r   r   Ú__exit__   s    zImportKiller.__exit__)N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r   r   r   r   r   r   r      s   
r   c                 k   s€   i }|  ¡ D ]6\}}|  |¡||< |dkr:|| krB| |= q|| |< qdV  |  ¡ D ](\}}|dkrr|| krz| |= qR|| |< qRdS )z¯Helper context manager to make and unmake changes to a dict.

    A None is not a valid value for the destination, and so means that the
    associated name should be removed.N)r   Úget)ÚdestinationÚchangesr   r   r   r   r   r   ÚNoNoneDictMutator#   s    
r%   c                  K   s   t tjf| ŽS )z6A context manager to temporarily change the os.environ)r%   ÚosÚenviron)r$   r   r   r   ÚEnviron:   s    r(   c                 C   s&   d}t d| ƒD ]}|t |¡7 }q|S )z-Generate a random string with length <i>k</i>Ú r   )ÚrangeÚrandomÚchoice)ÚkÚsourceÚresultÚir   r   r   Úrandom_stringB   s    r1   )Ú
contextlibr&   r   r+   Ústringr   Úcontextmanagerr%   r(   Úascii_lettersÚdigitsZALPHABETr1   r   r   r   r   Ú<module>   s   
