o vfh@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl mZddlmZddlmZejddkrRdd lmZed Zndd lmZd ZGd ddZGdddeZdS)z(Store, load, and handle problem reports.N) encode_base64) MIMEMultipart)MIMEBase)MIMEText)BytesIO3)IterableUserDictT)UserDictFc@sBeZdZdZdddZddZddZd d Zd d Zd dZ dS)CompressedValuez9Represent a ProblemReport value which is gzip compressed.NcCs(d|_||_d|_|r||dSdS)zAInitialize an empty CompressedValue object with an optional name.NF) gzipvaluename legacy_zlib set_value)selfvaluer r0/usr/lib/python3/dist-packages/problem_report.py__init__"s zCompressedValue.__init__cCs4t}tj|jd|dd|||_d|_dS)zSet uncompressed value.wbrmodefileobjmtimeFN)rgzipGzipFiler writegetvaluer r )rroutrrrr/s  zCompressedValue.set_valuecCs2|jsdS|jrt|jStjt|jdS)zReturn uncompressed value.Nr)r r zlib decompressrrrreadrrrr get_value7s  zCompressedValue.get_valuecCsX|jsJ|jr|t|jdStjt|jd} |d}|s&dS||q)z5Write uncompressed value into given file-like object.NrT) r r rrr rrrr!)rfilegzblockrrrrAs   zCompressedValue.writecCs:|jsJ|jrt|Sttd|jdddS)z$Return length of uncompressed value.sz.ProblemReport.extract_keys..)rI isinstancestrlistrKrVrWrXrSrYopenospathjoinrLrMrr rrNrOrPIOErrorrUvalues ValueErroritems) rr%bin_keysdirr^r missing_keysr_r`rrbr'rrr extract_keyssn          ( zProblemReport.extract_keysreturnc Csttj}z:z1zttjdttt|dWWttj|WSty8YWttj|WdSwttj|wtj yKYdSw)zgGet timestamp (seconds since epoch) from Date field Return None if it is not present. Cr7N) locale getlocaleLC_TIME setlocaler+r8mktimestrptimerUError)r orig_ctimerrr get_timestamps  zProblemReport.get_timestampcCs d|vS)z~Check if the report has any keys which were not loaded. This could happen when using binary=False in load(). N)rqr"rrrhas_removed_fields#s z ProblemReport.has_removed_fieldscCs4t|tkr|D]}|dkrt|sdSqdS)z0Check if the given strings contains binary data. TF)r=byteschrisspace)klassstringcrrr _is_binary*s zProblemReport._is_binarycCs>t|tkr||sz|dWSty|YSw|S)z)Try to convert bytearray value to unicodeUTF-8)r=rrrXUnicodeDecodeError)rrrrrrT4s  zProblemReport._try_unicodeFc Cs||g}g}|jD]?}|r||jvrq|j|}t|dr3||r-||q||qt|tsHt |dkrH|dsH||q||q| d|vra| d| dd| |D]}|j|}t|dst |dkr|ddur|d}nd}t |dko|d}t|dd r|d }nt|dd  } | }Wdn1swY|rt |dkrtd ||durt ||kr|j|=qgt|tr|d }||d d|vr|d||ddn |d|||dqg|D]K}|j|}d}d} |} ||d |dt|tr@|t|j|dq d|d d} |t| |dtd} tdtjtj tjd}t|dr| t |7} t|| } ||}|r|t||dnt |dkr|ddur|d}t|dd r|d} nt|dd } | d}| t |7} t|| } |dur| |kr|| | | |j|=d} n|r||}|r|t||dnnqt|dd s | !t |dkr(|dr(| dkr(td|t|df|r0| |krX|"}| rK|t#$d| d@7}|t#$d| d@7}|t||dq dS)aWrite information into the given file-like object. If only_new is True, only keys which have been added since the last load() are written (i. e. those returned by new_keys()). If a value is a string, it is written directly. Otherwise it must be a tuple of the form (file, encode=True, limit=None, fail_on_empty=False). The first argument can be a file name or a file-like object, which will be read and its content will become the value of this key. 'encode' specifies whether the contents will be gzip compressed and base64-encoded (this defaults to True). If limit is set to a positive integer, the file is not attached if it's larger than the given limit, and the entire key will be removed. If fail_on_empty is True, reading zero bytes will cause an IOError. file needs to be opened in binary mode. Files are written in RFC822 format. findrDr6rNr!rbzdid not get any data for field rrGrCs: s s: s : base64 s rATr$z)did not get any data for field %s from %sr(l)%rIr:r[r<hasattrrappendrir r*sortrSinsertr!rlrprjrZrreplacetellrL b64encoder rcrc32 compressobjDEFLATEDrO DEF_MEM_LEVELcompressseektruncatecloserRr,pack)rr%only_newasckeysbinkeyskvlimit fail_on_emptyfsizecurr_pos gzip_headercrcbcoutblockr'rrrr?s                                             zProblemReport.writec Cst|}z*t|d}t|d|||W|r't||j|jft||j dS|rs  8       @