B
    ½4¹[B	  ã               @   s:   d dl Z d dlZe jdd„ ƒZe jd	dd„ƒZdd„ ZdS )
é    Nc          	   c   sH   t j ¡ }t j ¡  t j | ¡ z
dV  W dt j ¡  t j |¡ X dS )z²Completely replace the environment variables with the specified dict.
    
    Use as a context manager::
    
        with temporary_env({'PATH': my_path}):
            ...
    N)ÚosÚenvironÚcopyÚclearÚupdate)ZnewenvÚorig_env© r   ú+lib/python3.7/site-packages/testpath/env.pyÚtemporary_env   s    	



r
   Tc          	   c   s‚   dd„ }|rt j ¡ }n,i }x&|  ¡ D ]\}}t j |d¡||< q&W || ƒ z
dV  W d|rtt j ¡  t j |¡ n||ƒ X dS )a™  Temporarily modify environment variables.
    
    Specify the changes as a dictionary mapping names to new values, using
    None as the value for names that should be deleted.
    
    Example use::
    
        with modified_env({'SHELL': 'bash', 'PYTHONPATH': None}):
            ...
    
    When the context exits, there are two possible ways to restore the
    environment. If *snapshot* is True, the default, it will reset the whole
    environment to its state when the context was entered. If *snapshot* is
    False, it will restore only the specific variables it modified, leaving
    any changes made to other environment variables in the context.
    c             S   s<   x6|   ¡ D ]*\}}|d kr*tj |d ¡ q
|tj|< q
W d S )N)Úitemsr   r   Úpop)ÚchangesÚkÚvr   r   r	   Ú
update_del(   s    z modified_env.<locals>.update_delN)r   r   r   r   Úgetr   r   )r   Zsnapshotr   Zsaved_variablesr   r   r   r   r	   Úmodified_env   s    

r   c                 s   t j ¡ ‰ ‡ fdd„} | S )a…  Snapshot the current environment, return a function to restore that.

    This is intended to produce cleanup functions for tests. For example,
    using the :class:`unittest.TestCase` API::

        def setUp(self):
            self.addCleanup(testpath.make_env_restorer())

    Any changes a test makes to the environment variables will be wiped out
    before the next test is run.
    c                  s   t j ¡  t j ˆ ¡ d S )N)r   r   r   r   r   )r   r   r	   ÚrestoreO   s    
z"make_env_restorer.<locals>.restore)r   r   r   )r   r   )r   r	   Úmake_env_restorerA   s    
r   )T)Ú
contextlibr   Úcontextmanagerr
   r   r   r   r   r   r	   Ú<module>   s
   *