o
    NDi                     @   sL   d dl mZ d dlmZ d dlmZ e Zdd ZG dd deZ	e	 Z
dS )	    )OrderedDict)warn)	namespacec                 C   s.   |r| j jt| j i dS | j jt| j dS )N)typer   options)r   r   )	__class____name__r   )objpolicy r   ]/var/www/Datamplify/venv/lib/python3.10/site-packages/cassandra/datastax/insights/registry.py_default_serializer_for_object   s   r   c                   @   sN   e Zd ZdZdddZdedfddZdd Zd	d
 Zdd Z	e
dd ZdS )InsightsSerializerRegistryFNc                    s.    pi  |   }t fdd|D | _d S )Nc                 3   s    | ]	}| | fV  qd S Nr   .0clsmapping_dictr   r   	<genexpr>+   s    z6InsightsSerializerRegistry.__init__.<locals>.<genexpr>)_class_topological_sortr   _mapping_dict)selfr   class_orderr   r   r   __init__'   s
   

z#InsightsSerializerRegistry.__init__c                 C   sT   z|  |d ur	|n|j|W S  ty)   |tu r#t||}| Y S |}| Y S w r   )_get_serializerr   	Exception_NOT_SETr   )r   r	   r
   defaultr   resultr   r   r   	serialize.   s   
z$InsightsSerializerRegistry.serializec                 C   sR   z| j | W S  ty(   | j  D ]\}}t||r$| j |    Y S qY tw r   )r   KeyErroritems
issubclass
ValueError)r   r   registered_cls
serializerr   r   r   r   9   s   
z*InsightsSerializerRegistry._get_serializerc                    s.   | j |< t fdd  j D  _ d S )Nc                 3   s    | ]
}| j | fV  qd S r   )r   r   r   r   r   r   E   s    z6InsightsSerializerRegistry.register.<locals>.<genexpr>)r   r   r   )r   r   r&   r   r'   r   registerB   s   



z#InsightsSerializerRegistry.registerc                    s    fdd}|S )z
        Parameterized registration helper decorator. Given a class `cls`,
        produces a function that registers the decorated function as a
        serializer for it.
        c                    s     |  | S r   )r(   )r&   r   r   r   r   	decoratorO   s   zEInsightsSerializerRegistry.register_serializer_for.<locals>.decoratorr   )r   r   r*   r   r)   r   register_serializer_forI   s   z2InsightsSerializerRegistry.register_serializer_forc                    s   t | g }}|r.|d |dd  }t fdd|D s%|  n|  |}|s	t|D ]\} ||d d D ]}t| rNtdj| |d q>q2|S )a  
        A simple topological sort for classes. Takes an iterable of class objects
        and returns a list A of those classes, ordered such that A[X] is never a
        superclass of A[Y] for X < Y.

        This is an inefficient sort, but that's ok because classes are infrequently
        registered. It's more important that this be maintainable than fast.

        We can't use `.sort()` or `sorted()` with a custom `key` -- those assume
        a total ordering, which we don't have.
        r      Nc                 3   s    | ]}t | V  qd S r   )r#   )r   xheadr   r   r   g   s    zEInsightsSerializerRegistry._class_topological_sort.<locals>.<genexpr>zKSorting classes produced an invalid ordering.
In:  {classes}
Out: {sorted_})classessorted_)listanyappend	enumerater#   r   format)r0   unsortedr1   tailiafter_head_valuer   r.   r   r   U   s"   

z2InsightsSerializerRegistry._class_topological_sortr   )r   
__module____qualname__initializedr   r   r    r   r(   r+   staticmethodr   r   r   r   r   r   #   s    
	r   N)collectionsr   warningsr    cassandra.datastax.insights.utilr   objectr   r   r   insights_registryr   r   r   r   <module>   s   
W