o
    wi[                     @  s  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
 d dlmZ d dl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 ddlmZmZm Z m!Z!m"Z"m#Z# ddl$m%Z%m&Z&m'Z' ddl(m)Z)m*Z* erd dl+m,Z, d dl-m.Z. G dd dZ/dS )    )annotations)TYPE_CHECKINGAnyLiteral)PositiveInt)Selfassert_never)gqlN)tracked)nameof)Team)User)wandb_internal_pb2)RegistryData   )AddOnlyArtifactTypesList)MemberId
MemberKind
MemberRole
TeamMember
UserMemberparse_member_ids)
Visibility"fetch_org_entity_from_organizationprepare_artifact_types_input)CollectionsVersions)RetryingClient)RegistryFragmentc                   @  s  e Zd ZU dZded< 	 ded< 	 	dddeddZdfddZedgddZedgddZ	edgddZ
e
jdhddZ
edgddZedgd d!Zedid#d$Zejdjd%d$Zedkd'd(Zejdld)d(Zedmd+d,Zedgd-d.Zedgd/d0Zednd2d3Zedod5d6Zejdpd7d6Ze	8dqdrd>d?Ze	8dqdsdAdBZee		dtdudHdIZedvdJdKZedvdLdMZedvdNdOZdwdQdRZdxdTdUZdydWdXZdzd[d\Z dzd]d^Z!d{dbdcZ"dS )|Registryz"A single registry in the Registry.r   _saved_currentNclientr   organizationstrentitynameattrsRegistryFragment | Nonec                 C  sD   || _ |d u rtj|||d}|| _|jdd| _d S | | d S )N)r#   r%   r&   Tdeep)r"   r   model_constructr    
model_copyr!   _update_attributes)selfr"   r#   r%   r&   r'   draft r0   Y/home/ubuntu/.local/lib/python3.10/site-packages/wandb/apis/public/registries/registry.py__init__/   s   zRegistry.__init__fragmentr   returnNonec                 C  s"   t |}|| _|jdd| _dS )z3Update instance attributes from a GraphQL fragment.Tr)   N)r   from_fragmentr    r,   r!   )r.   r3   savedr0   r0   r1   r-   D   s   
zRegistry._update_attributesc                 C     | j jS )z The unique ID for this registry.)r!   idr.   r0   r0   r1   r9   J      zRegistry.idc                 C  r8   )zAFull name of the registry including the `wandb-registry-` prefix.)r!   	full_namer:   r0   r0   r1   r<   O   r;   zRegistry.full_namec                 C  r8   )z:Name of the registry without the `wandb-registry-` prefix.r!   r&   r:   r0   r0   r1   r&   T   r;   zRegistry.namevaluec                 C  s   || j _d S Nr=   r.   r>   r0   r0   r1   r&   Y      c                 C  r8   )z$Organization entity of the registry.)r!   r%   r:   r0   r0   r1   r%   ]   r;   zRegistry.entityc                 C  r8   )z"Organization name of the registry.)r!   r#   r:   r0   r0   r1   r#   b   r;   zRegistry.organization
str | Nonec                 C  r8   )zDescription of the registry.r!   descriptionr:   r0   r0   r1   rD   g   r;   zRegistry.descriptionc                 C     || j _dS )z$Set the description of the registry.NrC   r@   r0   r0   r1   rD   l      boolc                 C  r8   )zReturn whether all artifact types are allowed in the registry.

        If `True`, artifacts of any type can be added. If `False`, artifacts are
        restricted to the types listed in `artifact_types`.
        r!   allow_all_artifact_typesr:   r0   r0   r1   rI   q   s   z!Registry.allow_all_artifact_typesc                 C  rE   )z;Set whether all artifact types are allowed in the registry.NrH   r@   r0   r0   r1   rI   z   rF   r   c                 C  r8   )a]  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
        import wandb

        registry = wandb.Api().create_registry()
        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!   artifact_typesr:   r0   r0   r1   rJ      s   zRegistry.artifact_typesc                 C  r8   )z+Timestamp of when the registry was created.)r!   
