o
    -wi5                     @   s   d dl mZmZmZmZmZmZ d dlmZ d dl	Z	d dl
mZ d dlmZmZ d dlmZ d dlmZmZmZmZmZmZmZ dd	lmZ dd
lmZmZmZm Z  ddl!m"Z"m#Z# erbd dlm$Z$ G dd dZ%dS )    )TYPE_CHECKINGAnyDictListLiteralOptional)gqlN)ServerFeature)REGISTRY_PREFIXvalidate_project_name)Api)DELETE_PROJECT_GQLFETCH_REGISTRY_GQLRENAME_PROJECT_GQLUPSERT_REGISTRY_PROJECT_GQLDeleteProjectRenameProjectUpsertRegistryProject   )AddOnlyArtifactTypesList)"fetch_org_entity_from_organizationformat_gql_artifact_types_inputgql_to_registry_visibilityregistry_visibility_to_gql)CollectionsVersions)Clientc                   @   s  e Zd ZdZ	d<dddedededeeeef  f
d	d
Zdeeef ddfddZ	e
defddZe
defddZejdefddZe
defddZe
defddZe
defddZejdefddZe
dd ZejdefddZe
defddZe
defd d!Ze
defd"d#Ze
d$d% Ze
ded& fd'd(Zejded& fd)d(Zd<d*eeeef  defd+d,Zd<d*eeeef  defd-d.Ze		d=dddeded/ed& d0ee d1eee  fd2d3Z d>d4d5Z!d>d6d7Z"d>d8d9Z#defd:d;Z$dS )?Registryz"A single registry in the Registry.Nclientr   organizationentitynameattrsc                 C   s8   || _ || _|| _|| _|| _|d ur| | d S d S N)r   _name_saved_name_entity_organization_update_attributes)selfr   r   r    r!   r"    r*   b/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/apis/public/registries/registry.py__init__#   s   zRegistry.__init__returnc                 C   s   | dd| _| jdu rtd| j d| dd| _| dd| _td	d
 | di  dg D | _| dd| _| dd| _	t
| dd| _dS )z>Helper method to update instance attributes from a dictionary.id Nz	Registry z's id is not founddescriptionallowAllArtifactTypesInRegistryFc                 s   s    | ]	}|d  d V  qdS )noder!   Nr*   ).0tr*   r*   r+   	<genexpr>=   s    
z.Registry._update_attributes.<locals>.<genexpr>artifactTypesedges	createdAt	updatedAtaccess)get_id
ValueErrorr!   _description_allow_all_artifact_typesr   _artifact_types_created_at_updated_atr   _visibility)r)   r"   r*   r*   r+   r(   3   s   

zRegistry._update_attributesc                 C   s   d| j  S )zAFull name of the registry including the `wandb-registry-` prefix.zwandb-registry-)r!   r)   r*   r*   r+   	full_nameD   s   zRegistry.full_namec                 C      | j S )z:Name of the registry without the `wandb-registry-` prefix.r$   rD   r*   r*   r+   r!   I      zRegistry.namevaluec                 C   s
   || _ d S r#   rG   r)   rI   r*   r*   r+   r!   N   s   
c                 C   rF   )z$Organization entity of the registry.)r&   rD   r*   r*   r+   r    R   rH   zRegistry.entityc                 C   rF   )z"Organization name of the registry.)r'   rD   r*   r*   r+   r   W   rH   zRegistry.organizationc                 C   rF   )zDescription of the registry.r>   rD   r*   r*   r+   r0   \   rH   zRegistry.descriptionc                 C   
   || _ dS )z$Set the description of the registry.NrK   rJ   r*   r*   r+   r0   a      
c                 C   rF   )zReturns whether all artifact types are allowed in the registry.

        If `True` then artifacts of any type can be added to this registry.
        If `False` then artifacts are restricted to the types in `artifact_types` for this registry.
        r?   rD   r*   r*   r+   allow_all_artifact_typesf   s   z!Registry.allow_all_artifact_typesc                 C   rL   )z;Set whether all artifact types are allowed in the registry.NrN   rJ   r*   r*   r+   rO   o   rM   c                 C   rF   )a6  Returns the artifact types allowed in the registry.

        If `allow_all_artifact_types` is `True` then `artifact_types` reflects the
        types previously saved or currently used in the registry.
        If `allow_all_artifact_types` is `False` then artifacts are restricted to the
        types in `artifact_types`.

        Note:
            Previously saved artifact types cannot be removed.

        Example:
            ```python
            registry.artifact_types.append("model")
            registry.save()  # once saved, the artifact type `model` cannot be removed
            registry.artifact_types.append("accidentally_added")
            registry.artifact_types.remove(
                "accidentally_added"
            )  # Types can only be removed if it has not been saved yet
            ```
        )r@   rD   r*   r*   r+   artifact_typest   s   zRegistry.artifact_typesc                 C   rF   )z+Timestamp of when the registry was created.)rA   rD   r*   r*   r+   
created_at   rH   zRegistry.created_atc                 C   rF   )z0Timestamp of when the registry was last updated.)rB   rD   r*   r*   r+   
updated_at   rH   zRegistry.updated_atc                 C   s   | j | jgS r#   )r    rE   rD   r*   r*   r+   path   s   zRegistry.path)r   
restrictedc                 C   rF   )a  Visibility of the registry.

        Returns:
            Literal["organization", "restricted"]: The visibility level.
                - "organization": Anyone in the organization can view this registry.
                  You can edit their roles later from the settings in the UI.
                - "restricted": Only invited members via the UI can access this registry.
                  Public sharing is disabled.
        rC   rD   r*   r*   r+   
