
    !fV                        d Z ddlZddlZddlZddlZddlZddlZddlZddl	Zddl
Z
ddlZddlZ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mZ dZdZ G d de      Z G d d	e      Z G d
 de      Zdee   dee   fdZdedefdZdee   dedee   fdZ d Z!dee   dedee   fdZ"dee   de#e$ee   e%f   fdZ&ejN                   G d d             Z( G d d      Z) G d dejT                        Z+y)z(Store, load, and handle problem reports.    N)	GeneratorIterableIterators   s   (/c                   ,     e Zd ZdZdedef fdZ xZS )MalformedProblemReportzRaised when a problem report violates the crash report format.

    This exception might be raised when the keys of the report are not ASCII.
    messageargsc                 .    t        |   d| dg|  y )NzMalformed problem report: z$. Is this a proper .crash text file?)super__init__)selfr   r	   	__class__s      0/usr/lib/python3/dist-packages/problem_report.pyr   zMalformedProblemReport.__init__,   s+    (	 22 3	
 	
    )__name__
__module____qualname____doc__strobjectr   __classcell__r   s   @r   r   r   &   s     

 
F 
 
r   r   c                       e Zd ZdZy)_SizeLimitExceededz>Raised internally to signal a value that is too big to encode.N)r   r   r   r    r   r   r   r   4   s    Hr   r   c                   J    e Zd ZdZdee   ddfdZdee   fdZdee   fdZy)_EntryParserzIterator over entries in RFC822 formatted files.

    This iterator reads ahead one line to determine the boundaries between
    the entries. Each returned entry is a line-based iterator.
    iteratorreturnNc                 .    || _         d | _        d| _        y )NT)r   	next_line
entry_read)r   r   s     r   r   z_EntryParser.__init__?   s     '+r   c              #      K   | j                   sJ | j                    d| _         | j                  D ]'  }|j                  d      r| || _          d| _        y d| _        yw)z$Iterate over all lines of one entry.N    T)r!   r   
startswithr"   )r   lines     r   entry_iteratorz_EntryParser.entry_iteratorD   sa     ~~~nnMM 	Dt$
!%	 s   A"A$c                     | j                   s| j                         D ]  } | j                  st        | j                        | _        d| _         | j                         S )NF)r"   r'   r!   nextr   )r   _s     r   __next__z_EntryParser.__next__Q   sQ    ((* ~~!$--0DN""$$r   )	r   r   r   r   r   bytesr   r'   r+   r   r   r   r   r   8   s>    % T 
 %(5/ %r   r   entryr   c              #      K   | D ]  }	 t        j                  |        y # t        j                  $ r}t	        t        |            d d }~ww xY wwN)base64	b64decodebinasciiErrorr   r   )r-   r&   errors      r   _base64_decoderr5   [   sQ      ?	?""4((? ~~ 	?(U4$>	?s$   A#AAAAAr&   c                     | d   }d}|dz  r|| |   dz   z  }|dz  r| |   dk7  r|dz  }| |   dk7  r|dz  }|dz  r| |   dk7  r|dz  }| |   dk7  r|dz  }|dz  r|dz  }| |d	 S )
z0Strip gzip header from line and return the rest.   
            r         Nr   )r&   flagsoffsets      r   _strip_gzip_headerr@   c   s    GEFqy$v,""qy6laaKF 6la!rz6laaKF 6la!qy!=r   
first_linec              #      K   |j                         }| t        |      }| D ]M  }|dkD  rd |j                  d      r|dd  |t        |      dz
  z  }6|dd   |t        |      dz
  z  }O y w)Nr      
r:   r=   )striplenendswith)r-   rA   r&   lengths       r   _text_decoderrI   w   s     D
JYF $A:K==q*c$i!m#Fqr(Nc$i!m#F$s   A3A5c                  n    	 dd l } | j                         S # t        $ r}t        d| d      d d }~ww xY w)Nr   z$Failed to import zstandard library: z#. Please install python3-zstandard.)	zstandardImportErrorRuntimeErrorZstdDecompressor)rK   r4   s     r   _get_zstandard_decompressorrO      sP     %%''  25' :1 2
 	s    	4/4first_blockc              #      K   t               j                         }|j                  |       | D ]  }|j                  |        y wr/   )rO   decompressobj
decompress)r-   rP   decompressorblocks       r   _zstandard_decoderrV      sH     .0>>@L

