o
    NDi-                  
   @   s  d dl Z d dlmZ d dlmZ d dlmZmZ d dlm	Z	m
Z
mZmZ d dlmZmZ g dZdZed	d
 eD ZdZG dd deZG dd deZeD ])Zed fddZed fddZed fddZeeed  eeeeed  qOG dd deZG dd deeZdd Z dd Z!d d! Z"d"d# Z#G d$d% d%eZ$G d&d' d'e$Z%G d(d) d)e$Z&e%Z'e&Z(G d*d+ d+eZ)dS ),    N)warn)ConsistencyLevel)	StatementSimpleStatement)VertexEdgePathVertexProperty)GraphSON2ReaderGraphSON3Reader)
GraphProtocolGraphOptionsGraphStatementSimpleGraphStatementsingle_object_row_factorygraph_result_row_factorygraph_object_row_factorygraph_graphson2_row_factoryResultgraph_graphson3_row_factory))
graph_namezname of the targeted graph.z
graph-name)graph_sourcezAchoose the graph traversal source, configured on the server side.zgraph-source)graph_languagez;the language used in the queries (default "gremlin-groovy")zgraph-language)graph_protocolzXthe graph protocol that the server should use for query results (default "graphson-1-0")zgraph-results)graph_read_consistency_levela  read `cassandra.ConsistencyLevel <http://docs.datastax.com/en/developer/python-driver/latest/api/cassandra/#cassandra.ConsistencyLevel>`_ for graph queries (if distinct from session default).
Setting this overrides the native `Statement.consistency_level <http://docs.datastax.com/en/developer/python-driver/latest/api/cassandra/query/#cassandra.query.Statement.consistency_level>`_ for read operations from Cassandra persistencegraph-read-consistency)graph_write_consistency_levela  write `cassandra.ConsistencyLevel <http://docs.datastax.com/en/developer/python-driver/latest/api/cassandra/#cassandra.ConsistencyLevel>`_ for graph queries (if distinct from session default).
Setting this overrides the native `Statement.consistency_level <http://docs.datastax.com/en/developer/python-driver/latest/api/cassandra/query/#cassandra.query.Statement.consistency_level>`_ for write operations to Cassandra persistence.graph-write-consistencyc                 c   s    | ]}|d  V  qdS )r   N ).0optionr   r   W/var/www/Datamplify/venv/lib/python3.10/site-packages/cassandra/datastax/graph/query.py	<genexpr>)   s    r"   zrequest-timeoutc                   @   s   e Zd ZdZ	 dZ	 dZdS )r   s   graphson-1.0s   graphson-2.0s   graphson-3.0N)__name__
__module____qualname__GRAPHSON_1_0GRAPHSON_2_0GRAPHSON_3_0r   r   r   r!   r   /   s    r   c                   @   s   e Zd ZdZejZdZdd Zdd Z	dd Z
dd
dZdd Zdd Zdd Zdd Zedd Zedd Zedd Zd	S )r   z.
    Options for DSE Graph Query handler.
    s   gremlin-groovyc                 K   sX   i | _ |dd |dtj | D ]\}}|tvr#td| t| || qd S )Nr   gr   z7Unknown keyword argument received for GraphOptions: {0})	_graph_options
setdefaultr   DEFAULT_GRAPH_LANGUAGEitems_graph_option_namesr   formatsetattr)selfkwargsattrvaluer   r   r!   __init__J   s   zGraphOptions.__init__c                 C   s   t  }| j |_|S N)r   r*   copy)r1   new_optionsr   r   r!   r7   S   s   zGraphOptions.copyc                 C   s   | j |j  d S r6   )r*   update)r1   optionsr   r   r!   r9   X      zGraphOptions.updateNc                 C   sL   | j  }|r||j  dD ]}||}|dur#tj|  ||< q|S )zx
        Returns a map for these options updated with other options,
        and mapped to graph payload types.
        )r   r   N)r*   r7   r9   getr   value_to_nameencode)r1   other_optionsr:   clcl_enumr   r   r!   get_options_map[   s   

zGraphOptions.get_options_mapc                 C   
   d| _ dS )zb
        Sets ``graph_source`` to the server-defined default traversal source ('default')
        defaultNr   r1   r   r   r!   set_source_defaultk      
