o
    3g                     @  sB   d dl mZ d dlZd dlmZ G dd deZG dd dZdS )    )annotationsN)contextmanagerc                      sd   e Zd Zg dZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Z  ZS )EnvPathList)_path_factory_pathsep__weakref__c                   s   t    || _|| _d S N)super__init__r   r   )selfpath_factorypathsep	__class__ L/home/garg/my-data/venv/lib/python3.10/site-packages/plumbum/machines/env.pyr
   
   s   

zEnvPathList.__init__c                 C     t | | | d S r   )listappendr   r   pathr   r   r   r         zEnvPathList.appendc                   s   t   fdd|D  d S )Nc                 3  s    | ]}  |V  qd S r   r   .0pr   r   r   	<genexpr>   s    z%EnvPathList.extend.<locals>.<genexpr>)r   extend)r   pathsr   r   r   r      s   zEnvPathList.extendc                 C  s   t | || | d S r   )r   insertr   )r   indexr   r   r   r   r          zEnvPathList.insertc                 C  r   r   )r   r!   r   r   r   r   r   r!      r   zEnvPathList.indexc                 C  s   t | | |S r   )r   __contains__r   r   r   r   r   r#      s   zEnvPathList.__contains__c                 C  r   r   )r   remover   r   r   r   r   r$      r   zEnvPathList.removec                   s&    fdd|  jD  d d < d S )Nc                   s   g | ]}  |qS r   r   r   r   r   r   
<listcomp>"   s    z&EnvPathList.update.<locals>.<listcomp>)splitr   )r   textr   r   r   update!   s   &zEnvPathList.updatec                 C  s   | j dd | D S )Nc                 s  s    | ]}t |V  qd S r   strr   r   r   r   r   %   s    z#EnvPathList.join.<locals>.<genexpr>)r   joinr   r   r   r   r+   $   r   zEnvPathList.join)__name__
__module____qualname__	__slots__r
   r   r   r    r!   r#   r$   r(   r+   __classcell__r   r   r   r   r      s    r   c                   @  s   e Zd ZdZg dZdZdd Zdd Zedd	 Z	d
d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zed(d) Zd*d+ Zd,d- ZeeeZ	 ed.d/ Zd0S )1BaseEnvz(The base class of LocalEnv and RemoteEnv)_curr_pathr   r   Tc                C  s$   || _ || _t||| _|   d S r   )r2   r   r   r3   _update_path)r   r   r   r2   r   r   r   r
   .   s   zBaseEnv.__init__c                 C  s   | j | dd d S )NPATH )r3   r(   getr   r   r   r   r4   4   r"   zBaseEnv._update_pathc                 o  sH    | j  }| jdi | zdV  W || _ |   dS || _ |   w )a\  A context manager that can be used for temporal modifications of the environment.
        Any time you enter the context, a copy of the old environment is stored, and then restored,
        when the context exits.

        :param args: Any positional arguments for ``update()``
        :param kwargs: Any keyword arguments for ``update()``
        Nr   )r2   copyr(   r4   )r   argskwargsprevr   r   r   __call__7   s   
	
zBaseEnv.__call__c                 C  s   t | j S )zdReturns an iterator over the items ``(key, value)`` of current environment
        (like dict.items))iterr2   itemsr   r   r   r   __iter__H   s   zBaseEnv.__iter__c                 C  s   t d)Nzunhashable type)	TypeErrorr   r   r   r   __hash__M   s   zBaseEnv.__hash__c                 C  s
   t | jS )z9Returns the number of elements of the current environment)lenr2   r   r   r   r   __len__P      
zBaseEnv.__len__c                 C  s   | j r|| jv S | | jv S )zGTests whether an environment variable exists in the current environment)CASE_SENSITIVEupperr2   r   namer   r   r   r#   T   s   zBaseEnv.__contains__c                 C  s   | j | jr| S |  S )zReturns the value of the given environment variable from current environment,
        raising a ``KeyError`` if it does not exist)r2   rE   rF   rG   r   r   r   __getitem__X   s   zBaseEnv.__getitem__c                 C  
   | j  S z<Returns the keys of the current environment (like dict.keys))r2   keysr   r   r   r   rL   ]   rD   zBaseEnv.keysc                 C  rJ   )z>Returns the items of the current environment (like dict.items))r2   r>   r   r   r   r   r>   a   rD   zBaseEnv.itemsc                 C  rJ   )z@Returns the values of the current environment (like dict.values))r2   valuesr   r   r   r   rM   e   rD   zBaseEnv.valuesc                 G  s"   | j j| jr|n| g|R  S rK   )r2   r7   rE   rF   )r   rH   defaultr   r   r   r7   i   s   "zBaseEnv.getc                 C  s2   | j r|n| }| j|= |dkr|   dS dS )z<Deletes an environment variable from the current environmentr5   NrE   rF   r2   r4   rG   r   r   r   __delitem__m   s
   zBaseEnv.__delitem__c                 C  s4   | j r|n| }|| j|< |dkr|   dS dS )zHSets/replaces an environment variable's value in the current environmentr5   NrO   )r   rH   valuer   r   r   __setitem__t   s
   
zBaseEnv.__setitem__c                 G  s:   | j r|n| }| jj|g|R  }|dkr|   |S )z<Pops an element from the current environment (like dict.pop)r5   )rE   rF   r2   popr4   )r   rH   rN   resr   r   r   rS   {   s
   zBaseEnv.popc                 C  s   | j   |   dS )z0Clears the current environment (like dict.clear)N)r2   clearr4   r   r   r   r   rU      s   
zBaseEnv.clearc                 O  sJ   | j j|i | | jst| j  D ]\}}|| j | < q|   dS )z2Updates the current environment (like dict.update)N)r2   r(   rE   r   r>   rF   r4   )r   r9   r:   kvr   r   r   r(      s
   zBaseEnv.updatec                 C  s$   | j  | jd< dd | j D S )z,Returns the environment as a real dictionaryr5   c                 S  s   i | ]	\}}|t |qS r   r)   )r   rV   rW   r   r   r   
<dictcomp>   s    z#BaseEnv.getdict.<locals>.<dictcomp>)r   r+   r2   r>   r   r   r   r   getdict   s   zBaseEnv.getdictc                 C  s   | j S )z5The system's ``PATH`` (as an easy-to-manipulate list))r3   r   r   r   r   r      s   zBaseEnv.pathc                 C  sP   d| v r|  | d S d| v r|  | d S d| v r&|  | dd| d S d S )NHOMEUSERPROFILEHOMEPATH	HOMEDRIVEr6   )r   r7   r   r   r   r   	_get_home   s   zBaseEnv._get_homec                 C  sX   d| v rt || d< d S d| v rt || d< d S d| v r$t || d< d S t || d< d S )NrZ   r[   r\   r)   )r   r   r   r   r   	_set_home   s   zBaseEnv._set_homec                 C  sR   dD ]}|| v r| |   S qzddl }W n
 ty   Y dS w |t d S )z2Return the user name, or ``None`` if it is not set)LOGNAMEUSERLNAMEUSERNAMEr   N)pwdImportErrorgetpwuidosgetuid)r   rH   rd   r   r   r   user   s   zBaseEnv.userN)r,   r-   r.   __doc__r/   rE   r
   r4   r   r<   r?   rA   rC   r#   rI   rL   r>   rM   r7   rP   rR   rS   rU   r(   rY   propertyr   r^   r_   homeri   r   r   r   r   r1   (   s>    

	

r1   )
__future__r   rg   
contextlibr   r   r   r1   r   r   r   r   <module>   s
    !