!
!+
.. -%%e,,-s   AA	c                 r   t        |       }	 |j                  dd      \  }}	 |j	                  d      }|j                         d	k(  }|rt        |       }nt        | |      }|||fS # t        $ r! t        d|j	                  d      d      dw xY w# t
        $ r}t        t        |            dd}~ww xY w)
zParse the given entry and return key and value.

    Return the key and a line iterator over the value. Also return a
    boolean if the entry is base64 encoded (i.e. is a binary value).
       :r:   zLine backslashreplace)errorsz? does not contain a colon for separating the key from the valueNASCIIs   base64)
r)   split
ValueErrorr   decodeUnicodeDecodeErrorr   rE   r5   rI   )r-   rA   key_in_bytesfirst_line_valuekeyr4   base64_encodedvalue_iterators           r   _parse_entryre      s     eJ+5+;+;D!+D(';!!'* &++-:N(/&u.>?..%  $J%%-?%@C&'
 		  ;$SZ0d:;s"   A& B &*B	B6B11B6c                   P    e Zd ZU dZeed<   deddfdZd Zdee	   fdZ
defdZy)	CompressedFilezRepresents a ProblemReport value which points to a compressed file.

    The file is expected to be compressed with gzip or zstandard.
    filenamer   Nc                 H    || _         t        | j                   d      | _        y )Nrb)rh   open_compressed_file)r   rh   s     r   r   zCompressedFile.__init__   s      $T]]D 9r   c                 R    t        | d      r| j                  j                          y y )Nrl   )hasattrrl   closer   s    r   __del__zCompressedFile.__del__   s$    4+,!!'') -r   c              #   R   K   	 | j                   j                  d      }|sy| #w)z?Iterate over the compressed content of the file in 1 MB chunks.   N)rl   read)r   rU   s     r   iter_compressedzCompressedFile.iter_compressed   s/     ))..w7EK	 s   %'c                 b    t        j                  | j                  t         j                  d      S )z?Check if the compressed file is readable by the effective user.T)effective_ids)osaccessrh   R_OKrp   s    r   is_readablezCompressedFile.is_readable   s    yytDDr   )r   r   r   r   r   __annotations__r   rq   r   r,   ru   boolr{   r   r   r   rg   rg      sD    
 M: : :
*% ET Er   rg   c            	           e Zd ZdZ	 	 	 ddedz  dedz  dedz  ddfdZdeddfdZdefd	Z	defd
Z
edee   dee   fd       ZdefdZdej                   ddfdZdefdZdee   fdZy)CompressedValuezRepresent a ProblemReport value which is gzip or zstandard compressed.

    By default, compressed values are in gzip format. Earlier versions of
    problem_report used zlib format (without gzip header).
    Nvaluenamecompressed_valuer   c                 H    || _         || _        |r| j                  |       yy)zAInitialize an empty CompressedValue object with an optional name.N)r   r   	set_value)r   r   r   r   s       r   r   zCompressedValue.__init__   s'     !1	NN5! r   c                     t        j                         }t        j                  | j                  d|d      5 }|j                  |       ddd       |j                         | _        y# 1 sw Y   xY w)zSet uncompressed value.wbr   modefileobjmtimeN)ioBytesIOgzipGzipFiler   writegetvaluer   )r   r   outgzs       r   r   zCompressedValue.set_value   sS    jjl]]4994AF 	"HHUO	 #	 	s   A((A1c                 H    | j                   J t        | j                         S )z=Return size of the compressed (but not base64 encoded) value.)r   rF   rp   s    r   get_compressed_sizez#CompressedValue.get_compressed_size   s%    $$0004(())r   c                 4    | j                         dz   dz  dz  S )a  Return the size needed on disk to store the compressed value.

        The compressed value will be base64 encoded when written to disk
        which adds an overhead of 1/3 plus up to 2 bytes of padding. Additional
        spaces and newlines are ignored in this calculation.
        r=   r7   r9   )r   rp   s    r   get_on_disk_sizez CompressedValue.get_on_disk_size   s"     ))+a/A5::r   r-   c              #     K   t        | d      }|y|j                  t              rt        | |      E d{    y|j                  t              rAt        j                  t
        j                         }|j                  t        |             n't        j                         }|j                  |       | D ]  }|j                  |        |j                          y7 w)z5Decode the given compressed value (iterator version).N)r)   r%   ZSTANDARD_MAGIC_NUMBERrV   GZIP_HEADER_STARTzlibrR   	MAX_WBITSrS   r@   flush)r-   rU   rT   s      r   decode_compressed_streamz(CompressedValue.decode_compressed_stream   s      UD!=23)%777-.--t~~o>L))*<U*CDD  --/L))%00 	1E))%00	1  "" 8s   4C(C&B0C(c                 \   | j                   J | j                   j                  t              r#t               j	                  | j                         S | j                   j                  t
              rt        j                  | j                         S t        j                  | j                         S )zReturn uncompressed value.)r   r%   r   rO   rS   r   r   r   rp   s    r   	get_valuezCompressedValue.get_value  s    $$000  ++,BC.0;;D<Q<QRR  ++,=>??4#8#899t4455r   filec                 N   | j                   sJ | j                   j                  t              r:t               }|j	                  t        j                  | j                         |       y| j                   j                  t              ret        j                  t        j                  | j                               5 }	 |j                  d      }|sn|j                  |       &	 ddd       y|j                  t        j                  | j                                y# 1 sw Y   8xY w)z5Write uncompressed value into given file-like object.N)r   rs   )r   r%   r   rO   copy_streamr   r   r   r   r   rt   r   r   rS   )r   r   rT   r   rU   s        r   r   zCompressedValue.write  s    $$$$  ++,BC68L$$RZZ0E0E%FM  ++,=>rzz$2G2G'HI RGGG,E JJu%	 
   	