zGraphOptions.set_source_defaultc                 C   rC   )z]
        Sets ``graph_source`` to the server-defined analytic traversal source ('a')
        aNrE   rF   r   r   r!   set_source_analyticsq   rH   z!GraphOptions.set_source_analyticsc                 C   rC   )zZ
        Sets ``graph_source`` to the server-defined graph traversal source ('g')
        r)   NrE   rF   r   r   r!   set_source_graphw   rH   zGraphOptions.set_source_graphc                 C   s
   || _ dS )z~
        Sets ``graph_protocol`` as server graph results format (See :class:`cassandra.datastax.graph.GraphProtocol`)
        N)r   )r1   protocolr   r   r!   set_graph_protocol}   rH   zGraphOptions.set_graph_protocolc                 C   s
   | j dv S )N)s   defaultNrE   rF   r   r   r!   is_default_source   s   
zGraphOptions.is_default_sourcec                 C   
   | j dkS )zh
        True if ``graph_source`` is set to the server-defined analytics traversal source ('a')
           arE   rF   r   r   r!   is_analytics_source      
z GraphOptions.is_analytics_sourcec                 C   rO   )zd
        True if ``graph_source`` is set to the server-defined graph traversal source ('g')
           grE   rF   r   r   r!   is_graph_source   rR   zGraphOptions.is_graph_sourcer6   )r#   r$   r%   __doc__r   r&   DEFAULT_GRAPH_PROTOCOLr,   r5   r7   r9   rB   rG   rJ   rK   rM   propertyrN   rQ   rT   r   r   r   r!   r   A   s$    	


r      c                 C   s   | j |S r6   )r*   r<   r1   keyr   r   r!   r<         r<   c                 C   s:   |d urt |tr| }|| j|< d S | j|d  d S r6   )
isinstancestrr>   r*   pop)r1   r4   rZ   r   r   r!   set   s
   
