
    U                         S r SSKrSSKJr  SSKJr  SSKrSSKrSSKrSSKrSSK	r	SSK
r
/ SQrSu  rrrrrSu  rrSS jrS	 r " S
 S\5      r " S S\
R.                  5      rSS jrS rg)zFunctions that read and write gzipped files.

The user of the file doesn't have to worry about the compression,
but random access is not allowed.    N)builtins)range)GzipFileopencompress
decompress)               )r	   r
   c                     [        XU5      $ )zShorthand for GzipFile(filename, mode, compresslevel).

The filename argument is required; mode defaults to 'rb'
and compresslevel defaults to 9.

)r   )filenamemodecompresslevels      (lib/third_party/apitools/base/py/gzip.pyr   r   "   s     HM22    c                 P    U R                  [        R                  " SU5      5        g )Nz<L)writestructpack)outputvalues     r   write32ur   ,   s     LLT5)*r   c                   H    \ rS rSrSrSS jrS rSS jrS rSS jr	S r
S	rg
)_PaddedFile2   zMinimal read-only file object that prepends a string to the contents
of an actual file. Shouldn't be used outside of gzip.py, as it lacks
essential functionality.c                 J    X l         [        U5      U l        Xl        SU l        g Nr   )_bufferlen_lengthfile_read)selffprepends      r   __init___PaddedFile.__init__7   s    7|	
r   c                    U R                   c  U R                  R                  U5      $ U R                   U-   U R                  ::  a9  U R                   nU =R                   U-  sl         U R                  X R                    $ U R                   nS U l         U R                  US  U R                  R                  XR                  -
  U-   5      -   $ N)r$   r#   readr"   r    )r%   sizer,   s      r   r,   _PaddedFile.read=   s    ::99>>$''::,::DJJ$J<<ZZ00::DDJ<<&		tll2T9:; ;r   c                 .   U R                   c  Xl        O`U(       a8  [        U5      U R                   ::  a  U =R                   [        U5      -  sl         g U R                  U R                   S  U-   U l        [        U R                  5      U l        SU l         g r   )r$   r    r!   r"   )r%   r'   readpreviouss      r   r'   _PaddedFile.prependJ   sj    ::"Lc'ldjj8JJ#g,&J<<

