B
    è?F[Ù!  ã               @   sÊ   d dl Z d dlZd dlZd dlZd dlmZmZmZmZm	Z	 dZ
e
gZe j edƒd¡Ze e¡ de jkr~ee jd ƒgZn8de jkr¶g Zx(e jd  e j¡D ]Ze eeƒ¡ q W G dd	„ d	eƒZdS )
é    N)Ú
expanduserÚConfigParserÚNoOptionErrorÚNoSectionErrorÚStringIOz/etc/boto.cfgú~z.botoZBOTO_CONFIGZ	BOTO_PATHc               @   sÐ   e Zd Zd2d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d3dd„Zd4dd„Zd5dd„Zd6dd„Zd7dd„Zd8d d!„Zd9d#d$„Zd:d&d'„Zd(d)„ Zd*d+„ Zd;d,d-„Zd.d/„ Zd0d1„ ZdS )<ÚConfigNTc             C   sŒ   t dddœƒ| _|rˆ|r$|  |¡ n|r4|  |¡ n
|  t¡ dtjkrˆttjd ƒ}y|  	|¡ W n" t
k
r†   t d| ¡ Y nX d S )Nz
/mnt/pyamiÚ0)Zworking_dirÚdebugZAWS_CREDENTIAL_FILEz'Unable to load AWS_CREDENTIAL_FILE (%s))r   Ú_parserÚload_from_pathÚreadfpÚreadÚBotoConfigLocationsÚosÚenvironr   Úload_credential_fileÚIOErrorÚwarningsÚwarn)ÚselfÚpathÚfpZdo_loadÚ	full_path© r   ú0lib/python3.7/site-packages/boto/pyami/config.pyÚ__init__6   s    

zConfig.__init__c             C   s   |d | _ d S )Nr   )r   )r   Ústater   r   r   Ú__setstate__G   s    zConfig.__setstate__c             C   s   t | j|ƒS )N)Úgetattrr   )r   Únamer   r   r   Ú__getattr__P   s    zConfig.__getattr__c             O   s   | j j||ŽS )N)r   Ú
has_option)r   ÚargsÚkwargsr   r   r   r"   S   s    zConfig.has_optionc             C   sZ   t ƒ }| d¡ x0t|dƒ ¡ D ]}| | dd¡ dd¡¡ q W | d¡ |  |¡ dS )	z:Load a credential file as is setup like the Java utilitiesz[Credentials]
ÚrZAWSAccessKeyIdZaws_access_key_idZAWSSecretKeyÚaws_secret_access_keyr   N)r   ÚwriteÚopenÚ	readlinesÚreplaceÚseekr   )r   r   Zc_dataÚliner   r   r   r   V   s    

zConfig.load_credential_filec             C   sf   t |ƒ}xN| ¡ D ]B}t d|¡}|r| d¡}tj |¡\}}|  tj 	||¡¡ qW |  
|¡ d S )Nz^#import[\s	]*([^\s^	]*)[\s	]*$é   )r(   r)   ÚreÚmatchÚgroupr   r   Úsplitr   Újoinr   )r   r   Úfiler,   r/   Zextended_fileÚdirr   r   r   r   _   s    
zConfig.load_from_pathc             C   st   t ƒ }| |¡ | |¡s$| |¡ | |||¡ t|dƒ}| |¡ | ¡  |  |¡sb|  |¡ |  |||¡ dS )zÛ
        Write the specified Section.Option to the config file specified by path.
        Replace any previous value.  If the path doesn't exist, create it.
        Also add the option the the in-memory config.
        ÚwN)r   r   Úhas_sectionÚadd_sectionÚsetr(   r'   Úclose)r   r   ÚsectionÚoptionÚvalueZconfigr   r   r   r   Úsave_optioni   s    






zConfig.save_optionc             C   s   |   t|||¡ d S )N)r=   ÚUserConfigPath)r   r:   r;   r<   r   r   r   Úsave_user_option{   s    zConfig.save_user_optionc             C   s   |   t|||¡ d S )N)r=   ÚBotoConfigPath)r   r:   r;   r<   r   r   r   Úsave_system_option~   s    zConfig.save_system_optionc          	   C   s2   y|   d|¡}W n ttfk
r,   |}Y nX |S )NZInstance)Úgetr   r   )r   r    ÚdefaultÚvalr   r   r   Úget_instance   s
    
