
    .Ph%                        d 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mZ 	 ddl	Z
n# e$ r dZ
Y nw xY wej        Zej        ZeZ	 ddlZn# e$ r dZY nw xY wdZd Zd Zd Zd	 Zd
 Zd Zd Zej        dd            ZddZdZddZd Z dS )zFUtilities for fast persistence of big data, with optional compression.    N   )_COMPRESSORS_ZFILE_PREFIXi   c                 X    t          | d|           } t          | t          j                  S )z=Check if fileobj is a raw file object, e.g created with open.raw)getattr
isinstanceioFileIO)fileobjs    Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/joblib/numpy_pickle_utils.py_is_raw_filer   &   s%    gug..Ggry)))    c                      d t          j                    D             } | t          t                    gz  } t	          |           S )Nc                 6    g | ]}t          |j                  S  )lenprefix).0
compressors     r   
<listcomp>z)_get_prefixes_max_len.<locals>.<listcomp>.   s#    OOO:J%&&OOOr   )r   valuesr   r   max)prefixess    r   _get_prefixes_max_lenr   ,   s?    OO9L9N9NOOOH]##$$Hx==r   c                    t           j        dk    o`| j        j        dk    pP| j        j        dk    o@| j        j        o4t	          d | j        j                                        D                       ppt           j        dk    o`| j        j        dk    pP| j        j        dk    o@| j        j        o4t	          d | j        j                                        D                       S )z2Check if numpy array is having byte order mismatchbig<|c              3   8   K   | ]}|d          j         dk    V  dS )r   r   N	byteorderr   es     r   	<genexpr>z6_is_numpy_array_byte_order_mismatch.<locals>.<genexpr><   -      SS!!#-SSSSSSr   little>c              3   8   K   | ]}|d          j         dk    V  dS )r   r(   Nr!   r#   s     r   r%   z6_is_numpy_array_byte_order_mismatch.<locals>.<genexpr>F   r&   r   )sysr"   dtypefieldsallr   arrays    r   #_is_numpy_array_byte_order_mismatchr0   3   s     	 	
K!S( %, TK&TSSu{7I7P7P7R7RSSSSS 	! 	
K!S( %, TK&TSSu{7I7P7P7R7RSSSSS#r   c                     t          |           r?|                                                     | j                            d                    } | S )zzUse the byte order of the host while preserving values

    Does nothing if array already uses the system byte order.
    =)r0   byteswapviewr+   newbyteorderr.   s    r   _ensure_native_byte_orderr6   L   sG    
 +511 E  %%ek&>&>s&C&CDDLr   c                 h   t                      }t          | d          r|                     |          }n*|                     |          }|                     d           |                    t                    rdS t          j                    D ]#\  }}|                    |j	                  r|c S $dS )zReturn the compressor matching fileobj.

    Parameters
    ----------
    fileobj: file object

    Returns
    -------
    str in {'zlib', 'gzip', 'bz2', 'lzma', 'xz', 'compat', 'not-compressed'}
    peekr   compatnot-compressed)
r   hasattrr8   readseek
startswithr   r   itemsr   )r   max_prefix_lenfirst_bytesnamer   s        r   _detect_compressorrC   X   s     +,,Nw  ll>22 ll>22Qm,, x , 2 4 4 	 	D*%%j&788  r   c                 8    t          j        | t                    S )z0Return a buffered version of a read file object.buffer_size)r
   BufferedReader_IO_BUFFER_SIZEfobjs    r   _buffered_read_filerK   x       T????r   c                 8    t          j        | t                    S )z1Return a buffered version of a write file object.rE   )r
   BufferedWriterrH   rI   s    r   _buffered_write_filerO   }   rL   r   c              #   T  K   t          |           }|}|dk    r't          j        d|z  t          d           ||fV  dS |t          v r1t          |         }|                    |           }t          |          } |d}t          | t          j	                  r&t          j        dt                      z  d           nc|dk    r&t          j        dt                      z  d           n7t          |           s&t          j        d	t                      z  d           n|}| |fV  dS )
