o
    ~ri&                     @   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   j/lsinfo/ai/hellotax_ai/llm_service/venv_embed/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   items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)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.r3   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 )Nr8    r9   z(job)r:   Fr    r;   c                 s   s8    | ]}t |tr|d   d|d  nt|V  qdS )r   r   r"   N)
isinstancedictstrr5   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>)r1   r2   headersrow_fnN)r   list_webhooksr   )r1   r2   r3   apiresultsr   r   r   webhooks_lsd   s   

rJ   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.r4      indentN)r   get_webhookprintjsondumpsr   rL   r3   rH   webhookr   r   r   webhooks_info   s   

rV   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.r9   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.r4   c                 S      g | ]}|j qS r   valuer   r   r   r   r7      r   z#webhooks_create.<locals>.<listcomp>)r9   rY   r<   r;   r[   zWebhook created: rM   rN   )
r%   r&   r   r.   create_webhookrQ   r8   rR   rS   r   )
rX   r9   rY   rZ   r[   r3   rH   watched_itemsr;   rU   r   r   r   webhooks_create   s   ,


ra   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.r4   Nc                 S   r\   r   r]   r   r   r   r   r7      r   z#webhooks_update.<locals>.<listcomp>)r9   r<   r;   r[   zWebhook updated: rM   rN   )r   r.   update_webhookrQ   r8   rR   rS   r   )
rL   r9   rX   rZ   r[   r3   rH   r`   r;   rU   r   r   r   webhooks_update   s   
&rd   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.r4   zWebhook enabled: N)r   enable_webhookrQ   r8   rT   r   r   r   webhooks_enable      

rh   disablezhf webhooks disable abc123z!The ID of the webhook to disable.c                 C   rf   )zDisable an active webhook.r4   zWebhook disabled: N)r   disable_webhookrQ   r8   rT   r   r   r   webhooks_disable  ri   rl   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.r4   zWebhook deleted: N)r%   confirmrQ   Abortr   delete_webhook)rL   rn   r3   ro   rH   r   r   r   webhooks_delete  s   

rr   )N)NNNNN)FN)+__doc__enumrR   typingr   r   r   r   r%   huggingface_hub.constantsr   huggingface_hub.hf_apir   
_cli_utilsr	   r
   r   r   r   r   r   r   r$   Enumr@   r   _DOMAIN_TYPESr   listr.   webhooks_clicommandtablerJ   ArgumentrV   Optionra   rd   rh   rl   boolrr   r   r   r   r   <module>   s  (
	



0




&


