
    0-Ph                        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Zd                    d eD                       Zej        d         dk    ZereZeZeZeZeZn" 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, G d de-          Z. G d de-          Z/ G d de/          Z0 G d de-          Z1d dZ2d dZ3d dZ4d dZ5e4Z6e2Z7 G d de-          Z8 G d de8          Z9 G d de8          Z:e9e:gZ;edk    rddl<Z< e<j=                     dS dS )!a!  
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.
    )absolute_importdivisionprint_functionN)BytesIO)      r   .c              #   4   K   | ]}t          |          V  d S N)str).0is     U/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/imageio/plugins/_bsdf.py	<genexpr>r   0   s(      //!s1vv//////       c                 P    | dk    rt          d|           S t          dd|           S )z?Encode an unsigned integer into a variable sized blob of bytes.   <B<BQ   )spack)xs    r   lencoder   J   s/     	CxxT1~~ UC###r   c                     t          d|                     d                    d         }|dk    r)t          d|                     d                    d         }|S )z'Decode an unsigned integer from a file.r   r   r   r   <Q   )	strunpackread)fns     r   	lendecoder"   Y   sL    $q		""1%ACxxdAFF1II&&q)Hr   c                     |I|                     d          }|                                 t          t          |                    z   |z   S | S )z9Encode the type identifier, with or without extension id.NUTF-8)encodeupperr   len)bext_idbbs      r   encode_type_idr+   a   sD    ]]7##wwyy73r77+++b00r   c                     t          | t                    o6t          j        d| t          j                  ot          j        d|           du S )z8Use of str.isidentifier() for Legacy Python, but slower.z^\w+$z^[0-9]N)
isinstancestring_typesrematchUNICODE)ss    r   _isidentifierr3   j   sG     	1l## 	+HXq"*--	+HY""d*r   c                   \    e Zd ZdZddZ	 	 	 	 	 ddZd Zd	 Zd
 Zd Z	d Z
d Zd Zd ZdS )BsdfSerializeraz  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.
    Nc                     i | _         i | _        |t          }|D ]}|                     |            | j        di | d S )N )_extensions_extensions_by_clsstandard_extensionsadd_extension_parse_options)self
extensionsoptions	extensions       r   __init__zBsdfSerializer.__init__   sb    "$,J# 	* 	*Iy))))&&g&&&&&r   r   FTc                 `   t          |t                    r.dddd}|                    |                                |          }|dvrt	          d          || _        t          |          | _        t          |          | _        t          |          | _	        t          |          | _
        d S )Nr   r   r   )nozlibbz2)r   r   r   z3Compression must be 0, 1, 2, "no", "zlib", or "bz2")r-   r.   getlower	TypeError_compressionbool_use_checksum_float64_load_streaming
_lazy_blob)r=   compressionuse_checksumfloat64load_streaming	lazy_blobms          r   r<   zBsdfSerializer._parse_options   s     k<00 	B!A..A%% 1 1 3 3[AAKi''TUUU' ",//W  $N33y//r   c                    t          |t                    rt          |t                    st	          d           |            }|j        }t          |t                    st	          d          t          |          dk    st          |          dk    rt          d          || j	        v rt                              d|z             |j        }|sg }n"t          |t          t          f          r|}n|g}|D ]&}t          |t                    st	          d          '|D ]}||j        f| j        |<   || j	        |<   |S )zAdd an extension to this serializer instance, which must be
        a subclass of Extension. Can be used as a decorator.
        z*add_extension() expects a Extension class.Extension name must be str.r   r   z<Extension names must be nonempty and shorter than 251 chars.zABSDF warning: overwriting extension "%s", consider removing firstz Extension classes must be types.)r-   type
