U
    Ü¦—\™  ã                   @   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   ú4/usr/lib/python3/dist-packages/keyring/tests/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)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}t d| ƒD ]}|t |¡7 }q|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   
