
    ⨽g                        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j)                  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jH                  fZe
jJ                  Z&e
jN                  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<jz                          yy)!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              #   2   K   | ]  }t        |        y wN)str).0is     ^/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/imageio/plugins/_bsdf.py	<genexpr>r   0   s     /!s1v/s      c                 >    | 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(     	CxT1~ UC##    c                     t        d| j                  d            d   }|dk(  rt        d| j                  d            d   }|S )z'Decode an unsigned integer from a file.r   r   r   r   <Q   )	strunpackread)fns     r   	lendecoder"   Y   s@    $q	"1%ACxdAFF1I&q)Hr   c                 |    |9|j                  d      }| j                         t        t        |            z   |z   S | S )z9Encode the type identifier, with or without extension id.UTF-8)encodeupperr   len)bext_idbbs      r   encode_type_idr+   a   s:    ]]7#wwy73r7++b00r   c                     t        | t              xr? t        j                  d| t        j                        xr 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   sD     	1l# 	+HHXq"**-	+HHY"d*r   c                   Z    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y)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                  |         | j                  di | y )N )_extensions_extensions_by_clsstandard_extensionsadd_extension_parse_options)self
extensionsoptions	extensions       r   __init__zBsdfSerializer.__init__   sM    "$,J# 	*Iy)	*&g&r   c                    t        |t              r&dddd}|j                  |j                         |      }|dvrt	        d      || _        t        |      | _        t        |      | _        t        |      | _	        t        |      | _
        y )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!A.A%% 1 1 3[AKi'TUU' ",/W  $N3y/r   c                 R   t        |t              rt        |t              st	        d       |       }|j
                  }t        |t              st	        d      t        |      dk(  st        |      dkD  rt        d      || j                  v rt        j                  d|z         |j                  }|sg }nt        |t        t        f      r|}n|g}|D ]  }t        |t              r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   s2    -*_i2XHII#%	 ~~$$9::t9>SY_Q  4###NN*,01 mmDeT]+D5D 	DCc:. BCC	D
  	BC+/1A1A+AD##C(	B!*r   c                 >   t        |t              st        d      || j                  v r| j                  j	                  |       t        | j                  j                               D ]3  }| j                  |   d   |k(  s| j                  j	                  |       5 y)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7 	1C&&s+A.$6''++C0	1r   c                 	   t         }||j                   |d|             y|du r|j                   |d|             y|du r|j                   |d|             yt        |t              rZd|cxk  rdk  r)n n&|j                   |d	|      t	        d
|      z          y|j                   |d|      t	        d|      z          yt        |t
              rX| j                  r&|j                   |d|      t	        d|      z          y|j                   |d|      t	        d|      z          yt        |t              rP|j                  d      }|j                   |d|      t        t        |            z          |j                  |       yt        |t        t        f      rI|j                   |d|      t        t        |            z          |D ]  }| j                  |||d        yt        |t              r|j                   |d|      t        t        |            z          |j                         D ]  \  }}t         r|j#                         sJ t%        |      sJ |j                  d      }	|j                  t        t        |	                   |j                  |	       | j                  |||d        yt        |t&              rL|j                   |d|             t)        || j*                  | j,                        }
|
j/                  |       yt        |t(              r*|j                   |d|             |j/                  |       yt        |t0              r|j2                  dk7  rt5        d      t        |t6              r'|j                   |d|      t	        ddd      z          nt9        d      t        |      dkD  rt5        d      |j;                  |       |j=                  || j                  | j>                         y|t5        d|z        | j@                  jC                  |jD                  d      }|F| jF                  j                         D ]'  \  }}|jI                  | |      s||j                  f} n d}|!|\  }}| j                  | || |      ||       yd}t9        ||jD                  jJ                  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
   =GGAdFO$d]GGAdFO$e^GGAdFO$}-'%'$%U*;;<$%e*<<=u%}}$%e*<<=$%e*<<=}-g&BGGAdFOgc"g&667GGBKe}-GGAdFOgc%j&99: 2Q7D12t$GGAdFOgc%j&99:++- 	2Q++---(---G,F,-Q7D1	2 u%GGAdFO$4#4#44CUCUD MM!t$GGAdFO$NN1z*zzS  !GHHE:.$%sA*>>? >?? 7|a !EFFNN5!OOAt||T\\:! >@FG  ((,,U__dCBz#//557 GD!wwtU+!188^
 B~,.))Q 0u =wP/   EOO$<$< <==r   c                    |j                  d      }|j                         }|s
