3
eb4                 @   sX   d Z ddlZddlZddlZddgZG dd deZG dd dZedkrTe	e  dS )z-An object-oriented interface to .netrc files.    NnetrcNetrcParseErrorc               @   s"   e Zd ZdZdddZdd ZdS )r   z5Exception raised on syntax errors in the .netrc file.Nc             C   s"   || _ || _|| _tj| | d S )N)filenamelinenomsg	Exception__init__)selfr   r   r    r
   /usr/lib/python3.6/netrc.pyr      s    zNetrcParseError.__init__c             C   s   d| j | j| jf S )Nz%s (%s, line %s))r   r   r   )r	   r
   r
   r   __str__   s    zNetrcParseError.__str__)NN)__name__
__module____qualname____doc__r   r   r
   r
   r
   r   r   
   s   
c               @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )r   Nc             C   sx   |d k}|d krFyt jjt jd d}W n tk
rD   tdY nX i | _i | _t|}| j	||| W d Q R X d S )NHOMEz.netrcz'Could not find .netrc: $HOME is not set)
ospathjoinenvironKeyErrorOSErrorhostsmacrosopen_parse)r	   filedefault_netrcfpr
   r
   r   r      s    
znetrc.__init__c             C   s  t j |}| jd7  _|jjdd|_x|j}|j  }}|sFP n|d dkrv|j|kr,t|dkr,|jj  q,n|dkr|j }nz|dkrd}nl|dkr|j }g | j	|< d	|_
x4|jj }	|	 s|	d
krd|_
P | j	| j|	 qW q,ntd| ||jd}
d  }}i | j|< x|j }|jds:|dkr~|r^|
||f| j|< |j| P ntd||t|f ||jq|dks|dkr|j }
q|dkr|j }q|dkrtjdkr|rtj|j }|jtj kr|dd l}y|j|jd }W n  tk
r*   d|j }Y nX y|jtj d }W n" tk
rd   dtj  }Y nX td||f ||j|jtjtjB @ rtd||j|j }ntd| ||jqW q,W d S )Nz !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~# r      machinedefaultmacdefz 	
z 	
zbad toplevel token %rz&malformed %s entry %s terminated by %sloginuseraccountpasswordposixzuid %sz9~/.netrc file owner (%s) does not match current user (%s)zY~/.netrc access too permissive: access permissions must restrict access to only the ownerzbad follower token %r>   r    r"   r#   r$   )shlexZ	wordcharsZ
commentersreplacer   Z	get_tokenlenZinstreamreadliner   Z
whitespaceappendr   r   
startswithZ
push_tokenreprr   namefstatfilenost_uidgetuidpwdgetpwuidr   st_modestatS_IRWXGS_IRWXO)r	   r   r   r   ZlexerZsaved_linenoZtoplevelZttZ	entrynameliner&   r(   r)   Zpropr7   Zfownerr'   r
   r
   r   r   #   s    












znetrc._parsec             C   s0   || j kr| j | S d| j kr(| j d S dS dS )z8Return a (user, account, password) tuple for given host.r#   N)r   )r	   hostr
   r
   r   authenticatorst   s
    



znetrc.authenticatorsc             C   s   d}xf| j j D ]X}| j | }|d| d|d  d7 }|d rT|d|d  d7 }|d|d	  d7 }qW xF| jj D ]8}|d
| d7 }x| j| D ]}||7 }qW |d7 }qxW |S )z3Dump the class data in the format of a .netrc file.r    zmachine z
	login r   r%   r!   z		account z
	password    zmacdef )r   keysr   )r	   Zrepr>   ZattrsZmacror=   r
   r
   r   __repr__}   s    
znetrc.__repr__)N)r   r   r   r   r   r?   rB   r
   r
   r
   r   r      s   
Q	__main__)
r   r   r+   r:   __all__r   r   r   r   printr
   r
   r
   r   <module>   s   w