4w>DL4<<(
r   c                 P    U R                   c  gU R                  U R                   S  $ )Nr   )r$   r    r%   s    r   unused_PaddedFile.unusedU   s$    ::||DJJK((r   c                 (   US:X  ad  U R                   bW  SXR                   -   s=::  a  U R                  ::  a  O  OU =R                   U-  sl         g XR                  U R                   -
  -  nS U l         S U l        U R                  R	                  X5      $ )Nr	   r   )r$   r"   r    r#   seek)r%   offsetwhences      r   r7   _PaddedFile.seekZ   sq    Q;4::1FZZ'74<<7

f$
,,33
yy~~f--r   c                 .    [        U R                  U5      $ r+   )getattrr#   )r%   names     r   __getattr___PaddedFile.__getattr__f   s    tyy$''r   )r    r"   r$   r#   N)r   )r   Fr   )__name__
__module____qualname____firstlineno____doc__r(   r,   r'   r4   r7   r>   __static_attributes__ r   r   r   r   2   s%     ;	)

.(r   r   c                      \ rS rSrSrSrSr  S!S jr\S 5       r	S r
S rS	 rS
 rS rS rS rS rS"S jrS"S jrS rS rS#S jrS rS r\S 5       rS r\R8                  4S jrS rS rS r S r!S r"S$S jr#S"S jr$S r%g)%r   j   a
  The GzipFile class simulates most of the methods of a file object with
the exception of the readinto() and truncate() methods.

This class only supports opening files in binary mode. If you need to open
a compressed file in text mode, use the gzip.open() function.

Ni   c                    U(       a&  SU;   d  SU;   a  [        SR                  U5      5      eU(       a  SU;  a  US-  nUc%  [        R                  " X=(       d    S5      =o@l        Uc.  [        USS5      n[        U[        R                  5      (       d  SnUc  [        US	S5      nUR                  S
5      (       a@  [        U l        SU l        SU l        SU l        SU l        Xl        SU l        [%        U5      nOUR                  S5      (       ag  [&        U l        U R)                  U5        [*        R,                  " U[*        R.                  [*        R0                  * [*        R2                  S5      U l        O[        SR                  U5      5      eX@l        SU l        XPl        U R                  [&        :X  a  U R=                  5         gg)a  Constructor for the GzipFile class.

At least one of fileobj and filename must be given a
non-trivial value.

The new class instance is based on fileobj, which can be a regular
file, an io.BytesIO object, or any other object which simulates a file.
It defaults to None, in which case filename is opened to provide
a file object.

When fileobj is not None, the filename argument is only used to be
included in the gzip file header, which may includes the original
filename of the uncompressed file.  It defaults to the filename of
fileobj, if discernible; otherwise, it defaults to the empty string,
and in this case the original filename is not included in the header.

The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', or 'wb',
depending on whether the file will be read or written.  The default
is the mode of fileobj if discernible; otherwise, the default is 'rb'.
A mode of 'r' is equivalent to one of 'rb', and similarly for 'w' and
'wb', and 'a' and 'ab'.

The compresslevel argument is an integer from 0 to 9 controlling the
level of compression; 1 is fastest and produces the least compression,
and 9 is slowest and produces the most compression. 0 is no compression
at all. The default is 9.

The mtime argument is an optional numeric timestamp to be written
to the stream when compressing.  All gzip compressed streams
are required to contain a timestamp.  If omitted or None, the
current time is used.  This module ignores the timestamp when
decompressing; however, some programs, such as gunzip, make use
of it.  The format of the timestamp is the same as that of the
return value of time.time() and of the st_mtime member of the
object returned by os.stat().

tUzInvalid mode: {!r}bNrbr=    r   rTr   r   d   )wa)
ValueErrorformatr   r   	myfileobjr<   
isinstancesixstring_types
startswithREADr   _new_memberextrabuf	extrasize
extrastartr=   min_readsizer   WRITE_init_writezlibcompressobjDEFLATED	MAX_WBITSDEF_MEM_LEVELr   fileobjr8   mtime_write_gzip_header)r%   r   r   r   rh   ri   s         r   r(   GzipFile.__init__v   s   P SD[C4K188>??CtOCKD?'/}}X|t'LLGnw3Hh(8(899<7FD1D??3DI#D  DMDNDO I #D!'*G__Z((DIX& ,,]-1]].2nn_-1-?-?-.	0DM 188>??
99##% r   c                     SS K nUR                  S[        S5        U R                  [        :X  a"  U R
                  SS  S:w  a  U R
                  S-   $ U R
                  $ )Nr   zuse the name attributer
   z.gz)warningswarnDeprecationWarningr   ra   r=   )r%   rn   s     r   r   GzipFile.filename   sM    .0BAF99$))BC.E"999u$$yyr   c                     U R                   n[        U[        5      (       a  UR                  n[	        U5      nSUSS -   S-   [        [        U 5      5      -   S-   $ )Nz<gzip r	    >)rh   rW   r   r#   reprhexid)r%   rh   ss      r   __repr__GzipFile.__repr__   sR    ,,g{++llGM!Ab'!C'#bh-7#==r   c                 <    U R                   (       a  [        S5      eg)zLRaises a ValueError if the underlying file object has been closed.

        zI/O operation on closed file.N)closedrT   r3   s    r   _check_closedGzipFile._check_closed   s     ;;<== r   c                 v    Xl         [        R                  " S5      S-  U l        SU l        / U l        SU l        g Nr       r   )r=   rc   crc32crcr-   writebufbufsize)r%   r   s     r   rb   GzipFile._init_write   s0    	::c?Z/	r   c                 V   U R                   R                  S5        U R                   R                  S5         [        R                  R	                  U R
                  5      n[        U[        R                  5      (       d  UR                  S5      nUR                  S5      (       a  US S nSnU(       a  [        nU R                   R                  [        R                  " U5      R                  S5      5        U R                  nUc  [        R                  " 5       n[!        U R                   [#        U5      5        U R                   R                  S5        U R                   R                  S	5        U(       a  U R                   R                  US
-   5        g g ! [         a    Sn Nf = f)N      zlatin-1s   .gzrm   r   r             )rh   r   ospathbasenamer=   rW   rX   binary_typeencodeendswithUnicodeEncodeErrorFNAMEunichrri   timer   int)r%   fnameflagsri   s       r   rj   GzipFile._write_gzip_header   s6   ;'7#		 GG$$TYY/EeS__55Y/~~f%%cr
 E3::e,33I>?

=IIKEs5z*7#7#LLuw/  " 	E	s   A4F F('F(c                 N    [         R                  " S5      S-  U l        SU l        g r   )rc   r   r   r-   r3   s    r   
_init_readGzipFile._init_read  s    ::c?Z/	r   c                     U R                   R                  U5      n[        U5      U:  aN  U R                   R                  U[        U5      -
  5      nU(       d  [        S5      eX#-  n[        U5      U:  a  MN  U$ )NzACompressed file ended before the end-of-stream marker was reached)rh   r,   r!   EOFError)r%   ndatarM   s       r   _read_exactGzipFile._read_exact  sp    ||  #$i!m!!!c$i-0A  B C CID $i!m r   c                 6   U R                   R                  S5      nUS:X  a  gUS:w  a  [        S5      e[        R                  " SU R                  S5      5      u  p#U l        US:w  a  [        S5      eU[        -  (       a9  [        R                  " S	U R                  S5      5      u  nU R                  U5        U[        -  (       a,   U R                   R                  S5      nU(       a  US:X  a  OM+  U[        -  (       a,   U R                   R                  S5      nU(       a  US:X  a  OM+  U[        -  (       a  U R                  S5        U R                   R                  5       nU(       a,  U R                  R                  U5      nU R                  U5        g
)Nr
   r   Fr   zNot a gzipped filez<BBIxxr   zUnknown compression methodz<HTr	   r   )rh   r,   OSErrorr   unpackr   ri   FEXTRAr   FCOMMENTFHCRCr4   r   _add_read_data)r%   magicmethodflag	extra_lenry   r4   
uncompresss           r   _read_gzip_headerGzipFile._read_gzip_header  sL   !!!$C<K.//#)==4;K;KA;N#O djQ;677&=tT-=-=a-@AJIY'%<LL%%a(AL  (?LL%%a(AL  %<Q$$&33F;J
+r   c                 \   U R                  5         U R                  [        :w  a  SS Kn[	        UR
                  S5      eU R                  c  [        S5      e[        U[        5      (       a  UR                  5       n[        U5      S:  a  U R                  R                  U R                  R                  U5      5        U =R                  [        U5      -  sl        [        R                   " XR"                  5      S-  U l        U =R$                  [        U5      -  sl        [        U5      $ )Nr   z$write() on read-only GzipFile objectz!write() on closed GzipFile objectr   )r~   r   ra   errnor   EBADFrh   rT   rW   
memoryviewtobytesr!   r   r   r-   rc   r   r   r8   )r%   r   r   s      r   r   GzipFile.write6  s    99%++'MNN<<@AA dJ''<<>Dt9q=LLt}}55d;<IIT"Izz$1J>DHKK3t9$K4yr   c                    U R                  5         U R                  [        :w  a  SS Kn[	        UR
                  S5      eU R                  S::  a  U R                  c  gSnUS:  aT  U R                  U5      (       a1  [        U R                  US-  5      nU R                  U5      (       a  M1  U R                  nOkXR                  :  a\  U R                  U5      (       d  XR                  :  a  U R                  nO*[        U R                  US-  5      nXR                  :  a  M\  U R                  U R                  -
  nU R                  XDU-    nU R                  U-
  U l        U =R                  U-  sl        U$ )Nr   z$read() on write-only GzipFile objectr      r
   )r~   r   r[   r   r   r   r^   rh   r$   minmax_read_chunkr8   r_   r]   )r%   r-   r   readsizer8   chunks         r   r,   GzipFile.readK  s,   99%++'MNN>>Q4<<#7!8**X&&t22HqLA **X&&>>D'zz(++nn,#~~t22HqLA ' t.ftm4$.tr   c                 B   U R                  5         U R                  [        :w  a  SS Kn[	        UR
                  S5      eU R                  S::  a  U R                  c  gU R                  S::  a=  U R                  5       (       a(   U R                  S::  a  U R                  5       (       a  M(  US:  d  XR                  :  a  U R                  nU R                  U R                  -
  nU R                  X3U-    nU =R                  U-  sl        U =R                  U-  sl	        U$ )Nr   z%read1() on write-only GzipFile objectr   )r~   r   r[   r   r   r   r^   rh   r$   r8   r_   r]   )r%   r-   r   r8   r   s        r   read1GzipFile.read1h  s    99%++'NOO>>Q4<<#7 nn!djjll nn!djjll!8tnn,>>Dt.ftm4$tr   c                    U R                   [        :w  a  SS Kn[        UR                  S5      eUS:  a  SnU R
                  S:X  aq  U R                  c  gU R
                  S:X  aS  U R                  [        US5      5      (       a3   U R
                  S:X  a"  U R                  [        US5      5      (       a  M3  U R                  U R                  -
  nU R
                  nU[        U R                  5      U-
  :X  d   eU R                  X3U-    $ )Nr   z$peek() on write-only GzipFile objectrQ   r   r   )r   r[   r   r   r   r^   rh   r$   maxr8   r_   r!   r]   )r%   r   r   r8   	remainings        r   peekGzipFile.peek~  s    99%++'MNN s7A>>Q||# ..A%$**SD\*B*B ..A%$**SD\*B*Bt.NN	C.7777}}VQJ//r   c                 z    [        U5      U R                  -   U l        U =R                  [        U5      -  sl        g r+   )r!   r^   r8   )r%   bufs     r   _unreadGzipFile._unread  s(    SDNN2s3xr   c                 $   U R                   c  gU R                  (       aW  U R                  5         U R                  5       (       d  g[        R
                  " [        R                  * 5      U l        SU l        U R                   R                  U5      nUS:X  al  U R                  R                  5       nU R                   R                  U R                  R                  S5        U R                  5         U R                  U5        gU R                  R                  U5      nU R                  U5        U R                  R                  S:w  aG  U R                   R                  U R                  R                  S5        U R                  5         SU l        g)NFr   T)rh   r\   r   r   rc   decompressobjrf   r   r,   flushr'   unused_data	_read_eofr   )r%   r-   r   r   s       r   r$   GzipFile._read  s-   << OO))++"00$..ADO$D ll%
 #:..0J LL  !<!<dCNN
+__//4
J'??&&#- LL  !<!<dC NN#Dr   c                 J   [         R                  " XR                  5      S-  U l        U R                  U R                  -
  nU R
                  US  U-   U l        U R                  [        U5      -   U l        U R                  U l        U R                  [        U5      -   U l        g Nr   )	rc   r   r   r8   r_   r]   r^   r!   r-   )r%   r   r8   s      r   r   GzipFile._add_read_data  sw    ::dHH-
:t.fg.5#d)3++IID	)	r   c                    [         R                  " SU R                  S5      5      u  pXR                  :w  a/  [	        S[        U5      < S[        U R                  5      < 35      eX R                  S-  :w  a  [	        S5      eSnUS:X  a#  U R                  R                  S5      nUS:X  a  M#  U(       a  U R                  R                  US	5        g g )
Nz<IIr   zCRC check failed z != r   z!Incorrect length of data producedr   r	   T)
r   r   r   r   r   rw   r-   rh   r,   r'   )r%   r   isizecs       r   r   GzipFile._read_eof  s    
 }}UD,<,<Q,?@HHU9<TXXH I Iyy:-.=>>
 7l!!!$A 7lLL  D) r   c                     U R                   S L $ r+   rh   r3   s    r   r}   GzipFile.closed  s    ||t##r   c                    U R                   nUc  g S U l          U R                  [        :X  aV  UR                  U R                  R                  5       5        [        XR                  5        [        XR                  S-  5        U R                  nU(       a  S U l	        UR                  5         g g ! U R                  nU(       a  S U l	        UR                  5         f f = fr   )rh   r   ra   r   r   r   r   r   r-   rV   close)r%   rh   rV   s      r   r   GzipFile.close  s    ,,?
	"yyE!dmm1134((+))j"89I!%!  I!%! s   A*B/ /-Cc                     U R                  5         U R                  [        :X  aO  U R                  R	                  U R
                  R                  U5      5        U R                  R                  5         g g r+   )r~   r   ra   rh   r   r   r   )r%   	zlib_modes     r   r   GzipFile.flush  sP    99LLt}}229=>LL  r   c                 6    U R                   R                  5       $ )zInvoke the underlying file object's fileno() method.

This will raise AttributeError if the underlying file object
doesn't support fileno().
)rh   filenor3   s    r   r   GzipFile.fileno  s     ||""$$r   c                     U R                   [        :w  a  [        S5      eU R                  R	                  S5        SU l        SU l        SU l        SU l        SU l	        g)zSReturn the uncompressed stream file position indicator to the
beginning of the filezCan't rewind in write moder   Tr   N)
r   r[   r   rh   r7   r\   r]   r^   r_   r8   r3   s    r   rewindGzipFile.rewind  sR     99677!r   c                 (    U R                   [        :H  $ r+   )r   r[   r3   s    r   readableGzipFile.readable  s    yyD  r   c                 (    U R                   [        :H  $ r+   )r   ra   r3   s    r   writableGzipFile.writable  s    yyE!!r   c                     g)NTrG   r3   s    r   seekableGzipFile.seekable  s    r   c                    U(       a!  US:X  a  U R                   U-   nO[        S5      eU R                  [        :X  a  XR                   :  a  [	        S5      eXR                   -
  n[        S5      n[        US-  5       H  nU R                  U5        M     U R                  [        US-  5      5        U R                   $ U R                  [        :X  ag  XR                   :  a  U R                  5         XR                   -
  n[        US-  5       H  nU R                  S5        M     U R                  US-  5        U R                   $ )Nr	   zSeek from end not supportedzNegative seek in write moder   )r8   rT   r   ra   r   bytesr   r   r[   r   r,   )r%   r8   r9   countr   is         r   r7   GzipFile.seek  s   {v- !>??99#;<<[[(E$KE5D=)

5! *JJuUT\*+ {{ YY$#[[(E5D=)		$ *IIedl#{{r   c                 ,   US:  a  U R                   U R                  -
  nU R                  R                  SU5      S-   nUS:  a<  U =R                  X2-
  -  sl        U =R                   X2-
  -  sl         U R                  X# $ [
        R                  nU R                  nOUn/ nUS:w  a  U R                  U5      nUR                  S5      nX::  d  US:X  a  [        U5      U:  a  US-
  nUS:  d  US:X  a.  UR                  US US-    5        U R                  XcS-   S  5        O5UR                  U5        U[        U5      -
  n[        XS-  5      nUS:w  a  M  X@R                  :  a  [        X@R                  S-  S5      U l        SR                  U5      $ )Nr      
r	   rs   r   r
   i   )r8   r_   r]   findr^   sysmaxsizer`   r,   r!   appendr   r   join)r%   r-   r8   r   r   bufsr   s          r   readlineGzipFile.readline2  sm   !8[[4??2F""5&1A5A1u!*,qz)}}V//;;D((HHai		(#AuA 	qBw3q6D=1HAvcAfq1uI&Q1uvY' KKN#a&=D4A.H' ai( ''' #H.?.?!.CS IDxx~r   )r\   r   r   r   r   r]   r^   r_   rh   r`   r   ri   rV   r=   r8   r-   r   )NN	   NN)rs   )r   r@   )&rA   rB   rC   rD   rE   rV   r   r(   propertyr   rz   r~   rb   rj   r   r   r   r   r,   r   r   r   r$   r   r   r}   r   rc   Z_SYNC_FLUSHr   r   r   r   r   r   r7   r   rF   rG   r   r   r   r   j   s     I%N+/6:T&l  >>04#J*:,0* *X*** $ $"" #// !%
!"2%r   r   c                     [         R                  " 5       n[        USUS9 nUR                  U 5        SSS5        UR	                  5       $ ! , (       d  f       UR	                  5       $ = f)zyCompress data in one shot and return the compressed string.
Optional argument is the compression level, in range of 0-9.
wb)rh   r   r   N)ioBytesIOr   r   getvalue)r   r   r   r&   s       r   r   r   Z  sP     **,C	#D	F!	 
G<<> 
G	F<<>s   A
A(c                     [        [        R                  " U 5      S9 nUR                  5       sSSS5        $ ! , (       d  f       g= f)zQDecompress a gzip compressed string in one shot.
Return the decompressed string.
r   N)r   r  r  r,   )r   r&   s     r   r   r   d  s,     
"**T*	+qvvx 
,	+	+s	   8
A)rN   r   )r   )rE   rX   	six.movesr   r   r   r   r   r   rc   r  __all__FTEXTr   r   r   r   r[   ra   r   r   objectr   BufferedIOBaser   r   r   rG   r   r   <module>r     s{   %     
  	  	
8(6 %ufeXe3+5(& 5(pmr   m`r   