o
    NDi2                     @   s  d dl Z d dlZd dlmZ dZzd dlZdZW n	 ey!   Y nw erd dlmZ d dl	m
Z
mZ d dlmZmZ d dlmZ d d	lmZmZmZ d d
lmZmZ d dlmZ d dlmZmZmZmZmZm Z  d dl!m"Z"m#Z# e $e%Z&g dZ'dZ(dZ)G dd deZ*G dd de*Z+G dd de*Z,e+Z-e-Z.e,Z/e/Z0G dd de*Z1G dd de*Z2e1Z3e2Z4G dd de
Z5G dd de6Z7dS dS )    N)FutureFT)Graph)RemoteConnectionRemoteTraversal)	TraverserTraversalSideEffects)GraphTraversal)SessionGraphExecutionProfileEXEC_PROFILE_GRAPH_DEFAULT)GraphOptionsGraphProtocol)_GraphSONContextRowFactory)GremlinGraphSONReaderV2GremlinGraphSONReaderV3dse_graphson2_deserializersgremlin_graphson2_deserializersdse_graphson3_deserializersgremlin_graphson3_deserializers)_DefaultTraversalBatch_query_from_traversal)BaseGraphRowFactorygraph_traversal_row_factory&graph_traversal_dse_object_row_factoryDSESessionRemoteGraphConnectionDseGraphbulkresultc                   @   s   e Zd ZdZdd ZdS )r   a
  
        Base row factory for graph traversal. This class basically wraps a
        graphson reader function to handle additional features of Gremlin/DSE
        and is callable as a normal row factory.

        Currently supported:
          - bulk results
        c                 c   sX    |D ]&}| j |d }|t V  |td}t|d D ]
}t|t V  qqd S )Nr      )graphson_reader
readObject_result_keyget	_bulk_keyrangecopydeepcopy)selfcolumn_namesrowsrow
parsed_rowr   _ r-   a/var/www/Datamplify/venv/lib/python3.10/site-packages/cassandra/datastax/graph/fluent/__init__.py__call__D   s   
zBaseGraphRowFactory.__call__N)__name__
__module____qualname____doc__r/   r-   r-   r-   r.   r   :   s    	r   c                   @      e Zd ZdZeZdeiZdS )_GremlinGraphSON2RowFactory/Row Factory that returns the decoded graphson2.deserializer_mapN)r0   r1   r2   r3   r   graphson_reader_classr   graphson_reader_kwargsr-   r-   r-   r.   r5   M       r5   c                   @   r4   )_DseGraphSON2RowFactoryz<Row Factory that returns the decoded graphson2 as DSE types.r7   N)r0   r1   r2   r3   r   r8   r   r9   r-   r-   r-   r.   r;   S   r:   r;   c                   @   r4   )_GremlinGraphSON3RowFactoryr6   r7   N)r0   r1   r2   r3   r   r8   r   r9   r-   r-   r-   r.   r<   a   r:   r<   c                   @   r4   )_DseGraphSON3RowFactoryz<Row Factory that returns the decoded graphson3 as DSE types.r7   N)r0   r1   r2   r3   r   r8   r   r9   r-   r-   r-   r.   r=   g   r:   r=   c                       sz   e Zd ZdZdZdZdZdef fdd	Ze	dd Z
dd Ze	d	d
 Ze	dd Zdd Zdd Zdd ZeZ  ZS )r   a6  
        A Tinkerpop RemoteConnection to execute traversal queries on DSE.

        :param session: A DSE session
        :param graph_name: (Optional) DSE Graph name.
        :param execution_profile: (Optional) Execution profile for traversal queries. Default is set to `EXEC_PROFILE_GRAPH_DEFAULT`.
        Nc                    s:   t t| d d  t|tstd|| _|| _|| _d S )NzBA DSE Session must be provided to execute graph traversal queries.)	superr   __init__
isinstancer	   
ValueErrorsession
graph_nameexecution_profile)r'   rB   rC   rD   	__class__r-   r.   r?   ~   s   

z(DSESessionRemoteGraphConnection.__init__c                 c   s    | D ]}t |V  qd S N)r   )
traverserstr-   r-   r.   _traversers_generator   s   z5DSESessionRemoteGraphConnection._traversers_generatorc                 C   s   | j | j}|j}| jp|j|_tj|_| j | d }|j	t
jkr&t}n|j	t
jkr:t}| j j|jdd}ntd|j	||_t||j	|}||fS )Nzutf-8)clusterrC   Unknown graph protocol: {})rB   execution_profile_clone_updaterD   graph_optionsrC   r   DSE_GRAPH_QUERY_LANGUAGEgraph_language"_resolve_execution_profile_optionsgraph_protocolr   GRAPHSON_2_0'gremlin_graphson2_traversal_row_factoryGRAPHSON_3_0'gremlin_graphson3_traversal_row_factoryrK   decoderA   formatrow_factoryquery_from_traversal)r'   bytecodeeprN   contextrY   queryr-   r-   r.   _prepare_query   s"   
z.DSESessionRemoteGraphConnection._prepare_queryc              
   C   sP   z| tt| t  W d S  ty' } z|| W Y d }~d S d }~ww rG   )
