o
    w7e	                     @   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   s,   t | }z|dW S  ty   | Y S w Nzutf-8)r   decodeUnicodeDecodeError)source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   |rzt | }t|j}W n	 ty   Y nw d }z3tjt j| d|d}t	
d| |j||t| || |  t |j| t |j|  W d S  tyf } z|d ur`t |j |d }~ww )NF)dirdeletemodezOAtomically writing to file %s (via temporary file %s) - %s: [%o] %d bytes/chars)osstatS_IMODEst_modeOSErrortempfileNamedTemporaryFilepathdirnameLOGdebugnamelenwriteclosechmodrename	Exceptionunlink)filenamecontentr   omodepreserve_mode	file_stattfer	   r	   r
   
write_file    s<   

	r.   c                 C   s2   zd t| W S  ty   d t|  Y S w )z1Handler for types which aren't json serializable.z
ci-b64:{0}z)Warning: redacted unserializable type {0})formatr   AttributeErrortype)_objr	   r	   r
   json_serialize_defaultD   s
   r3   c                 C   s   t j| dddtdS )z%Return data in nicely formatted json.   T),z: )indent	sort_keys
separatorsdefault)jsondumpsr3   )datar	   r	   r
   
json_dumpsL   s   r=   c                 C   s   t | t|d d|dS )N
w)r)   r   )r.   r=   )r'   r<   r   r	   r	   r
   
write_jsonW   s   
r@   )r:   loggingr   r   r   base64r   r   
_DEF_PERMS	getLogger__name__r   r   r   r.   r3   r=   r@   r	   r	   r	   r
   <module>   s   

	
$