
    ?h 7                         d Z ddlmZ ddlmZmZmZmZm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 h dZ G d	 d
      Zdededeee   edf   deee   edf   dedeeeee   ee   f   fdZdededeeef   fdZy)z
Backup class definition.
    )sleep)UnionListTupleAnyDict)ConnectionError)
Connection)BackupFailedExceptionEmptyResponseException)_capitalize_first_letter_decode_json_response_dict>   s3gcsazure
filesystemc                       e Zd ZdZdefdZ	 	 	 ddededeee   edf   deee   edf   d	e	d
e
fdZdeded
eeef   fdZ	 	 	 ddededeee   edf   deee   edf   d	e	d
e
fdZdeded
eeef   fdZy)Backupzl
    Backup class used to schedule and/or check the status of
    a backup process of Weaviate objects.
    
connectionc                     || _         y)z
        Initialize a Classification class instance.

        Parameters
        ----------
        connection : weaviate.connect.Connection
            Connection object to an active and running Weaviate instance.
        N)_connection)selfr   s     T/home/chris/cleankitchens-env/lib/python3.12/site-packages/weaviate/backup/backup.py__init__zBackup.__init__   s     &    N	backup_idbackendinclude_classesexclude_classeswait_for_completionreturnc                    t        |||||      \  }}}}|||d}d| }	 | j                  j                  ||      }t	        |d      }
|
J |rR	 | j                  ||      }|
j                  |       |d	   d
k(  r	 |
S |d	   dk(  rt        d|
       t        d       Q|
S # t        $ r}	t        d      |	d}	~	ww xY w)a  
        Create a backup of all/per class Weaviate objects.

        Parameters
        ----------
        backup_id : str
            The identifier name of the backup.
            NOTE: Case insensitive.
        backend : str
            The backend storage where to create the backup. Currently available options are:
                "filesystem", "s3", "gcs" and "azure".
            NOTE: Case insensitive.
        include_classes : Union[List[str], str, None], optional
            The class/list of classes to be included in the backup. If not specified all classes
            will be included. Either `include_classes` or `exclude_classes` can be set.
            By default None.
        exclude_classes : Union[List[str], str, None], optional
            The class/list of classes to be excluded in the backup. Either `include_classes` or
            `exclude_classes` can be set. By default None.
        wait_for_completion : bool, optional
            Whether to wait until the backup is done. By default False.

        Returns
        -------
        dict
            Backup creation response.

        Raises
        ------
        requests.ConnectionError
            If the network connection to weaviate fails.
        weaviate.UnexpectedStatusCodeException
            If weaviate reports a none OK status.
        TypeError
            One of the arguments have a wrong type.
        ValueError
            'backend' does not have an accepted value.
        r   r   r   r   r    )idincludeexclude	/backups/pathweaviate_objectz/Backup creation failed due to connection error.NzBackup creationr   r   statusSUCCESSFAILEDzBackup failed:    )	*_get_and_validate_create_restore_argumentsr   postRequestsConnectionErrorr   get_create_statusupdater   r   )r   r   r   r   r   r    payloadr)   responseconn_errcreate_statusr,   s               r   createzBackup.create*   s1   h 7++ 3
	
 &&

 7)$	'',, ' - H 38=NO(((#55'#  6   $$V,(#y0  (#x///-0QRRa  ' ' 	)A	s   B% %	B?.B::B?c                     t        ||      \  }}d| d| }	 | j                  j                  |      }t	        |d      }|
t               |S # t        $ r}t        d      |d}~ww xY w)a   
        Checks if a started classification job has completed.

        Parameters
        ----------
        backup_id : str
            The identifier name of the backup.
            NOTE: Case insensitive.
        backend : str
            The backend storage where the backup was created. Currently available options are:
                "filesystem", "s3", "gcs" and "azure".
            NOTE: Case insensitive.

        Returns
        -------
        dict
            Status of the backup create.
        r+   r'   /r)   z6Backup creation status failed due to connection error.NzBackup status check_get_and_validate_get_statusr   getr2   r   r   r   r   r   r)   r6   r7   typed_responses          r   r3   zBackup.get_create_status   s    ( :
	7
 7)1YK0	''++ , H 4H>ST!(** ' 	)H	s   A 	A*A%%A*c                    t        |||||      \  }}}}i ||d}d| d| d}	 | j                  j                  ||      }t	        |d	      }
|
J |rR	 | j                  ||
      }|
