
    ?hi                         d Z ddlZ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mZmZ ddlmZ dd	lmZmZ  G d
 d      Zddedee   defdZdededdfdZy)z
Reference class definition.
    N)UnionOptionalList)ConnectionError)
Connection)ConsistencyLevel) REF_DEPRECATION_NEW_V14_CLS_NS_W%REF_DEPRECATION_OLD_V14_FROM_CLS_NS_W#REF_DEPRECATION_OLD_V14_TO_CLS_NS_W)UnexpectedStatusCodeException)get_valid_uuid_capitalize_first_letterc                      e Zd ZdZdefdZ	 	 	 	 ddedededee   d	ee   d
ee   dee   ddfdZ		 	 	 	 ddedede
ee   ef   dee   de
ee   edf   d
ee   dee   ddfdZ	 	 	 	 ddedededee   d	ee   d
ee   dee   ddfdZy)	ReferencezG
    Reference class used to manipulate references within objects.
    
connectionc                     || _         y)z
        Initialize a Reference class instance.

        Parameters
        ----------
        connection : weaviate.connect.Connection
            Connection object to an active and running weaviate instance.
        N)_connection)selfr   s     f/home/chris/cleankitchens-env/lib/python3.12/site-packages/weaviate/data/references/crud_references.py__init__zReference.__init__   s     &    N	from_uuidfrom_property_nameto_uuidfrom_class_nameto_class_nameconsistency_leveltenantreturnc                    | j                   j                  dk\  }i }	|t        |      j                  |	d<   |||	d<   ||"|r t	        j
                  t        t        d       |/|s t	        j
                  t        t        d       t        |d       |/|s t	        j
                  t        t        d       t        |d	       t        |      }t        |      }t        |d
       |r|rt        |t        |            }