t               ||k7  r?t        d|j                  d            d   }|j                  |      j	                  d      }nd}|dk(  rd}nb|dk(  rd}nY|d	k(  rd
}nP|dk(  r t        d|j                  d            d   }n+|dk(  r t        d|j                  d            d   }n|dk(  r t        d|j                  d            d   }n|dk(  r t        d|j                  d            d   }n|dk(  rct        d|j                  d            d   }|dk(  rt        d|j                  d            d   }|j                  |      j	                  d      }nT|dk(  r&t        d|j                  d            d   }|dk\  r|dk(  }t        d|j                  d            d   }| j
                  r8t        |r|nd      }|j                  || j                  | j                         n|r)t        |      D 	cg c]  }	| j                  |       }}	ng }	 	 |j                  | j                  |             !|dk(  rt        d|j                  d            d   }t        |      D 	cg c]  }	| j                  |       }}	n(|dk(  rt               }t        d|j                  d            d   }|dk(  rt        d|j                  d            d   }t        |      D ]~  }	t        d|j                  d            d   }
|
dk(  rt        d|j                  d            d   }
|
dkD  sJ |j                  |
      j	                  d      }| j                  |      ||<    nK|dk(  r8| j                  rt        |df      }n,t        |d
f      }|j                         }nt!        d|z        |J| j"                  j%                  |d      }||j	                  | |      }|S t&        j)                  d|z         |S c c}	w # t        $ r Y ^w xY wc c}	w ) 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      rrv   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    vvayJJL *QY$q	*1-AVVAY%%g.FF9E$YE$YE$YdAFF1I.q1E$YdAFF1I.q1E$YdAFF1I.q1E$YdAFF1I.q1E$YD!&&),Q/CczaffQi03FF3K&&w/E$Y$q	*1-ACxcdAFF1I.q1''&Fq<EOOAt||T\\B6;Ah?T\\!_?E?E"!LLa9 # 8!$q	215A27(;Qa;;$YFE$q	*1-ACxdAFF1I.q11X ."43A6S=&tQVVAY7:Fz!zvvf~,,W5"ll1od. $YaYQJ'(/$677 ((,,VT:I$!((u5  H6QRU @ $  <s   *P-	"P2 Q2	P>=P>c                 Z    t               }| j                  ||       |j                         S )zSave the given object to bytes.)r   savegetvalue)r=   obr    s      r   r%   zBsdfSerializer.encode  s#    I		!Rzz|r   c                 z   |j                  d       |j                  t        j                  dt        d                |j                  t        j                  dt        d                g }| j	                  |||d       t        |      dkD  r.|d   }|j                  |j                         k7  rt        d      yy)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  AFFH, Q  - r   c                 :    t        |      }| j                  |      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    BKyy|r   c                    |j                  d      }|dk7  rt        d|z        t        d|j                  d            d   }t        d|j                  d            d   }d||fz  }|t        d   k7  rd}t        |t        |fz        |t        d   kD  r d	}t
        j                  |t        |fz         | j                  |      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     VVAY=H2MNN!$q	215!$q	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   sO    <' *0)V1Y>vXt&
