o
    NDiz                     @   s   d dl Z d dlmZmZ d dlmZ d dlmZ d dlm	Z
 d dlm	Z d dlmZmZmZ e eZg dZG dd	 d	eZG d
d dee
ZG dd deeZeZeZdddZG dd deZG dd deZdS )    N)SimpleGraphStatementGraphProtocol)EXEC_PROFILE_GRAPH_DEFAULT)GraphTraversal)GraphSONWriter)GremlinUserTypeIOdse_graphson2_serializersdse_graphson3_serializers)TraversalBatch_query_from_traversal_DefaultTraversalBatchc                       s<   e Zd Z fddZdd Zdd Zdd Zd	d
 Z  ZS )_GremlinGraphSONWriterAdapterc                    s&   t t| jdi | || _d | _d S )N )superr   __init__context
user_types)selfr   kwargs	__class__r   _/var/www/Datamplify/venv/lib/python3.10/site-packages/cassandra/datastax/graph/fluent/_query.pyr   #   s   
z&_GremlinGraphSONWriterAdapter.__init__c                 C   s
   |  |S N)toDict)r   value_r   r   r   	serialize(      
z'_GremlinGraphSONWriterAdapter.serializec                 C   s
  d }z	| j t| }W n ty%   | j  D ]\}}t||r"|}qY nw | jrn| jd u rRz| jd j| jd  }tt	t
| | _W n tyQ   i | _Y nw t| j v rc|| j t u rct|dsl|snt|| jv rnt}|rz||}W |S  ty   Y |S w |S )Ncluster
graph_name_fields)serializerstypeKeyErroritems
isinstancer   r   _user_typesdictmapreversedtuplehasattrr   get_specialized_serializerAttributeError)r   r   
serializerkeyserr   r   r   r   get_serializer+   s@   


"z,_GremlinGraphSONWriterAdapter.get_serializerc                 C   s   |  |}|r||| S |S r   )r1   dictify)r   objr.   r   r   r   r   K   s   
z$_GremlinGraphSONWriterAdapter.toDictc                 C   s   |  |}||| S r   )r1   
definition)r   r   r.   r   r   r   r4   O   s   
z(_GremlinGraphSONWriterAdapter.definition)	__name__
__module____qualname__r   r   r1   r   r4   __classcell__r   r   r   r   r   !   s     r   c                   @      e Zd ZdS )GremlinGraphSON2WriterNr5   r6   r7   r   r   r   r   r:   T       r:   c                   @   r9   )GremlinGraphSON3WriterNr;   r   r   r   r   r=   X   r<   r=   c                 C   sz   |t jkrt|td}n|t jkr |du rtdt|td}ntd|z|	| }W |S  t
y<   td  w )z
    From a GraphTraversal, return a query string.

    :param traversal: The GraphTraversal object
    :param graphson_protocol: The graph protocol to determine the output format.
    )serializer_mapNz5Missing context for GraphSON3 serialization requires.zUnknown graph protocol: {}z)Error preparing graphson traversal query:)r   GRAPHSON_2_0graphson2_writerr   GRAPHSON_3_0