issubclass	ExtensionrH   namer   r'   	NameErrorr8   loggerwarningclstuplelist
classtypesr%   r9   )r=   extension_classr@   rZ   r^   clsss         r   r;   zBsdfSerializer.add_extension   s    --	J2<_i2X2X	J HIII#O%%	 ~$$$ 	;9:::t99>>SYY__Q   4###NN*,01   m 	DDeT]++ 	DD5D 	D 	DCc:.. D BCCCD  	B 	BC+/1A+AD#C((!*r   c                 H   t          |t                    st          d          || j        v r| j                            |           t          | j                                                  D ]3}| j        |         d         |k    r| j                            |           4dS )z&Remove a converted by its unique name.rV   r   N)r-   r   rH   r8   popr`   r9   keys)r=   rZ   r^   s      r   remove_extensionzBsdfSerializer.remove_extension   s    $$$ 	;9:::4###  &&&/446677 	1 	1C&s+A.$66'++C000	1 	1r   c                    t           }|!|                     |d|                     dS |du r!|                     |d|                     dS |du r!|                     |d|                     dS t          |t                    rtd|cxk    rdk    r5n n2|                     |d	|          t	          d
|          z              dS |                     |d|          t	          d|          z              dS t          |t
                    rk| j        r2|                     |d|          t	          d|          z              dS |                     |d|          t	          d|          z              dS t          |t                    rh|                    d          }|                     |d|          t          t          |                    z              |                    |           dS t          |t          t          f          r[|                     |d|          t          t          |                    z              |D ]}|                     |||d           dS t          |t                    r|                     |d|          t          t          |                    z              |                                D ]\  }}t           r|                                sJ nt%          |          sJ |                    d          }	|                    t          t          |	                               |                    |	           |                     |||d           dS t          |t&                    rR|                     |d|                     t)          || j        | j                  }
|
                    |           dS t          |t(                    r6|                     |d|                     |                    |           dS t          |t0                    r|j        dk    rt5          d          t          |t6                    r2|                     |d|          t	          ddd          z              nt9          d          t          |          dk    rt5          d          |                    |           |                    || j        | j                   dS |t5          d|z            | j         !                    |j"        d          }|B| j#                                        D ]&\  }}|$                    | |          r||j        f} n'd}|)|\  }}|                     | || |          ||           dS d}t9          ||j"        j%        z            ) zMain encoder function.N   vT   yF   ni i     hh   i<q   d<d   f<fr$      s   l   m   b)rO   rP   wz$Cannot serialize a read-mode stream.r      r   zOnly ListStream is supportedz"Can only have one stream per file.zExtension %s wronfully encodes object to another extension object (though it may encode to a list/dict that contains other extension objects).zJClass %r is not a valid base BSDF type, nor is it handled by an extension.)&r+   writer-   integer_typesr   floatrL   unicode_typesr%   r   r'   r`   r_   _encodedictitemsPY3isidentifierr3   bytesBlobrI   rK   _to_file
BaseStreammode
ValueError
ListStreamrH   append	_activate_decoder9   rF   	__class__r8   r0   __name__)r=   r    valuestreamsr)   r   r*   vkeyname_bblobexrZ   cext_id2extension_encodets                    r   r~   zBsdfSerializer._encode   s   =GGAAdFOO$$$$$d]]GGAAdFOO$$$$$e^^GGAAdFOO$$$$$}-- O	>''''%'''''$%U*;*;;<<<<<$%e*<*<<=====u%% J	>} >$%e*<*<<=====$%e*<*<<=====}-- E	>g&&BGGAAdFOOgc"gg&6&66777GGBKKKKKe}-- A	>GGAAdFOOgc%jj&9&99::: 2 2Q7D11112 2t$$ =	>GGAAdFOOgc%jj&9&99:::++-- 	2 	2Q .++------(-----G,,F,,---Q7D1111	2 	2 u%% 1	>GGAAdFOO$$$4#44CU  D MM!t$$ +	>GGAAdFOO$$$NN1z** (	>zS   !GHHHE:.. @$%sA*>*>>???? >??? 7||a !EFFFNN5!!!OOAt|T\:::::! >@FG   (,,U_dCCBz#/5577  GD!wwtU++ !18^ B~,.))Q 0 0u = =wPPPPP/   EO$< <===r   c                 2
                         d          }|                                }|st                      ||k    rRt          d                     d                    d         }                     |                              d          }nd}|dk    rd}n$|dk    rd}n|d	k    rd
}n|dk    r+t          d                     d                    d         }n|dk    r+t          d                     d                    d         }n|dk    r+t          d                     d                    d         }n}|dk    r+t          d                     d                    d         }nL|dk    rt          d                     d                    d         }|dk    r)t          d                     d                    d         }                     |                              d          }n|dk    rFt          d                     d                    d         }|dk    r|dk    }t          d                     d                    d         } j        r6t          |r|nd          }|                     j         j	                   n"|r fdt          |          D             }ng }	 	 |                     	                                         )# t          $ r Y nw xY w|dk    r)t          d                     d                    d         } fdt          |          D             }nw|dk    rt                      }t          d                     d                    d         }|dk    r)t          d                     d                    d         }t          |          D ]}	t          d                     d                    d         }
|
dk    r)t          d                     d                    d         }
|
dk    sJ                      |
                              d          } 	                              ||<   nW|dk    r? j        rt          df          }n8t          d