4??4#8#89: s   :)DD$c                     | j                   sJ | j                   j                  t              r/t        t	        j
                  d| j                   dd       d         S t        | j                               S )z$Return length of uncompressed value.<LNr   )r   r%   r   intstructunpackrF   r   rp   s    r   __len__zCompressedValue.__len__4  s`    $$$$  ++,=>v}}T4+@+@+EFqIJJ4>>#$$r   c                 >    | j                         j                         S )z.Behaves like splitlines() for a normal string.)r   
splitlinesrp   s    r   r   zCompressedValue.splitlines<  s    ~~**,,r   )NNN)r   r   r   r   r,   r   r   r   r   r   r   staticmethodr   r   r   typingBinaryIOr   r   listr   r   r   r   r   r      s     #)-	
"t|
" Dj
"  $,	
"
 

"/u / /*S *
;# ; # #HUO # #*	65 	6;&// ;d ;*% %-DK -r   r   c            	       F    e Zd ZdZd,dededz  ddf fdZdee   ddfdZ	 	 d-d	ej                  e
j                  z  d
ee
j                  d   z  dee   dz  ddfdZd	ej                  e
j                  z  dee   ez  deddfdZdee   fdZdedz  fdZdefdZedeez  defd       Zedeez  deez  fd       Z	 d.dee   dz  deeeeez  ez  ez  f      fdZd/d	e
j                  deddfdZdedeee   ee   f   fdZ d	e
j                  deddfdZ!ed	e
j                  dedee   ddfd        Z"dede#eddf   fd!Z$d	e
j                  deddfd"Z%d/d#ed$eddfd%Z&	 	 	 	 d0d&Z'd'ed(ee(z  ez  ez  ez  ddfd)Z)dee   fd*Z*ed	e+ddfd+       Z, xZ-S )1ProblemReportz1Class to store, load, and handle problem reports.Nproblem_typedater   c                 t    |t        j                         }t        |   ||d       t	               | _        y)zInitialize a fresh problem report.

        problem_type can be 'Crash', 'Packaging', 'KernelCrash' or
        'KernelOops'. date is the desired date/time string; if
        None (default), the current local time is used.
        N)ProblemTypeDate)timeasctimer   r   setold_keys)r   r   r   r   s      r   r   zProblemReport.__init__D  s2     <<<>DtDE #&%r   tagsc                     | j                         }|j                  |      }dj                  t        |            | d<   y)z/Add tags to the report. Duplicates are dropped. TagsN)get_tagsunionjoinsorted)r   r   current_tagsnew_tagss       r   add_tagszProblemReport.add_tagsR  s4    }}%%d+xxx 01Vr   r   binary
compressed
key_filterc                    | j                  |       | j                  j                          |rt        |      }nd}t	        |      D ]  }t        |      \  }}}|||vr|r|du rd| j                  |<   n|dk(  r*t        |dj                  |            | j                  |<   nn| j                  dj                  t        j                  |                  | j                  |<   n-| j                  dj                  |            | j                  |<   ||j                  |       t        |      dk(  s n t        | j                  j                               | _        y)a  Initialize problem report from a file-like object.

        If binary is False, binary data is not loaded; the dictionary key is
        created, but its value will be an empty string. If it is True, it is
        transparently uncompressed and available as dictionary byte array
        values. If binary is 'compressed', the compressed value is retained,
        and the dictionary value will be a CompressedValue object. This is
        useful if the compressed value is still useful (to avoid recompression
        if the file needs to be written back).

        file needs to be opened in binary mode.

        If key_filter is given, only those keys will be loaded.

        Files are in RFC822 format, but with case sensitive keys.
        NFr   r   )r   r   r   )_assert_bin_modedataclearr   r   re   r   r   _try_unicoder   removerF   keysr   )	r   r   r   r   remaining_keysr-   rb   r   rc   s	            r   loadzProblemReport.loadX  s6   , 	d#		 _N!N!$' 	E,8,?)C>)c.GU?%)DIIcN|+%4 388H3E&DIIcN &*%6%6!I!I(!ST&DIIcN
 "&!2!2388H3E!F		#)%%c*~&!+/	2 DIINN,-r   bin_keys	directoryc                    | j                  |       t        |t              r|g}t        |      }i }t	        |      D ]  }t        |      \  }}}	||vr|	||<   |j                  |       |	s0t        j                  j                  ||      }
	 t        |
d      5 }t        j                  |      D ]  }|j                  |        	 ddd        |rt        ddj                  |       d      d|j!                         v r5|j#                         D cg c]  \  }}|du s| }}}t%        | d      y# 1 sw Y   sxY w# t        $ r}t        d|
       |d}~ww xY wc c}}w )	zExtract only given binary elements from the problem report.

        Binary elements like kernel crash dumps can be very big. This method
        extracts directly files without loading the report into memory.
        r   Nzunable to open zCannot find z, z
 in reportFz has no binary content)r   
isinstancer   r   r   re   r   rx   pathr   rk   r   r   r   OSErrorKeyErrorvaluesitemsr]   )r   r   r   r   missing_keys	b64_blockr-   rb   r   rc   key_pathr   rU   r4   itemelementr   s                    r   extract_keyszProblemReport.extract_keys  s    	d#h$ zHH~	!$' 	GE,8,?)C>,&+IcN$!ww||Is3HG(D) )S!0!I!I(!S )		%())	G$ \$))L*A)B*MNNI$$&&/8/@UmdGGuDTTUEUw&<=>> ') )  Gz:;FG Vs<   E,D9EE%#E%9E	>E	E"EE"c                 X    d| vr
t               S t        | d   j                  d            S )zReturn the set of tags.r   r   )r   r\   rp   s    r   r   zProblemReport.get_tags  s+    5L4<%%c*++r   c                 .   t        j                  t         j                        }	 	 t        j                  t         j                  d       t	        t        j                  t        j                  | d                     t        j                  t         j                  |       S # t        $ r' Y t        j                  t         j                  |       yw xY w# t        j                  t         j                  |       w xY w# t         j                  $ r Y yw xY w)zhGet timestamp (seconds since epoch) from Date field.

        Return None if it is not present.
        Cr   N)
locale	getlocaleLC_TIME	setlocaler   r   mktimestrptimer   r3   )r   
orig_ctimes     r   get_timestampzProblemReport.get_timestamp  s     %%fnn5
		=  54;;t}}T&\'BCD   <    <   <|| 		sB   AB" =$C> "	C+C ,$C> CC &C;;C> >DDc                 &    d| j                         v S )z~Check if the report has any keys which were not loaded.

        This could happen when using binary=False in load().
        N)r   rp   s    r   has_removed_fieldsz ProblemReport.has_removed_fields  s    
 t{{}$$r   stringc                 t    t        | t              r(| D ]#  }|dk  s	t        |      j                         r# y y)z0Check if the given strings contains binary data.    TF)r   r,   chrisspace)r   cs     r   	is_binaryzProblemReport.is_binary  s9     fe$  r6#a&.."2  r   r   c                     t        |t              r#| j                  |      s	 |j                  d      S |S # t        $ r |cY S w xY w)z*Try to convert bytearray value to Unicode.UTF-8)r   r,   r   r^   r_   )clsr   s     r   r   zProblemReport._try_unicode  sJ     eU#CMM%,@||G,,  & s   6 AAr   c              #   R  K   |r/t        t        | j                               t        |      z        }nt        | j                               }dD ]*  }||v s|j                  |       |j	                  d|       , |D ]%  }|j                  d      r| |   }||| |   f ' yw)a  Iterate over all non-internal items sorted.

        The most interesting fields will be returned first. The remaining
        items will be returned in alphabetical order. Keys starting with
        an underscore are considered internal and are skipped. Also values
        that are None will be skipped.

        If keys is provided, only those keys will be iterated over.
        )	Traceback
StackTraceTitler   PackageExecutablePathr   r*   N)r   r   r   r   insertr%   )r   r   rb   r   s       r   sorted_itemszProblemReport.sorted_items  s      #diik*SY67D$))+&D
 
	$C d{C As#
	$  	!C~~c"IE}tCy. 	!s   AB'AB'only_newc                     | j                  |       | j                  |      \  }}|D ]  }| j                  ||        |D ]  }| j                  ||        y)a  Write 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 OSError.

        file needs to be opened in binary mode.

        Files are written in RFC822 format.
        N)r   _get_sorted_keys_write_ascii_item)_write_binary_item_compressed_and_encoded)r   r   r   asckeysbinkeysks         r   r   zProblemReport.write  si    ( 	d#00: 	,A""4+	,  	DA::4C	Dr   c                 8   g }g }| j                   j                         D ]  }|r|| j                  v r| j                   |   }t        |d      r5| j	                  |      r|j                  |       R|j                  |       dt        |t        t        f      s%t        |      dk\  r|d   s|j                  |       |j                  |        |j                          d|v r#|j                  d       |j                  dd       |j                          ||fS )zySort keys into ASCII non-ASCII/binary attachment ones, so that
        the base64 ones appear last in the report
        findr=   r:   r   r   )r   r   r   rn   r   appendr   rg   r   rF   sortr   r   )r   r   r  r  r  vs         r   r  zProblemReport._get_sorted_keys/  s     ! 	"AA.		!Aq&!>>!$NN1%NN1%q>?"CDFaK! q!q!#	"& 	G#NN=)NN1m,r   rb   c                    | j                   |   }t        |d      st        |      dk\  r|d   |d   }nd }t        |      dk\  xr |d   }t        |d   d      r|d   j                         }n(t	        |d   d      5 }|j                         }d d d        |rt        |      dk(  rt        d|z         |t        |      |kD  r| j                   |= y t        |t              r|j                  d	      }|j                  |j                  d
             d|v r3|j                  d       |j                  |j                  dd             n"|j                  d       |j                  |       |j                  d       y # 1 sw Y   xY w)Nr	  r7   r=   r9   r   rt   rj   did not get any data for field r   r[   rC   s   :
    
 s   : )r   rn   rF   rt   rk   r   r   r   encoder   replace)r   r   rb   r  limitfail_on_emptyfs          r   r  zProblemReport._write_ascii_itemO  sX   IIcN q&!1v{qt/!FaK0AaDMqtV$aDIIK!A$% !A! Q1?#EFF SVe^IIcNa!A

3::g&'A:JJwJJqyy/0JJuJJqM

5-! !s   >E??Fchunksc                 f   | j                         }	 | j                  | dj                  d             |D ]7  }| j                  d       | j                  t        j                  |             9 | j                  d       y# t
        $ r$ | j                  |       | j                  |        w xY w)zCWrite out binary chunks as a base64-encoded RFC822 multiline field.z: base64r[   r  rC   N)tellr   r  r0   	b64encode	Exceptionseektruncate)r   rb   r  reset_positionchunks        r   !_write_binary_item_base64_encodedz/ProblemReport._write_binary_item_base64_encodedv  s    
 		JJ#h'..w78 4

6"

6++E234 JJu 	IIn%MM.)	s   A0B -B0c           	   #     K   | j                   |   }t        |t              r|j                         E d{    yt        |t              r|j
                  J |j
                   yt        dz   |j                  d      z   dz   }| t        j                  d      }t        j                  dt        j                  t        j                   t        j                  d      }d}t        |d      r>|t        |      z  }t        j                  ||      }|j!                  |      }|r	| nd}t        |      d	k\  r
|d
   |d
   }t        |d   d      r|d   }	nt#        |d   d      }		 |	j%                  d      }
|t        |
      z  }t        j                  |
|      }|||kD  r| j                   |= t'        d      |
r|j!                  |
      }|r| nnpt        |d   d      s|	j)                          t        |      dk\  r'|d	   r"|dk(  rt+        d| dt-        |d                |j/                         }
|
t1        j2                  d|dz        z  }
|
t1        j2                  d|dz        z  }
|
 y7 Jw)a  Generator taking the value out of self.data and outputing it
        in compressed chunks of binary data.

        Throws a _SizeLimitExceeded exception if the value exceeds its specified
        size limit, in which case it will also remove the value from self.data entirely.
        Ns       r       r      r   r	  r7   r=   rt   rj   rs   z,Binary data bigger than the limit ({limit}b)r9   r  z from r   l    )r   r   rg   ru   r   r   r   r  r   crc32compressobjDEFLATEDr   DEF_MEM_LEVELrn   rF   compressrk   rt   r   ro   r   r   r   r   pack)r   rb   r   gzip_headercrcbcsizeoutblockr  r  rU   s              r   _generate_compressed_chunksz)ProblemReport._generate_compressed_chunks  su     		#e^,,,...e_-))555(((-.jj!"  	 jjoaASASUVW 5&!CJD**UC(C{{5)H E5zQ58#7auQx(!H q4(wE
"jj,$e| IIcN0J  !{{51H&   58V,	5zQ5819!9#fSq]OT 
 
T3#344T4*#455E /s   3JI?I
Jc                 h    	 | j                  ||| j                  |             y# t        $ r Y yw xY w)z?Write the binary keys with gzip compression and base64 encodingN)r  r-  r   )r   r   rb   s      r   r  z7ProblemReport._write_binary_item_compressed_and_encoded  s;    	22c4;;C@ " 		s   "% 	11
reportfile
keep_timesc                    t        j                  |      }	 t        |d      5 }t        j                  |d       | j	                  |       ddd       |r,t        j
                  ||j                  |j                  f       t        j                  ||j                         y# 1 sw Y   XxY w# |r,t        j
                  ||j                  |j                  f       t        j                  ||j                         w xY w)a  Add 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.
        abr   N)	rx   statrk   chmodr   utimest_atimest_mtimest_mode)r   r/  r0  streports        r   add_to_existingzProblemReport.add_to_existing  s     WWZ 	-j$' #6Q'

6"# bkk2;;%?@HHZ,# # bkk2;;%?@HHZ,s#   B. (B"B. "B+'B. .AC>c                 ~	   | j                  |       t        | j                  j                               }d}g }d|v r#|j	                  d       |j                  dd       |r6d}	|D ]/  }
|
|v s|j	                  |
       |j                  |	|
       |	dz  }	1 |D ]  }|r||v r| j                  |   }d}t        |t              r|j                  }nt        |d      sd}t        |d   d      r|d   }nt        |d   d      }|j                  d	      r|j                         }not        j                         }t        j                   |d
|d      5 }	 |j                  d      }|r|j#                  |       nn'	 ddd       |j%                         }|j'                          n;| j)                  |      r*|j                  d	      r|}nt        ||      j                  }|rt*        j,                  j.                  j1                  dd      }|j                  d	      r|j3                  dd|       n|j3                  dd|d	z          |j5                  |       t*        j6                  j9                  |       |j;                  |       t=        |      }t        |t>              r|jA                  dd      }t        |tB              sJ t=        |jE                               }|dk  r%|dk(  r |jG                         }||dz   |z   dz   z  }w|dk  rK||k  rF||dz   z  }|j                  d      s|dz  }||jI                         jK                  dd      dz   z  }t*        j,                  jL                  jO                  |d      }|j3                  dd|dz          |j;                  |        t*        j,                  jL                  jO                  |d      }|j3                  dd       |j                  d|       t*        j,                  jP                  jS                         }|r*|jU                         D ]  \  }}|j3                  ||        |D ]  }|jW                  |        |j#                  |jY                         j[                  d             |j#                  d       y# 1 sw Y   5xY w)a  Write 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.
         r   r   r:   Nr	  rt   rj   z.gzr   r   rs   applicationzx-gzipzContent-Disposition
attachment)rh   r   r  i  z: 
z:
 z
 )_charsetz.txtinlinerC   ).r   r   r   r   r   r   r   r   r   rn   rk   rG   rt   r   r   r   r   r   r   ro   r   emailmimebaseMIMEBase
add_headerset_payloadencodersencode_base64r
  rF   r,   r^   r   r   rstriprE   r  textMIMEText	multipartMIMEMultipartr   attach	as_stringr  )r   r   attach_tresholdextra_headers	skip_keyspriority_fieldsr   rL  attachmentscounterpriority_fieldr  r  attach_valuer  r   r   rU   attr+  linesmsgas                          r   
write_mimezProblemReport.write_mime  s=   F 	d#diinn&'D KK&KK=)G"1 !!T)KK/KK8qLG	!  M	,AQ)^		!AL !_- 11 Q'!1Q4(!A QqT4(A::e$#$668L**,CqtSJ &b"$%FF7OE$ " % #
 !&& $'<<>L	 "::e$#$L#21a#8#I#IL jjoo..}hG::e$NN#8,QRNSNN-|a%i #  -,,S1""3' 1v a')4A!!S)))ALLN+4<EQJ
AAHqL4//DT\e&>AJ&D::d+T	AGGI--dE:TAAD  **//221w2GCNN-|a&j #   &&s+[M	,` jjoo&&tg&>,h71c"jj""002%++- %1q!$% 	AJJqM	 	