r_   c                 C   s   | j |d  d S r6   )r*   r^   rY   r   r   r!   delete   r;   r`      c                   @   s(   e Zd ZdZedd Zdd ZeZdS )r   z. An abstract class representing a graph query.c                 C   s   t  r6   )NotImplementedErrorrF   r   r   r!   query      zGraphStatement.queryc                 C   s   d | jS )Nz<GraphStatement query="{0}">)r/   rc   rF   r   r   r!   __str__   r[   zGraphStatement.__str__N)r#   r$   r%   rU   rW   rc   re   __repr__r   r   r   r!   r      s    
r   c                   @   s   e Zd ZdZedd ZdS )r   z
    Simple graph statement for :meth:`.Session.execute_graph`.
    Takes the same parameters as :class:`.SimpleStatement`.
    c                 C   s   | j S r6   )_query_stringrF   r   r   r!   rc      rd   zSimpleGraphStatement.queryN)r#   r$   r%   rU   rW   rc   r   r   r   r!   r      s    r   c                 C      dd |D S )z8
    returns the JSON string value of graph results
    c                 S   s   g | ]}|d  qS )r   r   r   rowr   r   r!   
<listcomp>   s    z-single_object_row_factory.<locals>.<listcomp>r   column_namesrowsr   r   r!   r      s   r   c                 C   rh   )z
    Returns a :class:`Result <cassandra.datastax.graph.Result>` object that can load graph results and produce specific types.
    The Result JSON is deserialized and unpacked from the top-level 'result' dict.
    c                 S   s"   g | ]}t t|d  d qS r   result)r   jsonloadsri   r   r   r!   rk      s   " z,graph_result_row_factory.<locals>.<listcomp>r   rl   r   r   r!   r      s   r   c                 C   s   t dd |D S )a7  
    Like :func:`~.graph_result_row_factory`, except known element types (:class:`~.Vertex`, :class:`~.Edge`) are
    converted to their simplified objects. Some low-level metadata is shed in this conversion. Unknown result types are
    still returned as :class:`Result <cassandra.datastax.graph.Result>`.
    c                 s   s"    | ]}t |d  d V  qdS )r   rp   N)rq   rr   ri   r   r   r!   r"      s     z+graph_object_row_factory.<locals>.<genexpr>)_graph_object_sequencerl   r   r   r!   r      s   r   c                 c   sV    | D ]%}t |}t|tr%|jd}|dkr| }n|dkr%| }|V  qd S )Ntypevertexedge)r   r\   dictr4   r<   	as_vertexas_edge)objectsorestypr   r   r!   rs      s   

rs   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )_GraphSONContextRowFactoryNc                 C   s*   d|i}| j pi }| j|fi || _d S )Ncluster)graphson_reader_kwargsgraphson_reader_classgraphson_reader)r1   r   contextr2   r   r   r!   r5      s   
z#_GraphSONContextRowFactory.__init__c                    s    fdd|D S )Nc                    s    g | ]} j |d  d qS ro   )r   readri   rF   r   r!   rk      s     z7_GraphSONContextRowFactory.__call__.<locals>.<listcomp>r   )r1   rm   rn   r   rF   r!   __call__   r;   z#_GraphSONContextRowFactory.__call__)r#   r$   r%   r   r   r5   r   r   r   r   r!   r~      s
    r~   c                   @      e Zd ZdZeZdS )_GraphSON2RowFactoryz-Row factory to deserialize GraphSON2 results.N)r#   r$   r%   rU   r
   r   r   r   r   r!   r          r   c                   @   r   )_GraphSON3RowFactoryz-Row factory to deserialize GraphSON3 results.N)r#   r$   r%   rU   r   r   r   r   r   r!   r      r   r   c                   @   sf   e Zd ZdZdZ	 dd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )r   zl
    Represents deserialized graph results.
    Property and item getters are provided for convenience.
    Nc                 C   s
   || _ d S r6   r4   )r1   r4   r   r   r!   r5   
     
zResult.__init__c                 C   s6   t | jts
td|| jv r| j| S td|f )Nz"Value cannot be accessed as a dictz$Result has no top-level attribute %r)r\   r4   rw   
ValueErrorAttributeError)r1   r3   r   r   r!   __getattr__  s
   

zResult.__getattr__c                 C   sN   t | jtrt |tr| j| S t | jtr t |tr | j| S td|f )NzResult cannot be indexed by %r)r\   r4   rw   r]   listintr   )r1   itemr   r   r!   __getitem__  s
   

zResult.__getitem__c                 C   s
   t | jS r6   )r]   r4   rF   r   r   r!   re     r   zResult.__str__c                 C   s   dt j| jf S )Nz%s(%r))r   r#   r4   rF   r   r   r!   rf   !  s   zResult.__repr__c                 C   s   | j |j kS r6   r   )r1   otherr   r   r!   __eq__$  r[   zResult.__eq__c              
   C   sD   zt | j| j| j| jdi W S  tttfy!   td| f w )z
        Return a :class:`Vertex` parsed from this result

        Raises TypeError if parsing fails (i.e. the result structure is not valid).
        
propertieszCould not create Vertex from %r)	r   idlabelrt   r4   r<   r   r   	TypeErrorrF   r   r   r!   rx   '  s
    zResult.as_vertexc              
   C   sT   zt | j| j| j| jdi | j| j| j| j	W S  t
ttfy)   td| f w )z
        Return a :class:`Edge` parsed from this result

        Raises TypeError if parsing fails (i.e. the result structure is not valid).
        r   zCould not create Edge from %r)r   r   r   rt   r4   r<   inVinVLabeloutV	outVLabelr   r   r   rF   r   r   r!   ry   2  s   zResult.as_edgec              
   C   s4   zt | j| jW S  tttfy   td| f w )z
        Return a :class:`Path` parsed from this result

        Raises TypeError if parsing fails (i.e. the result structure is not valid).
        zCould not create Path from %r)r   labelsrz   r   r   r   rF   r   r   r!   as_path>  s
   zResult.as_pathc                 C   s&   t | jd| jd| jdi S )Nr   r4   r   )r	   r4   r<   rF   r   r   r!   as_vertex_propertyI  s   &zResult.as_vertex_property)r#   r$   r%   rU   r4   r5   r   r   re   rf   r   rx   ry   r   r   r   r   r   r!   r      s    	r   )*rq   warningsr   	cassandrar   cassandra.queryr   r   cassandra.datastax.graph.typesr   r   r   r	   !cassandra.datastax.graph.graphsonr
   r   __all__r*   tupler.   _request_timeout_keyobjectr   r   optr<   r_   r`   r0   rW   r   r   r   r   r   rs   r~   r   r   r   r   r   r   r   r   r!   <module>   s:   
U	 
	