set_resultr   r   rJ   r   	Exceptionset_exception)
result_setgremlin_futureer-   r-   r.   _handle_query_results   s   
z5DSESessionRemoteGraphConnection._handle_query_resultsc                 C   s   | |  d S rG   )rb   )responserd   r-   r-   r.   _handle_query_error   s   z3DSESessionRemoteGraphConnection._handle_query_errorc                 C   s0   |  |\}}| jj||d}t| |t S N)rD   )r_   rB   execute_graphr   rJ   r   )r'   r[   r^   r\   rH   r-   r-   r.   submit   s   z&DSESessionRemoteGraphConnection.submitc                 C   sD   |  |\}}t }| jj||d}|| j| || j| |S ri   )r_   r   rB   execute_graph_asyncadd_callbackrf   add_errbackrh   )r'   r[   r^   r\   rd   response_futurer-   r-   r.   submitAsync   s   z+DSESessionRemoteGraphConnection.submitAsyncc                 C   s   d | jS )Nz3<DSESessionRemoteGraphConnection: graph_name='{0}'>)rX   rC   )r'   r-   r-   r.   __str__   s   z'DSESessionRemoteGraphConnection.__str__)r0   r1   r2   r3   rB   rC   rD   r   r?   staticmethodrJ   r_   rf   rh   rk   rp   rq   __repr____classcell__r-   r-   rE   r.   r   q   s"    



r   c                   @   sb   e Zd ZdZdZ	 ejZ	 eedfddZ	edde
dfddZeefdd	Zed
d ZdS )r   zX
        Dse Graph utility class for GraphTraversal construction and execution.
        zbytecode-jsonNc                 C   sP   t | tr"| jjD ]}|j}t |tr|js|js|jr!t	d q	t
| ||S )a  
            From a GraphTraversal, return a query string based on the language specified in `DseGraph.DSE_GRAPH_QUERY_LANGUAGE`.

            :param traversal: The GraphTraversal object
            :param graph_protocol: The graph protocol. Default is `DseGraph.DSE_GRAPH_QUERY_PROTOCOL`.
            :param context: The dict of the serialization context, needed for GraphSON3 (tuple, udt).
                            e.g: {'cluster': cluster, 'graph_name': name}
            zrGraphTraversal session, graph_name and execution_profile are only taken into account when executed with TinkerPop.)r@   r   traversal_strategiesremote_connectionr   rB   rC   rD   logwarningr   )	traversalrR   r]   strategyrcr-   r-   r.   rZ      s   


zDseGraph.query_from_traversalc                 C   s*   t  }||}| r|t| ||}|S )a$  
            Returns a TinkerPop GraphTraversalSource binded to the session and graph_name if provided.

            :param session: (Optional) A DSE session
            :param graph_name: (Optional) DSE Graph name
            :param execution_profile: (Optional) Execution profile for traversal queries. Default is set to `EXEC_PROFILE_GRAPH_DEFAULT`.
            :param traversal_class: (Optional) The GraphTraversalSource class to use (DSL).

            .. code-block:: python

                from cassandra.cluster import Cluster
                from cassandra.datastax.graph.fluent import DseGraph

                c = Cluster()
                session = c.connect()

                g = DseGraph.traversal_source(session, 'my_graph')
                print(g.V().valueMap().toList())

            )r   ry   
withRemoter   )rB   rC   rD   traversal_classgraphtraversal_sourcer-   r-   r.   r      s   

zDseGraph.traversal_sourcec                 K   sR   |t jkrt}n|t jkrt}ntd|td|t| t	j
|dd|}|S )a\  
            Creates an ExecutionProfile for GraphTraversal execution. You need to register that execution profile to the
            cluster by using `cluster.add_execution_profile`.

            :param graph_name: The graph name
            :param graph_protocol: (Optional) The graph protocol, default is `DSE_GRAPH_QUERY_PROTOCOL`.
            rL   )rC   rP   rR   )rY   rN   Nr-   )r   rS   #dse_graphson2_traversal_row_factoryrU   #dse_graphson3_traversal_row_factoryrA   rX   r
   r   r   rO   )rC   rR   kwargsrY   r\   r-   r-   r.   create_execution_profile  s   


z!DseGraph.create_execution_profilec                  O   s   t | i |S )z
            Returns the :class:`cassandra.datastax.graph.fluent.query.TraversalBatch` object allowing to
            execute multiple traversals in the same transaction.
            )r   )argsr   r-   r-   r.   batch)  s   zDseGraph.batch)r0   r1   r2   r3   rO   r   rS   DSE_GRAPH_QUERY_PROTOCOLrr   rZ   r   r   r   r   r-   r-   r-   r.   r      s      r   )8loggingr%   concurrent.futuresr   HAVE_GREMLINgremlin_pythonImportErrorgremlin_python.structure.graphr   'gremlin_python.driver.remote_connectionr   r    gremlin_python.process.traversalr   r   &gremlin_python.process.graph_traversalr   cassandra.clusterr	   r
   r   cassandra.datastax.graphr   r   cassandra.datastax.graph.queryr   +cassandra.datastax.graph.fluent.serializersr   r   r   r   r   r   %cassandra.datastax.graph.fluent.queryr   r   	getLoggerr0   rw   __all__r#   r!   r   r5   r;   rT   r   r   r   r<   r=   rV   r   r   objectr   r-   r-   r-   r.   <module>   sN    
[ 