f          }|                                }nt!          d |z            |Q j                            |d          }||                     |          }nt&                              d!|z             |S )"zMain decoder function.r   r   r   r$   Nri   rj   Trk   Frl   z<hr   rn   ro   r   rr   rs      rp   rq   rt   r   r   ru      rc                 :    g | ]}                               S r7   r   r   r   r    r=   s     r   
<listcomp>z*BsdfSerializer._decode.<locals>.<listcomp>r  s#    ???T\\!__???r   c                 :    g | ]}                               S r7   r   r   s     r   r   z*BsdfSerializer._decode.<locals>.<listcomp>~  s#    ;;;Qa;;;r   rv   rw   zParse error %rz'BSDF warning: no extension found for %r)r   rG   EOFErrorr   decoderM   r   r   r~   r   ranger   r   rN   r   	get_bytesRuntimeErrorr8   rF   r\   r]   )r=   r    charr   r!   r)   r   n_sclosedr   n_namerZ   r   r@   s   ``            r   r   zBsdfSerializer._decodeD  s    vvayyJJLL  	**QYY$q		**1-AVVAYY%%g..FFF99EE$YYEE$YYEE$YYdAFF1II..q1EE$YYdAFF1II..q1EE$YYdAFF1II..q1EE$YYdAFF1II..q1EE$YYD!&&)),,Q/CczzaffQii003FF3KK&&w//EE$YY$q		**1-ACxxcdAFF1II..q1' &F';qq<<EOOAt|T\BBBB ?????eAhh???EEE:!LLa999:#    88!$q		2215A;;;;;%((;;;$YYFFE$q		**1-ACxxdAFF1II..q11XX . ."433A6S==&tQVVAYY77:Fzzzzvvf~~,,W55"ll1ood. $YY )aYQJ''((/$6777 (,,VT::I$!((u55H6QRRRs   *K/ /
K=<K=c                 r    t                      }|                     ||           |                                S )zSave the given object to bytes.)r   savegetvalue)r=   obr    s      r   r%   zBsdfSerializer.encode  s-    II		!Rzz||r   c                    |                     d           |                     t          j        dt          d                              |                     t          j        dt          d                              g }|                     |||d           t          |          dk    r4|d         }|j        |                                k    rt          d          dS dS )z0Write the given object to the given file object.   BSDFr   r   r   Nz8The stream object must be the last object to be encoded.)	rz   structpackVERSIONr~   r'   
_start_postellr   )r=   r    r   r   streams        r   r   zBsdfSerializer.save  s    		D'!*--...	D'!*--... QGT*** w<<!QZF AFFHH,, Q   ,,r   c                 J    t          |          }|                     |          S )z@Load the data structure that is BSDF-encoded in the given bytes.)r   load)r=   r*   r    s      r   r   zBsdfSerializer.decode  s    BKKyy||r   c                    |                     d          }|dk    rt          d|z            t          d|                     d                    d         }t          d|                     d                    d         }d||fz  }|t          d         k    rd}t          |t          |fz            |t          d         k    r&d	}t
                              |t          |fz             |                     |          S )
