Spade
Mini Shell
3
\nO�@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddddgZ d\Z
ZZZ
Zd\ZZddd�Zdd�ZGdd�d�ZGdd�dej�ZGdd�dej�Zddd�Zdd�Zdd�Zedkr�e�dS)z�Functions
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�GzipFile�open�compress�
decompress������rb� cCs�d|kr
d|krPtd|f��n0|dk r0td��|dk r@td��|dk rPtd��|jdd�}t|tttjf�r|t|||�}n,t|d �s�t|d
�r�td|||�}nt d��d|kr�t
j||||�S|SdS)aOpen a gzip-compressed file in binary or text
mode.
The filename argument can be an actual filename (a str or bytes
object), or
an existing file object to read from or write to.
The mode argument can be "r", "rb", "w",
"wb", "x", "xb", "a" or
"ab" for
binary mode, or "rt", "wt", "xt" or
"at" for text mode. The default mode is
"rb", and the default compresslevel is 9.
For binary mode, this function is equivalent to the GzipFile
constructor:
GzipFile(filename, mode, compresslevel). In this case, the encoding,
errors
and newline arguments must not be provided.
For text mode, a GzipFile object is created, and wrapped in an
io.TextIOWrapper instance with the specified encoding, error handling
behavior, and line ending(s).
�t�bzInvalid mode: %rNz0Argument 'encoding' not
supported in binary modez.Argument 'errors' not supported in
binary modez/Argument 'newline' not supported in binary
mode��read�writez1filename must be a str or bytes object, or a
file)�
ValueError�replace�
isinstance�str�bytes�os�PathLiker�hasattr� TypeError�io�
TextIOWrapper)�filename�mode�
compresslevel�encoding�errors�newlineZgz_modeZbinary_file�r#�/usr/lib64/python3.6/gzip.pyrs$cCs|jtjd|��dS)Nz<L)r�struct�pack)�output�valuer#r#r$�write32u@sr)c@s<eZdZdZddd�Zdd�Zddd�Zd d
�Zdd�Zd
S)�_PaddedFilez�Minimal 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.�cCs
||_t|�|_||_d|_dS)Nr)�_buffer�len�_length�file�_read)�self�f�prependr#r#r$�__init__Js
z_PaddedFile.__init__cCs~|jdkr|jj|�S|j||jkrJ|j}|j|7_|j||j�S|j}d|_|j|d�|jj||j|�SdS)N)r0r/rr.r,)r1�sizerr#r#r$rPs
z_PaddedFile.readcCs>|jdkr||_n|jt|�8_dSt|j�|_d|_dS)Nr)r0r,r-r.)r1r3r#r#r$r3]s
z_PaddedFile.prependcCsd|_d|_|jj|�S)N)r0r,r/�seek)r1Zoffr#r#r$r6fsz_PaddedFile.seekcCsdS)NTr#)r1r#r#r$�seekableksz_PaddedFile.seekableN)r+)r+) �__name__�
__module__�__qualname__�__doc__r4rr3r6r7r#r#r#r$r*Es
r*c@s�eZdZdZdZd-dd�Zedd��Zedd ��Zd
d�Z dd
�Z
dd�Zdd�Zd/dd�Z
d1dd�Zdd�Zedd��Zdd�Zejfdd�Zdd
�Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zejfd)d*�Zd3d+d,�ZdS)4ra
The GzipFile class simulates most of the methods of a file object with
the exception of the truncate() method.
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.
NrcCs2|r"d|ksd|kr"tdj|���|r6d|kr6|d7}|dkrTtj||pJd�}|_|dkr|t|dd�}t|ttf�s�d}n
t j
|�}|dkr�t|d d�}|jd
�r�t|_
t|�}tj|�|_||_nN|jd��rt|_
|j|�tj|tjtjtjd�|_||_ntdj|���||_|j
tk�r.|j�dS)aGConstructor
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 include 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', 'wb',
'x', or
'xb' 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', 'a' and 'ab', and 'x'
and 'xb'.
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 last modification time field in the stream when compressing.
If omitted or None, the current time is used.
r
�UzInvalid mode:
{!r}rNr�namerr�r�w�a�xr)r?r@rA)r�format�builtinsr� myfileobj�getattrrrrr�fspath�
startswith�READr�_GzipReaderr�BufferedReaderr,r=�WRITE�_init_write�zlibZcompressobjZDEFLATED� MAX_WBITSZ
DEF_MEM_LEVELr�_write_mtime�fileobj�_write_gzip_header)r1rrrrP�mtime�rawr#r#r$r4{s>#
zGzipFile.__init__cCsBddl}|jdtd�|jtkr<|jdd�dkr<|jdS|jS)Nrzuse
the name
attributer�z.gz���)�warnings�warn�DeprecationWarningrrKr=)r1rVr#r#r$r�s
zGzipFile.filenamecCs
|jjjS)z0Last modification time read from stream, or
None)r,rS�_last_mtime)r1r#r#r$rR�szGzipFile.mtimecCs.t|j�}d|dd�dtt|��dS)Nz<gzip
r�
�>���)�reprrP�hex�id)r1�sr#r#r$�__repr__�s
zGzipFile.__repr__cCs.||_tjd�|_d|_g|_d|_d|_dS)Nr+r)r=rM�crc32�crcr5Zwritebuf�bufsize�offset)r1rr#r#r$rL�szGzipFile._init_writecCs�|jjd�|jjd�y<tjj|j�}t|t�s<|jd�}|j d�rR|dd�}Wnt
k
rld}YnXd}|rzt}|jjt|�jd��|j
}|dkr�tj�}t|jt|��|jjd�|jjd �|r�|jj|d
�dS)Ns��zlatin-1s.gzrTr+r����rU)rPrr�path�basenamer=rr�encode�endswith�UnicodeEncodeError�FNAME�chrrO�timer)�int)r1Zfname�flagsrRr#r#r$rQ�s,
zGzipFile._write_gzip_headercCs�|j�|jtkr&ddl}t|jd��|jdkr8td��t|t �rLt
|�}nt|�}|j}|dkr�|jj
|jj|��|j|7_tj||j�|_|j|7_|S)Nrz$write()
on read-only GzipFile objectz!write() on closed GzipFile
object)�_check_not_closedrrK�errno�OSError�EBADFrPrrrr-�
memoryview�nbytesrrr5rMrbrcre)r1�dataruZlengthr#r#r$r�s
zGzipFile.writercCs2|j�|jtkr&ddl}t|jd��|jj|�S)Nrz$read()
on write-only GzipFile object)rtrrHrurvrwr,r)r1r5rur#r#r$rs
z
GzipFile.readcCs@|j�|jtkr&ddl}t|jd��|dkr4tj}|jj |�S)zdImplements
BufferedIOBase.read1()
Reads up to a buffer's worth of data is size is
negative.rNz%read1() on write-only GzipFile object)
rtrrHrurvrwr�DEFAULT_BUFFER_SIZEr,�read1)r1r5rur#r#r$r|s
zGzipFile.read1cCs2|j�|jtkr&ddl}t|jd��|jj|�S)Nrz$peek()
on write-only GzipFile object)rtrrHrurvrwr,�peek)r1�nrur#r#r$r}#s
z
GzipFile.peekcCs
|jdkS)N)rP)r1r#r#r$�closed*szGzipFile.closedcCs�|j}|dkrdSd|_zP|jtkrR|j|jj��t||j�t||jd@�n|jt krf|j
j�Wd|j}|r�d|_|j�XdS)Nl��)
rPrrKrr�flushr)rcr5rHr,�closerD)r1rPrDr#r#r$r�.s
zGzipFile.closecCs4|j�|jtkr0|jj|jj|��|jj�dS)N)rtrrKrPrrr�)r1Z zlib_moder#r#r$r�As
zGzipFile.flushcCs
|jj�S)z�Invoke the underlying file object's fileno() method.
This will raise AttributeError if the underlying file object
doesn't support fileno().
)rP�fileno)r1r#r#r$r�HszGzipFile.filenocCs"|jtkrtd��|jjd�dS)z[Return
the uncompressed stream file position indicator to the
beginning of the filezCan't rewind in write
moderN)rrHrvr,r6)r1r#r#r$�rewindPs
zGzipFile.rewindcCs
|jtkS)N)rrH)r1r#r#r$�readableWszGzipFile.readablecCs
|jtkS)N)rrK)r1r#r#r$�writableZszGzipFile.writablecCsdS)NTr#)r1r#r#r$r7]szGzipFile.seekablecCs�|jtkr�|tjkr2|tjkr*|j|}ntd��||jkrDtd��||j}dd}xt|d�D]}|j |�qdW|j d|d�n
|jt
kr�|j�|jj
||�S|jS)NzSeek from end not supportedzNegative
seek in write
moderii)rrKr�SEEK_SET�SEEK_CURrerrv�rangerrHrtr,r6)r1re�whence�count�chunk�ir#r#r$r6`s
z
GzipFile.seekcCs|j�|jj|�S)N)rtr,�readline)r1r5r#r#r$r�tszGzipFile.readline)NNrNNr\)r\r\)r\r\)r\)r8r9r:r;rDr4�propertyrrRrarLrQrrr|r}rr�rMZZ_SYNC_FLUSHr�r�r�r�r�r7rr�r6r�r#r#r#r$rns.
H
csZeZdZ�fdd�Zdd�Zdd�Zdd�Zdd
d�Zdd
�Zdd�Z �fdd�Z
�ZS)rIcs,t�jt|�tjtjd�d|_d|_dS)N)ZwbitsT)�superr4r*rMZ
decompressobjrN�_new_memberrY)r1�fp)� __class__r#r$r4zsz_GzipReader.__init__cCstjd�|_d|_dS)Nr+r)rMrb�_crc�_stream_size)r1r#r#r$�
_init_read�sz_GzipReader._init_readcCsJ|jj|�}x8t|�|krD|jj|t|��}|s:td��||7}qW|S)z�Read
exactly *n* bytes from `self._fp`
This method is required because self._fp may be unbuffered,
i.e. return short reads.
zACompressed file ended before the end-of-stream marker was
reached)�_fprr-�EOFError)r1r~rzrr#r#r$�_read_exact�sz_GzipReader._read_exactcCs�|jjd�}|dkrdS|dkr,td|��tjd|jd��\}}|_|dkrVtd��|t@r|tjd |jd��\}|j|�|t@r�x
|jjd
�}|s�|dkr�Pq�W|t @r�x |jjd
�}|s�|dkr�Pq�W|t
@r�|jd�dS)
Nrr+Fs�zNot a gzipped file
(%r)z<BBIxxr zUnknown compression
methodz<HrriT)r�rrvr%�unpackr�rY�FEXTRAro�FCOMMENT�FHCRC)r1�magic�method�flagZ extra_lenr`r#r#r$�_read_gzip_header�s0
z_GzipReader._read_gzip_headerrcCs�|dkr|j�S|sdSx�|jjr@|j�d|_|jf|j�|_|jrh|j�|j�sb|j |_
dSd|_|jjt
j�}|jj||�}|jjdkr�|jj|jj�n|jjdkr�|jj|jj�|dkr�P|dkrtd��qW|j|�|j t|�7_ |S)Nrr+TFzACompressed
file ended before the end-of-stream marker was
reached)�readallZ
_decompressor�eof� _read_eofr�Z_decomp_factoryZ_decomp_argsr�r�Z_posZ_sizer�rrr{rZunconsumed_tailr3Zunused_datar��_add_read_datar-)r1r5�bufZ
uncompressr#r#r$r�s:
z_GzipReader.readcCs$tj||j�|_|jt|�|_dS)N)rMrbr�r�r-)r1rzr#r#r$r��sz_GzipReader._add_read_datacCs�tjd|jd��\}}||jkr<tdt|�t|j�f��n||jd@krRtd��d}x|dkrn|jjd�}qXW|r�|jj |�dS)Nz<IIr zCRC
check failed %s != %sl��z!Incorrect length of data producedrir)
r%r�r�r�rvr^r�r�rr3)r1rbZisize�cr#r#r$r��s
z_GzipReader._read_eofcst�j�d|_dS)NT)r��_rewindr�)r1)r�r#r$r�s
z_GzipReader._rewindr\)r\)r8r9r:r4r�r�r�rr�r�r��
__classcell__r#r#)r�r$rIys!
3rIc Cs4tj�}t|d|d��}|j|�WdQRX|j�S)z�Compress
data in one shot and return the compressed string.
Optional argument is the compression level, in range of 0-9.
�wb)rPrrN)r�BytesIOrr�getvalue)rzrr�r2r#r#r$rsc Cs$ttj|�d��
}|j�SQRXdS)zYDecompress a gzip compressed string in one shot.
Return the decompressed string.
)rPN)rrr�r)rzr2r#r#r$rscCs>tjdd�}|o|ddk}|r.|dd�}|s8dg}�x�|D]�}|r�|dkrltddtjjd�}tjj}q�|d
d�d kr�td
t|��q@t|d�}t j|dd�d�}n>|dkr�tjj}tddtjjd�}nt j|d�}t|d d�}x
|j
d�}|�s�P|j|�q�W|tjjk �r"|j�|tjjk r@|j�q@WdS)Nrrz-d�-rr)rrrPrTz.gzzfilename
doesn't end in
.gz:r�irUrU)
�sys�argvr�stdin�buffer�stdout�printr]rrCrrr�)�argsr�argr2�gr�r#r#r$�_tests<
r��__main__)rrrr r
)rr)rrNNN)r)r;r%r�rqrrMrCrZ_compression�__all__ZFTEXTr�r�ror�rHrKrr)r*Z
BaseStreamrZDecompressReaderrIrrr�r8r#r#r#r$�<module>s,
+)
&