zConfig.get_instancec          	   C   s2   y|   d|¡}W n ttfk
r,   |}Y nX |S )NÚUser)rB   r   r   )r   r    rC   rD   r   r   r   Úget_userˆ   s
    
zConfig.get_userr   c          	   C   s2   y|   d|¡}W n ttfk
r,   |}Y nX |S )NrF   )Úgetintr   r   )r   r    rC   rD   r   r   r   Úgetint_user   s
    
zConfig.getint_userc             C   s   |   |||¡S )N)rB   )r   r:   r    rC   r   r   r   Ú	get_value–   s    zConfig.get_valuec          	   C   s,   y| j  ||¡S  ttfk
r&   |S X d S )N)r   rB   r   r   )r   r:   r    rC   r   r   r   rB   ™   s    z
Config.getc          	   C   s0   y| j  ||¡S  ttfk
r*   t|ƒS X d S )N)r   rH   r   r   Úint)r   r:   r    rC   r   r   r   rH   Ÿ   s    zConfig.getintç        c          	   C   s0   y| j  ||¡S  ttfk
r*   t|ƒS X d S )N)r   Úgetfloatr   r   Úfloat)r   r:   r    rC   r   r   r   rM   ¥   s    zConfig.getfloatFc             C   s8   |   ||¡r0|  ||¡}| ¡ dkr*d}q4d}n|}|S )NÚtrueTF)r"   rB   Úlower)r   r:   r    rC   rD   r   r   r   Úgetbool«   s    zConfig.getboolc             C   s&   |r|   ||d¡ n|   ||d¡ d S )NrO   Zfalse)r8   )r   r:   r    r<   r   r   r   Úsetbool¶   s    zConfig.setboolc             C   s    t ƒ }|  |¡ t| ¡ ƒ d S )N)r   r'   ÚprintÚgetvalue)r   Úsr   r   r   Údump¼   s    
zConfig.dumpc          
   C   st   |s
t ƒ }xd|  ¡ D ]X}| d| ¡ xD|  |¡D ]6}|dkrN| d| ¡ q2| d||  ||¡f ¡ q2W qW d S )Nz[%s]
r&   z%s = xxxxxxxxxxxxxxxxxx
z%s = %s
)r   Úsectionsr'   ÚoptionsrB   )r   r   r:   r;   r   r   r   Ú	dump_safeÁ   s    zConfig.dump_safec       
      C   s   ddl m} t ¡ }| |¡}|s,| |¡}| |¡}d|_xF|  ¡ D ]:}i }x"|  	|¡D ]}	|  
||	¡||	< qZW | |¡||< qFW | ¡  d S )Nr   )ÚjsonF)Úboto.compatrZ   ÚbotoÚconnect_sdbÚlookupZcreate_domainZnew_itemZactiverW   rX   rB   ÚdumpsZsave)
r   Údomain_nameÚ	item_namerZ   ÚsdbÚdomainÚitemr:   Údr;   r   r   r   Údump_to_sdbÌ   s    


zConfig.dump_to_sdbc             C   s®   ddl m} t ¡ }| |¡}| |¡}x€| ¡ D ]t}|  |¡sJ|  |¡ | 	|| ¡}xL| ¡ D ]@}	||	 }
|
d krzd}
t
|
tƒr”|  ||	|
¡ qb|  ||	|
¡ qbW q2W d S )Nr   )rZ   ÚNone)r[   rZ   r\   r]   r^   Zget_itemÚkeysr6   r7   ÚloadsÚ
isinstanceÚboolrR   r8   )r   r`   ra   rZ   rb   rc   rd   r:   re   Z	attr_nameZ
attr_valuer   r   r   Úload_from_sdbÛ   s    




zConfig.load_from_sdb)NNT)N)N)r   )N)N)r   )rL   )F)N)Ú__name__Ú
__module__Ú__qualname__r   r   r!   r"   r   r   r=   r?   rA   rE   rG   rI   rJ   rB   rH   rM   rQ   rR   rV   rY   rf   rl   r   r   r   r   r   4   s,   
		









r   )r   r.   r   r\   r[   r   r   r   r   r   r@   r   r   r2   r>   Úappendr   r1   ÚpathsepÚobjectr   r   r   r   r   Ú<module>   s   


