o
    wi                     @  s   d Z ddlmZ 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ZddlmZ er6d	d
lmZmZ G dd deZdS )a  W&B Public API for managing users and API keys.

This module provides classes for managing W&B users and their API keys.

Note:
    This module is part of the W&B Public API and provides methods to manage
    users and their authentication. Some operations require admin privileges.
    )annotations)MutableMapping)TYPE_CHECKINGAny)Self)gqlN)Attrs   )ApiRetryingClientc                      s|   e Zd ZdZd% fddZed&d
dZed'd(ddZed)ddZ	ed)ddZ
d*ddZd+d,d!d"Zd-d#d$Z  ZS ).Userax  A user on a W&B instance.

    This allows managing a user's API keys and accessing information like
    team memberships. The `create` class method can be used to create a new
    user.

    Args:
        client: The GraphQL client to use for network operations.
        attrs: A subset of the User type in the GraphQL schema.

    <!-- lazydoc-ignore-init: internal -->
    clientr   attrsMutableMapping[str, Any]c                   s   t  | || _d | _d S N)super__init___client	_user_api)selfr   r   	__class__ K/home/ubuntu/.local/lib/python3.10/site-packages/wandb/apis/public/users.pyr   '   s   
zUser.__init__return
Api | Nonec                 C  s*   | j du r| jrtj| jd d| _ | j S )z4A `wandb.Api` instance using the user's credentials.Nr   )api_key)r   api_keyswandbr
   r   r   r   r   user_api,   s   zUser.user_apiFapir
   emailstradminboolr   c           	      C  sJ   ddl m}m} t|}|j|||d}||jj}| |j|	 S )a  Create a new user.

        This is an internal method. Use the `create_user()` method of
        `wandb.Api` instead.

        Args:
            api: The API instance to use to create the user.
            email: The email for the user.
            admin: Whether this user should be a global instance admin.

        Returns:
            A `User` object.

        <!-- lazydoc-ignore-classmethod: internal -->
        r   )CREATE_USER_FROM_ADMIN_GQLCreateUserFromAdmin)r"   r$   )
wandb.apis._generatedr&   r'   r   r   executemodel_validateresultuser
model_dump)	clsr!   r"   r$   r&   r'   gql_opdatar,   r   r   r   create3   s
   zUser.create	list[str]c                 C  ,   | j ddu r
g S dd | j d d D S )a4  Names of the user's API keys.

        This property returns the names of the the API keys, *not* the secret
        associated with the key. The name of the key cannot be used as an API
        key.

        The list is empty if the user has no API keys or if API keys have not
        been loaded.
        apiKeysNc                 S     g | ]}|d  d qS nodenamer   .0kr   r   r   
<listcomp>[       z!User.api_keys.<locals>.<listcomp>edges_attrsgetr   r   r   r   r   N   s   zUser.api_keysc                 C  r3   )zNames of the user's teams.

        This is an empty list if the user has no team memberships or if teams
        data was not loaded.
        teamsNc                 S  r5   r6   r   r9   r   r   r   r<   f   r=   zUser.teams.<locals>.<listcomp>r>   r?   r   r   r   r   rB   ]   s   z
User.teamsr   c                 C  sp   ddl m} ddlm} | j|}| jd d | d d }z| jt	|d|i W d	S  |y7   Y dS w )
a1  Delete a user's API key.

        Only the owner of the key or an admin can delete it.

        Args:
            api_key: The name of the API key to delete. Use one of
                the names returned by the `api_keys` property.

        Returns:
            True on success, false on failure.
        r   	HTTPError)DELETE_API_KEY_GQLr4   r>   r7   idFT)
requestsrD   r(   rE   r   indexr@   r   r)   r   )r   r   rD   rE   idx
api_key_idr   r   r   delete_api_keyh   s   zUser.delete_api_keyNdescription
str | Nonec              	   C  s   ddl m} ddlm}m} z't|}| jj|d|i}|	|j
j}| jd d d| i W |jS  |tfyA   Y dS w )	a.  Generate a new API key.

        Args:
            description: A description for the new API key. This can be
                used to identify the purpose of the API key.

        Returns:
            The generated API key (the full secret, not just the name), or
            None on failure.
        r   rC   )GENERATE_API_KEY_GQLGenerateApiKeyrL   r4   r>   r7   N)rG   rD   r(   rN   rO   r   r    r   r)   r*   r+   r   r@   appendr-   AttributeErrorr8   )r   rL   rD   rN   rO   r/   r0   key_fragmentr   r   r   generate_api_key   s   zUser.generate_api_keyc                 C  st   | j d }rd| dS | j d }rd| dS | j d }r*d| dS | j d }r8d|dS dS )Nr"   z<User >usernamerF   r8   z
<User ???>r?   )r   r"   rU   id_r8   r   r   r   __repr__   s   zUser.__repr__)r   r   r   r   )r   r   )F)r!   r
   r"   r#   r$   r%   r   r   )r   r2   )r   r#   r   r%   r   )rL   rM   r   rM   )r   r#   )__name__
__module____qualname____doc__r   propertyr    classmethodr1   r   rB   rK   rS   rW   __classcell__r   r   r   r   r      s    

r   )r[   
__future__r   collections.abcr   typingr   r   typing_extensionsr   	wandb_gqlr   r   wandb.apis.attrsr   r!   r
   r   r   r   r   r   r   <module>   s    	