j                  |       |d   dk(  r	 |
S |d   dk(  rt        d|
       t        d       Q|
S # t        $ r}	t        d      |	d}	~	ww xY w)aZ  
        Restore a backup of all/per class Weaviate objects.

        Parameters
        ----------
        backup_id : str
            The identifier name of the backup.
            NOTE: Case insensitive.
        backend : str
            The backend storage from where to restore the backup. Currently available options are:
                "filesystem", "s3", "gcs" and "azure".
            NOTE: Case insensitive.
        include_classes : Union[List[str], str, None], optional
            The class/list of classes to be included in the backup restore. If not specified all
            classes will be included (that were backup-ed). Either `include_classes` or
            `exclude_classes` can be set. By default None.
        exclude_classes : Union[List[str], str, None], optional
            The class/list of classes to be excluded in the backup restore.
            Either `include_classes` or `exclude_classes` can be set. By default None.
        wait_for_completion : bool, optional
            Whether to wait until the backup restore is done.

        Returns
        -------
        dict
            Backup restore response.

        Raises
        ------
        requests.ConnectionError
            If the network connection to weaviate fails.
        weaviate.UnexpectedStatusCodeException
            If weaviate reports a none OK status.
        r#   )configr%   r&   r'   r;   /restorer(   z.Backup restore failed due to connection error.NzBackup restorer+   r,   r-   r.   zBackup restore failed: r/   )	r0   r   r1   r2   r   get_restore_statusr4   r   r   )r   r   r   r   r   r    r5   r)   r6   r7   restore_statusr,   s               r   restorezBackup.restore   s;   ` 7++ 3
	
 &&

 7)1YKx8	'',, ' - H 4H>NO)))#66'#  7   %%f-(#y0  (#x//2I.IY0Z[[a  % ' 	)@	s   B) )	C2B>>Cc                     t        ||      \  }}d| d| d}	 | j                  j                  |      }t	        |d      }|
t               |S # t        $ r}t        d      |d}~ww xY w)	a  
        Checks if a started classification job has completed.

        Parameters
        ----------
        backup_id : str
            The identifier name of the backup.
            NOTE: Case insensitive.
        backend : str
            The backend storage where to create the backup. Currently available options are:
                "filesystem", "s3", "gcs" and "azure".
            NOTE: Case insensitive.

        Returns
        -------
        dict
            Status of the backup create.
        r+   r'   r;   rD   r<   z5Backup restore status failed due to connection error.NzBackup restore status checkr=   r@   s          r   rE   zBackup.get_restore_status  s    ( :
	7 7)1YKx8	''++ , H 4H>[\!(** ' 	)G	s   A 	A+A&&A+)NNF)__name__
__module____qualname____doc__r
   r   strr   r   booldictr9   r   r   r3   rG   rE    r   r   r   r      s<   

&: 
&  8<7;$)[[ [ tCy#t34	[
 tCy#t34[ "[ 
[z'3 ' 'c3h 'Z 8<7;$)VV V tCy#t34	V
 tCy#t34V "V 
Vp&C &# &$sCx. &r   r   r   r   r   Nr   r    r!   c                    t        | t              st        dt        |        d      |t        vrt        dt         d| d      t        |t              st        dt        |       d      |<t        |t              r|g}n*t        |t              st        dt        |       d      g }|<t        |t              r|g}n*t        |t              st        dt        |       d      g }|r|rt        d      |D cg c]  }t        |       }}|D cg c]  }t        |       }}| j                         |j                         ||fS c c}w c c}w )	aN  
    Validate and return the Backup.create/Backup.restore arguments.

    Parameters
    ----------
    backup_id : str
        The identifier name of the backup.
    backend : str
        The backend storage. Currently available options are:
            "filesystem", "s3", "gcs" and "azure".
    include_classes : Union[List[str], str, None]
        The class/list of classes to be included in the backup. If not specified all classes
        will be included. Either `include_classes` or `exclude_classes` can be set.
    exclude_classes : Union[List[str], str, None]
        The class/list of classes to be excluded from the backup.
        Either `include_classes` or `exclude_classes` can be set.
    wait_for_completion : bool
        Whether to wait until the backup restore is done.

    Returns
    -------
    Tuple[str, str, List[str], List[str]]
        Validated and processed (backup_id, backend, include_classes, exclude_classes).

    Raises
    ------
    TypeError
        One of the arguments have a wrong type.
    ValueError
        'backend' does not have an accepted value.
    -'backup_id' must be of type str. Given type: .)'backend' must have one of these values: . Given value: z8'wait_for_completion' must be of type bool. Given type: zH'include_classes' must be of type str, list of str or None. Given type: zH'exclude_classes' must be of type str, list of str or None. Given type: zCEither 'include_classes' OR 'exclude_classes' can be set, not both.)

isinstancerM   	TypeErrortypeSTORAGE_NAMES
ValueErrorrN   listr   lower)r   r   r   r   r    clss         r   r0   r0   1  s   N i%GYGXXYZ[[m#7FYZaYbbcd
 	
 )40FtL_G`Faabc
 	
 "os+./OOT2#O45Q8 
 "os+./OOT2#O45Q8 
 ?]^^@OP/4POP@OP/4POPOOw}}QQ QPs   EEc                     t        | t              st        dt        |        d      |t        vrt        dt         d| d      | j                         |j                         fS )aV  
    Checks if a started classification job has completed.

    Parameters
    ----------
    backup_id : str
        The identifier name of the backup.
        NOTE: Case insensitive.
    backend : str
        The backend storage where to create the backup. Currently available options are:
            "filesystem", "s3", "gcs" and "azure".
        NOTE: Case insensitive.

    Returns
    -------
    Tuple[str, str]
        Validated and processed (backup_id, backend, include_classes, exclude_classes).

    Raises
    ------
    TypeError
        One of the arguments is of a wrong type.
    rR   rS   rT   rU   )rV   rM   rW   rX   rY   rZ   r\   r+   s     r   r>   r>     sn    2 i%GYGXXYZ[[m#7FYZaYbbcd
 	
 OOw}}//r   )rL   timer   typingr   r   r   r   r   requests.exceptionsr	   r2   weaviate.connectr
   weaviate.exceptionsr   r   weaviate.utilr   r   rY   r   rM   rN   r0   r>   rP   r   r   <module>re      s     0 0 J ' OV VrNRNRNR 49c4/0NR 49c4/0	NR
 NR 3T#YS	)*NRb 0C  0#  0%S/  0r   