ó
g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 g Z d d d d d	 f \ Z Z Z	 Z
 Z d d f \ Z Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d „ Z d 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.

This consisted on a patched version of of standard gzip python
module based on Andrew Kuchling's minigzip.py distributed with the zlib module

iÿÿÿÿNt   GzipFilet   openi   i   i   i   i   c         C   s   |  d k  r |  d 7}  n  |  S(   s‹   Return i as an unsigned integer, assuming it fits in 32 bits.

    If it's >= 2GB when viewed as a 32-bit unsigned int, return a long.
    i    l    i    l        (    (   t   i(    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   U32   s    c         C   s   |  d @S(   s>   Return the low-order 32 bits of an int, as a non-negative int.l   ÿÿ (    (   R   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   LOWU32    s    c         C   s   |  j  t j d | ƒ ƒ d  S(   Ns   <l(   t   writet   structt   pack(   t   outputt   value(    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   write32$   s    c         C   s   |  j  t j d | ƒ ƒ d  S(   Ns   <L(   R   R   R   (   R   R	   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   write32u'   s    c         C   s   t  j d |  j d ƒ ƒ d S(   Ns   <li   i    (   R   t   unpackt   read(   t   input(    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   read32,   s    c         C   s   t  j d |  ƒ d S(   Ns   <li    (   R   R   (   t   buf(    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   unpack32/   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,   lib/python2.7/site-packages/obitools/gzip.pyR   2   s    c           B   s"  e  Z d  Z d  Z d" Z d  d  d d  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 d „  Z d „  Z e j d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d d „ Z d „  Z d „  Z  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 d | k r | d 7} n  | d k rM t j | | p= d ƒ } |  _ n  | d k r} t | d ƒ rt | j } q} d } n  | d k r­ t | d ƒ r¤ | j } q­ d } n  | d d !d k rù t |  _ t |  _	 d |  _
 d |  _ | |  _ d	 |  _ nx | d d !d
 k s| d d !d k r`t |  _ |  j | ƒ t j | t j t j t j d ƒ |  _ n t d | d ‚ | |  _ d |  _ d |  _ d |  _ |  j t k r±|  j ƒ  n  d S(   sA  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 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'.
        Be aware that only the 'rb', 'ab', and 'wb' values should be used
        for cross-platform portability.

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

        t   bR   t   namet    R   i    i   t   rid   t   wt   as   Mode s    not supportedN(   t   Nonet   __builtin__R   t	   myfileobjt   hasattrR   R   t   READt   Truet   _new_membert   extrabuft	   extrasizeR   t   min_readsizet   WRITEt   _init_writet   zlibt   compressobjt   DEFLATEDt	   MAX_WBITSt   DEF_MEM_LEVELt   compresst   IOErrort   fileobjt   offsett   inputbuft   last8t   _write_gzip_header(   t   selfR   R   R   R/   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   __init__D   sD     " 	 						&						c         C   s6   t  |  j ƒ } d | d d !d t t |  ƒ ƒ d S(   Ns   <gzip i   iÿÿÿÿt    t   >(   t   reprR/   t   hext   id(   R4   t   s(    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   __repr__Œ   s    c         C   sW   | d d k r | d } n  | |  _  t j d ƒ |  _ d |  _ g  |  _ d |  _ d  S(   Niýÿÿÿs   .gzR   i    (   R   R(   t   crc32t   crct   sizet   writebuft   bufsize(   R4   R   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyR'      s    			c         C   sµ   |  j  j d ƒ |  j  j d ƒ |  j d  } d } | rB t } n  |  j  j t | ƒ ƒ t |  j  t t j ƒ  ƒ ƒ |  j  j d ƒ |  j  j d ƒ | r± |  j  j | d ƒ n  d  S(   Ns   ‹s   iýÿÿÿi    s   s   ÿs    (   R/   R   R   t   FNAMEt   chrR   t   longt   time(   R4   t   fnamet   flags(    (    s,   lib/python2.7/site-packages/obitools/gzip.pyR3   ™   s    	c         C   s   t  j d ƒ |  _ d |  _ d  S(   NR   i    (   R(   R=   R>   R?   (   R4   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt
   _init_read§   s    c         C   s   t  |  j ƒ | k  r@ |  j |  j j | t  |  j ƒ ƒ 7_ n  |  j |  } t  | ƒ d k  r| |  j t  | ƒ | |  _ n | d |  _ |  j | |  _ | S(   Ni   iøÿÿÿ(   t   lenR1   R/   R   R2   (   R4   R?   t   chunk(    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   _read_internal«   s    +c         C   s~  |  j  d ƒ } t | ƒ d k r- t d ‚ n  | d k rE t d ‚ n  t |  j  d ƒ ƒ } | d k rr t d ‚ n  t |  j  d ƒ ƒ } |  j  d ƒ | t @rà t |  j  d ƒ ƒ } | d	 t |  j  d ƒ ƒ } |  j  | ƒ n  | t @r x3 t r|  j  d ƒ } | s| d
 k rí Pqí qí Wn  | t @r`x3 t r\|  j  d ƒ } | sU| d
 k r-Pq-q-Wn  | t	 @rz|  j  d ƒ n  d  S(   Ni   s   Reached EOFs   ‹s   Not a gzipped filei   i   s   Unknown compression methodi   i   s    (
   RK   RI   t   EOFErrorR.   t   ordt   FEXTRARB   R!   t   FCOMMENTt   FHCRC(   R4   t   magict   methodt   flagt   xlenR;   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   _read_gzip_header·   s4    

	
	
c         C   sÃ   |  j  t k r0 d d  l } t | j d ƒ ‚ n  |  j d  k rK t d ‚ n  t | ƒ d k r¿ |  j	 t | ƒ |  _	 t
 j | |  j ƒ |  _ |  j j |  j j | ƒ ƒ |  j t | ƒ 7_ n  d  S(   Niÿÿÿÿs$   write() on read-only GzipFile objects!   write() on closed GzipFile objecti    (   R   R&   t   errnoR.   t   EBADFR/   R   t
   ValueErrorRI   R?   R(   R=   R>   R   R-   R0   (   R4   t   dataRV   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyR   Û   s    iÿÿÿÿc         C   sd  |  j  t k r0 d d  l } t | j d ƒ ‚ n  |  j d k rR |  j d  k rR d Sd } | d k  r¸ y4 x- t r– |  j	 | ƒ t
 |  j | d ƒ } qj 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 | 7_ | S(   Niÿÿÿÿs$   read() on write-only GzipFile objecti    R   i   i   (   R   R    RV   R.   RW   R$   R/   R   R!   t   _readt   mint   max_read_chunkRL   R#   R0   (   R4   R?   RV   t   readsizeRJ   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyR   è   s2    	c         C   s?   | |  j  |  _  t | ƒ |  j |  _ |  j t | ƒ 8_ d  S(   N(   R#   RI   R$   R0   (   R4   R   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   _unread  s    c         C   s  |  j  d  k r t d ‚ n  |  j rZ |  j ƒ  |  j ƒ  t j t j ƒ |  _	 t
 |  _ n  |  j | ƒ } | d k r§ |  j	 j ƒ  } |  j ƒ  |  j | ƒ t d ‚ n  |  j	 j	 | ƒ } |  j | ƒ |  j	 j d k r|  j	 j |  j |  _ |  j d ƒ |  j ƒ  t |  _ n  d  S(   Ns   Reached EOFR   i   (   R/   R   RL   R"   RH   RU   R(   t   decompressobjR+   t
   decompresst   FalseRK   t   flusht	   _read_eoft   _add_read_datat   unused_dataR1   R!   (   R4   R?   R   t
   uncompress(    (    s,   lib/python2.7/site-packages/obitools/gzip.pyRZ     s(    	



c         C   sX   t  j | |  j ƒ |  _ |  j | |  _ |  j t | ƒ |  _ |  j t | ƒ |  _ d  S(   N(   R(   R=   R>   R#   R$   RI   R?   (   R4   RY   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyRd   6  s    c         C   sx   t  |  j d  ƒ } t t  |  j d ƒ ƒ } t | ƒ t |  j ƒ k rS t d ‚ n! | t |  j ƒ k rt t d ‚ n  d  S(   Ni   s   CRC check faileds!   Incorrect length of data produced(   R   R2   R   R>   R.   R   R?   (   R4   R=   t   isize(    (    s,   lib/python2.7/site-packages/obitools/gzip.pyRc   <  s    c         C   s§   |  j  t k rf |  j j |  j j ƒ  ƒ t |  j t |  j ƒ ƒ t |  j t |  j	 ƒ ƒ d  |  _ n |  j  t k r d  |  _ n  |  j r£ |  j j ƒ  d  |  _ n  d  S(   N(   R   R&   R/   R   R-   Rb   R   R   R>   R?   R   R    R   t   close(   R4   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyRh   I  s    	c         C   sI   y& |  j  d  k r% |  j d  k r% d  SWn t k
 r: d  SX|  j ƒ  d  S(   N(   R   R   R/   t   AttributeErrorRh   (   R4   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   __del__\  s    c         C   s?   |  j  t k r. |  j j |  j j | ƒ ƒ n  |  j j ƒ  d  S(   N(   R   R&   R/   R   R-   Rb   (   R4   t	   zlib_mode(    (    s,   lib/python2.7/site-packages/obitools/gzip.pyRb   e  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(   R4   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyRl   k  s    c         C   s   t  S(   N(   Ra   (   R4   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   isattys  s    c         C   s   |  j  S(   N(   R0   (   R4   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   tellv  s    c         C   sV   |  j  t k r t d ƒ ‚ n  |  j j d ƒ t |  _ 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/   t   seekR!   R"   R#   R$   R0   (   R4   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   rewindy  s    			c         C   sö   |  j  t k r~ | |  j k  r- t d ƒ ‚ n  | |  j } x) t | d ƒ D] } |  j d d ƒ qK 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  d  S(   Ns   Negative seek in write modei   s    (	   R   R&   R0   R.   t   rangeR   R    Rp   R   (   R4   R0   t   countR   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyRo   „  s    c         C   sF  | d k  r! t  j } |  j } n | } g  } xÛ | d k r
|  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 ƒ } q0 W| |  j k r9t | |  j d d ƒ |  _ n  d j	 | ƒ S(   Ni    s   
iÿÿÿÿi   R   i   i   (
   t   syst   maxintR%   R   t   findRI   t   appendR^   R[   t   join(   R4   R?   R]   t   bufst   cR   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   readline•  s(    	*i    c         C   sn   | d k r t  j } n  g  } xI | d k ri |  j ƒ  } | d k rI Pn  | j | ƒ | t | ƒ } q! W| S(   Ni    R   (   Rs   Rt   Rz   Rv   RI   (   R4   t   sizehintt   Lt   line(    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt	   readlines´  s    c         C   s"   x | D] } |  j  | ƒ q Wd  S(   N(   R   (   R4   R|   R}   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt
   writelinesÂ  s    c         C   s   |  S(   N(    (   R4   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   __iter__Æ  s    c         C   s    |  j  ƒ  } | r | St ‚ d  S(   N(   Rz   t   StopIteration(   R4   R}   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   nextÉ  s    Ni (  i    ("   t   __name__t
   __module__t   __doc__R   R   R\   R5   R<   R'   R3   RH   RK   RU   R   R   R^   RZ   Rd   Rc   Rh   Rj   R(   t   Z_SYNC_FLUSHRb   Rl   Rm   Rn   Rp   Ro   Rz   R~   R   R€   R‚   (    (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyR    ;   s<   G							$	 	)												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   (   Rs   t   argvR    t   stdint   stdoutR8   R   R   R!   R   R   Rh   (   t   argsR`   t   argt   ft   gRJ   (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   _testÑ  s<    	!	t   __main__(   R…   R   Rs   RE   R(   R   t   __all__t   FTEXTRP   RN   RB   RO   R    R&   R   R   R
   R   R   R   R   R    R   Rƒ   (    (    (    s,   lib/python2.7/site-packages/obitools/gzip.pyt   <module>	   s$   $$								ÿ —	&