
    Rǻi2                    H   U d dl m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 dd	lmZ dd
lmZmZmZ ddlmZ ddlmZmZmZmZ  ej<                  d      Ze e!z  Z"de#d<    G d de$ejJ                        Z& G d de      Z' G d de'      Z( G d de'      Z)y)    )annotations)ABCabstractmethod)reduce)xor   )_typing)BrokenHydrationObject)
iter_items)BrokenRecordError)NodePathRelationship)Point)DateDateTimeDurationTime_Tzt.TypeAlias_Kc                       e Zd ZU dZded<   ddZd Z fdZd fdZeZ	ddZ
d	 Zd fd
Z	 	 	 	 d fdZdddZddZdddZddZddZd ZddZ xZS ) Recordz
    Immutable, ordered collection of key-value pairs.

    It is generally closer to a :func:`collections.namedtuple` than to a
    :class:`OrderedDict` in as much as iteration of the collection will yield
    values rather than keys.
    z
tuple[str]_Record__keysc                    g }g }t        |      D ]'  \  }}|j                  |       |j                  |       ) t        j                  | |      }t        |      |_        |S N)r   appendtuple__new__r   )clsiterablekeysvalueskeyvalueinsts          I/opt/lhia/marcimex/agent/venv/lib/python3.12/site-packages/neo4j/_data.pyr   zRecord.__new__;   s^    $X. 	!JCKKMM% 	! }}S&)Dk    c                @    t        d| d| j                  |    d      S )NzRecord contains broken data at z ('z'))r   r   )selfindexs     r&   _broken_record_errorzRecord._broken_record_errorE   s+     -eWCE8J7K2N
 	
r'   c                ~    t         |   |      }t        |t              r| j	                  |      |j
                  |S r   )super__getitem__
isinstancer
   r+   error)r)   r*   r$   	__class__s      r&   _super_getitem_singlezRecord._super_getitem_singleJ   s9    #E*e23++E2Cr'   c           	         dj                  d t        | j                  t        |          d      D              }d| j
                  j                   d| dS )N c              3  0   K   | ]  \  }}| d |  yw)=N ).0fieldr$   s      r&   	<genexpr>z"Record.__repr__.<locals>.<genexpr>Q   s'      
u gQui 
s   Tstrict<>)joinzipr   r-   __iter__r1   __name__)r)   fieldsr1   s     r&   __repr__zRecord.__repr__P   sX     
 #UW-/!
 
 4>>**+1VHA66r'   c                   t        |t        j                        }t        |t        j                        }|rV|rTt        j                  t        j                  |      }t        |       t        |      k(  xr t        |       t        |      k(  S |r;t        j                  t        j                  |      }t        |       t        |      k(  S |r;t        j                  t        j                  |      }t        |       t        |      k(  S t        S )a  
        Compare this record with another object for equality.

        In order to be flexible regarding comparison, the equality rules
        for a record permit comparison with any other Sequence or Mapping.

        :param other:
        :returns:
        )r/   tSequenceMappingcastlistdictNotImplemented)r)   othercompare_as_sequencecompare_as_mappings       r&   __eq__zRecord.__eq__[   s     )

;'qyy9#5FF199e,E:e,JdtE{1JJ FF1::u-E:e,,FF199e,E:e,,!!r'   c                Z    t        t        t        t        | j	                                     S r   )r   xor_operatormaphashitemsr)   s    r&   __hash__zRecord.__hash__s   s    lCdjjl$;<<r'   c              #     K   t        t        | 	               D ]5  \  }}t        |t              r| j                  |      |j                  | 7 y wr   )	enumerater-   rA   r/   r
   r+   r0   )r)   ivr1   s      r&   rA   zRecord.__iter__v   sN     eg.01 	DAq!23//2?G	s   AAc                    t        |t              r;| j                  |   }t        |   |      }| j                  t        ||d            S 	 | j                  |      }| j                  |      S # t        $ r Y y w xY w)NTr;   )
r/   slicer   r-   r.   r1   r@   r*   r2   
IndexError)r)   r#   r!   r"   r*   r1   s        r&   r.   zRecord.__getitem__|   s}     c5!;;s#DW(-F>>#dF4"@AA	5JJsOE --e44  		s   A0 0	A<;A<c                    	 | j                   j                  t        |            }d|cxk  rt	        |       k  rn |S | j                  |      S |S # t        $ r |cY S w xY w)z
        Obtain a value from the record by key.

        The ``default`` is returned if the key does not exist.

        :param key: a key
        :param default: default value

        :returns: a value
        r   )r   r*   str
ValueErrorlenr2   r)   r#   defaultr*   s       r&   getz
Record.get   sg    	KK%%c#h/E !D	! N --e44N  	N	s   $A AAc                8   t        |t              r7d|cxk  rt        | j                        k  r|S  t	        |      t	        |      t        |t
              r	 | j                  j                  |      S t        |      # t        $ r}t        |      |d}~ww xY w)zi
        Return the index of the given item.

        :param key: a key

        :returns: index
        r   N)
