>SY@sdZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m Z ddlmZddlmZejddkrdd lmZed Zndd lmZd ZGd ddZGdddeZdS)z(Store, load, and handle problem reports.N) encode_base64) MIMEMultipart)MIMEBase)MIMEText)BytesIO3)IterableUserDictT)UserDictFc@sdeZdZdZddddZddZddZd d Zd d Zd dZ dS)CompressedValuez9Represent a ProblemReport value which is gzip compressed.NcCs5d|_||_d|_|r1|j|ndS)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__cCsJt}tj|jddd|j||j|_d|_dS)zSet uncompressed value.modewbfileobjFN)rgzipGzipFiler writegetvaluer r )rroutrrrr.s %zCompressedValue.set_valuecCsE|js dS|jr&tj|jStjdt|jjS)zReturn uncompressed value.Nr)r r zlib decompressrrrread)rrrr get_value6s   zCompressedValue.get_valuecCs|jst|jr5|jtj|jdStjdt|j}x*|j d}|slPn|j|qSWdS)z5Write uncompressed value into given file-like object.Nri) r AssertionErrorr rrrrrrr)rfileZgzblockrrrr@s zCompressedValue.writecCsO|jst|jr(t|jSttjd|jdddS)z$Return length of uncompressed value.zZ b64encoder rZcrc32Z compressobjZDEFLATEDr?Z DEF_MEM_LEVELcompressseektruncatecloserBr'Zpack)rr!Zonly_newZasckeysZbinkeyskvlimitZ fail_on_emptyfsizeZcurr_posZ gzip_headerZcrcZbcZoutblockr"rrrrs  -      "             "         & zProblemReport.writec Cstj|}z:t|d}tj|d|j||jWd|rqtj||j|jfntj||j XdS)aAdd this report's data to an already existing report file. The file will be temporarily chmod'ed to 000 to prevent frontends from picking up a hal-updated report file. If keep_times is True, then the file's atime and mtime restored after updating. ZabrN) osstatrichmodrrqutimest_atimest_mtimest_mode)rZ reportfileZ keep_timesstrurrradd_to_existings zProblemReport.add_to_existingcCs|j|t|jj}d}g}d|krZ|jd|jddn|rd} xG|D]<} | |krm|j| |j| | | d7} qmqmWnx;|D]3} |r| |krqn|j| } d} t| tr| j} nt | dsd} t | ddr8| d}nt | dd}| j d ri|j } ngt }tj| d d d |}x4|j d }|r|j|q|jPqW|j} |jn<|j| r| j d r| } qt| | j} n| rtdd}| j d rV|jddd| n|jddd| d |j| t||j|qt| }t| tkr| jdd} ntrt| tstnt| t stt| j!}|dkrL|dkrL| j"} || d| d7}q|dkr||kr|| d7}| j ds| d7} n|| j#j$ddd7}qt%| dd}|jddd| d|j|qWt%|dd}|jdd|jd|t&}x*|j'D]\} } |j| | q6Wx|D]}|j(|q]W|j|j)j*d|jddS)aWrite MIME/Multipart RFC 2822 formatted data into file. file must be a file-like object, not a path. It needs to be opened in binary mode. If a value is a string or a CompressedValue, it is written directly. Otherwise it must be a tuple containing the source file and an optional boolean value (in that order); 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. The file will be gzip compressed, unless the key already ends in .gz. attach_treshold specifies the maximum number of lines for a value to be included into the first inline text part. All bigger values (as well as all non-ASCII ones) will become an attachment, as well as text values bigger than 1 kB. Extra MIME preamble headers can be specified, too, as a dictionary. skip_keys is a set/list specifying keys which are filtered out and not written to the destination file. priority_fields is a set/list specifying the order in which keys should appear in the destination file. rOr/rr7Nr^rraz.gzrrriZ applicationzx-gzipzContent-DispositionZ attachmentfilenamezUTF-8rliz:  z: z Z_charsetz.txtZinlines )+r;sortedr1rIrgrhrer r rcrirArrrrrrqrrZrZ add_headerZ set_payloadrrdr%r4rTrFrEr\r rkr)rstriprGrlrritemsZattachZ as_stringrH)rr!Zattach_tresholdZ extra_headersZ skip_keysZpriority_fieldsrItextZ attachmentsZcounterZpriority_fieldrrrsZ attach_valueruioZgfr"Zattrvlinesmsgarrr write_mimes                    #  zProblemReport.write_mimecCst|dst|jddjddjddjsKtt|tst|dst|drt|dkst|dkr|dd krt|d dst|d d st|jj||S)Nisalnum.rO-_ __getitem__r7r_TFrr)TF) rcr rlrrer r%r1 __setitem__)rrrrsrrrr1s64,zProblemReport.__setitem__cCst|jj|jS)zReturn newly added keys. Return the set of keys which have been added to the report since it was constructed or loaded. )r2r1rIr3)rrrrnew_keys=szProblemReport.new_keyscCstr|j|S|d}d}|d@rB|||d7}n|d@rzx||dkrl|d7}qOW|d7}n|d@rx||dkr|d7}qW|d7}n|d@r|d7}n||d S) z0Strip gzip header from line and return the rest.r` r#r7rr_N)rE_strip_gzip_header_py2)rWrLflagsoffsetrrrr@Es"         z ProblemReport._strip_gzip_headercCst|d}d}|d@r5|||d7}n|d@rsx$t||dkre|d7}qBW|d7}n|d@rx$t||dkr|d7}qW|d7}n|d@r|d7}n||d S) z;Strip gzip header from line and return the rest. (Python 2)r`rr#r7rrrr_N)ord)rWrLrrrrrr]s       z$ProblemReport._strip_gzip_header_py2cCsVtr6t|tksRd|jksRtdnt|d sRtddS)z/Assert that given file object is in binary modebz"file stream must be in binary modeencodingN)rEr4rrr rc)rWr!rrrr;rs0zProblemReport._assert_bin_mode)r*r+r,rrNrQ classmethodrZrSrCrrrrrr@rr;rrrrr.^s R     r.)r-rr>r0sysrr'rwZemail.encodersrZemail.mime.multipartrZemail.mime.baserZemail.mime.textrrrversionr rrE collectionsr r.rrrrs T @