U
    ”Ê¹jz)  ã                   @   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© úW/var/www/html/bwave/python/venv/lib/python3.8/site-packages/huggingface_hub/cli/repo.pyÚ<lambda>0   ó    z2RepoCommands.register_subcommand.<locals>.<lambda>)ÚfuncÚcreatez#Create a new repo on huggingface.coÚrepo_idz‡The 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r¸tdƒ t	dƒ td| j› dt | j¡› d�ƒ |jD ]}t|jƒ qÞd 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   Ú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