r/   intrb   r   r^   r`   r*   ra   KeyError	TypeError)r)   r#   excs      r&   r*   zRecord.index   s     c3C*#dkk**
 +S/!*S/!S!-{{((-- C.   -sm,-s   A? ?	BBBc                ^    	 | j                  |      }| |   S # t        t        f$ r |cY S w xY w)aI  
        Obtain a single value from the record by index or key.

        If no index or key is specified, the first value is returned.
        If the specified item does not exist, the default value is returned.

        :param key: an index or key
        :param default: default value

        :returns: a single value
        )r*   r^   rh   rc   s       r&   r$   zRecord.value   s;    	JJsOE ; H% 	N	s    ,,c                ,    t        | j                        S )zU
        Return the keys of the record.

        :returns: list of key names
        )rJ   r   rV   s    r&   r!   zRecord.keys   s     DKK  r'   c                    |r1g }|D ](  }	 | j                  |      }|j                  | |          * |S t        |       S # t        $ r |j                  d       Y Tw xY w)aA  
        Return the values of the record.

        The values returned can optionally be filtered to include only certain
        values by index or key.

        :param keys: indexes or keys of the items to include; if none
            are provided, all values will be included

        :returns: list of values
        N)r*   r   rh   rJ   r)   r!   dr#   rZ   s        r&   r"   zRecord.values   sl     A &&

3A HHT!W%& HDz   #HHTN#s   ?AAc                T   |r@g }|D ]7  }	 | j                  |      }|j                  | j                  |   | |   f       9 |S t	        t        |             D cg c]"  }| j                  |   | j                  |      f$ c}S # t        $ r |j                  |df       Y w xY wc c}w )z~
        Return the fields of the record as a list of key and value tuples.

        :returns: a list of value tuples
        N)r*   r   r   rh   rangerb   r2   rn   s        r&   rU   zRecord.items   s     A 88

3A HHdkk!nd1g678 H 3t9%
 [[^T77:;
 	
   *HHc4[)*

s   B'B%B"!B"c                `    t               j                  t         | j                  |             S )a  
        Return the record as a dictionary.

        Return the keys and values of this record as a dictionary, optionally
        including only certain values by index or key.
        Keys provided in the items that are not in the record will be inserted
        with a value of :data:`None`; indexes provided that are out of bounds
        will trigger an :exc:`IndexError`.

        This function provides a convenient but opinionated way to transform
        the record into a mostly JSON serializable format. It is mainly useful
        for interactive sessions and rapid prototyping.

        The transformation works as follows:

         * Nodes are transformed into dictionaries of their
           properties.

           * No indication of their original type remains.
           * Not all information is serialized (e.g., labels and element_id are
             absent).

         * Relationships are transformed to a tuple of
           ``(start_node, type, end_node)``, where the nodes are transformed
           as described above, and type is the relationship type name
           (:class:`str`).

           * No indication of their original type remains.
           * No other information (properties, element_id, start_node,
             end_node, ...) is serialized.

         * Paths are transformed into lists of nodes and relationships. No
           indication of the original type remains.
         * :class:`list` and :class:`dict` values are recursively transformed.
         * Every other type remains unchanged.

           * Spatial types and durations inherit from :class:`tuple`. Hence,
             they are JSON serializable, but, like graph types, type
             information will be lost in the process.
           * The remaining temporal types are not JSON serializable.

        You will have to implement a custom serializer should you need more
        control over the output format.

        :param keys: Indexes or keys of the items to include. If none are
            provided, all values will be included.

        :returns: dictionary of values, keyed by field name

        :raises: :exc:`IndexError` if an out-of-bounds index is specified.
        )RecordExporter	transformrK   rU   )r)   r!   s     r&   datazRecord.data   s)    h ))$ztzz4/@*ABBr'   )r7   )returnr`   )rM   objectrv   bool)rv   zt.Iterator[t.Any])r#   z
_K | slicerv   t.Anyr   )r#   r`   rd   rw   rv   ry   )r#   r   rv   rg   )r   N)r#   r   rd   rw   rv   ry   )rv   z	list[str])r!   r   rv   zlist[t.Any])r!   r   rv   zdict[str, t.Any])rB   
__module____qualname____doc____annotations__r   r+   r2   rD   __str__rP   rW   rA   r.   re   r*   r$   r!   r"   rU   ru   __classcell__)r1   s   @r&   r   r   0   ss     

