ó
å™xTc           @   s�  d  Z  d d l m Z d Z d Z d Z d Z d Z d Z d d	 l	 Z	 d d	 l
 Z
 d
 Z d Z d Z d Z d „  Z d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d  „  Z d! d" „ Z d! d# „ Z d$ „  Z d	 d% „ Z d& „  Z  d' „  Z! d( „  Z" d	 S()   s°  
A stream friendly, simple compression library, built around
iterators. See L{compress} and L{decompress} for the easiest way to
get started.

After the TIFF implementation of LZW, as described at
U{http://www.fileformat.info/format/tiff/corion-lzw.htm}


In an even-nuttier-shell, lzw compresses input bytes with integer
codes. Starting with codes 0-255 that code to themselves, and two
control codes, we work our way through a stream of bytes. When we
encounter a pair of codes c1,c2 we add another entry to our code table
with the lowest available code and the value value(c1) + value(c2)[0]

Of course, there are details :)

The Details
===========

    Our control codes are

        - CLEAR_CODE (codepoint 256). When this code is encountered, we flush
          the codebook and start over.
        - END_OF_INFO_CODE (codepoint 257). This code is reserved for
          encoder/decoders over the integer codepoint stream (like the
          mechanical bit that unpacks bits into codepoints)

    When dealing with bytes, codes are emitted as variable
    length bit strings packed into the stream of bytes.

    codepoints are written with varying length
        - initially 9 bits
        - at 512 entries 10 bits
        - at 1025 entries at 11 bits
        - at 2048 entries 12 bits
        - with max of 4095 entries in a table (including Clear and EOI)

    code points are stored with their MSB in the most significant bit
    available in the output character.

>>> import lzw
>>>
>>> mybytes = lzw.readbytes("README.txt")
>>> lessbytes = lzw.compress(mybytes)
>>> newbytes = b"".join(lzw.decompress(lessbytes))
>>> oldbytes = b"".join(lzw.readbytes("README.txt"))
>>> oldbytes == newbytes
True


