U
    Ld	                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZmZ dZe	e
Zdd Zdd Zedd	fd
dZdd Zdd ZefddZdS )    N)	b64decode	b64encodei  c                 C   s2   t | }z|dW S  tk
r,   | Y S X d S Nzutf-8)r   decodeUnicodeDecodeError)sourceZdecoded r   9/usr/lib/python3/dist-packages/cloudinit/atomic_helper.pyb64d   s
    r
   c                 C   s"   t | ts| d} t| dS r   )
isinstancebytesencoder   r   )r   r   r   r	   b64e   s    

r   wbFc              
   C   s   |r4zt | }t|j}W n tk
r2   Y nX d }zdtjt j| d|d}t	
d| |j||t| || |  t |j| t |j|  W n: tk
r } z|d k	rt |j |W 5 d }~X Y nX d S )NF)dirdeletemodezOAtomically writing to file %s (via temporary file %s) - %s: [%o] %d bytes/chars)osstatS_IMODEst_modeOSErrortempfileZNamedTemporaryFilepathdirnameLOGdebugnamelenwriteclosechmodrename	Exceptionunlink)filenameZcontentr   omodeZpreserve_modeZ	file_statZtfer   r   r	   
write_file    s:    

  	
r(   c                 C   s8   zd t| W S  tk
r2   d t|  Y S X dS )z1Handler for types which aren't json serializable.z
ci-b64:{0}z)Warning: redacted unserializable type {0}N)formatr   AttributeErrortype)Z_objr   r   r	   json_serialize_defaultD   s    r,   c                 C   s   t j| dddtdS )z%Return data in nicely formatted json.   T),z: )indentZ	sort_keysZ
separatorsdefault)jsondumpsr,   )datar   r   r	   
json_dumpsL   s    r4   c                 C   s   t | t|d d|dS )N
w)r&   r   )r(   r4   )r%   r3   r   r   r   r	   
write_jsonW   s    
r7   )r1   Zloggingr   r   r   base64r   r   Z
_DEF_PERMSZ	getLogger__name__r   r
   r   r(   r,   r4   r7   r   r   r   r	   <module>   s    

	  
$