a  Utility function opening the right fileobject from a filename.

    The magic number is used to choose between the type of file object to open:
    * regular file object (default)
    * zlib file object
    * gzip file object
    * bz2 file object
    * lzma file object (for xz and lzma compressor)

    Parameters
    ----------
    fileobj: file object
    filename: str
        filename path corresponding to the fileobj parameter.
    mmap_mode: str
        memory map mode that should be used to open the pickle file. This
        parameter is useful to verify that the user is not trying to one with
        compression. Default: None.

    Returns
    -------
        a tuple with a file like object, and the validated mmap_mode.

    r9   zjThe file '%s' has been generated with a joblib version less than 0.10. Please regenerate this pickle file.   )
stacklevelNzuIn memory persistence is not compatible with mmap_mode "%(mmap_mode)s" flag passed. mmap_mode option will be ignored.r:   ztmmap_mode "%(mmap_mode)s" is not compatible with compressed file %(filename)s. "%(mmap_mode)s" flag will be ignored.zP"%(fileobj)r" is not a raw file, mmap_mode "%(mmap_mode)s" flag will be ignored.)rC   warningswarnDeprecationWarningr   decompressor_filerK   r	   r
   BytesIOlocalsr   )r   filename	mmap_moder   validated_mmap_modecompressor_wrapperinsts          r   _validate_fileobject_and_memmapr^      s     6 $G,,J#X 	24<= 	
 	
 	
 	
 +++++++%% ".j!9%77@@D)$//G
  "&'2:.. 08:@((C  !	     ///<>DhhG  !	     "'** 0<>DhhG      '0#*******r   zlib   c                    |d         }|d         }|t          j                    v r1t           |                             | |          }t          |          S t           d                             | |          }t          |          S )z6Return the right compressor file object in write mode.r   r   )compresslevelr`   )r   keyscompressor_filerO   )rY   compresscompressmethodrc   file_instances        r   _write_fileobjectri      s    a[NQKM*,,,,$^4DDM E 
 
 $M222$V,<<M = 
 
 $M222r   i   ran out of datac                 d   t                      }	 	 |                     |t          |          z
            }||z  }t          |          dk    st          |          |k    rnn# t          j        $ r Y nw xY wit          |          |k    r$d}t          |||t          |          fz            |S )a  Read from file-like object until size bytes are read.

    TODO python2_drop: is it still needed? The docstring mentions python 2.6
    and it looks like this can be at least simplified ...

    Raises ValueError if not EOF is encountered before size bytes are read.
    Non-blocking objects only supported if they derive from io objects.

    Required as e.g. ZipExtFile in python 2.6 can return less data than
    requested.

    This function was taken from numpy/lib/format.py in version 1.10.2.

    Parameters
    ----------
    fp: file-like object
    size: int
    error_template: str

    Returns
    -------
    a bytes object
        The data read in bytes.

    Tr   z)EOF: reading %s, expected %d bytes got %d)bytesr<   r   r
   BlockingIOError
ValueError)fpsizeerror_templatedatarmsgs         r   _read_bytesru      s    4 77D
	s4yy())AAID1vv{{c$ii4// 0! 	 	 	D	
 4yyD9c$ii@@AAAs   AA# #A54A5c                  l    t           j        d d         }|dk    rddlm} n|dk    rddlm}  || i |S )NrQ   z1.r   )_reconstructz2.)np__version__numpy.core.multiarrayrw   numpy._core.multiarray)argskwargsnp_major_versionnp_reconstructs       r   rw   rw     sg     ~bqb)4HHHHHHH	T	!	!IIIIII>4*6***r   )N)r_   )rj   )!__doc__
contextlibr
   pickler*   rS   r   r   r   numpyrx   ImportError
_Unpickler	Unpickler_PicklerPicklerrangexrangebz2rH   r   r   r0   r6   rC   rK   rO   contextmanagerr^   ri   BUFFER_SIZEru   rw   r   r   r   <module>r      s   L L     				  



  3 3 3 3 3 3 3 3   	BBB 	
/	
 JJJJ   
CCC * * *    2    @@ @ @
@ @ @
 M+ M+ M+ M+`3 3 3 3( * * * *Z+ + + + +s   % //A AA