o
    0i&                     @   s  d Z ddlZddlZddlmZmZmZmZ ddl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mZmZ eeed Zejd	d
d eD edZee
Zejddd eD edZdee dee fddZeddZejdg ddej ddfdedededdfddZ!ejddgd	dRd eeej"d!df deddfd"d#Z#ejd$g d%d					dSd&eee ej$d'd(df d)eee ej$d*df d+eee ej$d,d-df d.eeee  ej$d/d0df d1eee ej$d2df deddfd3d4Z%ejd5g d6d					dSd eeej"d7df d)eee ej$d8df d&eeee  ej$d'd9df d.eeee  ej$d/d:df d1eee ej$d;df deddfd<d=Z&ejd>d?gd	dRd eeej"d@df deddfdAdBZ'ejdCdDgd	dRd eeej"dEdf deddfdFdGZ(ejdHdIdJgd		dTd eeej"dKdf dLee)ej$dMdNdOdf deddfdPdQZ*dS )Ua  Contains commands to manage webhooks on the Hugging Face Hub.

Usage:
    # list all webhooks
    hf webhooks ls

    # show details of a single webhook
    hf webhooks info <webhook_id>

    # create a new webhook
    hf webhooks create --url https://example.com/hook --watch model:bert-base-uncased

    # create a webhook watching multiple items and domains
    hf webhooks create --url https://example.com/hook --watch org:HuggingFace --watch model:gpt2 --domain repo

    # update a webhook
    hf webhooks update <webhook_id> --url https://new-url.com/hook

    # enable / disable a webhook
    hf webhooks enable <webhook_id>
    hf webhooks disable <webhook_id>

    # delete a webhook
    hf webhooks delete <webhook_id>
    N)	AnnotatedOptionalget_argsget_type_hints)WEBHOOK_DOMAIN_T)WebhookWatchedItem   )	FormatOptOutputFormatQuietOptTokenOptapi_object_to_dict
get_hf_apiprint_list_outputtyper_factorytypeWatchedItemTypec                 C      i | ]}||qS  r   ).0tr   r   V/home/kim/smarthome/.venv/lib/python3.10/site-packages/huggingface_hub/cli/webhooks.py
<dictcomp>?       r   )r   WebhookDomainc                 C   r   r   r   r   dr   r   r   r   B   r   valuesreturnc              	   C   s   g }t t}| D ]9}d|vrtd| dd| d|dd\}}||vr8td| dd| d|t||d q|S )	aY  Parse 'type:name' strings into WebhookWatchedItem objects.

    Args:
        values: List of strings in the format 'type:name'
            (e.g., 'model:bert-base-uncased', 'org:HuggingFace').

    Returns:
        List of WebhookWatchedItem objects.

    Raises:
        typer.BadParameter: If any value doesn't match the expected format.
    :zCExpected format 'type:name' (e.g. 'model:bert-base-uncased'), got 'z'. Valid types: , .r   zInvalid type ')r   name)tuple_WATCHED_TYPEStyperBadParameterjoinsplitappendr   )r   itemsZvalid_typesvkindr"   r   r   r   _parse_watchE   s   r-   zManage webhooks on the Hub.)helpz	list | ls)zhf webhooks lszhf webhooks ls --format jsonzhf webhooks ls -q)ZexamplesFformatquiettokenc                 C   s:   t |d}dd | D }t|| |g ddd d dS )	z'List all webhooks for the current user.r1   c                 S   s   g | ]}t |qS r   )r   r   wr   r   r   
<listcomp>s   s    zwebhooks_ls.<locals>.<listcomp>)idurldisableddomainswatchedc              
   S   sV   |  dd|  dpdt|  ddd|  dpg dd	d
 |  dp&g D gS )Nr6    r7   z(job)r8   Fr    r9   c                 s   s8    | ]}t |tr|d   d|d  nt|V  qdS )r   r   r"   N)
isinstancedictstrr3   r   r   r   	<genexpr>~   s    (
z0webhooks_ls.<locals>.<lambda>.<locals>.<genexpr>r:   )getr>   r'   )itemr   r   r   <lambda>y   s   

zwebhooks_ls.<locals>.<lambda>)r/   r0   headersZrow_fnN)r   Zlist_webhooksr   )r/   r0   r1   apiresultsr   r   r   webhooks_lsd   s   