created_atr:   r0   r0   r1   rK      r;   zRegistry.created_atc                 C  r8   )z0Timestamp of when the registry was last updated.)r!   
updated_atr:   r0   r0   r1   rL      r;   zRegistry.updated_at	list[str]c                 C  s   | j | jgS r?   )r%   r<   r:   r0   r0   r1   path   rA   zRegistry.path%Literal['organization', 'restricted']c                 C  s
   | j jjS )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.
        )r!   
visibilityr&   r:   r0   r0   r1   rP      s   
zRegistry.visibilityc                 C  rE   )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.
        N)r!   rP   r@   r0   r0   r1   rP      s   d   filterdict[str, Any] | Noneper_pager   r   c                 C  s   t | j| jd| ji||dS )z2Returns the collections belonging to the registry.r&   )r"   r#   registry_filtercollection_filterrT   )r   r"   r#   r<   r.   rR   rT   r0   r0   r1   collections   s   zRegistry.collectionsr   c                 C  s   t | j| jd| jid||dS )z/Returns the versions belonging to the registry.r&   N)r"   r#   rU   rV   artifact_filterrT   )r   r"   r#   r<   rW   r0   r0   r1   versions   s   zRegistry.versionsrP   rD   rJ   list[str] | Noner   c              
   C  s   ddl m}m}m}	 ddlm}
m} d|d|d}t||}t|}|||||
 | t	
|j| t|d}d| i}z|j||d	}|	|j}W n tya } zt||d
}~ww |rl|jrl|j }spt|| |||||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.
        r   )UPSERT_REGISTRY_GQLUpsertModelInputUpsertRegistry)REGISTRY_PREFIXvalidate_project_namezFailed to create registry  in organization .rD   entity_namer&   access$allow_all_artifact_types_in_registryrJ   inputvariable_valuesN)r#   r%   r&   r'   )wandb.sdk.artifacts._generatedr\   r]   r^   wandb.sdk.artifacts._validatorsr_   r`   r   r	   r   from_pythonr>   r   
model_dumpexecutemodel_validateupsert_model	Exception
ValueErrorinsertedproject)clsr"   r#   r&   rP   rD   rJ   r\   r]   r^   r_   r`   
failed_msg
org_entitygql_op	gql_inputgql_varsdataresulteregistry_projectr0   r0   r1   create   s>   


zRegistry.createc           	   
   C  s   ddl m}m} d| jd| j}t|}d| ji}z| jj||d}|	|j
}W n ty< } zt||d}~ww |rB|jsFt|dS )z*Delete the registry. This is irreversible.r   )DELETE_REGISTRY_GQLDeleteRegistryzFailed to delete registry ra   r9   rh   N)rj   r   r   r&   r#   r	   r9   r"   rn   ro   delete_modelrq   rr   success)	r.   r   r   rv   rx   rz   r{   r|   r}   r0   r0   r1   delete&  s   


zRegistry.deletec              
   C  s   ddl m}m} d| jd| jd}t|}| j| jd}z| jj	||d}|
|}W n ty> } zt||d}~ww |j }	rI|	j }
sMt|| |
 dS )	z*Load registry attributes from the backend.r   )FETCH_REGISTRY_GQLFetchRegistryzFailed to load registry ra   rb   )r&   r%   rh   N)rj   r   r   r&   r#   r	   r<   r%   r"   rn   ro   rq   rr   rt   r-   )r.   r   r   rv   rx   rz   r{   r|   r}   r%   r~   r0   r0   r1   load7  s"   

