B
    øoh\™  ã               @   sl   d dl Z d dlZd dlZd dlZd dlZe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   ú1lib/python3.7/site-packages/keyring/tests/util.pyÚ__init__   s    zImportKiller.__init__Nc             C   s   || j kr| S d S )N)r   )r   ÚfullnameÚpathr   r   r   Úfind_module   s    
zImportKiller.find_modulec             C   s   || j kst‚t|ƒ‚d S )N)r   ÚAssertionErrorÚImportError)r   r   r   r   r   Úload_module   s    zImportKiller.load_modulec             C   s<   i | _ x"| jD ]}tj |d ¡| j |< qW tj d| ¡ d S )Nr   )Úoriginalr   ÚsysÚmodulesÚpopÚ	meta_pathÚinsert)r   Únamer   r   r   Ú	__enter__   s    zImportKiller.__enter__c             G   s:   t j | ¡ x(| j ¡ D ]\}}|d k	r|t j|< qW d S )N)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 }xB|  ¡ D ]6\}}|  |¡||< |dkr<|| krD| |= q|| |< qW dV  x4|  ¡ D ](\}}|dkrx|| kr€| |= qX|| |< qXW d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)Z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}x t d| ƒD ]}|t |¡7 }qW |S )z2Generate a random string with length <i>k</i>
    Ú r   )ÚrangeÚrandomZchoice)ÚkÚsourceÚresultÚir   r   r   Úrandom_stringD   s    r-   )Ú
contextlibr#   r   r(   ÚstringÚtypeZ__metaclass__r   Úcontextmanagerr"   r%   Zascii_lettersZdigitsZALPHABETr-   r   r   r   r   Ú<module>   s   