z6Load a BSDF-encoded object from the given file object.r   r   z'This does not look like a BSDF file: %rr   r   r   z%i.%izLReading file with different major version (%s) from the implementation (%s).zWBSDF warning: reading file with higher minor version (%s) than the implementation (%s).)r   r   r   r   __version__r\   r]   r   )r=   r    f4major_versionminor_versionfile_versionr   s          r   r   zBsdfSerializer.load  s     VVAYY==H2MNNN!$q		2215!$q		2215-!??GAJ&&0  qK#>>???71:%%0  NN1\::;;;||Ar   r   )r   FTFF)r   
__module____qualname____doc__rA   r<   r;   rg   r~   r   r%   r   r   r   r7   r   r   r5   r5   t   s         <' ' ' ' * * * *0) ) )V1 1 1Y> Y> Y>vX X Xt    &  
    r   r5   c                   6    e Zd ZdZddZd Zed             ZdS )r   zBase class for streams.rx   c                     d| _         d| _        t          |t                    r
|| _        d}n|dk    rd| _        |dv sJ || _        d | _        d| _        d S )Nr   r   rx   )r   rx   )_i_countr-   int_mode_fr   )r=   r   s     r   rA   zBaseStream.__init__  sj    dC   	DKDDS[[DKz!!!!
r   c                     | j         t          d          || _         | j                                         | _        || _        || _        d S )Nz(Stream object cannot be activated twice?)r   IOErrorr   r   r~   r   )r=   fileencode_funcdecode_funcs       r   r   zBaseStream._activate  sC    7DEEE',,..""r   c                     | j         S )z$The mode of this stream: 'r' or 'w'.)r   r=   s    r   r   zBaseStream.mode  s     zr   N)rx   )r   r   r   r   rA   r   propertyr   r7   r   r   r   r     sX        !!   # # #   X  r   r   c                   ^    e Zd ZdZed             Zed             Zd ZddZd Z	d Z
d	 Zd
S )r   zjA streamable list object used for writing or reading.
    In read mode, it can also be iterated over.
    c                     | j         S )zdThe number of elements in the stream (can be -1 for unclosed
        streams in read-mode).
        )r   r   s    r   countzListStream.count  s    
 {r   c                     | j         S )z/The current index of the element to read/write.)r   r   s    r   indexzListStream.index  s     wr   c                 "   | j         | j        k    rt          d          | j        t          d          | j        j        rt          d          |                     | j        || gd           | xj        dz  c_        | xj         dz  c_         dS )zAppend an item to the streaming list. The object is immediately
        serialized and written to the underlying file.
        z/Can only append items to the end of the stream.Nz.List stream is not associated with a file yet.zCannot stream to a close file.r   )r   r   r   r   r   r~   )r=   items     r   r   zListStream.append  s     ;$'!!KLLL7?JKKK7> 	<:;;;TWdTFD1111qr   Fc                    | j         | j        k    rt          d          | j        t          d          | j        j        rt          d          | j                                        }| j                            | j        dz
  dz
             | j                            t          d|rdnd	                     | j                            t          d
| j                              | j                            |           dS )zClose 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).
        z-Can only close when at the end of the stream.N-ListStream is not associated with a file yet.z&Cannot close a stream on a close file.r   r   r   r   r   r   )
r   r   r   r   r   r   seekr   rz   r   )r=   unstreamr   s      r   closezListStream.close  s     ;$'!!IJJJ7?IJJJ7> 	DBCCCGLLNNT_q(1,---eD":##s;;<<<eD$+..///Qr   c                    | j         dk    rt          d          | j        t          d          t          | j        dd          rt          d          | j        dk    rH| j        | j        k    rt                      | xj        dz  c_        |                     | j                  S 	 |                     | j                  }| xj        dz  c_        |S # t          $ r | j        | _        t                      w xY w)	zyRead and return the next element in the streaming list.
        Raises StopIteration if the stream is exhausted.
        r   z$This ListStream in not in read mode.Nr   r   z'Cannot read a stream from a close file.r   r   )	r   r   r   getattrr   r   StopIterationr   r   )r=   ress     r   nextzListStream.next,  s     :@AAA7?IJJJ47Hd++ 	ECDDD;!w$+%%#oo%GGqLGG<<(((&ll47++1
 & & &"g#oo%&s   *+C %C;c                 :    | j         dk    rt          d          | S )Nr   z/Cannot iterate: ListStream in not in read mode.)r   r   r   s    r   __iter__zListStream.__iter__E  s#    :KLLLr   c                 *    |                                  S r   )r   r   s    r   __next__zListStream.__next__J  s    yy{{r   N)F)r   r   r   r   r   r   r   r   r   r   r   r   r7   r   r   r   r     s            X   X      (& & &2  
    r   r   c                   P    e Zd ZdZddZd Zd Zd Zd Zd	 Z	d
 Z