3==?))'23

5K& &s   )R22R<	r  r  c                    t        |d      sJ |j                  dd      j                  dd      j                  dd      j                         st        d| d      t	        |t
        t        f      szt        |d      snt	        |t              rOt        |      dk(  s#t        |      d	k\  r3t        |      d
k  r%|d   dv rt        |d   d      st        |d   d      st        d| d      | j                  j                  ||      S )Nisalnum.r=  -r*   zkey 'zT' contains invalid characters (only numbers, letters, '.', '_', and '-' are allowed)r:   r=   r9   >   FTr   rt   zvalue for key z7 must be a string, CompressedValue, or a file reference)rn   r  r`  r]   r   rg   r   tuplerF   	TypeErrorr   __setitem__)r   r  r  s      r   re  zProblemReport.__setitem__  s	    q)$$$yyb!))#r2::3CKKMs J K  q>?;<q)$1e$FaKA!A!!8MQqT9-1v1F   $' ( 
 yy$$Q**r   c                 b    t        | j                  j                               | j                  z
  S )zReturn newly added keys.

        Return the set of keys which have been added to the report since it
        was constructed or loaded.
        )r   r   r   r   rp   s    r   new_keyszProblemReport.new_keys  s#     499>>#$t}}44r   c                 *    t        | d      rJ d       y)z0Assert that given file object is in binary mode.encodingz"file stream must be in binary modeN)rn   )r   s    r   r   zProblemReport._assert_bin_mode  s     4,R.RR,,r   )CrashN)TNr/   )F)   NNN).r   r   r   r   r   r   r   r   r   r   r   r   r}   Literalr   r   r   r   r   r   r   r   r,   r   classmethodr   r   rc  r   r   r   r   r  r  r  r   r-  r  r;  r^  rg   re  rg  r   r   r   r   s   @r   r   r   A  s   ;(S (#* (PT (2Xc] 2t 2 7;+/	6.mmfoo-6. v~~l336. SMD(	6.
 
6.p(?mmfoo-(? 3-#%(? 	(?
 
(?T,#c( ,sTz $%D % %#+ $       ,0#!SMD(#!	%eo5;eCDE	F#!JD&// DT Dd D<   %S	498L2M  @%foo %C %D %N oo$'19%	  Ms MydAR7S M^
OO
*-
	
-# -4 -D -( Rh++//ACG%O+	+<5#c( 5 Sv S$ S Sr   r   ),r   r0   r2   collectionsdataclassesemail.encodersrC  email.mime.baseemail.mime.multipartemail.mime.textr   r   r   rx   r   r   r   r   collections.abcr   r   r   r   r   r]   r   rM   r   r   r,   r5   r@   rI   rO   rV   rc  r   r}   re   	dataclassrg   r   UserDictr   r   r   r   <module>rw     sX   .          	  	     9 9 $ , 
Z 
I I %8  %F?8E? ?x ?U u ($% $e $ $	(-huo -E -huo -/ /E#x2L,M /< E E E<h- h-Vo	SK(( o	Sr   