r   r5   c                   .    e Zd ZdZddZd Zed        Zy)r   zBase class for streams.c                     d| _         d| _        t        |t              r
|| _        d}n|dk(  rd| _        |dv sJ || _        d | _        d| _        y )Nr   r   rx   )r   rx   )_i_countr-   int_mode_fr   )r=   r   s     r   rA   zBaseStream.__init__  sW    dC DKDS[DKz!!!
r   c                     | j                   t        d      || _         | j                   j                         | _        || _        || _        y )Nz(Stream object cannot be activated twice?)r   IOErrorr   r   r~   r   )r=   fileencode_funcdecode_funcs       r   r   zBaseStream._activate  s>    77DEE'',,.""r   c                     | j                   S )z$The mode of this stream: 'r' or 'w'.)r   r=   s    r   r   zBaseStream.mode  s     zzr   N)rx   )r   r   r   r   rA   r   propertyr   r7   r   r   r   r     s#    !#  r   r   c                   P    e Zd ZdZed        Zed        Zd Zd
dZd Z	d Z
d Zy	)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     wwr   c                 N   | j                   | j                  k7  rt        d      | j                  t        d      | j                  j                  rt        d      | j                  | j                  || gd       | xj                  dz  c_        | xj                   dz  c_         y)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77?JKK77>>:;;TWWdTFD11qr   c                 ,   | j                   | j                  k7  rt        d      | j                  t        d      | j                  j                  rt        d      | j                  j                         }| j                  j                  | j                  dz
  dz
         | j                  j                  t        d|rdnd	             | j                  j                  t        d
| j                                | j                  j                  |       y)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77?IJJ77>>BCCGGLLNT__q(1,-eD#s;<eD$++./Qr   c                 "   | j                   dk7  rt        d      | j                  t        d      t        | j                  dd      rt        d      | j                  dk\  rS| j
                  | j                  k\  r
t               | xj
                  dz  c_        | j                  | j                        S 	 | j                  | 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77?IJJ477Hd+CDD;;!ww$++%#o%GGqLG<<((&ll477+1
 &"gg#o%&s   71C) )%Dc                 :    | j                   dk7  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r   c                 "    | j                         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     sH         (&2
r   r   c                   N    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y)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.
    c                 ~   t        |t              rAd | _        | j                  ||      | _        || _        | j                  |z   | _        || _        y t        |t              rRt        |      dk(  rDt        |d   d      r5|\  | _        }d | _        | j                  | j                  |       d| _        y 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"s2w!|1v8N"$DGZ"DOOODGGZ0"DN<==r   c                     |dk(  r|}n?|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   zUnknown compression identifier)rD   compressrE   r'   	data_sizer   )r=   r   rO   r   s       r   r   zBlob._from_bytesg  sf    !JAua0JAeQ/J:::5UZr   c                 x   | j                   dk  r~| j                  dk(  ro|j                  t        d| j                                |j                  t        d| j                               |j                  t        | j                               nr|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                         z          n|j                  d       | j                  dk(  rId	|j                         d