7 G"0=55	5(!(&!0
*4Cr'   r   c                       e Zd ZdZed        Zy)DataTransformerzEAbstract base class for transforming data from one form into another.c                     y)z{
        Transform a value, or collection of values.

        :param x: input value
        :returns: output value
        Nr7   )r)   xs     r&   rt   zDataTransformer.transform4  s    r'   N)rB   rz   r{   r|   r   rt   r7   r'   r&   r   r   1  s    O r'   r   c                      e Zd ZdZd Zy)rs   z:Transformer class used by the :meth:`.Record.data` method.c           	         t        |t              r j                  t        |            S t        |t              r] j                  t        |j
                              |j                  j                   j                  t        |j                              fS t        |t              r j                  |j
                        g}t        |j                        D ]i  \  }}|j                   j                  |j                  j                               |j                   j                  |j                  |dz                   k |S t        |t        t        t         t"        t$        t&        f      r|S t        |t(        j*                  t(        j,                  f      r't/        |      } |t1         j                  |            S t        |t(        j2                        r*t/        |      } | fd|j5                         D              S |S )Nr   c              3  J   K   | ]  \  }}|j                  |      f  y wr   )rt   )r8   kr[   r)   s      r&   r:   z+RecordExporter.transform.<locals>.<genexpr>W  s#     D$!Q4>>!,-Ds    #)r/   r   rt   rK   r   
start_noder1   rB   end_noder   rY   relationshipsr   nodesr`   r   r   r   r   r   rF   rG   SettyperS   rH   rU   )r)   r   pathrZ   relationshiptyps   `     r&   rt   zRecordExporter.transformA  sv   a>>$q'**<(tALL12$$tAJJ/0 
 4 NN1<<01D#,Q__#= <<DNN<+A+A+J+JKLDNN1771q5>:;< KCdHhGHHAJJ./q'Cs4>>1-..199%q'CD!'')DDDHr'   N)rB   rz   r{   r|   rt   r7   r'   r&   rs   rs   >  s
    Dr'   rs   c                  .    e Zd ZdZedd       Zd Zd Zy)RecordTableRowExporterz;Transformer class used by the :meth:`.Result.to_df` method.c                F    | j                  dd      j                  dd      S )N\z\\.z\.)replace)r#   s    r&   _escape_map_keyz&RecordTableRowExporter._escape_map_key_  s     {{4(00e<<r'   c                     t        |t        j                        sJ t        |      } | fd|j	                         D              S )Nc              3     K   | ]>  \  }}j                  |j                  |             j                         D ]  }|  @ yw)prefixN
_transformr   rU   )r8   r   r[   itemr)   s       r&   r:   z3RecordTableRowExporter.transform.<locals>.<genexpr>f  sW      
1$..q1 ( eg
  

s   AA)r/   rF   rH   r   rU   )r)   r   r   s   `  r&   rt   z RecordTableRowExporter.transformc  s@    !QYY'''1g 
	
 
 	
r'   c                    t        |t              rJ d|j                   d|j                  i}|j	                  fd|j                         D               |S t        |t              r d|j                   d|j                  j                   d|j                  j                   d|j                  j                  i}|j	                  fd|j                         D               |S t        |t        t        f      r|iS t        |t        j                        rt         fd	t!        |      D              S t        |t        j"                        r+t%        |      } | fd
|j                         D              S |iS )Nz().element_idz	().labelsc              3  6   K   | ]  \  }} d | |f  yw)z().prop.Nr7   r8   r   r[   r   s      r&   r:   z4RecordTableRowExporter._transform.<locals>.<genexpr>t  %     Itq!6((1#.2I   z->.element_idz->.start.element_idz->.end.element_idz->.typec              3  6   K   | ]  \  }} d | |f  yw)z->.prop.Nr7   r   s      r&   r:   z4RecordTableRowExporter._transform.<locals>.<genexpr>}  r   r   c              3  ~   K   | ]4  \  }}j                  | d |       j                         D ]  }|  6 yw)z[].r   N)r   rU   )r8   rZ   r[   r   r   r)   s       r&   r:   z4RecordTableRowExporter._transform.<locals>.<genexpr>  sW      Aq OOA3/ , %'  s   :=c           	   3     K   | ]C  \  }}j                  | d j                  |             j                         D ]  }|  E yw)z{}.r   Nr   )r8   r   r[   r   r   r)   s       r&   r:   z4RecordTableRowExporter._transform.<locals>.<genexpr>  sc      Aq OOd.B.B1.E-FG , %'  s   A	A)r/   r   
element_idlabelsupdaterU   r   r   r   r1   rB   r   r`   rF   rG   rK   rY   rH   r   )r)   r   r   resr   s   ` `  r&   r   z!RecordTableRowExporter._transformn  s`   a(-(!,,()$ahhC JJIqwwyIIJ<((-(!,,(-.0G0G(+,ajj.C.C('"AKK$8$8	C JJIqwwyIIJD#;'A;1::& %aL   199%q'C GGI   A;r'   N)r#   r`   rv   r`   )rB   rz   r{   r|   staticmethodr   rt   r   r7   r'   r&   r   r   \  s!    E= =	
%r'   r   N)*
__future__r   abcr   r   	functoolsr   operatorr   rR    r	   rF   _codec.hydrationr
   _confr   
exceptionsr   graphr   r   r   spatialr   timer   r   r   r   TypeVarr   rg   r`   r   r}   r   rH   r   r   rs   r   r7   r'   r&   <module>r      s   " #  (  3  ) 
   QYYt_)K ~CUAII ~CB
c 
_ <7_ 7r'   