d Zd Zd ZdS )r   a8  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.
    r   Fc                    t          |t                    rAd | _        |                     ||          | _        || _        | j        |z   | _        || _        d S t          |t                    r^t          |          dk    rKt          |d         d          r5|\  | _        }d | _        |                     | j        |           d| _        d S t          d          )Nr   r   r   FzWrong argument to create Blob.)r-   r   r   _from_bytes
compressedrO   	used_sizeallocated_sizerP   r_   r'   hasattr
_from_file	_modifiedrH   )r=   r*   rO   
extra_sizerP   
allow_seeks         r   rA   zBlob.__init__X  s    b%   	>DG"..r;??DO*D"&.:"=D ,DE"" 	>s2ww!||1v8N8N|"$DGZ"DOOODGZ000"DNNN<===r   c                     |dk    r|}nB|dk    rt          j        |d          }n&|dk    rt          j        |d          }n
J d            t          |          | _        t          |          | _        |S )z"When used to wrap bytes in a blob.r   r   	   r   FzUnknown compression identifier)rD   compressrE   r'   	data_sizer   )r=   r   rO   r   s       r   r   zBlob._from_bytesg  s    !JJAua00JJAeQ//JJ:::::UZr   c                    | j         dk    r| j        dk    rx|                    t          d| j                              |                    t          d| j                             |                    t          | j                             n{|                    t          dd| j                              |                    t          dd| j                             |                    t          dd| j                             |                    t          d| j                             | j        rB|                    dt          j	        | j
                                                  z              n|                    d           | j        dk    rYd	|                                d
z   d	z  z
  }|                    t          d|                     |                    d|z             n#|                    t          dd                     |                    | j
                   |                    d| j         | j        z
  z             dS )zBPrivate friend method called by encoder to write a blob to a file.r   r   r   r   r   B       r   r   N)r   rO   rz   r   r   r   r   rP   hashlibmd5r   digestr   )r=   r    	alignments      r   r   zBlob._to_filev  s    #%%$*:a*?*?GGE$ 344555GGE$//000GGGDN++,,,,GGE%d&9::;;;GGE%dn55666GGE%dn55666	c4+,,--- 	GGGgk$/::AACCCDDDDGGGq  QVVXX\Q..IGGE$	**+++GGGi'((((GGE$NN###	   	4.?@AAAAAr   c                    t          d|                    d                    d         }|dk    r)t          d|                    d                    d         }t          d|                    d                    d         }|dk    r)t          d|                    d                    d         }t          d|                    d                    d         }|dk    r)t          d|                    d                    d         }t          d|                    d                    d         }t          d|                    d                    d         }|r|                    d          }t          d|                    d                    d         }	|                    |	           |rF|                                | _        | j        |z   | _        |                    | j        |z              n@d| _        d| _        |                    |          | _        |                    ||z
             |	| _        || _        |r|nd| _	        || _
        || _        || _        dS )	z(Used when a blob is read by the decoder.r   r   r   r   r   r      N)r   r   r   	start_posend_posr   r   r   rO   rP   r   r   r   )
r=   r    r   r   r   r   rO   has_checksumchecksumr   s
             r   r   zBlob._from_file  s    #433A6S  &tQVVAYY77:NdAFF1II..q1	!$q		2215IdAFF1II..q1	!$q		2215IaffQii003 qvvayy11!4 	"vvbzzHdAFF1II..q1		y 	/VVXXDN>I5DLFF4>N23333!DNDLffY//DOFF>I-..."&(4>HH$","r   c                     | j         t          d          |dk     r
