U
    diz)                     @   s   d 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mZ dd	lmZmZmZ dd
lmZ ddlmZ eeZG dd deZG dd dZG dd dZG dd deZG dd deZG dd deZdS )a  Contains commands to interact with repositories on the Hugging Face Hub.

Usage:
    # create a new dataset repo on the Hub
    hf repo create my-cool-dataset --repo-type=dataset

    # create a private model repo on the Hub
    hf repo create my-cool-model --private
    N)_SubParsersAction)Optional)	HTTPError)BaseHuggingfaceCLICommand)ANSI)
REPO_TYPESSPACES_SDK_TYPES)HfHubHTTPErrorRepositoryNotFoundErrorRevisionNotFoundError)HfApi)loggingc                   @   s   e Zd ZeedddZdS )RepoCommands)parserc                    s6  | j ddd  jdd} j fddd |j dd	d}|jd
tdd |jdtdd |jdtdtd |jdddd |jdtdd |jdddd |jdtdd |jdd d |j ddd}|jdd d!d"}|j dd#d}|jd
td$d |jdtd%d |jd&d'td(d |jd)td*d |jdtd+d |jdd,d-d.gd,d/d0 |jd1d d |j d2d3d}|jd
td4d |jdtd+d |jdd,d-d.gd,d/d0 |jd5d d |j d6d7d}|jd
td8d |jdtd9d |jd:d;dd<d |jdtd+d |jdd,d-d.gd,d/d0 |jd=d d d S )>NrepozManage repos on the Hub.)helpz%huggingface.co repos related commandsc                    s      S N)
print_helpargsZrepo_parser </tmp/pip-unpacked-wheel-ohruig0i/huggingface_hub/cli/repo.py<lambda>0       z2RepoCommands.register_subcommand.<locals>.<lambda>)funccreatez#Create a new repo on huggingface.corepo_idzThe ID of the repo to create to (e.g. `username/repo-name`). The username is optional and will be set to your username if not provided.)typer   z--repo-typezWOptional: set to "dataset" or "space" if creating a dataset or space, default is model.z--space_sdkzOOptional: Hugging Face Spaces SDK type. Required when --type is set to "space".)r   r   choicesz	--private
store_truezhWhether to create a private repository. Defaults to public unless the organization's default is private.)actionr   z--tokenzLHugging Face token. Will default to the locally saved token if not provided.z
--exist-okz-Do not raise an error if repo already exists.z--resource-group-idzoResource group in which to create the repo. Resource groups is only available for Enterprise Hub organizations.c                 S   s   t | S r   )RepoCreateCommandr   r   r   r   r   X   r   tagz"Manage tags for a repo on the Hub.zTag actionsZ
tag_actionT)r   destrequiredzCreate a tag for a repo.z6The ID of the repo to tag (e.g. `username/repo-name`).zThe name of the tag to create.z-mz	--messagez%The description of the tag to create.z
--revisionzThe git revision to tag.zJA User Access Token generated from https://huggingface.co/settings/tokens.modelZdatasetspacez6Set the type of repository (model, dataset, or space).)r   defaultr   c                 S   s   t | S r   )RepoTagCreateCommandr   r   r   r   r   o   r   listzList tags for a repo.z@The ID of the repo to list tags for (e.g. `username/repo-name`).c                 S   s   t | S r   )RepoTagListCommandr   r   r   r   r      r   deletezDelete a tag from a repo.zFThe ID of the repo to delete the tag from (e.g. `username/repo-name`).zThe name of the tag to delete.z-yz--yesz$Answer Yes to prompts automatically.c                 S   s   t | S r   )RepoTagDeleteCommandr   r   r   r   r      r   )
add_parseradd_subparsersset_defaultsadd_argumentstrr   )r   Zrepo_subparsersZrepo_create_parserZrepo_tag_parserZtag_subparsersZtag_create_parserZtag_list_parserZtag_delete_parserr   r   r   register_subcommand*   s                z RepoCommands.register_subcommandN)__name__
__module____qualname__staticmethodr   r3   r   r   r   r   r   )   s   r   c                   @   s$   e Zd ZejdddZdd ZdS )r"   r   c                 C   sD   |j | _ |j| _|j| _|j| _|j| _|j| _|j| _t | _d S r   )	r   	repo_type	space_sdkprivatetokenexist_okresource_group_idr   _apiselfr   r   r   r   __init__   s    zRepoCreateCommand.__init__c              	   C   sX   | j j| j| j| j| j| j| j| jd}t	dt
|j d t	dt
|  d S )N)r   r8   r:   r;   r<   r=   r9   zSuccessfully created z on the Hub.zYour repo is now available at )r>   Zcreate_repor   r8   r:   r;   r<   r=   r9   printr   bold)r@   repo_urlr   r   r   run   s    	zRepoCreateCommand.runN)r4   r5   r6   argparse	NamespacerA   rE   r   r   r   r   r"      s   
r"   c                   @   s   e Zd Zdd ZdS )RepoTagCommandc                 C   sN   || _ tt|dd d| _|j| _t|dd| _| jtkrJtd td d S )Nr;   )r;   r8   r&   zInvalid repo --repo-type   )	r   r   getattrapir   r8   r   rB   exitr?   r   r   r   rA      s    
zRepoTagCommand.__init__N)r4   r5   r6   rA   r   r   r   r   rH      s   rH   c                   @   s   e Zd Zdd ZdS )r)   c                 C   sz  t dtt| jj d| j dt| j  z6| jj	| j| jjt
| jdd t
| jdd | jd W n tk
r   t | j  dt| j d td Y n tk
r   t d	ttt
| jdd  d td Y nh tk
rJ } zH|jjd
kr6t dtt| jj dt| j  td |W 5 d }~X Y nX t dtt| jj dt| j  d S )NzYou are about to create tag  on  messagerevision)r   r#   Ztag_messagerP   r8    not found.rI   z	Revision i  Tag z already exists on z created on )rB   r   rC   r2   r   r#   r8   r   rK   Z
create_tagrJ   r
   