zRegistry.loadc              
   C  s  ddl m}m}m}m}m}m} ddlm} ddl	m
} || jtjs'td| jj }	r;| jr;tdddd	d
d| jd| j}
|| jj}|| jj}t|}|| j| j|| jjj| jt|	d}d| i}z| jj||d}| |j!}W n t"y } zt|
|d}~ww |r|j#rt$%d| jd| jd |r|j& }st|
| '| ||krt|}|| j||d}d| i}| jj||d}| |j(}|r|j& }st|
|j#rt$%d| jd | '| dS dS )z(Save registry attributes to the backend.r   )RENAME_REGISTRY_GQLr\   RenameProjectInputRenameRegistryr]   r^   )server_supports)r`   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 registry ra   rc   rg   rh   NzCreated registry z on save)rd   old_project_namenew_project_namezCreated new registry ))rj   r   r\   r   r   r]   r^   wandb.sdk.artifacts._gqlutilsr   rk   r`   r"   pb+INCLUDE_ARTIFACT_TYPES_IN_REGISTRY_CREATIONRuntimeErrorrJ   r/   rI   rr   r&   r#   r    r<   r!   r	   rD   r%   rP   r>   r   rm   rn   ro   rp   rq   rs   wandbtermlogrt   r-   rename_project)r.   r   r\   r   r   r]   r^   r   r`   new_artifact_typesrv   r   r   	upsert_opupsert_inputupsert_varsr{   r|   r}   r~   	rename_oprename_inputrename_varsr0   r0   r1   saveN  sv    



zRegistry.savelist[UserMember | TeamMember]c                 C  s   g |   |  S )z?Returns the current members (users and teams) of this registry.)user_membersteam_membersr:   r0   r0   r1   members  s   zRegistry.memberslist[UserMember]c                   n   ddl m}m} t|} j jd} jj||d}||}|j	 }s-t
d j fdd|jD S )z2Returns the current member users of this registry.r   )REGISTRY_USER_MEMBERS_GQLRegistryUserMembersrt   r%   rh   z*Failed to fetch user members for registry c              	     s2   g | ]}t t j|jd dhdd|jjdqS )Trole)exclude_noneexclude)r"   r'   )userr   )r   r   r"   rm   r   r&   .0mr:   r0   r1   
<listcomp>  s    
z)Registry.user_members.<locals>.<listcomp>)rj   r   r   r	   r<   r%   r"   rn   ro   rt   rr   r&   r   )r.   r   r   rx   rz   r{   r|   rt   r0   r:   r1   r        



zRegistry.user_memberslist[TeamMember]c                   r   )z2Returns the current member teams of this registry.r   )REGISTRY_TEAM_MEMBERS_GQLRegistryTeamMembersr   rh   z*Failed to fetch team members for registry c              	     s6   g | ]}t t j|jj|jjd dd|jjdqS )T)r   )r"   r&   r'   )teamr   )r   r   r"   r   r&   rm   r   r   r:   r0   r1   r     s    
z)Registry.team_members.<locals>.<listcomp>)rj   r   r   r	   r<   r%   r"   rn   ro   rt   rr   r&   r   )r.   r   r   rx   rz   r{   r|   rt   r0   r:   r1   r     r   zRegistry.team_membersr   +User | UserMember | Team | TeamMember | strc                 G     ddl m}m}m} |stdt| jdt|\}}t|}|||| j	d}d|
 i}	| jj||	d}
||
j}|rB|jsJtd| j| S )	af  Adds users or teams to this registry.

        Args:
            members: The users or teams to add to the registry. Accepts
                `User` objects, `Team` objects, or their string IDs.

        Returns:
            This registry for further method chaining, if needed.

        Raises:
            TypeError: If no members are passed as arguments.
            ValueError: If unable to infer or parse the user or team IDs.

        Examples:
        ```python
        import wandb

        api = wandb.Api()

        # Fetch an existing registry
        registry = api.registry(name="my-registry", organization="my-org")

        user1 = api.user(username="some-user")
        user2 = api.user(username="other-user")
        registry.add_members(user1, user2)

        my_team = api.team(name="my-team")
        registry.add_members(my_team)
        ```
        r   )CREATE_REGISTRY_MEMBERS_GQLCreateProjectMembersInputCreateRegistryMembers$Must provide at least one member to rb   user_idsteam_ids