iÿÿÿÿ(   t   divisions
   Joe Bowerss   MIT Licenses   0.01.01t   Developments   joerbowers@gmail.coms$   http://www.joe-bowers.com/static/lzwNi   i  i	   i   c         C   s   t  ƒ  } | j |  ƒ S(   s  
    Given an iterable of bytes, returns a (hopefully shorter) iterable
    of bytes that you can store in a file or pass over the network or
    what-have-you, and later use to get back your original bytes with
    L{decompress}. This is the best place to start using this module.
    (   t   ByteEncodert   encodetobytes(   t   plaintext_bytest   encoder(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyt   compressK   s    	c         C   s   t  ƒ  } | j |  ƒ S(   sˆ   
    Given an iterable of bytes that were the result of a call to
    L{compress}, returns an iterator over the uncompressed bytes.
    (   t   ByteDecodert   decodefrombytes(   t   compressed_bytest   decoder(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyt
   decompressV   s    	R   c           B   s#   e  Z d  Z e d „ Z d „  Z RS(   s¹  
    Takes a stream of uncompressed bytes and produces a stream of
    compressed bytes, usable by L{ByteDecoder}. Combines an L{Encoder}
    with a L{BitPacker}.


    >>> import lzw
    >>>
    >>> enc = lzw.ByteEncoder(12)
    >>> bigstr = b"gabba gabba yo gabba gabba gabba yo gabba gabba gabba yo gabba gabba gabba yo"
    >>> encoding = enc.encodetobytes(bigstr)
    >>> encoded = b"".join( b for b in encoding )
    >>> encoded
    '3\x98LF#\x08\x82\x05\x04\x83\x1eM\xf0x\x1c\x16\x1b\t\x88C\xe1q(4"\x1f\x17\x85C#1X\xec.\x00'
    >>>
    >>> dec = lzw.ByteDecoder()
    >>> decoding = dec.decodefrombytes(encoded)
    >>> decoded = b"".join(decoding)
    >>> decoded == bigstr
    True

    c         C   s5   t  d d | ƒ |  _ t d |  j j ƒ  ƒ |  _ d S(   sp   
       max_width is the maximum width in bits we want to see in the
       output stream of codepoints.
       t   max_code_sizei   t   initial_code_sizeN(   t   Encodert   _encodert	   BitPackert	   code_sizet   _packer(   t   selft	   max_width(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyt   __init__z   s    c         C   s(   |  j  j | ƒ } |  j j | ƒ } | S(   sÏ   
        Returns an iterator of bytes, adjusting our packed width
        between minwidth and maxwidth when it detects an overflow is
        about to occur. Dual of L{ByteDecoder.decodefrombytes}.
        (   R   t   encodeR   t   pack(   R   t
   bytesourcet
   codepointst	   codebytes(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   ƒ   s    (   t   __name__t
   __module__t   __doc__t   DEFAULT_MAX_BITSR   R   (    (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   b   s   	R   c           B   s    e  Z d  Z d „  Z d „  Z RS(   s¼   
    Decodes, combines bit-unpacking and interpreting a codepoint
    stream, suitable for use with bytes generated by
    L{ByteEncoder}.

    See L{ByteDecoder} for a usage example.
    c         C   s4   t  ƒ  |  _ t d |  j j ƒ  ƒ |  _ g  |  _ d S(   s   
       R   N(   t   Decodert   _decodert   BitUnpackerR   t	   _unpackert	   remaining(   R   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   —   s    c         C   s(   |  j  j | ƒ } |  j j | ƒ } | S(   sÖ   
       Given an iterator over BitPacked, Encoded bytes, Returns an
       iterator over the uncompressed bytes. Dual of
       L{ByteEncoder.encodetobytes}. See L{ByteEncoder} for an
       example of use.
       (   R"   t   unpackR    t   decode(   R   R   R   t
   clearbytes(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   Ÿ   s    (   R   R   R   R   R   (    (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   �   s   	R   c           B   s    e  Z d  Z d „  Z d „  Z RS(   s¸  
    Translates a stream of lzw codepoints into a variable width packed
    stream of bytes, for use by L{BitUnpacker}.  One of a (potential)
    set of encoders for a stream of LZW codepoints, intended to behave
    as closely to the TIFF variable-width encoding scheme as closely
    as possible.

    The inbound stream of integer lzw codepoints are packed into
    variable width bit fields, starting at the smallest number of bits
    it can and then increasing the bit width as it anticipates the LZW
    code size growing to overflow.

    This class knows all kinds of intimate things about how it's
    upstream codepoint processors work; it knows the control codes
    CLEAR_CODE and END_OF_INFO_CODE, and (more intimately still), it
    makes assumptions about the rate of growth of it's consumer's
    codebook. This is ok, as long as the underlying encoder/decoders
    don't know any intimate details about their BitPackers/Unpackers
    c         C   s   | |  _  d S(   s‰   
       Takes an initial code book size (that is, the count of known
       codes at the beginning of encoding, or after a clear)
       N(   t   _initial_code_size(   R   R   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   Á   s    c         c   sz  g  } |  j  } d } x d | >| k  r5 | d } q W| } x | D]ø } t | | ƒ } | | } | d } | t k rŸ x$ t | ƒ d r› | j d ƒ q{ Wn  | t t g k rÃ | } |  j  } n | d | k rà | d } n  xX t | ƒ d k r:| d  } t | ƒ }	 x |	 D] }
 t j d |
 ƒ VqW| d } qã WqC W| rvt | ƒ } x" | D] }
 t j d |
 ƒ VqXWn  d S(   sŸ  
        Given an iterator of integer codepoints, returns an iterator
        over bytes containing the codepoints packed into varying
        lengths, with bit width growing to accomodate an input code
        that it assumes will grow by one entry per codepoint seen.

        Widths will be reset to the given initial_code_size when the
        LZW CLEAR_CODE or END_OF_INFO_CODE code appears in the input,
        and bytes following END_OF_INFO_CODE will be aligned to the
        next byte boundary.

        >>> import lzw
        >>> pkr = lzw.BitPacker(258)
        >>> [ b for b in pkr.pack([ 1, 257]) ] == [ chr(0), chr(0xC0), chr(0x40) ]
        True
        i   i   i    i   t   BN(	   R'   t	   inttobitst   END_OF_INFO_CODEt   lent   appendt
   CLEAR_CODEt   bitstobytest   structR   (   R   R   t   tailbitst   codesizet   minwidtht	   nextwidtht   ptt   newbitst   nextbitst	   nextbytest   btt   tail(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   É   s8    	


(   R   R   R   R   R   (    (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   ¬   s   	R!   c           B   s    e  Z d  Z d „  Z d „  Z RS(   sÁ   
    An adaptive-width bit unpacker, intended to decode streams written
    by L{BitPacker} into integer codepoints. Like L{BitPacker}, knows
    about code size changes and control codes.
    c         C   s   | |  _  d S(   sx   
       initial_code_size is the starting size of the codebook
       associated with the to-be-unpacked stream.
       N(   R'   (   R   R   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR     s    c   
      c   s2  g  } d } d } |  j  } d } x d | >| k  rA | d } q$ W| } xã t | ƒ D]Õ } | d d } | d k r… | d } qU n  | j | ƒ t | ƒ | k rU t | ƒ }	 g  } |	 V| d } |	 t t g k ré |  j  } | } n! x | d | k r	| d } qì W|	 t k r*d | d } q*qU qU Wd S(   s'  
        Given an iterator of bytes, returns an iterator of integer
        code points. Auto-magically adjusts point width when it sees
        an almost-overflow in the input stream, or an LZW CLEAR_CODE
        or END_OF_INFO_CODE

        Trailing bits at the end of the given iterator, after the last
        codepoint, will be dropped on the floor.

        At the end of the iteration, or when an END_OF_INFO_CODE seen
        the unpacker will ignore the bits after the code until it
        reaches the next aligned byte. END_OF_INFO_CODE will *not*
        stop the generator, just reset the alignment and the width


        >>> import lzw
        >>> unpk = lzw.BitUnpacker(initial_code_size=258)
        >>> [ i for i in unpk.unpack([ chr(0), chr(0xC0), chr(0x40) ]) ]
        [1, 257]
        i    i   i   i   N(   R'   t   bytestobitsR,   R+   t   intfrombitsR-   R*   (
   R   R   t   bitst   offsett   ignoreR1   R2   t
   pointwidtht   nextbitt	   codepoint(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR$     s4    	

		(   R   R   R   R   R$   (    (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR!     s   	R   c           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s  
    Uncompresses a stream of lzw code points, as created by
    L{Encoder}. Given a list of integer code points, with all
    unpacking foolishness complete, turns that list of codepoints into
    a list of uncompressed bytes. See L{BitUnpacker} for what this
    doesn't do.
    c         C   s   |  j  ƒ  g  |  _ d S(   sb   
       Creates a new Decoder. Decoders should not be reused for
       different streams.
       N(   t   _clear_codest	   remainder(   R   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   ]  s    
c         C   s   t  |  j ƒ S(   sù   
       Returns the current size of the Decoder's code book, that is,
       it's mapping of codepoints to byte strings. The return value of
       this method will change as the decode encounters more encoded
       input, or control codes.
       (   R+   t   _codepoints(   R   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   f  s    c         c   sS   g  | D] } | ^ q } x3 | D]+ } |  j  | ƒ } x | D] } | Vq< Wq  Wd S(   sÎ  
        Given an iterable of integer codepoints, yields the
        corresponding bytes, one at a time, as byte strings of length
        E{1}. Retains the state of the codebook from call to call, so
        if you have another stream, you'll likely need another
        decoder!

        Decoders will NOT handle END_OF_INFO_CODE (rather, they will
        handle the code by throwing an exception); END_OF_INFO should
        be handled by the upstream codepoint generator (see
        L{BitUnpacker}, for example)

        >>> import lzw
        >>> dec = lzw.Decoder()
        >>> ''.join(dec.decode([103, 97, 98, 98, 97, 32, 258, 260, 262, 121, 111, 263, 259, 261, 256]))
        'gabba gabba yo gabba'

        N(   t   _decode_codepoint(   R   R   t   cpt   decodedt	   character(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR%   p  s
    c         C   sÃ   d } | t  k r |  j ƒ  n  | t k r: t d ƒ ‚ n… | |  j k rŒ |  j | } d |  j k r¶ |  j | d |  j t |  j ƒ <q¶ n* |  j |  j d } | |  j t |  j ƒ <| |  _ | S(   s!  
        Will raise a ValueError if given an END_OF_INFORMATION
        code. EOI codes should be handled by callers if they're
        present in our source stream.

        >>> import lzw
        >>> dec = lzw.Decoder()
        >>> beforesize = dec.code_size()
        >>> dec._decode_codepoint(0x80)
        '\x80'
        >>> dec._decode_codepoint(0x81)
        '\x81'
        >>> beforesize + 1 == dec.code_size()
        True
        >>> dec._decode_codepoint(256)
        ''
        >>> beforesize == dec.code_size()
        True
        t    s>   End of information code not supported directly by this Decoderi    N(   R-   RB   R*   t
   ValueErrorRD   t   Nonet   _prefixR+   (   R   RA   t   ret(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyRE   Œ  s    '	c         C   sa   t  g  t d ƒ D] } | t j d | ƒ f ^ q ƒ |  _ t |  j t <t |  j t <d  |  _ d  S(   Ni   R(   (	   t   dictt   rangeR/   R   RD   R-   R*   RK   RL   (   R   R4   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyRB   ¶  s    :(   R   R   R   R   R   R%   RE   RB   (    (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   U  s   			
		*R   c           B   sT   e  Z d  Z d e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s½   
    Given an iterator of bytes, returns an iterator of integer
    codepoints, suitable for use by L{Decoder}. The core of the
    "compression" side of lzw compression/decompression.
    i   c         C   sY   t  |  _ | |  _ d |  _ |  j ƒ  | |  j ƒ  k  rU t d j |  j ƒ  ƒ ƒ ‚ n  d S(   s�   
        When the encoding codebook grows larger than max_code_size,
        the Encoder will clear its codebook and emit a CLEAR_CODE
        RI   s/   Max code size too small, (must be at least {0})N(   t   Falset   closedt   _max_code_sizet   _bufferRB   R   RJ   t   format(   R   R   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   Ã  s    			
c         C   s   t  |  j ƒ S(   s£   
        Returns a count of the known codes, including codes that are
        implicit in the data but have not yet been produced by the
        iterator.
        (   R+   t	   _prefixes(   R   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   Ó  s    c         c   s=   g  } |  j  r* |  j |  j  Vd |  _  n  t V|  j ƒ  d S(   s}   
        Yields any buffered codepoints, followed by a CLEAR_CODE, and
        clears the codebook as a side effect.
        RI   N(   RS   RU   R-   RB   (   R   t   flushed(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyt   flushÜ  s    	c         c   s„   xa | D]Y } x |  j  | ƒ D] } | Vq W|  j ƒ  |  j k r x |  j ƒ  D] } | VqN Wq q Wx |  j ƒ  D] } | Vqq Wd S(   se  
        Given an iterator over bytes, yields the
        corresponding stream of codepoints.
        Will clear the codes at the end of the stream.

        >>> import lzw
        >>> enc = lzw.Encoder()
        >>> [ cp for cp in enc.encode("gabba gabba yo gabba") ]
        [103, 97, 98, 98, 97, 32, 258, 260, 262, 121, 111, 263, 259, 261, 256]

        N(   t   _encode_byteR   RR   RW   (   R   R   t   bt   pointR4   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   î  s    	c         c   sh   |  j  } | | |  j k r) | | } n2 | r[ |  j | } |  j | | ƒ | } | Vn  | |  _  d  S(   N(   RS   RU   t	   _add_code(   R   t   bytet
   new_prefixt   encoded(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyRX     s    	c         C   sX   t  g  t d ƒ D] } t j d | ƒ | f ^ q ƒ |  _ t |  j t <t |  j t <d  S(   Ni   R(   (   RN   RO   R/   R   RU   R-   R*   (   R   t   codept(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyRB     s    :c         C   s   t  |  j ƒ |  j | <d  S(   N(   R+   RU   (   R   t	   newstring(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR[   '  s    (   R   R   R   R   R   R   RW   R   RX   RB   R[   (    (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   ½  s   						
t   PagingEncoderc           B   s    e  Z d  Z d „  Z d „  Z RS(   sŽ   
    UNTESTED. Handles encoding of multiple chunks or streams of encodable data,
    separated with control codes. Dual of PagingDecoder.
    c         C   s   | |  _  | |  _ d  S(   N(   R'   RR   (   R   R   R   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   1  s    	c   	      c   s‹   x„ | D]| } t  d |  j ƒ } | j | ƒ } t j t g | t g ƒ } t d | j ƒ  ƒ } | j	 | ƒ } x | D] } | Vqt Wq Wd S(   sò  
        Given an iterator of iterators of bytes, produces a single
        iterator containing a delimited sequence of independantly
        compressed LZW sequences, all beginning on a byte-aligned
        spot, all beginning with a CLEAR code and all terminated with
        an END_OF_INFORMATION code (and zero to seven trailing junk
        bits.)

        The dual of PagingDecoder.decodepages

        >>> import lzw
        >>> enc = lzw.PagingEncoder(257, 2**12)
        >>> coded = enc.encodepages([ "say hammer yo hammer mc hammer go hammer",
        ...                           "and the rest can go and play",
        ...                           "can't touch this" ])
        ...
        >>> b"".join(coded)
        '\x80\x1c\xcc\'\x91\x01\xa0\xc2m6\x99NB\x03\xc9\xbe\x0b\x07\x84\xc2\xcd\xa68|"\x14 3\xc3\xa0\xd1c\x94\x02\x02\x80\x18M\xc6A\x01\xd0\xd0e\x10\x1c\x8c\xa73\xa0\x80\xc7\x02\x10\x19\xcd\xe2\x08\x14\x10\xe0l0\x9e`\x10\x10\x80\x18\xcc&\xe19\xd0@t7\x9dLf\x889\xa0\xd2s\x80@@'

        R   R   N(
   R   RR   R   t	   itertoolst   chainR-   R*   R   R   R   (	   R   t   pagest   pageR   R   t   codes_and_eoit   packert   packedR\   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyt   encodepages6  s    (   R   R   R   R   Ri   (    (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyRa   ,  s   	t   PagingDecoderc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s•   
    UNTESTED. Dual of PagingEncoder, knows how to handle independantly encoded,
    END_OF_INFO_CODE delimited chunks of an inbound byte stream
    c         C   s   | |  _  g  |  _ d  S(   N(   R'   t   _remains(   R   R   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR   a  s    	c         c   sU   g  |  _  y4 x- | j ƒ  } | t k r/ | Vq | |  _  Pq Wn t k
 rP n Xd S(   s<   
        Iterator over the next page of codepoints.
        N(   Rk   t   nextR*   t   StopIteration(   R   R   RF   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyt	   next_pagee  s    		c   	      c   sŸ   t  d |  j ƒ } | j | ƒ } | |  _ xn |  j rš |  j |  j ƒ } g  | D] } | ^ qO } t ƒ  } | j | ƒ } g  | D] } | ^ q€ } | Vq- Wd S(   s—  
        Takes an iterator of bytes, returns an iterator of iterators
        of uncompressed data. Expects input to conform to the output
        conventions of PagingEncoder(), in particular that "pages" are
        separated with an END_OF_INFO_CODE and padding up to the next
        byte boundary.

        BUG: Dangling trailing page on decompression.

        >>> import lzw
        >>> pgdec = lzw.PagingDecoder(initial_code_size=257)
        >>> pgdecoded = pgdec.decodepages(
        ...     ''.join([ '\x80\x1c\xcc\'\x91\x01\xa0\xc2m6',
        ...               '\x99NB\x03\xc9\xbe\x0b\x07\x84\xc2',
        ...               '\xcd\xa68|"\x14 3\xc3\xa0\xd1c\x94',
        ...               '\x02\x02\x80\x18M\xc6A\x01\xd0\xd0e',
        ...               '\x10\x1c\x8c\xa73\xa0\x80\xc7\x02\x10',
        ...               '\x19\xcd\xe2\x08\x14\x10\xe0l0\x9e`\x10',
        ...               '\x10\x80\x18\xcc&\xe19\xd0@t7\x9dLf\x889',
        ...               '\xa0\xd2s\x80@@' ])
        ... )
        >>> [ b"".join(pg) for pg in pgdecoded ]
        ['say hammer yo hammer mc hammer go hammer', 'and the rest can go and play', "can't touch this", '']

        R   N(   R!   R'   R$   Rk   Rn   R   R%   (	   R   R   t   unpackerR   t
   nextpointst   nxR
   RG   t   dec(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyt   decodepagesx  s     		(   R   R   R   R   Rn   Rs   (    (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyRj   [  s   		c         C   s   t  j d |  ƒ \ } | S(   se   
   Given a one-byte long byte string, returns an integer. Equivalent
   to struct.unpack("B", b)
   R(   (   R/   R$   (   RY   RM   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyt
   unpackbyte­  s    i   c         c   sE   |  j  | ƒ } x/ | r@ x | D] } | Vq W|  j  | ƒ } q Wd S(   sª   
    Convenience for iterating over the bytes in a file. Given a
    file-like object (with a read(int) method), returns an iterator
    over the bytes of that file.
    N(   t   read(   t   fileobjt
   buffersizet   buffR\   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyt	   filebytesº  s
    	 	c         c   s,   x% t  t |  d ƒ | ƒ D] } | Vq Wd S(   s‹   
    Opens a file named by filename and iterates over the L{filebytes}
    found therein.  Will close the file when the bytes run out.
    t   rbN(   Ry   t   open(   t   filenameRw   R\   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyt	   readbytesÆ  s    c         C   s1   t  |  d ƒ } x | D] } | j | ƒ q Wd S(   s²   
    Convenience for emitting the bytes we generate to a file. Given a
    filename, opens and truncates the file, dumps the bytes
    from bytesource into it, and closes it
    t   wbN(   R{   t   write(   R|   R   t   outfileR8   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyt
   writebytesÐ  s    c         C   sx   |  } g  } x% | r3 | j  | d @ƒ | d ?} q W| j ƒ  | } d | k rt d g | t | ƒ } | | } n  | S(   sI  
    Produces an array of booleans representing the given argument as
    an unsigned integer, MSB first. If width is given, will pad the
    MSBs to the given width (but will NOT truncate overflowing
    results)

    >>> import lzw
    >>> lzw.inttobits(304, width=16)
    [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0]

    i   i    N(   R,   t   reverseRK   R+   (   t   anintt   widtht   remainst
   retreverseRM   t   ret_head(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR)   Ü  s    	
c         C   se   d } g  |  D] } | ^ q } | j  ƒ  x5 t t | ƒ ƒ D]! } | | r< | d | >B} q< q< W| S(   sü   
    Given a list of boolean values, interprets them as a binary
    encoded, MSB-first unsigned integer (with True == 1 and False
    == 0) and returns the result.

    >>> import lzw
    >>> lzw.intfrombits([ 1, 0, 0, 1, 1, 0, 0, 0, 0 ])
    304
    i    i   (   R‚   RO   R+   (   R<   RM   RY   t	   lsb_firstt	   bit_index(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR;   ø  s    


c         c   s[   xT |  D]L } t  | ƒ } x7 t d d d ƒ D]# } | d } d | | ?@} | Vq, Wq Wd S(   sý   
    Breaks a given iterable of bytes into an iterable of boolean
    values representing those bytes as unsigned integers.

    >>> import lzw
    >>> [ x for x in lzw.bytestobits(b"\x01\x30") ]
    [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0]
    i   i    iÿÿÿÿi   N(   Rt   RO   (   R   RY   t   valuet
   bitplusonet   bitindexR@   (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR:     s    	
c         C   s†   g  } d } d } xQ |  D]I } | r6 | d | >B} n  | rI | d } q | j  | ƒ d } d } q W| d k  r‚ | j  | ƒ n  | S(   sP  
    Interprets an indexable list of booleans as bits, MSB first, to be
    packed into a list of integers from 0 to 256, MSB first, with LSBs
    zero-padded. Note this padding behavior means that round-trips of
    bytestobits(bitstobytes(x, width=W)) may not yield what you expect
    them to if W % 8 != 0

    Does *NOT* pack the returned values into a bytearray or the like.

    >>> import lzw
    >>> bitstobytes([0, 0, 0, 0, 0, 0, 0, 0, "Yes, I'm True"]) == [ 0x00, 0x80 ]
    True
    >>> bitstobytes([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0]) == [ 0x01, 0x30 ]
    True
    i    i   i   (   R,   (   R<   RM   t   nextbyteR@   t   bit(    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyR.      s    
 (#   R   t
   __future__R    t
   __author__t   __license__t   __version__t
   __status__t	   __email__t   __url__R/   Rb   R-   R*   t   DEFAULT_MIN_BITSR   R   R   t   objectR   R   R   R!   R   R   Ra   Rj   Rt   Ry   R}   R�   RK   R)   R;   R:   R.   (    (    (    s8   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/lzw.pyt   <module>4   s>   		-\Mho/R	
			