capitalizerL   r   r	   responsestatus_code)r@   er   r   r   rE      s.    ,
"$*zRepoTagCreateCommand.runNr4   r5   r6   rE   r   r   r   r   r)      s   r)   c                   @   s   e Zd Zdd ZdS )r+   c              
   C   s   z| j j| j| jd}W n tk
rV   t| j  dt| j d t	d Y nD t
k
r } z&t| tt|jj t	d W 5 d }~X Y nX t|jdkrtd t	d td| j dt| j d |jD ]}t|j qd S )	N)r   r8   rN   rQ   rI   r   zNo tags foundz	Tags for :)rK   Zlist_repo_refsr   r8   r
   rB   rS   r   rC   rL   r   ZredrT   textlentagsname)r@   refsrV   r#   r   r   r   rE      s$    
" 
zRepoTagListCommand.runNrW   r   r   r   r   r+      s   r+   c                   @   s   e Zd Zdd ZdS )r-   c              
   C   s&  t dt| jj d| j dt| j  t| jdds^td	 }|dkr^t d t
  z| jj| j| jj| jd	 W n~ tk
r   t | j  dt| j d
 t
d Y nB tk
r   t dt| jj dt| j  t
d Y nX t dt| jj dt| j  d S )NzYou are about to delete tag rM   rN   yesFzProceed? [Y/n] ) yr^   ZAbort)r   r#   r8   rQ   rI   rR   z not found on z deleted on )rB   r   rC   r   r#   r8   r   rJ   inputlowerrL   rK   Z
delete_tagr
   rS   r   )r@   choicer   r   r   rE      s    ."&zRepoTagDeleteCommand.runNrW   r   r   r   r   r-      s   r-   )__doc__rF   r   typingr   Zrequests.exceptionsr   Zhuggingface_hub.commandsr   Z#huggingface_hub.commands._cli_utilsr   Zhuggingface_hub.constantsr   r   Zhuggingface_hub.errorsr	   r
   r   Zhuggingface_hub.hf_apir   Zhuggingface_hub.utilsr   Z
get_loggerr4   loggerr   r"   rH   r)   r+   r-   r   r   r   r   <module>   s"   

k