U
    cc                     @   sX   d dl mZmZmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ G dd deZd	S )
   )quote_stringrandom_stringstringify_param_value   )GraphCommands)Edge)Node)Pathc                   @   s   e Zd ZdZe fddZe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d Zdd ZddddZd d! Zd"d# Zd$d% Zd&S )'Graphz/
    Graph, collection of nodes and edges.
    c                 C   s<   || _ || _|j| _i | _g | _g | _g | _g | _d| _dS )z%
        Create a new graph.
            N)	NAMEclientZexecute_commandnodesedges_labels_properties_relationship_typesversion)selfr   name r   W/var/www/html/project/venv/lib/python3.8/site-packages/redis/commands/graph/__init__.py__init__   s    zGraph.__init__c                 C   s   | j S N)r   r   r   r   r   r      s    z
Graph.namec                 C   s   g | _ g | _g | _d S r   )r   r   r   r   r   r   r   _clear_schema    s    zGraph._clear_schemac                 C   s$   |    |   |   |   d S r   )r   _refresh_labels_refresh_relations_refresh_attributesr   r   r   r   _refresh_schema%   s    zGraph._refresh_schemac                 C   s<   |   }d gt| | _t|D ]\}}|d | j|< q d S Nr   )labelslenr   	enumerate)r   Zlblsilr   r   r   r   +   s    zGraph._refresh_labelsc                 C   s<   |   }d gt| | _t|D ]\}}|d | j|< q d S r    )relationship_typesr"   r   r#   )r   Zrelsr$   rr   r   r   r   3   s    zGraph._refresh_relationsc                 C   s<   |   }d gt| | _t|D ]\}}|d | j|< q d S r    )property_keysr"   r   r#   )r   propsr$   pr   r   r   r   ;   s    zGraph._refresh_attributesc                 C   s:   z| j | }W n& tk
r4   |   | j | }Y nX |S )zo
        Returns a label by it's index

        Args:

        idx:
            The index of the label
        )r   
IndexErrorr   )r   idxlabelr   r   r   	get_labelC   s    	zGraph.get_labelc                 C   s:   z| j | }W n& tk
r4   |   | j | }Y nX |S )z~
        Returns a relationship type by it's index

        Args:

        idx:
            The index of the relation
        )r   r+   r   )r   r,   Zrelationship_typer   r   r   get_relationT   s    	zGraph.get_relationc                 C   s:   z| j | }W n& tk
r4   |   | j | }Y nX |S )zu
        Returns a property by it's index

        Args:

        idx:
            The index of the property
        )r   r+   r   )r   r,   Z	propertier   r   r   get_propertye   s    	zGraph.get_propertyc                 C   s"   |j dkrt |_ || j|j < dS )z+
        Adds a node to the graph.
        N)aliasr   r   )r   noder   r   r   add_nodev   s    
zGraph.add_nodec                 C   s4   | j |jj r| j |jj s$td| j| dS )z,
        Adds an edge to the graph.
        z$Both edge's end must be in the graphN)r   Zsrc_noder1   Z	dest_nodeAssertionErrorr   append)r   edger   r   r   add_edge~   s    zGraph.add_edgec                 C   sH   t |tstdd}| D ]$\}}|t|d t| d 7 }q|S )Nz'params' must be a dictzCYPHER = )
isinstancedict	TypeErroritemsstrr   )r   paramsZparams_headerkeyvaluer   r   r   _build_params_header   s    
zGraph._build_params_headerF	read_onlyc                O   sX   dd |D }d| dd | d}|dd }|rJ|dd | 7 }| j||d	S )
Nc                 S   s   g | ]}t |qS r   )r   ).0argr   r   r   
<listcomp>   s     z(Graph.call_procedure.<locals>.<listcomp>zCALL (,)yz YIELD rC   )joingetquery)r   Z	procedurerD   argsZkwagrsqrK   r   r   r   call_procedure   s    zGraph.call_procedurec                 C   s   | j dddjS )Nz	db.labelsTrC   rQ   Z
result_setr   r   r   r   r!      s    zGraph.labelsc                 C   s   | j dddjS )Nzdb.relationshipTypesTrC   rR   r   r   r   r   r&      s    zGraph.relationship_typesc                 C   s   | j dddjS )Nzdb.propertyKeysTrC   rR   r   r   r   r   r(      s    zGraph.property_keysN)__name__
__module____qualname____doc__r   r   propertyr   r   r   r   r   r   r.   r/   r0   r3   r7   rB   rQ   r!   r&   r(   r   r   r   r   r
      s&   
	

r
   N)helpersr   r   r   commandsr   r6   r   r2   r   pathr	   r
   r   r   r   r   <module>   s
   