U
    3ìdÙ!  ã                   @   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n4de jkr²g Ze jd  e j¡D ]Ze eeƒ¡ qž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 ƒ}z|  	|¡ 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   ú5/tmp/pip-unpacked-wheel-d7dsrkjd/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 r   )r   Ú
has_option)r   ÚargsÚkwargsr   r   r   r#   S   s    zConfig.has_optionc                 C   sV   t ƒ }| d¡ t|dƒ ¡ D ]}| | dd¡ dd¡¡ q| 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   sb   t |ƒ}| ¡ D ]B}t d|¡}|r| d¡}tj |¡\}}|  tj 	||¡¡ q|  
|¡ d S )Nz^#import[\s	]*([^\s^	]*)[\s	]*$é   )r)   r*   ÚreÚmatchÚgroupr   r   Úsplitr   Újoinr   )r   r   Úfiler-   r0   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ÚvalueÚconfigr   r   r   r   Úsave_optioni   s    






zConfig.save_optionc                 C   s   |   t|||¡ d S r   )r?   ÚUserConfigPath©r   r;   r<   r=   r   r   r   Úsave_user_option{   s    zConfig.save_user_optionc                 C   s   |   t|||¡ d S r   )r?   ÚBotoConfigPathrA   r   r   r   Úsave_system_option~   s    zConfig.save_system_optionc              	   C   s2   z|   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   z|   d|¡}W n ttfk
r,   |}Y nX |S ©NZUserrE   rG   r   r   r   Úget_userˆ   s
    
zConfig.get_userr   c              	   C   s2   z|   d|¡}W n ttfk
r,   |}Y nX |S rK   )Úgetintr   r   rG   r   r   r   Úgetint_user   s
    
zConfig.getint_userc                 C   s   |   |||¡S r   )rF   ©r   r;   r!   rH   r   r   r   Ú	get_value–   s    zConfig.get_valuec              	   C   s2   z| j  ||¡W S  ttfk
r,   | Y S X d S r   )r   rF   r   r   rO   r   r   r   rF   ™   s    z
Config.getc              	   C   s6   z| j  ||¡W S  ttfk
r0   t|ƒ Y S X d S r   )r   rM   r   r   ÚintrO   r   r   r   rM   Ÿ   s    zConfig.getintç        c              	   C   s6   z| j  ||¡W S  ttfk
r0   t|ƒ Y S X d S r   )r   Úgetfloatr   r   ÚfloatrO   r   r   r   rS   ¥   s    zConfig.getfloatFc                 C   s8   |   ||¡r0|  ||¡}| ¡ dkr*d}q4d}n|}|S )NÚtrueTF)r#   rF   Úlower)r   r;   r!   rH   rI   r   r   r   Úgetbool«   s    zConfig.getboolc                 C   s&   |r|   ||d¡ n|   ||d¡ d S )NrU   Úfalse)r9   )r   r;   r!   r=   r   r   r   Úsetbool¶   s    zConfig.setboolc                 C   s    t ƒ }|  |¡ t| ¡ ƒ d S r   )r   r(   ÚprintÚgetvalue)r   Úsr   r   r   Údump¼   s    
zConfig.dumpc              
   C   sl   |s
t ƒ }|  ¡ D ]T}| d| ¡ |  |¡D ]6}|dkrJ| d| ¡ q.| d||  ||¡f ¡ q.qd S )Nz[%s]
r'   z%s = xxxxxxxxxxxxxxxxxx
z%s = %s
)r   Úsectionsr(   ÚoptionsrF   )r   r   r;   r<   r   r   r   Ú	dump_safeÁ   s    zConfig.dump_safec           
      C   sˆ   ddl m} t ¡ }| |¡}|s,| |¡}| |¡}d|_|  ¡ D ]6}i }|  	|¡D ]}	|  
||	¡||	< qV| |¡||< qD| ¡  d S )Nr   ©ÚjsonF)Úboto.compatrb   ÚbotoÚconnect_sdbÚlookupZcreate_domainZnew_itemÚactiver^   r_   rF   ÚdumpsÚsave)
r   Údomain_nameÚ	item_namerb   ÚsdbÚdomainÚitemr;   Údr<   r   r   r   Údump_to_sdbÌ   s    


zConfig.dump_to_sdbc                 C   s¦   ddl m} t ¡ }| |¡}| |¡}| ¡ D ]p}|  |¡sH|  |¡ | 	|| ¡}| ¡ D ]@}	||	 }
|
d krvd}
t
|
tƒr|  ||	|
¡ q^|  ||	|
¡ q^q0d S )Nr   ra   ÚNone)rc   rb   rd   re   rf   Zget_itemÚkeysr7   r8   ÚloadsÚ
isinstanceÚboolrY   r9   )r   rj   rk   rb   rl   rm   rn   r;   ro   Ú	attr_nameÚ
attr_valuer   r   r   Úload_from_sdbÛ   s    




zConfig.load_from_sdb)NNT)N)N)r   )N)N)r   )rR   )F)N)Ú__name__Ú
__module__Ú__qualname__r   r   r"   r#   r   r   r?   rB   rD   rJ   rL   rN   rP   rF   rM   rS   rW   rY   r]   r`   rp   rx   r   r   r   r   r   4   s,   
		









r   )r   r/   r   rd   rc   r   r   r   r   r   rC   r   r   r3   r@   Úappendr   r2   ÚpathsepÚobjectr   r   r   r   r   Ú<module>   s   