project_idrg   rh   z"Failed to add members to registry )rj   r   r   r   	TypeErrorr   add_membersr   r	   r9   rm   r"   rn   ro   r|   r   rr   r&   )r.   r   r   r   r   r   r   rx   ry   rz   r{   r|   r0   r0   r1   r         !
zRegistry.add_membersc                 G  r   )	ay  Removes users or teams from this registry.

        Args:
            members: The users or teams to remove from the registry. Accepts
                `User` objects, `Team` objects, or their string IDs.

        Returns:
            This registry for further method chaining, if needed.

        Raises:
            TypeError: If no members are passed as arguments.
            ValueError: If unable to infer or parse the user or team IDs.

        Examples:
        ```python
        import wandb

        api = wandb.Api()

        # Fetch an existing registry
        registry = api.registry(name="my-registry", organization="my-org")

        user1 = api.user(username="some-user")
        user2 = api.user(username="other-user")
        registry.remove_members(user1, user2)

        old_team = api.team(name="old-team")
        registry.remove_members(old_team)
        ```
        r   )DELETE_REGISTRY_MEMBERS_GQLDeleteProjectMembersInputDeleteRegistryMembersr   rb   r   rg   rh   z'Failed to remove members from registry )rj   r   r   r   r   r   r   r   r	   r9   rm   r"   rn   ro   r|   r   rr   r&   )r.   r   r   r   r   r   r   rx   ry   rz   r{   r|   r0   r0   r1   remove_members  r   zRegistry.remove_membersmemberr   MemberRole | strc                 C  s   ddl m}m}m}m}m}m} t|}	|	j	t
ju r,t|}
||	 | j|d}|}n|	j	t
ju rCt|}
||	 | j|d}|}nt|	j	 d| i}| jj|
|d}||j}|ra|jsotd|d|d	| j| S )
a  Updates the role of a member (user or team) within this registry.

        Args:
            member: The user or team to update the role of.
                Accepts a `User` object, `Team` object, or their string ID.
            role: The new role to assign to the member. May be one of:
                - "admin"
                - "member"
                - "viewer"
                - "restricted_viewer" (if supported by the W&B server)

        Returns:
            This registry for further method chaining, if needed.

        Raises:
            ValueError: If unable to infer the user or team ID.

        Examples:
        Make all users in the registry admins
        ```python
        import wandb

        api = wandb.Api()

        # Fetch an existing registry
        registry = api.registry(name="my-registry", organization="my-org")

        for member in registry.user_members():
            registry.update_member(member.user, role="admin")
        ```
        r   )UPDATE_TEAM_REGISTRY_ROLE_GQLUPDATE_USER_REGISTRY_ROLE_GQLUpdateProjectMemberInputUpdateProjectTeamMemberInputUpdateTeamRegistryRoleUpdateUserRegistryRole)user_idr   user_project_role)team_idr   team_project_rolerg   rh   zFailed to update member z	 role to z in registry )rj   r   r   r   r   r   r   r   from_objkindr   USERr	   encoder9   ENTITYr   rm   r"   rn   ro   r|   r   rr   r&   )r.   r   r   r   r   r   r   r   r   id_rx   ry   
result_clsrz   r{   r|   r0   r0   r1   update_memberO  s.    $
	

zRegistry.update_memberr?   )
r"   r   r#   r$   r%   r$   r&   r$   r'   r(   )r3   r   r4   r5   )r4   r$   )r>   r$   )r4   rB   )r>   r$   r4   r5   )r4   rG   )r>   rG   r4   r5   )r4   r   )r4   rM   )r4   rO   )r>   rO   )NrQ   )rR   rS   rT   r   r4   r   )rR   rS   rT   r   r4   r   )NN)r"   r   r#   r$   r&   r$   rP   rO   rD   rB   rJ   r[   r4   r   )r4   r5   )r4   r   )r4   r   )r4   r   )r   r   r4   r   )r   r   r   r   r4   r   )#__name__
__module____qualname____doc____annotations__r2   r-   propertyr9   r<   r&   setterr%   r#   rD   rI   rJ   rK   rL   rN   rP   r
   rX   rZ   classmethodr   r   r   r   r   r   r   r   r   r   r0   r0   r0   r1   r   &   s   
 
G
R



99r   )0
__future__r   typingr   r   r   pydanticr   typing_extensionsr   r   	wandb_gqlr	   r   wandb._analyticsr
   wandb._strutilsr   wandb.apis.public.teamsr   wandb.apis.public.usersr   wandb.protor   r   wandb.sdk.artifacts._modelsr   _freezable_listr   _membersr   r   r   r   r   r   _utilsr   r   r   registries_searchr   r   wandb.apis.public.apir   rj   r   r   r0   r0   r0   r1   <module>   s(     