z   d	z  z
  }|j                  t        d|             |j                  d|z         n|j                  t        dd             |j                  | j                         |j                  d| j                   | j                  z
  z         y)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 345GGE$/0GGGDNN+,GGE%d&9&9:;GGE%dnn56GGE%dnn56	c4++,-GGGgkk$//:AACCDGGGq QVVX\Q..IGGE$	*+GGGi'(GGE$N#	 	4..?@Ar   c                    t        d|j                  d            d   }|dk(  rt        d|j                  d            d   }t        d|j                  d            d   }|dk(  rt        d|j                  d            d   }t        d|j                  d            d   }|dk(  rt        d|j                  d            d   }t        d|j                  d            d   }t        d|j                  d            d   }|r|j                  d      }t        d|j                  d            d   }	|j                  |	       |rH|j                         | _        | j                  |z   | _        |j                  | j                  |z          n8d| _        d| _        |j                  |      | _        |j                  ||z
         |	| _        || _        |rnd| _	        || _
        || _        || _        y)	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3A6S &tQVVAY7:NdAFF1I.q1	!$q	215IdAFF1I.q1	!$q	215IaffQi03 qvvay1!4vvbzHdAFF1I.q1		yVVXDN>>I5DLFF4>>N23!DNDLffY/DOFF>I-."&(4H$","r   c                     | j                   t        d      |dk  r| j                  |z   }|dk  s|| j                  kD  rt        d      | j                   j	                  | j
                  |z          y)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  sm    77?S  q5##a'Aq5A+++899T^^a'(r   c                 ~    | j                   t        d      | j                   j                         | j                  z
  S )zCGet the current file pointer position (relative to the blob start).z>Cannot tell in a blob that is not created by the BSDF decoder.)r   r   r   r   r   s    r   r   z	Blob.tell  s6    77?S  ww||~..r   c                    | j                   t        d      | j                  rt        d      | j                   j	                         t        |      z   | j                  kD  rt        d      d| _        | j                   j                  |      S )zWrite bytes to the blob.z?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  sw    77?T  HII77<<>CG#dll29::ww}}R  r   c                     | j                   t        d      | j                  rt        d      | j                   j	                         |z   | j
                  kD  rt        d      | j                   j                  |      S )zRead n bytes from the blob.z>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  sk    77?S  GHH77<<>A,899ww||Ar   c                    | j                   | j                   }nk| j                  j                         }| j                  d       | j                  j	                  | j
                        }| j                  j                  |       | j                  dk(  r|}|S | j                  dk(  rt        j                  |      }|S | j                  dk(  rt        j                  |      }|S t        d| j                  z        )z&Get the contents of the blob as bytes.r   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    ??&JAIIaLdnn5JGGLLOq E  "OOJ/E
 	 "NN:.E  7$:J:JJKKr   c                    | j                   r| j                  r| j                  d       | j                  j	                  | j
                        }| j                  j                  | j                  | j                  z
  dz
  dz
         | j                  j                  t        j                  |      j                                yyy)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    
 IIaLdnn5JGGLL$..81<rABGGMM'++j188:;	 "0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8%#N
)/!
&	<r   r   c                 <    t        |fi |}|j                  |       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 }|j	                  ||      cddd       S |j	                  | |      S # 1 sw Y   yxY w)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     sa     	z-W-A!\"!T] 	"b66"b>	" 	" vva}	" 	"s   AA c                 <    t        |fi |}|j                  |       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                 "   t        |fi |}t        | t              rW| j                  d      rt        j
                  j                  |       } t        | d      5 }|j                  |      cddd       S |j                  |       S # 1 sw Y   yxY w)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":	 	 vvay	 	s   BBc                   0    e Zd ZdZdZdZd Zd Zd Zd Z	y)	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                 L    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$x=IIr   c                 .    t        || j                        S r   )r-   r^   r=   r2   r   s      r   r0   zExtension.match^  s    !TXX&&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   >  s(    2 D
CJ'$$r   rY   c                        e Zd ZdZeZd Zd Zy)ComplexExtensionr   c                 2    |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y)NDArrayExtensionndarrayc                 T    dt         j                  v rdd l}|j                  | _        y y )Nnumpyr   )sysmodulesr/  r-  r^   )r=   nps     r   rA   zNDArrayExtension.__init__v  s!    ckk!zzDH "r   c                 R    t        |d      xr t        |d      xr t        |d      S )Nshapedtypetobytes)r   r  s      r   r0   zNDArrayExtension.match|  s'    q'"Twq'':Twq)?TTr   c                 t    t        |j                  t        |j                        |j	                               S )N)r4  r5  data)r   r4  	text_typer5  r6  r  s      r   r%   zNDArrayExtension.encode  s$    !''177);!))+NNr   c                 x    	 dd l }|j                  |d   |d         }|d   |_        |S # t        $ r |cY S w xY w)Nr   r8  r5  )r5  r4  )r/  ImportError
frombufferr4  )r=   r2   r   r2  as        r   r   zNDArrayExtension.decode  sL    	 MM!F)1W:M6G*	  	H	s   + 99N)r   r   r   rZ   rA   r0   r%   r   r7   r   r   r,  r,  s  s    D"UOr   r,  __main__r   )>r   
__future__r   r   r   rE   r   loggingr  r/   r   r0  typesrD   ior   	getLoggerr   r\   r   joinr   version_infor   r   r9  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%  r,  r:   bsdf_climainr7   r   r   <module>rQ     s  
& A @ 
   	 	  
   			8	$ hh/w// 
qQILMMJGILM$KMu&J 	MM	$_V _J <T Tnt<6 t<t	 	'$ '$T#y #y 2 ()9:  zHMMO	 r   