nt        |      }
|r|rt        |      }d| d| d| }nd| d| }	 | j                   j                  ||
|	      }|j                   dk(  ryt#        d|      # t        $ r}t        d      |d}~ww xY w)a  
        Remove a reference to another object. Equal to removing one direction of an edge from the
        graph.

        Parameters
        ----------
        from_uuid : str
            The ID of the object that references another object.
        from_property_name : str
            The property from which the reference should be deleted.
        to_uuid : str
            The UUID of the referenced object.
        from_class_name : Optional[str], optional
            The class name of the object for which to delete the reference (with UUID `from_uuid`),
            it is included in Weaviate 1.14.0, where all objects are namespaced by class name.
            STRONGLY recommended to set it with Weaviate >= 1.14.0. It will be required in future
            versions of Weaviate Server and Clients. Use None value ONLY for Weaviate < v1.14.0,
            by default None
        to_class_name : Optional[str], optional
            The referenced object class name to which to delete the reference (with UUID `to_uuid`),
            it is included in Weaviate 1.14.0, where all objects are namespaced by class name.
            STRONGLY recommended to set it with Weaviate >= 1.14.0. It will be required in future
            versions of Weaviate Server and Clients. Use None value ONLY for Weaviate < v1.14.0,
            by default None
        consistency_level : Optional[ConsistencyLevel], optional
            Can be one of 'ALL', 'ONE', or 'QUORUM'. Determines how many replicas must acknowledge
        tenant: Optional[str], optional
            The name of the tenant for which this operation is being performed.

        Examples
        --------
        Assume we have two classes, Author and Book.

        >>> # Create the objects first
        >>> client.data_object.create(
        ...     data_object={'name': 'Ray Bradbury'},
        ...     class_name='Author',
        ...     uuid='e067f671-1202-42c6-848b-ff4d1eb804ab'
        ... )
        >>> client.data_object.create(
        ...     data_object={'title': 'The Martian Chronicles'},
        ...     class_name='Book',
        ...     uuid='a9c1b714-4f8a-4b01-a930-38b046d69d2d'
        ... )
        >>> # Add the cross references
        >>> ## Author -> Book
        >>> client.data_object.reference.add(
        ...     from_uuid='e067f671-1202-42c6-848b-ff4d1eb804ab',
        ...     from_property_name='wroteBooks',
        ...     to_uuid='a9c1b714-4f8a-4b01-a930-38b046d69d2d',
        ...     from_class_name='Author', # ONLY with Weaviate >= 1.14.0
        ...     to_class_name='Book', # ONLY with Weaviate >= 1.14.0
        ... )
        >>> client.data_object.get(
        ...     uuid='e067f671-1202-42c6-848b-ff4d1eb804ab',
        ...     class_name='Author', # ONLY with Weaviate >= 1.14.0
        ... )
        {
            "additional": {},
            "class": "Author",
            "creationTimeUnix": 1617177700595,
            "id": "e067f671-1202-42c6-848b-ff4d1eb804ab",
            "lastUpdateTimeUnix": 1617177700595,
            "properties": {
                "name": "Ray Bradbury",
                "wroteBooks": [
                {
                    "beacon": "weaviate://localhost/Book/a9c1b714-4f8a-4b01-a930-38b046d69d2d",
                    "href": "/v1/objects/Book/a9c1b714-4f8a-4b01-a930-38b046d69d2d"
                }
                ]
            },
            "vectorWeights": null
        }
        >>> # delete the reference
        >>> client.data_object.reference.delete(
        ...     from_uuid='e067f671-1202-42c6-848b-ff4d1eb804ab',
        ...     from_property_name='wroteBooks',
        ...     to_uuid='a9c1b714-4f8a-4b01-a930-38b046d69d2d',
        ...     from_class_name='Author', # ONLY with Weaviate >= 1.14.0
        ...     to_class_name='Book', # ONLY with Weaviate >= 1.14.0
        ... )
        >>> >>> client.data_object.get(
        ...     uuid='e067f671-1202-42c6-848b-ff4d1eb804ab',
        ...     class_name='Author', # ONLY with Weaviate >= 1.14.0
        ... )
        {
            "additional": {},
            "class": "Author",
            "creationTimeUnix": 1617177700595,
            "id": "e067f671-1202-42c6-848b-ff4d1eb804ab",
            "lastUpdateTimeUnix": 1617177864970,
            "properties": {
                "name": "Ray Bradbury",
                "wroteBooks": []
            },
            "vectorWeights": null
        }

        Raises
        ------
        requests.ConnectionError
            If the network connection to weaviate fails.
        weaviate.UnexpectedStatusCodeException
            If weaviate reports a none OK status.
        TypeError
            If parameter has the wrong type.
        ValueError
            If uuid is not properly formed.
        1.14Nr   r      messagecategory
stacklevelr   argumentargument_namer   r   r   
class_namer   	/objects///references/pathweaviate_objectparamszReference was not deleted.   z#Delete property reference to object)r   server_versionr   valuewarningswarnr	   DeprecationWarningr
   _validate_string_argumentsr   r   _get_beaconr   deleteRequestsConnectionErrorstatus_coder   r   r   r   r   r   r   r   r   is_server_version_14r3   beacon_class_namer1   responseconn_errs                  r   r<   zReference.delete(   s   r  $//>>&H(*:;L*M*S*SF&'%F8#}'<BVMM8+
 &'A/ 
 '(/ $'?/ 
 '&- #9-	 )"'.	

 1 3MBF
 !F 32?CK{m1YK|DVCWXDyk6H5IJD	V''..D&Y_.`H 3&+,QS[\\	 ' 	V)*FGXU	V   3E- -	F6FFto_uuidsto_class_namesc                    | j                   j                  dk\  }i }	|t        |      j                  |	d<   |||	d<   ||"|r t	        j
                  t        t        d       |/|s t	        j
                  t        t        d       t        |d       |~|s t	        j
                  t        t        d       t        |t              st        |d	       n>|D ])  }
t        |
t              rt        d
t        |
              t!        |      dk(  rd}t        |t              s|g}t        |t              r|gt!        |      z  }|"t!        |      t!        |      k7  rt#        d      t%        |      }t        |d       g }|rI|rGt'        ||      D ]7  \  }}
t%        |      }t)        |t+        |
            }|j-                  |       9 n/|D ]*  }t%        |      }t)        |      }|j-                  |       , |r|rt+        |      }d| d| d| }nd| d| }	 | j                   j/                  |||	      }|j2                  dk(  ryt5        d|      # t0        $ r}t1        d      |d}~ww xY w)aX  
        Allows to update all references in that property with a new set of references.
        All old references will be deleted.

        Parameters
        ----------
        from_uuid : str
            The object that should have the reference as part of its properties.
            Should be in the form of an UUID or in form of an URL.
            E.g.
            'http://localhost:8080/v1/objects/Book/fc7eb129-f138-457f-b727-1b29db191a67'
            or
            'fc7eb129-f138-457f-b727-1b29db191a67'
        from_property_name : str
            The name of the property within the object.
        to_uuids : list or str
            The UUIDs of the objects that should be referenced.
            Should be a list of str in the form of an UUID or str in form of an URL.
            E.g.
            ['http://localhost:8080/v1/objects/Book/fc7eb129-f138-457f-b727-1b29db191a67', ...]
            or
            ['fc7eb129-f138-457f-b727-1b29db191a67', ...]
            If `str` it is converted internally into a list of str.
        from_class_name : Optional[str], optional
            The class name of the object for which to delete the reference (with UUID `from_uuid`),
            it is included in Weaviate 1.14.0, where all objects are namespaced by class name.
            STRONGLY recommended to set it with Weaviate >= 1.14.0. It will be required in future
            versions of Weaviate Server and Clients. Use None value ONLY for Weaviate < v1.14.0,
            by default None
        to_class_names : Union[list, str, None], optional
            The referenced objects class name to which to delete the reference (with UUID
            `to_uuid`), it is included in Weaviate 1.14.0, where all objects are namespaced by
            class name. It can be a single class name (assumes all `to_uuids` are of the same
            class) or a list of class names where for each UUID in `to_uuids` we have a class name.
            STRONGLY recommended to set it with Weaviate >= 1.14.0. It will be required in future
            versions of Weaviate Server and Clients. Use None value ONLY for Weaviate < v1.14.0,
            by default None
        consistency_level : Optional[ConsistencyLevel], optional
            Can be one of 'ALL', 'ONE', or 'QUORUM'. Determines how many replicas must acknowledge
        tenant: Optional[str]
            The name of the tenant for which this operation is being performed.

        Examples
        --------
        You have data object 1 with reference property `wroteBooks` and currently has one reference
        to data object 7. Now you say, I want to update the references of data object 1.wroteBooks
        to this list 3,4,9. After the update, the data object 1.wroteBooks is now 3,4,9, but no
        longer contains 7.

        >>> client.data_object.get(
        ...     uuid='e067f671-1202-42c6-848b-ff4d1eb804ab'
        ...     class_name='Author', # ONLY with Weaviate >= 1.14.0
        ... )
        {
            "additional": {},
            "class": "Author",
            "creationTimeUnix": 1617177700595,
            "id": "e067f671-1202-42c6-848b-ff4d1eb804ab",
            "lastUpdateTimeUnix": 1617177700595,
            "properties": {
                "name": "Ray Bradbury",
                "wroteBooks": [
                {
                    "beacon": "weaviate://localhost/Book/a9c1b714-4f8a-4b01-a930-38b046d69d2d",
                    "href": "/v1/objects/Book/a9c1b714-4f8a-4b01-a930-38b046d69d2d"
                }
                ]
            },
            "vectorWeights": null
        }
        Currently there is only one `Book` reference.
        Update all the references of the Author for property name `wroteBooks`.
        >>> client.data_object.reference.update(
        ...     from_uuid = 'e067f671-1202-42c6-848b-ff4d1eb804ab',
        ...     from_property_name = 'wroteBooks',
        ...     to_uuids = [
        ...         '8429f68f-860a-49ea-a50b-1f8789515882',
        ...         '3e2e6795-298b-47e9-a2cb-3d8a77a24d8a'
        ...     ],
        ...     from_class_name='Author', # ONLY with Weaviate >= 1.14.0
        ...     to_class_name='Book', # ONLY with Weaviate >= 1.14.0
        ... )
        >>> client.data_object.get(
        ...     uuid='e067f671-1202-42c6-848b-ff4d1eb804ab'
        ...     class_name='Author', # ONLY with Weaviate >= 1.14.0
        ... )
        {
            "additional": {},
            "class": "Author",
            "creationTimeUnix": 1617181292677,
            "id": "e067f671-1202-42c6-848b-ff4d1eb804ab",
            "lastUpdateTimeUnix": 1617181409405,
            "properties": {
                "name": "Ray Bradbury",
                "wroteBooks": [
                {
                    "beacon": "weaviate://localhost/Book/8429f68f-860a-49ea-a50b-1f8789515882",
                    "href": "/v1/objects/Book/8429f68f-860a-49ea-a50b-1f8789515882"
                },
                {
                    "beacon": "weaviate://localhost/Book/3e2e6795-298b-47e9-a2cb-3d8a77a24d8a",
                    "href": "/v1/objects/Book/3e2e6795-298b-47e9-a2cb-3d8a77a24d8a"
                }
                ]
            },
            "vectorWeights": null
        }
        All the previous references were removed and now we have only those specified in the
        `update` method.

        Raises
        ------
        requests.ConnectionError
            If the network connection to weaviate fails.
        weaviate.UnexpectedStatusCodeException
            If weaviate reports a none OK status.
        TypeError
            If the parameters are of the wrong type.
        ValueError
            If the parameters are of the wrong value.
        r!   Nr   r   r"   r#   r   r'   rG   zJ'to_class_names' must be of type str of List[str]. Found element of type: r   zH'to_class_names' and 'to_uuids' have different lengths, they must match.r   r*   r,   r-   r.   r/   r0   zReference was not updated.   z#Update property reference to object)r   r5   r   r6   r7   r8   r	   r9   r
   r:   r   
isinstanceliststr	TypeErrortypelen
ValueErrorr   zipr;   r   appendputr=   r>   r   )r   r   r   rF   r   rG   r   r   r@   r3   r   beaconsr   rA   rB   r1   rC   rD   s                     r   updatezReference.update   s   H  $//>>&H(*:;L*M*S*SF&'%F8#~'=CWMM8+
 &'A/ 
 '(/ %'?/  nd3*+"2
 &4 M%mS9'66:=6I5JL  ~&!+%)N(D) zHnc*,-H=N%#h-3~;N*NZ 
 #9-	"'.	
 2*-h*G '&(1$#7F v&' $ '(1$# v&' 32?CK{m1YK|DVCWXDyk6H5IJD	V''++ ' , H 3&+,QS[\\	 ' 	V)*FGXU	Vs   "I 	I6%I11I6c                    | j                   j                  dk\  }i }	|t        |      j                  |	d<   |||	d<   ||"|r t	        j
                  t        t        d       |/|s t	        j
                  t        t        d       t        |d       |/|s t	        j
                  t        t        d       t        |d	       t        |      }t        |      }t        |d
       |r|rt        |t        |            }
nt        |      }
|r|rt        |      }d| d| d| }nd| d| }	 | j                   j                  ||
|	      }|j                   dk(  ryt#        d|      # t        $ r}t        d      |d}~ww xY w)ah  
        Allows to link an object to an object uni-directionally.

        Parameters
        ----------
        from_uuid : str
            The ID of the object that should have the reference as part
            of its properties. Should be a plane UUID or an URL.
            E.g.
            'http://localhost:8080/v1/objects/Book/fc7eb129-f138-457f-b727-1b29db191a67'
            or
            'fc7eb129-f138-457f-b727-1b29db191a67'
        from_property_name : str
            The name of the property within the object.
        to_uuid : str
            The UUID of the object that should be referenced.
            Should be a plane UUID or an URL.
            E.g.
            'http://localhost:8080/v1/objects/Book/fc7eb129-f138-457f-b727-1b29db191a67'
            or
            'fc7eb129-f138-457f-b727-1b29db191a67'
        from_class_name : Optional[str], optional
            The class name of the object for which to delete the reference (with UUID `from_uuid`),
            it is included in Weaviate 1.14.0, where all objects are namespaced by class name.
            STRONGLY recommended to set it with Weaviate >= 1.14.0. It will be required in future
            versions of Weaviate Server and Clients. Use None value ONLY for Weaviate < v1.14.0,
            by default None
        to_class_name : Optional[str], optional
            The referenced object class name to which to delete the reference (with UUID `to_uuid`),
            it is included in Weaviate 1.14.0, where all objects are namespaced by class name.
            STRONGLY recommended to set it with Weaviate >= 1.14.0. It will be required in future
            versions of Weaviate Server and Clients. Use None value ONLY for Weaviate < v1.14.0,
            by default None
        consistency_level : Optional[ConsistencyLevel], optional
            Can be one of 'ALL', 'ONE', or 'QUORUM'. Determines how many replicas must acknowledge
        tenant: Optional[str]
            The name of the tenant for which this operation is being performed.

        Examples
        --------
        Assume we have two classes, Author and Book.

        >>> # Create the objects first
        >>> client.data_object.create(
        ...     data_object={'name': 'Ray Bradbury'},
        ...     class_name='Author',
        ...     uuid='e067f671-1202-42c6-848b-ff4d1eb804ab'
        ... )
        >>> client.data_object.create(
        ...     data_object={'title': 'The Martian Chronicles'},
        ...     class_name='Book',
        ...     uuid='a9c1b714-4f8a-4b01-a930-38b046d69d2d'
        ... )
        >>> # Add the cross references
        >>> ## Author -> Book
        >>> client.data_object.reference.add(
        ...     from_uuid='e067f671-1202-42c6-848b-ff4d1eb804ab',
        ...     from_property_name='wroteBooks',
        ...     to_uuid='a9c1b714-4f8a-4b01-a930-38b046d69d2d',
        ...     from_class_name='Author', # ONLY with Weaviate >= 1.14.0
        ...     to_class_name='Book', # ONLY with Weaviate >= 1.14.0
        ... )
        >>> client.data_object.get(
        ...     uuid='e067f671-1202-42c6-848b-ff4d1eb804ab',
        ...     class_name='Author', # ONLY with Weaviate >= 1.14.0
        ... )
        {
            "additional": {},
            "class": "Author",
            "creationTimeUnix": 1617177700595,
            "id": "e067f671-1202-42c6-848b-ff4d1eb804ab",
            "lastUpdateTimeUnix": 1617177700595,
            "properties": {
                "name": "Ray Bradbury",
                "wroteBooks": [
                {
                    "beacon": "weaviate://localhost/Book/a9c1b714-4f8a-4b01-a930-38b046d69d2d",
                    "href": "/v1/objects/Book/a9c1b714-4f8a-4b01-a930-38b046d69d2d"
                }
                ]
            },
            "vectorWeights": null
        }

        Raises
        ------
        requests.ConnectionError
            If the network connection to weaviate fails.
        weaviate.UnexpectedStatusCodeException
            If weaviate reports a none OK status.
        TypeError
            If the parameters are of the wrong type.
        ValueError
            If the parameters are of the wrong value.
        r!   Nr   r   r"   r#   r   r'   r   r   r*   r,   r-   r.   r/   r0   zReference was not added.rI   z Add property reference to object)r   r5   r   r6   r7   r8   r	   r9   r
   r:   r   r   r;   r   postr=   r>   r   r?   s                  r   addzReference.add  s   T  $//>>&H(*:;L*M*S*SF&'%F8#}'<BVMM8+
 &'A/ 
 '(/ $'?/ 
 '&- #9-	 )"'.	

 1 3MBF
 !F 32?CK{m1YK|DVCWXDyk6H5IJD	T'',, & - H 3&+,NPXYY	 ' 	T)*DE8S	TrE   )NNNN)__name__
__module____qualname____doc__r   r   rL   r   r   r<   r   r   rU   rX    r   r   r   r      s   
&: 
&" *.'+8< ${]{]  {] 	{]
 "#{]  }{] $$45{] {] 
{]D *.6:8< $d]d]  d] S	3'	d]
 "#d] d3id23d] $$45d] d] 
d]V *.'+8< $pZpZ  pZ 	pZ
 "#pZ  }pZ $$45pZ pZ 
pZr   r   r   r+   r   c                 (    |dd|  iS dd| d|  iS )an  
    Get a weaviate-style beacon.

    Parameters
    ----------
    to_uuid : str
        The UUID to create beacon for.
    class_name : Optional[str], optional
        The class name of the `to_uuid` object. Used with Weaviate >= 1.14.0.
        For Weaviate < 1.14.0 use None value.

    Returns
    -------
    dict
        Weaviate-style beacon as a dict.
    rA   zweaviate://localhost/r.   r]   r*   s     r   r;   r;   ~  s6    $ 1';<<-j\7)DEEr   r(   r)   c                 X    t        | t              st        d| dt        |              y)a  
    Validate string arguments.

    Parameters
    ----------
    argument : str
        Argument value to be validated.
    argument_name : str
        Argument name to be included in error message.

    Raises
    ------
    TypeError
        If 'argument' is not of type str.
    'z%' must be of type 'str'. Given type: N)rJ   rL   rM   rN   r'   s     r   r:   r:     s4    " h$!M?*OPTU]P^O_`aa %r   )N)r\   r7   typingr   r   r   requests.exceptionsr   r=   weaviate.connectr   weaviate.data.replicationr   weaviate.error_msgsr	   r
   r   weaviate.exceptionsr   weaviate.utilr   r   r   rL   dictr;   r:   r]   r   r   <module>ri      s     ( ( J ' 6 
 >d	Z d	ZNF F(3- F4 F.b bS bT br   