o
    ÞñÐa~  ã                   @   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   ú6/usr/lib/python3/dist-packages/keyring/testing/util.pyÚ__init__   s   
zImportKiller.__init__Nc                 C   s   || j v r| S d S r   r   )r   ÚfullnameÚpathr   r   r   Úfind_module   s   
ÿzImportKiller.find_modulec                 C   s   || j v sJ ‚t|ƒ‚r   )r   Ú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 ur|t j|< qd S r   )r   r   Úremover   Úitemsr   )r   ÚargsÚkeyÚvaluer   r   r   Ú__exit__   s   
€þzImportKiller.__exit__r   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r   r   r   r   r   r   r      s    
r   c                 k   s‚   � i }|  ¡ D ]\}}|  |¡||< |du r|| v r| |= q|| |< qdV  |  ¡ D ]\}}|du r:|| v r9| |= q*|| |< q*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)ÚdestinationÚchangesr   r   r   r   r   r   ÚNoNoneDictMutator#   s"   €€
€
ûr$   c                  K   s   t tjfi | ¤Ž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   r0   )Ú
contextlibr%   r   r*   Ústringr   Úcontextmanagerr$   r'   Úascii_lettersÚdigitsÚALPHABETr0   r   r   r   r   Ú<module>   s    