rF   infozhf webhooks info abc123
webhook_idzThe ID of the webhook.c                 C   s.   t |d}|| }ttjt|dd dS )z/Show full details for a single webhook as JSON.r2      indentN)r   Zget_webhookprintjsondumpsr   rH   r1   rD   webhookr   r   r   webhooks_info   s   

rQ   create)zQhf webhooks create --url https://example.com/hook --watch model:bert-base-uncasedzjhf webhooks create --url https://example.com/hook --watch org:HuggingFace --watch model:gpt2 --domain repozJhf webhooks create --job-id 687f911eaea852de79c4a50a --watch user:julien-cwatchz--watchzRItem to watch, in 'type:name' format (e.g. 'model:bert-base-uncased'). Repeatable.r7   zBURL to send webhook payloads to. Mutually exclusive with --job-id.job_idz--job-idz]ID of a Job to trigger (from job.id) instead of pinging a URL. Mutually exclusive with --url.domainz--domainzNDomain to watch: 'repo' or 'discussions'. Repeatable. Defaults to all domains.secretz.Optional secret used to sign webhook payloads.c           
      C   s   |dur|durt d|du r|du rt dt|d}t| }|r,dd |D nd}|j|||||d}	td|	j  ttjt	|	d	d
 dS )z|Create a new webhook.

    Provide either --url (to ping a remote server) or --job-id (to trigger a Job), but not both.
    Nz+Provide either --url or --job-id, not both.z!Provide either --url or --job-id.r2   c                 S      g | ]}|j qS r   valuer   r   r   r   r5      r   z#webhooks_create.<locals>.<listcomp>)r7   rT   r:   r9   rV   zWebhook created: rI   rJ   )
r%   r&   r   r-   Zcreate_webhookrL   r6   rM   rN   r   )
rS   r7   rT   rU   rV   r1   rD   watched_itemsr9   rP   r   r   r   webhooks_create   s   ,


r[   update)z8hf webhooks update abc123 --url https://new-url.com/hookz:hf webhooks update abc123 --watch model:gpt2 --domain repoz,hf webhooks update abc123 --secret newsecretz The ID of the webhook to update.z$New URL to send webhook payloads to.ziNew list of items to watch, in 'type:name' format. Repeatable. Replaces the entire existing watched list.zBNew list of domains to watch: 'repo' or 'discussions'. Repeatable.z)New secret used to sign webhook payloads.c           
      C   sn   t |d}|rt|nd}|rdd |D nd}|j| ||||d}	td|	j  ttjt|	dd dS )	z>Update an existing webhook. Only provided options are changed.r2   Nc                 S   rW   r   rX   r   r   r   r   r5      r   z#webhooks_update.<locals>.<listcomp>)r7   r:   r9   rV   zWebhook updated: rI   rJ   )r   r-   Zupdate_webhookrL   r6   rM   rN   r   )
rH   r7   rS   rU   rV   r1   rD   rZ   r9   rP   r   r   r   webhooks_update   s   
&r]   enablezhf webhooks enable abc123z The ID of the webhook to enable.c                 C   (   t |d}|| }td|j  dS )zEnable a disabled webhook.r2   zWebhook enabled: N)r   Zenable_webhookrL   r6   rO   r   r   r   webhooks_enable      

r`   disablezhf webhooks disable abc123z!The ID of the webhook to disable.c                 C   r_   )zDisable an active webhook.r2   zWebhook disabled: N)r   Zdisable_webhookrL   r6   rO   r   r   r   webhooks_disable  ra   rc   deletezhf webhooks delete abc123zhf webhooks delete abc123 --yesz The ID of the webhook to delete.yesz--yesz-yzSkip confirmation prompt.c                 C   sP   |st d|  d}|std t  t|d}||  td|   dS )zDelete a webhook permanently.z)Are you sure you want to delete webhook 'z'?zAborted.r2   zWebhook deleted: N)r%   confirmrL   ZAbortr   Zdelete_webhook)rH   re   r1   rf   rD   r   r   r   webhooks_delete  s   

rg   )N)NNNNN)FN)+__doc__enumrM   typingr   r   r   r   r%   Zhuggingface_hub.constantsr   Zhuggingface_hub.hf_apir   Z
_cli_utilsr	   r
   r   r   r   r   r   r   r$   Enumr>   r   Z_DOMAIN_TYPESr   listr-   Zwebhooks_clicommandtablerF   ZArgumentrQ   Optionr[   r]   r`   rc   boolrg   r   r   r   r   <module>   s  (
	



0




&


