a 'Dg, @sddlZddlZddlZddlZddlZddlmZmZddlm Z m Z dZ e e Ze dddZe dd d Ze d d fd dZe d d fddZddZe dddZe fddZdS)N) b64decode b64encode) performanceutilizBase64 decodingcCs0t|}z |dWSty*|YS0dS)zbase64 decode data :param source: a bytes or str to decode :return: base64 as a decoded str if utf-8 encoded, otherwise bytes utf-8N)rdecodeUnicodeDecodeError)sourceZdecodedr ;/usr/lib/python3.9/site-packages/cloudinit/atomic_helper.pyb64ds   r zBase64 encodingcCs"t|ts|d}t|dS)zdbase64 encode data :param source: a bytes or str to decode :return: base64 encoded str r) isinstancebytesencoderr)r r r r b64es  rwbFcCsDtd| t|||||WdS1s60YdS)zCopen filename in mode omode, write content, set permissions to modezWriting N)rZTimed _write_file)filenamecontentmodeomode preserve_moder r r write_file*src Cs|r2zt|}t|j}Wnty0Yn0d}zrtj|}t|t j |d|d}t d||j ||t||||t|j |t|j |Wn<ty}z$|durt|j |WYd}~n d}~00dS)NF)dirdeleterzOAtomically writing to file %s (via temporary file %s) - %s: [%o] %d bytes/chars)osstatS_IMODEst_modeOSErrorpathdirnamerZ ensure_dirtempfileZNamedTemporaryFileLOGdebugnamelenwriteclosechmodrename Exceptionunlink) rrrrrZ file_statZtfr!er r r r3s6      rcCs6zdt|WSty0dt|YS0dS)z1Handler for types which aren't json serializable.z ci-b64:{0}z)Warning: redacted unserializable type {0}N)formatrAttributeErrortype)Z_objr r r json_serialize_defaultUs r1z Dumping jsoncCstj|dddtdS)z%Return data in nicely formatted json.T),z: )indentZ sort_keysZ separatorsdefault)jsondumpsr1)datar r r json_dumps]sr9cCst|t|dd|dS)N w)rr)rr9)rr8rr r r write_jsonis  r<)r6Zloggingrrr"base64rrZ cloudinitrrZ _DEF_PERMSZ getLogger__name__r#Ztimedr rrrr1r9r<r r r r s(     "