visibility   s   zRegistry.visibilityc                 C   rL   )a  Set the visibility of the registry.

        Args:
            value: The visibility level. Options are:
                - "organization": Anyone in the organization can view this registry.
                  You can edit their roles later from the settings in the UI.
                - "restricted": Only invited members via the UI can access this registry.
                  Public sharing is disabled.
        NrU   rJ   r*   r*   r+   rV      s   
filterc                 C   s   d| j i}t| j| j||S )z2Returns the collections belonging to the registry.r!   )rE   r   r   r   r)   rW   registry_filterr*   r*   r+   collections   s   zRegistry.collectionsc                 C   s   d| j i}t| j| j|d|S )z/Returns the versions belonging to the registry.r!   N)rE   r   r   r   rX   r*   r*   r+   versions   s   zRegistry.versionsrV   r0   rP   c              	   C   s   t ||}t| }t| g }	|rt|}	t|}
d|d|d}z|jtt||||
|	 |	dd}W n ty@   t	|w |d d sKt	|t
|||||d d S )	a%  Create a new registry.

        The registry name must be unique within the organization.
        This function should be called using `api.create_registry()`

        Args:
            client: The GraphQL client.
            organization: The name of the organization.
            name: The name of the registry (without the `wandb-registry-` prefix).
            visibility: The visibility level ('organization' or 'restricted').
            description: An optional description for the registry.
            artifact_types: An optional list of allowed artifact types.

        Returns:
            Registry: The newly created Registry object.

        Raises:
            ValueError: If a registry with the same name already exists in the
                organization or if the creation fails.
        zFailed to create registry  in organization .r0   
entityNamer!   r:   r1   r6   variable_valuesupsertModelinsertedproject)r   r
   r   r   r   executer   r   	Exceptionr=   r   )clsr   r   r!   rV   r0   rP   
org_entityrE   accepted_artifact_typesvisibility_valueregistry_creation_errorresponser*   r*   r+   create   s@   


zRegistry.createc                 C   st   z| j jttd| jid}t|}W n ty'   td| j	d| j
w |jjs8td| j	d| j
dS )z*Delete the registry. This is irreversible.r.   r`   zFailed to delete registry:  in organization: N)r   re   r   r   r<   r   model_validaterf   r=   r!   r   delete_modelsuccess)r)   rl   resultr*   r*   r+   delete  s   zRegistry.deletec                 C   s   d| j d| jd}z| jjtt| j| jdd}W n ty'   t	|w |d du r2t	||d d | _
| j
du rBt	|| | j
 dS )	zPLoad the registry attributes from the backend to reflect the latest saved state.zFailed to load registry r\   r]   )r!   r_   r`   r    Nrd   )r!   r   r   re   r   r   rE   r    rf   r=   r"   r(   )r)   load_failure_messagerl   r*   r*   r+   load  s(   


zRegistry.loadc              	   C   s`  t  tjstd|  rtdddddt| j t	| j
}t| jj}d| j d| j }t | j }z| jjtt| j| j||| j|d	d
}t|}W n tya   t|w |jjrttd| jd| jd |  |d d  | j| jkr| jjtt!| j|| jdd
}t"|}| j| _|j#jrtd| jd dS dS dS )z(Save registry attributes to the backend.zsaving the registry is not enabled on this wandb server version. Please upgrade your server version or contact support at support@wandb.com.zACannot update artifact types when `allows_all_artifact_types` is Tz. Set it to Fz first.z$Failed to save and update registry: rn   r^   r`   zCreated registry r\   z on saverb   rd   )r_   oldProjectNamenewProjectNamezCreated new registry N)$InternalApi_server_supportsr	   +INCLUDE_ARTIFACT_TYPES_IN_REGISTRY_CREATIONRuntimeError_no_updating_registry_typesr=   r   rE   r   rV   r   rP   draftr!   r   r
   r%   r   re   r   r   r0   r    rO   r   ro   rf   upsert_modelrc   wandbtermlogr(   r   r   rename_project)r)   rj   newly_added_typesregistry_save_errorfull_saved_namerl   rr   r*   r*   r+   save*  sb   


zRegistry.savec                 C   s   t | jjdko
| jS )Nr   )lenrP   r}   rO   rD   r*   r*   r+   r|   d  s   z$Registry._no_updating_registry_typesr#   )NN)r-   N)%__name__
__module____qualname____doc__strr   r   r   r,   r(   propertyrE   r!   setterr    r   r0   rO   boolr   rP   rQ   rR   rS   r   rV   r   rZ   r   r[   classmethodr   rm   rs   ru   r   r|   r*   r*   r*   r+   r       s    


  

@

:r   )&typingr   r   r   r   r   r   	wandb_gqlr   r   wandb.proto.wandb_internal_pb2r	   wandb.sdk.artifacts._validatorsr
   r   wandb.sdk.internal.internal_apir   rx   wandb.sdk.projects._generatedr   r   r   r   r   r   r   _freezable_listr   _utilsr   r   r   r   registries_searchr   r   r   r   r*   r*   r*   r+   <module>   s     $
