ó
;c›]c           @` s‡  d  Z  d d l m Z m Z m 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 l Z d d l Z d d l m Z e j e ƒ Z d d d f Z d j d „  e Dƒ ƒ Z e j d d	 k Z e re Z e Z e Z e Z e Z n7 e j  ƒ  e! Z e" Z e! Z e e# f Z e e j$ f Z e
 j% Z& e
 j' Z( d
 „  Z) d „  Z* d „  Z+ d „  Z, d e- f d „  ƒ  YZ. d e- f d „  ƒ  YZ/ d e/ f d „  ƒ  YZ0 d e- f d „  ƒ  YZ1 e2 d „ Z3 e2 d „ Z4 e2 d „ Z5 e2 d „ Z6 e5 Z7 e3 Z8 d e- f d „  ƒ  YZ9 d e9 f d „  ƒ  YZ: d e9 f d „  ƒ  YZ; e: e; g Z< e d  k rƒd d l= Z= e= j> ƒ  n  d S(!   s!  
Python implementation of the Binary Structured Data Format (BSDF).

BSDF is a binary format for serializing structured (scientific) data.
See http://bsdf.io for more information.

This is the reference implementation, which is relatively relatively
sophisticated, providing e.g. lazy loading of blobs and streamed
reading/writing. A simpler Python implementation is available as
``bsdf_lite.py``.

This module has no dependencies and works on Python 2.7 and 3.4+.

Note: on Legacy Python (Python 2.7), non-Unicode strings are encoded as bytes.
i    (   t   absolute_importt   divisiont   print_functionN(   t   BytesIOi   i   t   .c         c` s   |  ] } t  | ƒ Vq d  S(   N(   t   str(   t   .0t   i(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pys	   <genexpr>0   s    i   c         C` s-   |  d k r t  d |  ƒ St  d d |  ƒ Sd S(   sE    Encode an unsigned integer into a variable sized blob of bytes.
    iú   s   <Bs   <BQiý   N(   t   spack(   t   x(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt   lencodeJ   s    c         C` sK   t  d |  j d ƒ ƒ d } | d k rG t  d |  j d ƒ ƒ d } n  | S(   s-    Decode an unsigned integer from a file.
    s   <Bi   i    iý   s   <Qi   (   t	   strunpackt   read(   t   ft   n(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt	   lendecodeZ   s    c         C` sA   | d k	 r9 | j d ƒ } |  j ƒ  t t | ƒ ƒ | S|  Sd S(   s?    Encode the type identifier, with or without extension id.
    s   UTF-8N(   t   Nonet   encodet   upperR
   t   len(   t   bt   ext_idt   bb(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt   encode_type_idc   s    c         C` s=   t  |  t ƒ o< t j d |  t j ƒ o< t j d |  ƒ d k S(   s>    Use of str.isidentifier() for Legacy Python, but slower.
    s   ^\w+$s   ^[0-9]N(   t
   isinstancet   string_typest   ret   matcht   UNICODER   (   t   s(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt   _isidentifierm   s    t   BsdfSerializerc           B` sz   e  Z d  Z d d „ Z d e e e e d „ Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z RS(   s{   Instances of this class represent a BSDF encoder/decoder.

    It acts as a placeholder for a set of extensions and encoding/decoding
    options. Use this to predefine extensions and options for high
    performance encoding/decoding. For general use, see the functions
    `save()`, `encode()`, `load()`, and `decode()`.

    This implementation of BSDF supports streaming lists (keep adding
    to a list after writing the main file), lazy loading of blobs, and
    in-place editing of blobs (for streams opened with a+).

    Options for encoding:

    * compression (int or str): ``0`` or "no" for no compression (default),
      ``1`` or "zlib" for Zlib compression (same as zip files and PNG), and
      ``2`` or "bz2" for Bz2 compression (more compact but slower writing).
      Note that some BSDF implementations (e.g. JavaScript) may not support
      compression.
    * use_checksum (bool): whether to include a checksum with binary blobs.
    * float64 (bool): Whether to write floats as 64 bit (default) or 32 bit.

    Options for decoding:

    * load_streaming (bool): if True, and the final object in the structure was
      a stream, will make it available as a stream in the decoded object.
    * lazy_blob (bool): if True, bytes are represented as Blob objects that can
      be used to lazily access the data, and also overwrite the data if the
      file is open in a+ mode.
    c         K` sV   i  |  _  i  |  _ | d  k r' t } n  x | D] } |  j | ƒ q. W|  j |   d  S(   N(   t   _extensionst   _extensions_by_clsR   t   standard_extensionst   add_extensiont   _parse_options(   t   selft
   extensionst   optionst	   extension(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt   __init__—   s    			i    c         C` s©   t  | t ƒ rE i d d 6d d 6d d 6} | j | j ƒ  | ƒ } n  | d k r` t d ƒ ‚ n  | |  _ t | ƒ |  _ t | ƒ |  _ t | ƒ |  _	 t | ƒ |  _
 d  S(	   Ni    t   noi   t   zlibi   t   bz2s3   Compression must be 0, 1, 2, "no", "zlib", or "bz2"(   i    i   i   (   R   R   t   gett   lowert	   TypeErrort   _compressiont   boolt   _use_checksumt   _float64t   _load_streamingt
   _lazy_blob(   R%   t   compressiont   use_checksumt   float64t   load_streamingt	   lazy_blobt   m(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR$       s    
	c         C` sY  t  | t ƒ o t | t ƒ s- t d ƒ ‚ n  | ƒ  } | j } t  | t ƒ s] t d ƒ ‚ n  t | ƒ d k s t | ƒ d k r t d ƒ ‚ n  | |  j	 k r³ t
 j d | ƒ n  | j } | sË g  } n' t  | t t f ƒ ré | } n	 | g } x, | D]$ } t  | t ƒ sù t d ƒ ‚ qù qù Wx$ | D] } | | j f |  j | <q(W| |  j	 | <| S(   s‚    Add an extension to this serializer instance, which must be
        a subclass of Extension. Can be used as a decorator.
        s*   add_extension() expects a Extension class.s   Extension name must be str.i    iú   s<   Extension names must be nonempty and shorter than 251 chars.sA   BSDF warning: overwriting extension "%s", consider removing firsts    Extension classes must be types.(   R   t   typet
   issubclasst	   ExtensionR/   t   nameR   R   t	   NameErrorR    t   loggert   warningt   clst   tuplet   listt
   classtypesR   R!   (   R%   t   extension_classR(   R?   RC   t   clss(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR#   ¹   s4    		$				c         C` sŽ   t  | t ƒ s t d ƒ ‚ n  | |  j k r@ |  j j | ƒ n  xG t |  j j ƒ  ƒ D]0 } |  j | d | k rV |  j j | ƒ qV qV Wd S(   s0    Remove a converted by its unique name.
        s   Extension name must be str.i    N(   R   R   R/   R    t   popRE   R!   t   keys(   R%   R?   RC   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt   remove_extensionä   s    c         C` s  t  } | d k r+ | j | d | ƒ ƒ nÕ| t k rP | j | d | ƒ ƒ n°| t k ru | j | d | ƒ ƒ n‹t | t ƒ rì d | k o› d k n rÆ | j | d | ƒ t d | ƒ ƒ q | j | d | ƒ t d | ƒ ƒ nt | t ƒ rP|  j	 r*| j | d	 | ƒ t d
 | ƒ ƒ q | j | d | ƒ t d | ƒ ƒ n°t | t
 ƒ r¤| j d ƒ } | j | d | ƒ t t | ƒ ƒ ƒ | j | ƒ n\t | t t f ƒ r	| j | d | ƒ t t | ƒ ƒ ƒ x| D] } |  j | | | d ƒ qæWn÷t | t ƒ rÖ| j | d | ƒ t t | ƒ ƒ ƒ x¿| j ƒ  D]„ \ } } t rr| j ƒ  s„t ‚ n t | ƒ s„t ‚ | j d ƒ }	 | j t t |	 ƒ ƒ ƒ | j |	 ƒ |  j | | | d ƒ qKWn*t | t ƒ r)| j | d | ƒ ƒ t | d |  j d |  j ƒ}
 |
 j | ƒ n×t | t ƒ r^| j | d | ƒ ƒ | j | ƒ n¢t | t ƒ r| j d k r‹t d ƒ ‚ nD t | t ƒ rÃ| j | d | ƒ t d d d ƒ ƒ n t  d ƒ ‚ t | ƒ d k rðt d ƒ ‚ n  | j! | ƒ | j" | |  j |  j# ƒ nç | d k	 r8t d | ƒ ‚ n  |  j$ j% | j& d ƒ } | d k rªxK |  j' j ƒ  D]1 \ } } | j( |  | ƒ rl| | j f } PqlqlWd } n  | d k	 rä| \ } } |  j | | |  | ƒ | | ƒ n d } t  | | j& j) ƒ ‚ d S(   s     Main encoder function.
        t   vt   yR   i €ÿÿiÿ  t   hR   s   <qt   ds   <dR   s   <fs   UTF-8R   t   lR;   R   R6   R7   t   ws$   Cannot serialize a read-mode stream.s   <BQiÿ   i    s   Only ListStream is supporteds"   Can only have one stream per file.sŽ   Extension %s wronfully encodes object to another extension object (though it may encode to a list/dict that contains other extension objects).sJ   Class %r is not a valid base BSDF type, nor is it handled by an extension.N(*   R   R   t   writet   Truet   FalseR   t   integer_typesR   t   floatR3   t   unicode_typesR   R
   R   RE   RD   t   _encodet   dictt   itemst   PY3t   isidentifiert   AssertionErrorR   t   bytest   BlobR0   R2   t   _to_filet
   BaseStreamt   modet
   ValueErrort
   ListStreamR/   t   appendt	   _activatet   _decodeR!   R-   t	   __class__R    R   t   __name__(   R%   R   t   valuet   streamsR   R	   R   RL   t   keyt   name_bt   blobt   exR?   t   ct   ext_id2t   extension_encodet   t(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRX   ï   s†    &&	&&&&&)	"c         C` sâ  | j  d ƒ } | j ƒ  } | s- t ƒ  ‚ nI | | k rp t d | j  d ƒ ƒ d } | j  | ƒ j d ƒ } n d } | d k r‹ d } ný| d k r  t } nè| d k rµ t } nÓ| d k rà t d	 | j  d
 ƒ ƒ d } n¨| d k rt d | j  d ƒ ƒ d } n}| d k r6t d | j  d ƒ ƒ d } nR| d k rat d | j  d ƒ ƒ d } n'| d k rÏt d | j  d ƒ ƒ d } | d k r´t d | j  d ƒ ƒ d } n  | j  | ƒ j d ƒ } n¹| d k r3t d | j  d ƒ ƒ d } | d k rÝ| d k } t d | j  d ƒ ƒ d } |  j rht	 | rC| n d ƒ } | j
 | |  j |  j ƒ q0| r™g  t | ƒ D] }	 |  j | ƒ ^ q{} q0g  } y' x  t rÄ| j |  j | ƒ ƒ q¥WWq0t k
 rÙq0Xqˆ| d k rt d | j  d ƒ ƒ d } n  g  t | ƒ D] }	 |  j | ƒ ^ q} nU| d k r-t ƒ  } t d | j  d ƒ ƒ d } | d k rt d | j  d ƒ ƒ d } n  xö t | ƒ D]Š }	 t d | j  d ƒ ƒ d }
 |
 d k rét d | j  d ƒ ƒ d }
 n  |
 d k sût ‚ | j  |
 ƒ j d ƒ } |  j | ƒ | | <qœWn[ | d k rx|  j rWt | t f ƒ } qˆt | t f ƒ } | j ƒ  } n t d | ƒ ‚ | d k	 rÞ|  j j | d ƒ } | d k	 rÊ| j |  | ƒ } qÞt j d | ƒ n  | S(   s     Main decoder function.
        i   s   <Bi    s   UTF-8RL   RM   R   RN   s   <hi   R   s   <qi   R   s   <fi   RO   s   <dR   iý   s   <QRP   iþ   t   rR;   R   s   Parse error %rs'   BSDF warning: no extension found for %rN(   R   R.   t   EOFErrorR   t   decodeR   RS   RT   R4   Rd   Rf   RX   Rg   t   rangeRe   RY   R]   R5   R_   t	   get_bytest   RuntimeErrorR    R-   RA   RB   (   R%   R   t   charRp   R   R   Rj   t   n_st   closedR   t   n_nameR?   Rn   R(   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRg   K  sŒ    				+	+		c         C` s#   t  ƒ  } |  j | | ƒ | j ƒ  S(   s)    Save the given object to bytes.
        (   R   t   savet   getvalue(   R%   t   obR   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR   ¦  s    	c         C` sª   | j  d ƒ | j  t j d t d ƒ ƒ | j  t j d t d ƒ ƒ g  } |  j | | | d ƒ t | ƒ d k r¦ | d } | j | j ƒ  k r¦ t	 d ƒ ‚ q¦ n  d S(   s:    Write the given object to the given file object.
        t   BSDFs   <Bi    i   s8   The stream object must be the last object to be encoded.N(
   RR   t   structt   packt   VERSIONRX   R   R   t
   _start_post   tellRc   (   R%   R   R€   Rk   t   stream(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR~   ­  s    
c         C` s   t  | ƒ } |  j | ƒ S(   sJ    Load the data structure that is BSDF-encoded in the given bytes.
        (   R   t   load(   R%   R   R   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRv   Á  s    c         C` sâ   | j  d ƒ } | d k r. t d | ƒ ‚ n  t d | j  d ƒ ƒ d } t d | j  d ƒ ƒ d } d | | f } | t d k r¥ d } t | t | f ƒ ‚ n  | t d k rÕ d	 } t j | t | f ƒ n  |  j | ƒ S(
   s@    Load a BSDF-encoded object from the given file object.
        i   R   s'   This does not look like a BSDF file: %rs   <Bi   i    s   %i.%isL   Reading file with different major version (%s) from the implementation (%s).sW   BSDF warning: reading file with higher minor version (%s) than the implementation (%s).(   R   Ry   R   R„   t   __version__RA   RB   Rg   (   R%   R   t   f4t   major_versiont   minor_versiont   file_versionRs   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRˆ   Ç  s    N(   Ri   t
   __module__t   __doc__R   R)   RT   RS   R$   R#   RK   RX   Rg   R   R~   Rv   Rˆ   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR   x   s   	+		\	[			Ra   c           B` s2   e  Z d  Z d d „ Z d „  Z e d „  ƒ Z RS(   s    Base class for streams.
    RQ   c         C` s|   d |  _  d |  _ t | t ƒ r3 | |  _ d } n | d k rK d |  _ n  | d k s] t ‚ | |  _ d  |  _ d |  _ d  S(   Ni    iÿÿÿÿRt   RQ   (   Rt   RQ   (	   t   _it   _countR   t   intR]   t   _modeR   t   _fR…   (   R%   Rb   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR)   é  s    						c         C` sO   |  j  d  k	 r t d ƒ ‚ n  | |  _  |  j  j ƒ  |  _ | |  _ | |  _ d  S(   Ns(   Stream object cannot be activated twice?(   R”   R   t   IOErrorR†   R…   RX   Rg   (   R%   t   filet   encode_funct   decode_func(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRf   ö  s    		c         C` s   |  j  S(   s.    The mode of this stream: 'r' or 'w'.
        (   R“   (   R%   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRb   þ  s    (   Ri   RŽ   R   R)   Rf   t   propertyRb   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRa   å  s   	Rd   c           B` s\   e  Z d  Z e d „  ƒ Z e d „  ƒ Z d „  Z e d „ Z d „  Z	 d „  Z
 d „  Z RS(   sk    A streamable list object used for writing or reading.
    In read mode, it can also be iterated over.
    c         C` s   |  j  S(   se    The number of elements in the stream (can be -1 for unclosed
        streams in read-mode).
        (   R‘   (   R%   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt   count
  s    c         C` s   |  j  S(   s9    The current index of the element to read/write.
        (   R   (   R%   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt   index  s    c         C` s˜   |  j  |  j k r! t d ƒ ‚ n  |  j d k r? t d ƒ ‚ n  |  j j rZ t d ƒ ‚ n  |  j |  j | |  g d ƒ |  j d 7_ |  j  d 7_  d S(   s€    Append an item to the streaming list. The object is immediately
        serialized and written to the underlying file.
        s/   Can only append items to the end of the stream.s.   List stream is not associated with a file yet.s   Cannot stream to a close file.i   N(   R‘   R   R•   R”   R   R|   RX   (   R%   t   item(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRe     s    c         C` sÙ   |  j  |  j k r! t d ƒ ‚ n  |  j d
 k r? t d ƒ ‚ n  |  j j rZ t d ƒ ‚ n  |  j j ƒ  } |  j j |  j d d ƒ |  j j	 t
 d | rŸ d n d ƒ ƒ |  j j	 t
 d	 |  j  ƒ ƒ |  j j | ƒ d
 S(   sû    Close the stream, marking the number of written elements. New
        elements may still be appended, but they won't be read during decoding.
        If ``unstream`` is False, the stream is turned into a regular list
        (not streaming).
        s-   Can only close when at the end of the stream.s-   ListStream is not associated with a file yet.s&   Cannot close a stream on a close file.i   i   s   <Biý   iþ   s   <QN(   R‘   R   R•   R”   R   R|   R†   t   seekR…   RR   R   (   R%   t   unstreamR   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt   close'  s    %c         C` s  |  j  d k r t d ƒ ‚ n  |  j d k r< t d ƒ ‚ n  t |  j d d ƒ r` t d ƒ ‚ n  |  j d k r¬ |  j |  j k r t ƒ  ‚ n  |  j d 7_ |  j |  j ƒ Sy) |  j |  j ƒ } |  j d 7_ | SWn& t	 k
 rý |  j |  _ t ƒ  ‚ n Xd S(	   sz    Read and return the next element in the streaming list.
        Raises StopIteration if the stream is exhausted.
        Rt   s$   This ListStream in not in read mode.s-   ListStream is not associated with a file yet.R|   s'   Cannot read a stream from a close file.i    i   N(
   R“   R•   R”   R   t   getattrR‘   R   t   StopIterationRg   Ru   (   R%   t   res(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt   next;  s$    c         C` s"   |  j  d k r t d ƒ ‚ n  |  S(   NRt   s/   Cannot iterate: ListStream in not in read mode.(   R“   R•   (   R%   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt   __iter__T  s    c         C` s
   |  j  ƒ  S(   N(   R£   (   R%   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt   __next__Y  s    (   Ri   RŽ   R   R™   Rš   R›   Re   RT   RŸ   R£   R¤   R¥   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRd     s   			R_   c           B` sq   e  Z d  Z d d e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z RS(   s9   Object to represent a blob of bytes. When used to write a BSDF file,
    it's a wrapper for bytes plus properties such as what compression to apply.
    When used to read a BSDF file, it can be used to read the data lazily, and
    also modify the data if reading in 'r+' mode and the blob isn't compressed.
    i    c         C` sÍ   t  | t ƒ rR d  |  _ |  j | | ƒ |  _ | |  _ |  j | |  _ | |  _	 nw t  | t
 ƒ r½ t | ƒ d k r½ t | d d ƒ r½ | \ |  _ } d  |  _ |  j |  j | ƒ t |  _ n t d ƒ ‚ d  S(   Ni   i    R   s   Wrong argument to create Blob.(   R   R^   R   R”   t   _from_bytest
   compressedR6   t	   used_sizet   allocated_sizeR7   RD   R   t   hasattrt
   _from_fileRT   t	   _modifiedR/   (   R%   R   R6   t
   extra_sizeR7   t
   allow_seek(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR)   g  s    		4	c         C` s‹   | d k r | } nT | d k r6 t  j | d ƒ } n3 | d k rW t j | d ƒ } n t si t d ƒ ‚ t | ƒ |  _ t | ƒ |  _ | S(   s,    When used to wrap bytes in a blob.
        i    i   i	   i   s   Unknown compression identifier(   R+   t   compressR,   RT   R]   R   t	   data_sizeR¨   (   R%   Rj   R6   R§   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR¦   v  s    	c         C` s¨  |  j  d k ri |  j d k ri | j t d |  j  ƒ ƒ | j t d |  j ƒ ƒ | j t |  j ƒ ƒ nT | j t d d |  j  ƒ ƒ | j t d d |  j ƒ ƒ | j t d d |  j ƒ ƒ | j t d |  j ƒ ƒ |  j r| j d t j	 |  j
 ƒ j ƒ  ƒ n | j d ƒ |  j d k rcd	 | j ƒ  d
 d	 } | j t d | ƒ ƒ | j d | ƒ n | j t d d ƒ ƒ | j |  j
 ƒ | j d |  j  |  j ƒ d S(   sL    Private friend method called by encoder to write a blob to a file.
        iú   i    s   <Bs   <BQiý   t   Bs   ÿs    i   i   N(   R©   R6   RR   R   R¨   R
   R°   R7   t   hashlibt   md5R§   t   digestR†   (   R%   R   t	   alignment(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR`   †  s$    	&c   
      C` s  t  d | j d ƒ ƒ d } | d k rG t  d | j d ƒ ƒ d } n  t  d | j d ƒ ƒ d } | d k rŽ t  d | j d ƒ ƒ d } n  t  d | j d ƒ ƒ d } | d k rÕ t  d | j d ƒ ƒ d } n  t  d | j d ƒ ƒ d } t  d | j d ƒ ƒ d } | r%| j d ƒ } n  t  d | j d ƒ ƒ d }	 | j |	 ƒ | rŠ| j ƒ  |  _ |  j | |  _ | j |  j | ƒ n5 d |  _ d |  _ | j | ƒ |  _ | j | | ƒ |	 |  _ | |  _	 | rÝ| n d |  _
 | |  _ | |  _ | |  _ d S(	   s2    Used when a blob is read by the decoder.
        s   <Bi   i    iý   s   <Qi   i   N(   R   R   R†   t	   start_post   end_posR   R   R§   Rµ   R6   R7   R¨   R©   R°   (
   R%   R   R®   R©   R¨   R°   R6   t   has_checksumt   checksumRµ   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR«   £  s:    						c         C` s   |  j  d k r t d ƒ ‚ n  | d k  r: |  j | } n  | d k  sU | |  j k rd t d ƒ ‚ n  |  j  j |  j | ƒ d S(   sB    Seek to the given position (relative to the blob start).
        s>   Cannot seek in a blob that is not created by the BSDF decoder.i    s   Seek beyond blob boundaries.N(   R”   R   Ry   R©   R•   R   R¶   (   R%   t   p(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR   Ë  s    c         C` s2   |  j  d k r t d ƒ ‚ n  |  j  j ƒ  |  j S(   sM    Get the current file pointer position (relative to the blob start).
        s>   Cannot tell in a blob that is not created by the BSDF decoder.N(   R”   R   Ry   R†   R¶   (   R%   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR†   Ø  s    c         C` s€   |  j  d k r t d ƒ ‚ n  |  j r6 t d ƒ ‚ n  |  j  j ƒ  t | ƒ |  j k rg t d ƒ ‚ n  t |  _	 |  j  j
 | ƒ S(   s"    Write bytes to the blob.
        s?   Cannot write in a blob that is not created by the BSDF decoder.s,   Cannot arbitrarily write in compressed blob.s   Write beyond blob boundaries.N(   R”   R   Ry   R6   R•   R†   R   R·   RS   R¬   RR   (   R%   R   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRR   á  s    	"	c         C` sq   |  j  d k r t d ƒ ‚ n  |  j r6 t d ƒ ‚ n  |  j  j ƒ  | |  j k ra t d ƒ ‚ n  |  j  j | ƒ S(   s%    Read n bytes from the blob.
        s>   Cannot read in a blob that is not created by the BSDF decoder.s+   Cannot arbitrarily read in compressed blob.s   Read beyond blob boundaries.N(   R”   R   Ry   R6   R•   R†   R·   R   (   R%   R   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR   ï  s    	c         C` sÍ   |  j  d k	 r |  j  } nA |  j j ƒ  } |  j d ƒ |  j j |  j ƒ } |  j j | ƒ |  j d k rt | } nU |  j d k r• t j	 | ƒ } n4 |  j d k r¶ t
 j	 | ƒ } n t d |  j ƒ ‚ | S(   s0    Get the contents of the blob as bytes.
        i    i   i   s   Invalid compression %iN(   R§   R   R”   R†   R   R   R¨   R6   R+   t
   decompressR,   Ry   (   R%   R§   R   Rj   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRx   ü  s    	c         C` s|   |  j  rx |  j rx |  j d ƒ |  j j |  j ƒ } |  j j |  j |  j d d ƒ |  j j t	 j
 | ƒ j ƒ  ƒ n  d S(   s[    Reset the blob's checksum if present. Call this after modifying
        the data.
        i    i   i   N(   R7   R¬   R   R”   R   R¨   R¶   Rµ   RR   R²   R³   R´   (   R%   R§   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt   update_checksum  s
    "(   Ri   RŽ   R   RT   R)   R¦   R`   R«   R   R†   RR   R   Rx   R¼   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR_   ]  s   			(						c         K` s   t  | |  } | j |  ƒ S(   ss    Save (BSDF-encode) the given object to bytes.
    See `BSDFSerializer` for details on extensions and options.
    (   R   R   (   R€   R&   R'   R   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR     s    c         K` s]   t  | |  } t |  t ƒ rI t |  d ƒ  } | j | | ƒ SWd QXn | j |  | ƒ Sd S(   s    Save (BSDF-encode) the given object to the given filename or
    file object. See` BSDFSerializer` for details on extensions and options.
    t   wbN(   R   R   R   t   openR~   (   R   R€   R&   R'   R   t   fp(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR~   '  s
    c         K` s   t  | |  } | j |  ƒ S(   sq    Load a (BSDF-encoded) structure from bytes.
    See `BSDFSerializer` for details on extensions and options.
    (   R   Rv   (   R   R&   R'   R   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRv   3  s    c         K` s{   t  | |  } t |  t ƒ rj |  j d ƒ rB t j j |  ƒ }  n  t |  d ƒ  } | j | ƒ SWd QXn | j |  ƒ Sd S(   s    Load a (BSDF-encoded) structure from the given filename or file object.
    See `BSDFSerializer` for details on extensions and options.
    s   ~/s   ~\t   rbN(   s   ~/s   ~\(	   R   R   R   t
   startswitht   ost   patht
   expanduserR¾   Rˆ   (   R   R&   R'   R   R¿   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRˆ   ;  s    R>   c           B` s>   e  Z d  Z d Z d Z d „  Z d „  Z d „  Z d „  Z RS(   sE   Base class to implement BSDF extensions for special data types.

    Extension classes are provided to the BSDF serializer, which
    instantiates the class. That way, the extension can be somewhat dynamic:
    e.g. the NDArrayExtension exposes the ndarray class only when numpy
    is imported.

    A extension instance must have two attributes. These can be attribiutes of
    the class, or of the instance set in ``__init__()``:

    * name (str): the name by which encoded values will be identified.
    * cls (type): the type (or list of types) to match values with.
      This is optional, but it makes the encoder select extensions faster.

    Further, it needs 3 methods:

    * `match(serializer, value) -> bool`: return whether the extension can
      convert the given value. The default is ``isinstance(value, self.cls)``.
    * `encode(serializer, value) -> encoded_value`: the function to encode a
      value to more basic data types.
    * `decode(serializer, encoded_value) -> value`: the function to decode an
      encoded value back to its intended representation.

    t    c         C` s   d |  j  t t |  ƒ ƒ f S(   Ns   <BSDF extension %r at 0x%s>(   R?   t   hext   id(   R%   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt   __repr__r  s    c         C` s   t  | |  j ƒ S(   N(   R   RC   (   R%   R   RL   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR   u  s    c         C` s   t  ƒ  ‚ d  S(   N(   t   NotImplementedError(   R%   R   RL   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR   x  s    c         C` s   t  ƒ  ‚ d  S(   N(   RÉ   (   R%   R   RL   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRv   {  s    (    (	   Ri   RŽ   R   R?   RC   RÈ   R   R   Rv   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR>   U  s   			t   ComplexExtensionc           B` s&   e  Z d  Z e Z d „  Z d „  Z RS(   Rp   c         C` s   | j  | j f S(   N(   t   realt   imag(   R%   R   RL   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR   „  s    c         C` s   t  | d | d ƒ S(   Ni    i   (   t   complex(   R%   R   RL   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRv   ‡  s    (   Ri   RŽ   R?   RÍ   RC   R   Rv   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRÊ     s   	t   NDArrayExtensionc           B` s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   t   ndarrayc         C` s.   d t  j k r* d d  l } | j |  _ n  d  S(   Nt   numpyi    (   t   syst   modulesRÐ   RÏ   RC   (   R%   t   np(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR)     s    c         C` s+   t  | d ƒ o* t  | d ƒ o* t  | d ƒ S(   Nt   shapet   dtypet   tobytes(   Rª   (   R%   R   RL   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR   •  s    c         C` s+   t  d | j d t | j ƒ d | j ƒ  ƒ S(   NRÔ   RÕ   t   data(   RY   RÔ   t	   text_typeRÕ   RÖ   (   R%   R   RL   (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyR   ˜  s    c         C` sS   y d d  l  } Wn t k
 r$ | SX| j | d d | d ƒ} | d | _ | S(   Ni    R×   RÕ   RÔ   (   RÐ   t   ImportErrort
   frombufferRÔ   (   R%   R   RL   RÓ   t   a(    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRv   ›  s    (   Ri   RŽ   R?   R)   R   R   Rv   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyRÎ   ‹  s
   			t   __main__(?   R   t
   __future__R    R   R   R,   R²   t   loggingRÂ   R   R‚   RÑ   t   typesR+   t   ioR   t	   getLoggerRi   RA   R„   t   joinR‰   t   version_infoR[   R   RØ   R   RW   R’   RU   R<   RF   t   basicConfigt   unicodet
   basestringt   longt	   ClassTypeRƒ   R   t   unpackR   R
   R   R   R   t   objectR   Ra   Rd   R_   R   R   R~   Rv   Rˆ   t   loadst   dumpsR>   RÊ   RÎ   R"   t   bsdf_clit   main(    (    (    s4   lib/python2.7/site-packages/imageio/plugins/_bsdf.pyt   <module>   sf   
	
						
	ÿ n XÂ
*