| j        |z   }|dk     s|| j        k    rt          d          | j                             | j        |z              dS )z8Seek to the given position (relative to the blob start).Nz>Cannot seek in a blob that is not created by the BSDF decoder.r   zSeek beyond blob boundaries.)r   r   r   r   r   r  )r=   ps     r   r   z	Blob.seek  s|    7?S   q55#a'Aq55A+++8999T^a'(((((r   c                 p    | j         t          d          | j                                         | j        z
  S )zCGet the current file pointer position (relative to the blob start).Nz>Cannot tell in a blob that is not created by the BSDF decoder.)r   r   r   r  r   s    r   r   z	Blob.tell  s7    7?S   w||~~..r   c                    | j         t          d          | j        rt          d          | j                                         t          |          z   | j        k    rt          d          d| _        | j                             |          S )zWrite bytes to the blob.Nz?Cannot write in a blob that is not created by the BSDF decoder.z,Cannot arbitrarily write in compressed blob.zWrite beyond blob boundaries.T)	r   r   rO   r   r   r'   r  r   rz   )r=   r*   s     r   rz   z
Blob.write  s    7?T    	JHIII7<<>>CGG#dl229:::w}}R   r   c                     | j         t          d          | j        rt          d          | j                                         |z   | j        k    rt          d          | j                             |          S )zRead n bytes from the blob.Nz>Cannot read in a blob that is not created by the BSDF decoder.z+Cannot arbitrarily read in compressed blob.zRead beyond blob boundaries.)r   r   rO   r   r   r  r   )r=   r!   s     r   r   z	Blob.read  sx    7?S    	IGHHH7<<>>A,,8999w||Ar   c                    | j         | j         }ng| j                                        }|                     d           | j                            | j                  }| j                            |           | j        dk    r|}nW| j        dk    rt          j        |          }n7| j        dk    rt          j        |          }nt          d| j        z            |S )z&Get the contents of the blob as bytes.Nr   r   r   zInvalid compression %i)r   r   r   r   r   r   rO   rD   
decompressrE   r   )r=   r   r   r   s       r   r   zBlob.get_bytes  s    ?&JJAIIaLLLdn55JGLLOOOq  EE""OJ//EE""N:..EE7$:JJKKKr   c                 h   | j         r| j        r|                     d           | j                            | j                  }| j                            | j        | j        z
  dz
  dz
             | j                            t          j
        |                                                     dS dS dS )zZReset the blob's checksum if present. Call this after modifying
        the data.
        r   r   r   N)rP   r   r   r   r   r   r  r   rz   r   r   r   )r=   r   s     r   update_checksumzBlob.update_checksum  s    
  	< 	<IIaLLLdn55JGLL$.81<rABBBGMM'+j1188::;;;;;		< 	< 	< 	<r   N)r   r   F)r   r   r   r   rA   r   r   r   r   r   rz   r   r   r  r7   r   r   r   r   N  s         > > > >  B B B8%# %# %#N
) 
) 
)/ / /! ! !
 
 
  &	< 	< 	< 	< 	<r   r   c                 F    t          |fi |}|                    |           S )zrSave (BSDF-encode) the given object to bytes.
    See `BSDFSerializer` for details on extensions and options.
    )r5   r%   )r   r>   r?   r2   s       r   r%   r%     )     	z--W--A88B<<r   c                     t          |fi |}t          | t                    r@t          | d          5 }|                    ||          cddd           S # 1 swxY w Y   dS |                    | |          S )zSave (BSDF-encode) the given object to the given filename or
    file object. See` BSDFSerializer` for details on extensions and options.
    wbN)r5   r-   r.   openr   )r    r   r>   r?   r2   fps         r   r   r     s     	z--W--A!\"" !T]] 	"b66"b>>	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" vva}}s   AAAc                 F    t          |fi |}|                    |           S )zpLoad a (BSDF-encoded) structure from bytes.
    See `BSDFSerializer` for details on extensions and options.
    )r5   r   )r*   r>   r?   r2   s       r   r   r     r  r   c                 V   t          |fi |}t          | t                    rs|                     d          rt          j                            |           } t          | d          5 }|                    |          cddd           S # 1 swxY w Y   dS |                    |           S )zLoad a (BSDF-encoded) structure from the given filename or file object.
    See `BSDFSerializer` for details on extensions and options.
    )z~/z~\rbN)	r5   r-   r.   