ValueErrorgraphson3_writerr	   formatwriteObject	Exceptionlog	exception)	traversalgraph_protocolr   graphson_writerqueryr   r   r   r   `   s   


r   c                   @   sd   e Zd ZdZdZdZdddZdd Zdd Zd	d
 Z	e
jfddZdd Zdd Zdd ZeZdS )r
   a$  
    A `TraversalBatch` is used to execute multiple graph traversals in a
    single transaction. If any traversal in the batch fails, the entire
    batch will fail to apply.

    If a TraversalBatch is bounded to a DSE session, it can be executed using
    `traversal_batch.execute()`.
    Nc                 C   s   || _ || _dS )z
        :param session: (Optional) A DSE session
        :param execution_profile: (Optional) The execution profile to use for the batch execution
        N)_session_execution_profile)r   sessionexecution_profiler   r   r   r      s   
zTraversalBatch.__init__c                 C      t  )zc
        Add a traversal to the batch.

        :param traversal: A gremlin GraphTraversal
        NotImplementedErrorr   rI   r   r   r   add      zTraversalBatch.addc                 C   rQ   )z~
        Adds a sequence of traversals to the batch.

        :param traversals: A sequence of gremlin GraphTraversal
        rR   )r   
traversalsr   r   r   add_all   rV   zTraversalBatch.add_allc                 C   rQ   )zL
        Execute the traversal batch if bounded to a `DSE Session`.
        rR   r   r   r   r   execute      zTraversalBatch.executec                 C   rQ   )z
        Return the traversal batch as GraphStatement.

        :param graph_protocol: The graph protocol for the GraphSONWriter. Default is GraphProtocol.GRAPHSON_2_0.
        rR   )r   rJ   r   r   r   as_graph_statement   rV   z!TraversalBatch.as_graph_statementc                 C   rQ   )z4
        Clear a traversal batch for reuse.
        rR   rY   r   r   r   clear   r[   zTraversalBatch.clearc                 C   rQ   r   rR   rY   r   r   r   __len__   s   zTraversalBatch.__len__c                 C   s   d t| S )Nz<TraversalBatch traversals={0}>)rD   lenrY   r   r   r   __str__   s   zTraversalBatch.__str__)NN)r5   r6   r7   __doc__rM   rN   r   rU   rX   rZ   r   r?   r\   r]   r^   r`   __repr__r   r   r   r   r
   y   s    	
r
   c                       sX   e Zd ZdZ fddZdd Zdd Zejdfdd	Z	d
d Z
dd Zdd Z  ZS )r   Nc                    s    t t| j|i | g | _d S r   )r   r   r   _traversals)r   argsr   r   r   r   r      s   
z_DefaultTraversalBatch.__init__c                 C   s"   t |ts	td| j| | S )Nz,traversal should be a gremlin GraphTraversal)r%   r   rB   rc   appendrT   r   r   r   rU      s   
z_DefaultTraversalBatch.addc                 C   s   |D ]}|  | qd S r   )rU   )r   rW   rI   r   r   r   rX      s   z_DefaultTraversalBatch.add_allc                    s.    fdd| j D }dd|}t|S )Nc                    s   g | ]}t | qS r   )r   ).0tr   rJ   r   r   
<listcomp>   s    z=_DefaultTraversalBatch.as_graph_statement.<locals>.<listcomp>z[{0}],)rc   rD   joinr   )r   rJ   r   
statementsrL   r   rh   r   r\      s   z)_DefaultTraversalBatch.as_graph_statementc                 C   sr   | j d u r	td| jr| jnt}| j |j}| j j|jd}|jr+| j	|j|dn| j	|d}| j j
||dS )Nz>A DSE Session must be provided to execute the traversal batch.)r   r   )r   )rP   )rM   rB   rN   r   get_execution_profilegraph_optionsr   r   rJ   r\   execute_graph)r   rP   rn   r   	statementr   r   r   rZ      s   

z_DefaultTraversalBatch.executec                 C   s   | j d d = d S r   )rc   rY   r   r   r   r]      s   z_DefaultTraversalBatch.clearc                 C   s
   t | jS r   )r_   rc   rY   r   r   r   r^      r   z_DefaultTraversalBatch.__len__)r5   r6   r7   rc   r   rU   rX   r   r?   r\   rZ   r]   r^   r8   r   r   r   r   r      s    r   r   )loggingcassandra.graphr   r   cassandra.clusterr   &gremlin_python.process.graph_traversalr   (gremlin_python.structure.io.graphsonV2d0r   GraphSONWriterV2(gremlin_python.structure.io.graphsonV3d0GraphSONWriterV3+cassandra.datastax.graph.fluent.serializersr   r   r	   	getLoggerr5   rG   __all__objectr   r:   r=   r@   rC   r   r
   r   r   r   r   r   <module>   s"   
3
A