
    &`i|                     2   d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
 ddlmZ ddlmZ e G d de                      Ze G d	 d
ej                              Ze G d de                      Ze G d de                      Z G d d          ZdS )z
Serialization handlers for preprocessor save/load functionality.

This module implements a factory pattern to abstract different serialization formats,
making it easier to add new formats and maintain existing ones.
    N)Enum)AnyDictOptionalUnion)cloudpickle)DeveloperAPIc                       e Zd ZdZdZdZdS )HandlerFormatNamez1Enum for consistent format naming in the factory.r   pickleN)__name__
__module____qualname____doc__CLOUDPICKLEPICKLE     /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/data/preprocessors/serialization_handlers.pyr   r      s        ;;KFFFr   r   c                      e Zd ZdZej        dedeee	f         f         deee
f         fd            Zej        deee
f         de	fd            Zej        deee
f         fd            Zdeee
f         deee
f         fd	Zd
S )SerializationHandlerzDAbstract base class for handling preprocessor serialization formats.dataPreprocessorreturnc                     dS )zSerialize preprocessor data to the specific format.

        Args:
            data: Dictionary containing preprocessor metadata and stats

        Returns:
            Serialized data in format-specific representation
        Nr   selfr   s     r   	serializezSerializationHandler.serialize   s	     	r   
serializedc                     dS )aU  Deserialize data from the specific format.

        Args:
            serialized: Serialized data in format-specific representation

        Returns:
            For structured formats (CloudPickle/JSON/MessagePack): Dictionary containing preprocessor metadata and stats
            For pickle format: The actual deserialized object
        Nr   r   r   s     r   deserializez SerializationHandler.deserialize,   s	     	r   c                     dS )z+Get the magic bytes/prefix for this format.Nr   r   s    r   get_magic_bytesz$SerializationHandler.get_magic_bytes9   s	     	r   c                     |                                  }t          |t          t          f          r,|                    |          r|t          |          d         S |S )z(Remove magic bytes from serialized data.N)r%   
isinstancestrbytes
startswithlen)r   r   magics      r   strip_magic_bytesz&SerializationHandler.strip_magic_bytes>   sY    $$&&j3,// 	,J4I4I%4P4P 	,c%jjll++r   N)r   r   r   r   abcabstractmethodr   r   r(   r   r)   r   r"   r%   r-   r   r   r   r   r      s
       NN.$sCx.89	sEz	    	
eCJ&7 
C 
 
 
 
 	sEz!2    E#u*,= %U
BS      r   r   c                   r    e Zd ZdZdZdedeeef         f         de	fdZ
de	deeef         fdZde	fd	Zd
S )CloudPickleSerializationHandlerz-Handler for CloudPickle serialization format.s   CPKL:r   r   r   c                 :    | j         t          j        |          z   S )z2Serialize to CloudPickle format with magic prefix.)MAGIC_CLOUDPICKLEr   dumpsr   s     r   r   z)CloudPickleSerializationHandler.serializeL   s     %(9$(?(???r   r   c                 $   t          |t                    st          dt          |                     |                    | j                  st          d|dd                    |                     |          }t          j        |          S )z$Deserialize from CloudPickle format.z4Expected bytes for CloudPickle deserialization, got z!Invalid CloudPickle magic bytes: N
   )	r'   r)   
ValueErrortyper*   r3   r-   r   loads)r   r   cloudpickle_datas      r   r"   z+CloudPickleSerializationHandler.deserializeR   s    *e,, 	YtJGWGWYY   $$T%;<< 	TRCRCRRSSS11*== !1222r   c                     | j         S )N)r3   r$   s    r   r%   z/CloudPickleSerializationHandler.get_magic_bytes_   s    %%r   N)r   r   r   r   r3   r   r   r(   r   r)   r   r"   r%   r   r   r   r1   r1   F   s        77 @.$sCx.89@	@ @ @ @3e 3S#X 3 3 3 3& & & & & & &r   r1   c                   ^    e Zd ZdZdedeeef         f         defdZdedefdZ	defdZ
d	S )
PickleSerializationHandlerz/Handler for legacy Pickle serialization format.r   r   r   c                 t    t          j        t          j        |                                        d          S )z
        Serialize using pickle format (for backward compatibility).
        data is ignored, but kept for consistency

        ascii)base64	b64encoder   r4   decoder   s     r   r   z$PickleSerializationHandler.serializeg   s-     T 2 233::7CCCr   r   c                 N    t          j        t          j        |                    S )z0Deserialize from pickle format (legacy support).)r   r9   r@   	b64decoder!   s     r   r"   z&PickleSerializationHandler.deserializeq   s    
 |F,Z88999r   c                     dS )N r   r$   s    r   r%   z*PickleSerializationHandler.get_magic_bytesx   s    rr   N)r   r   r   r   r   r   r(   r   r   r"   r%   r   r   r   r=   r=   c   s        99D.$sCx.89D	D D D D::	: : : :      r   r=   c            	           e Zd ZdZej        eej        eiZ	e
dedefd            Ze
	 	 ddee         deeeef                  defd	            Ze
d
eeef         defd            ZdS )SerializationHandlerFactoryz>Factory class for creating appropriate serialization handlers.format_namehandler_classc                     || j         |<   dS )z%Register a new serialization handler.N)	_handlers)clsrI   rJ   s      r   register_handlerz,SerializationHandlerFactory.register_handler   s     &3k"""r   Nformat_identifierr   r   c           	          |s|                      |          }|| j        vr>t          d|j         dt	          | j                                                             | j        |         } |            S )a  Get the appropriate serialization handler for a format or serialized data.

        Args:
            format_identifier: The format to use for serialization. If None, will detect from data.
            data: Serialized data to detect format from (used when format_identifier is None).
            **kwargs: Additional keyword arguments (currently unused).

        Returns:
            SerializationHandler instance for the format

        Raises:
            ValueError: If format is not supported or cannot be detected
        z"Unsupported serialization format: z. Supported formats: )detect_formatrL   r7   valuelistkeys)rM   rO   r   kwargsrJ   s        r   get_handlerz'SerializationHandlerFactory.get_handler   s    * ! 	8 # 1 1$ 7 7CM11C5F5L C C&*3=+=+=+?+?&@&@C C  
 &78}r   r   c                     t          |t                    r+|                    t          j                  rt
          j        S t          |t                    rt
          j        S t          d|dd          d          )zDetect the serialization format from the magic bytes.

        Args:
            serialized: Serialized data

        Returns:
            Format name enum

        Raises:
            ValueError: If format cannot be detected
        z)Cannot detect serialization format from: N   z...)
r'   r)   r*   r1   r3   r   r   r(   r   r7   )rM   r   s     r   rQ   z)SerializationHandlerFactory.detect_format   s     j%(( 	1Z-B-B+=.
 .
 	1 %00 j#&& 	,$++L
3B3LLL
 
 	
r   )NN)r   r   r   r   r   r   r1   r   r=   rL   classmethodr8   rN   r   r   r(   r)   r   rV   rQ   r   r   r   rH   rH   |   s        HH 	%'F "<I
 3+< 3T 3 3 3 [3  :>,0 #$56 uS%Z()
 
   [@ 
uS%Z'8 
=N 
 
 
 [
 
 
r   rH   )r   r.   r@   r   enumr   typingr   r   r   r   ray.cloudpickler   ray.util.annotationsr	   r   ABCr   r1   r=   rH   r   r   r   <module>r_      s    


         - - - - - - - - - - - - ' ' ' ' ' ' - - - - - -         ( ( ( ( (37 ( ( (V & & & & &&: & & &8     !5   0G
 G
 G
 G
 G
 G
 G
 G
 G
 G
r   