startswithospath
expanduserr  r   )r    r>   r?   r2   r  s        r   r   r   $  s     	z--W--A!\"" <<&& 	&""1%%A!T]] 	b66"::	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 vvayys   'B		BBc                   2    e Zd ZdZdZdZd Zd Zd Zd Z	dS )	rY   aC  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 attributes 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.

     r7   c                 R    d| j         dt          t          |                     dS )Nz<BSDF extension z at 0x>)rZ   hexidr   s    r   __repr__zExtension.__repr__[  s%     04			3r$xx====IIr   c                 ,    t          || j                  S r   )r-   r^   r=   r2   r   s      r   r0   zExtension.match^  s    !TX&&&r   c                     t                      r   NotImplementedErrorr#  s      r   r%   zExtension.encodea      !###r   c                     t                      r   r%  r#  s      r   r   zExtension.decoded  r'  r   N)
r   r   r   r   rZ   r^   r!  r0   r%   r   r7   r   r   rY   rY   >  si         2 D
CJ J J' ' '$ $ $$ $ $ $ $r   rY   c                   "    e Zd ZdZeZd Zd ZdS )ComplexExtensionr   c                     |j         |j        fS r   )realimagr#  s      r   r%   zComplexExtension.encodel  s    r   c                 :    t          |d         |d                   S )Nr   r   )complexr#  s      r   r   zComplexExtension.decodeo  s    qtQqT"""r   N)r   r   r   rZ   r/  r^   r%   r   r7   r   r   r*  r*  h  s<        D
C     # # # # #r   r*  c                   *    e Zd ZdZd Zd Zd Zd ZdS )NDArrayExtensionndarrayc                 F    dt           j        v rdd l}|j        | _        d S d S )Nnumpyr   )sysmodulesr4  r2  r^   )r=   nps     r   rA   zNDArrayExtension.__init__v  s0    ck!!zDHHH "!r   c                 b    t          |d          ot          |d          ot          |d          S )Nshapedtypetobytes)r   r#  s      r   r0   zNDArrayExtension.match|  s0    q'""Twq'':':Twq)?T?TTr   c                 x    t          |j        t          |j                  |                                          S )N)r9  r:  data)r   r9  	text_typer:  r;  r#  s      r   r%   zNDArrayExtension.encode  s+    !'17););!))++NNNNr   c                     	 dd l }n# t          $ r |cY S w xY w|                    |d         |d                   }|d         |_        |S )Nr   r=  r:  )r:  r9  )r4  ImportError
frombufferr9  )r=   r2   r   r7  as        r   r   zNDArrayExtension.decode  sg    	 	 	 	HHH	MM!F)1W:M66G*s    N)r   r   r   rZ   rA   r0   r%   r   r7   r   r   r1  r1  s  s[        D" " "U U UO O O    r   r1  __main__r   )>r   
__future__r   r   r   rE   r   loggingr  r/   r   r5  typesrD   ior   	getLoggerr   r\   r   joinr   version_infor   r   r>  r.   r}   r   r{   rW   ra   basicConfigunicode
basestringlong	ClassTyper   r   unpackr   r   r"   r+   r3   objectr5   r   r   r   r%   r   r   r   loadsdumpsrY   r*  r1  r:   bsdf_climainr7   r   r   <module>rV     s_  
 & A @ @ @ @ @ @ @ @ @ 



   				 				  



        		8	$	$ hh//w///// 
qQ 'ILMMJJGILM$KMu&J 	M	$ $ $      _ _ _ _ _V _ _ _J       <T T T T T T T Tnt< t< t< t< t<6 t< t< t<t   	 	 	 	       	'$ '$ '$ '$ '$ '$ '$ '$T# # # # #y # # #    y   2 ()9:  zOOOHMOOOOO	 r   