3 \me @sddlZddlZddlZddlZddlZddlmZmZdZej e Z ddZ ddZ edd fd d Zd d ZddZefddZdS)N) b64decode b64encodeic Cs,t|}y |jdStk r&|SXdS)Nzutf-8)rdecodeUnicodeDecodeError)sourceZdecodedr#/usr/lib/python3.6/atomic_helper.pyb64ds  r cCs"t|ts|jd}t|jdS)Nzutf-8) isinstancebytesencoderr)rrrrb64es  r wbFcCs|r4ytj|}tj|j}Wntk r2YnXd}ydtjtjj|d|d}t j d||j ||t ||j ||jtj|j |tj|j |Wn:tk r}z|dk rtj|j |WYdd}~XnXdS)NF)dirdeletemodezOAtomically writing to file %s (via temporary file %s) - %s: [%o] %d bytes/chars)osstatS_IMODEst_modeOSErrortempfileZNamedTemporaryFilepathdirnameLOGdebugnamelenwriteclosechmodrename Exceptionunlink)filenameZcontentromodeZ preserve_modeZ file_statZtferrr write_file s2    r'c Cs2ydjt|Stk r,djt|SXdS)z1Handler for types which aren't json serializable.z ci-b64:{0}z)Warning: redacted unserializable type {0}N)formatr AttributeErrortype)Z_objrrrjson_serialize_defaultDsr+cCstj|dddtdS)z%Return data in nicely formatted json.T,: )indentZ sort_keysZ separatorsdefault)r-r.)jsondumpsr+)datarrr json_dumpsLs r4cCst|t|dd|dS)N w)r%r)r'r4)r$r3rrrr write_jsonWs  r7)r1Zloggingrrrbase64rrZ _DEF_PERMSZ getLogger__name__rr r r'r+r4r7rrrrs   #