ó
čžbc           @   sů   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 g Z d d d d d	 f \ Z	 Z
 Z Z Z d d f \ Z Z d
   Z d   Z d d d  Z d e j f d     YZ d   Z e d k rő e   n  d S(   s   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.i˙˙˙˙Nt   GzipFilet   openi   i   i   i   i   c         C   s   |  j  t j d |   d  S(   Ns   <L(   t   writet   structt   pack(   t   outputt   value(    (    s   /usr/lib/python2.7/gzip.pyt   write32u   s    c         C   s   t  j d |  j d   d S(   Ns   <Ii   i    (   R   t   unpackt   read(   t   input(    (    s   /usr/lib/python2.7/gzip.pyt   read32   s    t   rbi	   c         C   s   t  |  | |  S(   s   Shorthand for GzipFile(filename, mode, compresslevel).

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

    (   R    (   t   filenamet   modet   compresslevel(    (    s   /usr/lib/python2.7/gzip.pyR      s    c           B   s  e  Z d  Z d Z d  Z d d d d d d  Z e d    Z d   Z	 d   Z
 d   Z d	   Z d
   Z d   Z d   Z d d  Z d   Z d d  Z d   Z d   Z e d    Z d   Z e j d  Z d   Z d   Z d   Z d   Z d   Z d d  Z d d  Z  RS(!   s   The GzipFile class simulates most of the methods of a file object with
    the exception of the readinto() and truncate() methods.

    i
   i   i	   c         C   sć  | r | j  d d  } n  | r: d | k r: | d 7} n  | d k rh t j | | pX d  } |  _ n  | d k rŽ t | d d  } t | t  s˘ | d k rŽ d } qŽ n  | d k rŢ t | d  rŐ | j	 } qŢ d } n  | d d	 !d
 k r3t
 |  _	 t |  _ d |  _ d |  _ d |  _ | |  _ d |  _ nx | d d	 !d k sY| d d	 !d k rt |  _	 |  j |  t j | t j t j t j d  |  _ n t d | d  | |  _ d |  _ | |  _ |  j	 t k râ|  j   n  d S(   sz  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, a StringIO 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', 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'.
        Be aware that only the 'rb', 'ab', and 'wb' values should be used
        for cross-platform portability.

        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   Ut    t   bR   t   names   <fdopen>R   i    i   t   rid   t   wt   as   Mode s    not supportedN(   t   replacet   Nonet   __builtin__R   t	   myfileobjt   getattrt
   isinstancet
   basestringt   hasattrR   t   READt   Truet   _new_membert   extrabuft	   extrasizet
   extrastartR   t   min_readsizet   WRITEt   _init_writet   zlibt   compressobjt   DEFLATEDt	   MAX_WBITSt   DEF_MEM_LEVELt   compresst   IOErrort   fileobjt   offsett   mtimet   _write_gzip_header(   t   selfR   R   R   R/   R1   (    (    s   /usr/lib/python2.7/gzip.pyt   __init__-   sH    *" 							&					c         C   sS   d d  l  } | j d t d  |  j t k rL |  j d d k rL |  j d S|  j S(   Ni˙˙˙˙s   use the name attributei   iý˙˙˙s   .gz(   t   warningst   warnt   DeprecationWarningR   R&   R   (   R3   R5   (    (    s   /usr/lib/python2.7/gzip.pyR      s
    "c         C   s6   t  |  j  } d | d d !d t t |    d S(   Ns   <gzip i   i˙˙˙˙t    t   >(   t   reprR/   t   hext   id(   R3   t   s(    (    s   /usr/lib/python2.7/gzip.pyt   __repr__   s    c         C   s   |  j  r t d   n  d S(   sL   Raises a ValueError if the underlying file object has been closed.

        s   I/O operation on closed file.N(   t   closedt
   ValueError(   R3   (    (    s   /usr/lib/python2.7/gzip.pyt   _check_closed   s    	c         C   s>   | |  _  t j d  d @|  _ d |  _ g  |  _ d |  _ d  S(   NR   l   ˙˙ i    (   R   R(   t   crc32t   crct   sizet   writebuft   bufsize(   R3   R   (    (    s   /usr/lib/python2.7/gzip.pyR'      s
    			c         C   s6  |  j  j d  |  j  j d  yV t j j |  j  } t | t  sY | j d  } n  | j	 d  ru | d  } n  Wn t
 k
 r d } n Xd } | rĽ t } n  |  j  j t |   |  j } | d  k rß t j   } n  t |  j  t |   |  j  j d  |  j  j d	  | r2|  j  j | d
  n  d  S(   Ns   s   s   latin-1s   .gziý˙˙˙R   i    s   s   ˙s    (   R/   R   t   ost   patht   basenameR   R   t   strt   encodet   endswitht   UnicodeEncodeErrort   FNAMEt   chrR1   R   t   timeR   t   long(   R3   t   fnamet   flagsR1   (    (    s   /usr/lib/python2.7/gzip.pyR2   Ł   s,    
		c         C   s#   t  j d  d @|  _ d |  _ d  S(   NR   l   ˙˙ i    (   R(   RB   RC   RD   (   R3   (    (    s   /usr/lib/python2.7/gzip.pyt
   _init_read˝   s    c         C   s  |  j  j d  } | d k r* t d  n  t |  j  j d   } | d k rZ t d  n  t |  j  j d   } t |  j   |  _ |  j  j d  | t @ré t |  j  j d   } | d t |  j  j d   } |  j  j |  n  | t @r,x6 t r(|  j  j d  } | s!| d k rö Pqö qö Wn  | t	 @rox6 t rk|  j  j d  } | sd| d k r9Pq9q9Wn  | t
 @r|  j  j d  n  d  S(	   Ni   s   s   Not a gzipped filei   i   s   Unknown compression methodi   s    (   R/   R	   R.   t   ordR   R1   t   FEXTRARN   R    t   FCOMMENTt   FHCRC(   R3   t   magict   methodt   flagt   xlenR=   (    (    s   /usr/lib/python2.7/gzip.pyt   _read_gzip_headerÁ   s2    
 
	
	
c         C   sô   |  j    |  j t k r: d d  l } t | j d   n  |  j d  k rU t d  n  t	 | t
  rs | j   } n  t |  d k rę |  j j |  j j |   |  j t |  7_ t j | |  j  d @|  _ |  j t |  7_ n  t |  S(   Ni˙˙˙˙s$   write() on read-only GzipFile objects!   write() on closed GzipFile objecti    l   ˙˙ (   RA   R   R&   t   errnoR.   t   EBADFR/   R   R@   R   t
   memoryviewt   tobytest   lenR   R-   RD   R(   RB   RC   R0   (   R3   t   dataR^   (    (    s   /usr/lib/python2.7/gzip.pyR   â   s    
i˙˙˙˙c         C   su  |  j    |  j t k r: d d  l } t | j d   n  |  j d k r\ |  j d  k r\ d Sd } | d k  rÂ y4 x- t	 r  |  j
 |  t |  j | d  } qt WWq.t k
 rž |  j } q.Xnl y= x6 | |  j k rý |  j
 |  t |  j | d  } qČ WWn, t k
 r-| |  j k r.|  j } q.n X|  j |  j } |  j | | | !} |  j | |  _ |  j | 7_ | S(   Ni˙˙˙˙s$   read() on write-only GzipFile objecti    R   i   i   (   RA   R   R   R^   R.   R_   R#   R/   R   R    t   _readt   mint   max_read_chunkt   EOFErrorR0   R$   R"   (   R3   RD   R^   t   readsizeR0   t   chunk(    (    s   /usr/lib/python2.7/gzip.pyR	   ÷   s4    
	c         C   s/   t  |  |  j |  _ |  j t  |  8_ d  S(   N(   Rb   R#   R0   (   R3   t   buf(    (    s   /usr/lib/python2.7/gzip.pyt   _unread  s    c         C   sl  |  j  d  k r t d  n  |  j r­ |  j  j   } |  j  j d d  | |  j  j   k rg t d  n |  j  j |  |  j   |  j   t j	 t j
  |  _ t |  _ n  |  j  j |  } | d k rý |  j j   } |  j   |  j |  t d  n  |  j j |  } |  j |  |  j j d k rh|  j  j t |  j j  d d  |  j   t |  _ n  d  S(   Ns   Reached EOFi    i   R   i   i   (   R/   R   Rg   R!   t   tellt   seekRT   R]   R(   t   decompressobjR+   t
   decompresst   FalseR	   t   flusht	   _read_eoft   _add_read_datat   unused_dataRb   R    (   R3   RD   t   posRj   t
   uncompress(    (    s   /usr/lib/python2.7/gzip.pyRd     s0    	


$
c         C   s|   t  j | |  j  d @|  _ |  j |  j } |  j | | |  _ |  j t |  |  _ |  j |  _ |  j t |  |  _ d  S(   Nl   ˙˙ (	   R(   RB   RC   R0   R$   R"   R#   Rb   RD   (   R3   Rc   R0   (    (    s   /usr/lib/python2.7/gzip.pyRs   N  s    c         C   sŇ   |  j  j d d  t |  j   } t |  j   } | |  j k rh t d t |  t |  j  f   n | |  j d @k r t d  n  d } x" | d k rą |  j  j d  } q W| rÎ |  j  j d d  n  d  S(   Niř˙˙˙i   s   CRC check failed %s != %sl   ˙˙ s!   Incorrect length of data produceds    i˙˙˙˙(   R/   Rm   R   RC   R.   R;   RD   R	   (   R3   RB   t   isizet   c(    (    s   /usr/lib/python2.7/gzip.pyRr   V  s    c         C   s   |  j  d  k S(   N(   R/   R   (   R3   (    (    s   /usr/lib/python2.7/gzip.pyR?   n  s    c         C   s   |  j  } | d  k r d  Sd  |  _  zP |  j t k rq | j |  j j    t | |  j  t | |  j	 d @ n  Wd  |  j
 } | r d  |  _
 | j   n  Xd  S(   Nl   ˙˙ (   R/   R   R   R&   R   R-   Rq   R   RC   RD   R   t   close(   R3   R/   R   (    (    s   /usr/lib/python2.7/gzip.pyRy   r  s    				c         C   sI   |  j    |  j t k rE |  j j |  j j |   |  j j   n  d  S(   N(   RA   R   R&   R/   R   R-   Rq   (   R3   t	   zlib_mode(    (    s   /usr/lib/python2.7/gzip.pyRq     s    
c         C   s   |  j  j   S(   sĽ   Invoke the underlying file object's fileno() method.

        This will raise AttributeError if the underlying file object
        doesn't support fileno().
        (   R/   t   fileno(   R3   (    (    s   /usr/lib/python2.7/gzip.pyR{     s    c         C   s_   |  j  t k r t d   n  |  j j d  t |  _ d |  _ d |  _ d |  _	 d |  _
 d S(   s[   Return the uncompressed stream file position indicator to the
        beginning of the files   Can't rewind in write modei    R   N(   R   R   R.   R/   Rm   R    R!   R"   R#   R$   R0   (   R3   (    (    s   /usr/lib/python2.7/gzip.pyt   rewind  s    				c         C   s   |  j  t k S(   N(   R   R   (   R3   (    (    s   /usr/lib/python2.7/gzip.pyt   readable  s    c         C   s   |  j  t k S(   N(   R   R&   (   R3   (    (    s   /usr/lib/python2.7/gzip.pyt   writableĄ  s    c         C   s   t  S(   N(   R    (   R3   (    (    s   /usr/lib/python2.7/gzip.pyt   seekable¤  s    i    c         C   s*  | r1 | d k r" |  j  | } q1 t d   n  |  j t k rŻ | |  j  k  r^ t d   n  | |  j  } x) t | d  D] } |  j d d  q| W|  j | d d  nt |  j t k r#| |  j  k  rÚ |  j   n  | |  j  } x% t | d  D] } |  j	 d  qř W|  j	 | d  n  |  j  S(   Ni   s   Seek from end not supporteds   Negative seek in write modei   s    (
   R0   R@   R   R&   R.   t   xrangeR   R   R|   R	   (   R3   R0   t   whencet   countt   i(    (    s   /usr/lib/python2.7/gzip.pyRm   §  s&    c         C   sŻ  | d k  r |  j  |  j } |  j j d |  d } | d k ru |  j | | 8_ |  j  | | 7_  |  j | | !St j } |  j } n | } g  } xŰ | d k rs|  j |  } | j d  } | | k sí | d k rú t	 |  | k rú | d } n  | d k s| d k r@| j
 | | d   |  j | | d  Pn  | j
 |  | t	 |  } t | | d  } q W| |  j k r˘t | |  j d d  |  _ n  d j |  S(   Ni    s   
i   i˙˙˙˙R   i   i   (   R0   R$   R"   t   findR#   t   syst   maxintR%   R	   Rb   t   appendRk   Re   t   join(   R3   RD   R0   R   Rh   t   bufsRx   (    (    s   /usr/lib/python2.7/gzip.pyt   readlineż  s4    	*Ni (  i    (!   t   __name__t
   __module__t   __doc__R   R   Rf   R4   t   propertyR   R>   RA   R'   R2   RT   R]   R   R	   Rk   Rd   Rs   Rr   R?   Ry   R(   t   Z_SYNC_FLUSHRq   R{   R|   R}   R~   R   Rm   R   (    (    (    s   /usr/lib/python2.7/gzip.pyR    $   s8   [						!	!	2								c          C   sŹ  t  j d }  |  o  |  d d k } | r6 |  d }  n  |  sH d g }  n  x]|  D]U} | rŢ | d k r t d d d d d	 t  j  } t  j } q9| d
 d k rś d Gt |  GHqO n  t | d  } t j | d
  d  } n[ | d k rt  j } t d d d d d	 t  j  } n% t j | d  } t | d d  } x0 t rk| j	 d  } | s[Pn  | j
 |  q<W| t  j k	 r| j   n  | t  j k	 rO | j   qO qO Wd  S(   Ni   i    s   -dt   -R   R   R   R   R/   iý˙˙˙s   .gzs   filename doesn't end in .gz:t   wbi   (   R   t   argvR    t   stdint   stdoutR:   R   R   R    R	   R   Ry   (   t   argsRo   t   argt   ft   gRi   (    (    s   /usr/lib/python2.7/gzip.pyt   _testç  s<    	!	t   __main__(   R   R   R   RP   RG   R(   t   ioR   t   __all__t   FTEXTRX   RV   RN   RW   R   R&   R   R   R   t   BufferedIOBaseR    R   R   (    (    (    s   /usr/lib/python2.7/gzip.pyt   <module>   s   0$			˙ Ä	&