o
     
jn1                     @   st  d Z ddlZddlZddlmZ ddlmZ ddl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mZ ddlmZ G d	d
 d
eejZG dd deejZddhZeeejdddf ZdedB dedB dedB fddZeddZej dg ddej!ej"ddej#dfdedeeej$ddd df d!eeej$d"d#d$df d%ed&ed'ed(eddfd)d*Z%ej d+d,d-gdej#dfded.ed'ed(eddf
d/d0Z&ej d1g d2dddd3ej#dfded4eeej$d5d6df deedB ej$d7d8df deedB ej$d9d:df d;ee'ej$d<d=d>df d'ed(eddfd?d@Z(ej dAdBdCgdddej#dfded.edeedB ej$d7dDdf deedB ej$d9dEdf d'ed(eddfdFdGZ)ej dHdIdJgddd3ej#dfded.edAeedB ej$dKdLdf dMee'ej$dNdOdPdf d'ed(eddfdQdRZ*ej dSdTdUgddd3ej#dfded.edAeedB ej$dKdVdf dMee'ej$dNdOdPdf d'ed(eddfdWdXZ+ej dYdZgdej#dfded.ed[eeejd\df d'ed(eddfd]d^Z,ej d_d`dagddd3ej#dfded.edAeedB ej$dKdbdf dMee'ej$dNdOdPdf d'ed(eddfdcddZ-ej dedfgdej#dfded.ed'ed(eddf
dgdhZ.dS )izYContains commands to interact with discussions and pull requests on the Hugging Face Hub.    N)Path)	Annotated)	constants   )		AuthorOptLimitOpt	RepoIdArgRepoTypeRepoTypeOptTokenOptapi_object_to_dict
get_hf_apityper_factory)outc                   @   s    e Zd ZdZdZdZdZdZdS )DiscussionStatusopenclosedmergeddraftallN)__name__
__module____qualname__r   r   r   r   r    r   r   R/home/kuhnn/.local/lib/python3.10/site-packages/huggingface_hub/cli/discussions.pyr   '   s    r   c                   @   s   e Zd ZdZdZdZdS )DiscussionKindr   
discussionpull_requestN)r   r   r   r   r   r   r   r   r   r   r   /   s    r   r   r   z&The discussion or pull request number.)helpminbody	body_filereturnc                 C   sH   | dur|durt d|dur"t|dkrtj S |jddS | S )zFResolve body text from --body or --body-file (supports '-' for stdin).Nz'Cannot use both --body and --body-file.-zutf-8)encoding)typerBadParameterstrsysstdinread	read_text)r    r!   r   r   r   
_read_bodyE   s   

r,   z0Manage discussions and pull requests on the Hub.)r   z	list | ls)z%hf discussions list username/my-modelzIhf discussions list username/my-model --kind pull_request --status mergedzFhf discussions list username/my-dataset --type dataset --status closedzBhf discussions list username/my-model --author alice --format json)examples   repo_idstatusz-sz--statusz4Filter by status (open, closed, merged, draft, all).kindz-kz--kindz/Filter by kind (discussion, pull_request, all).authorlimit	repo_typetokenc                 C   s   t |d}|tjkrd}n
|tjkrd}nd}|tjkrd}	n|j}	g }
|j| ||	||jdD ]}|jtv r=|j	|jkr=q/|

| t|
|krJ nq/dd |
D }tj|g dd	d	d
id dS )z-List discussions and pull requests on a repo.r5   r   r   N)r/   r2   discussion_typediscussion_statusr4   c                 S   s   g | ]}t |qS r   )r   ).0dr   r   r   
<listcomp>   s    z#discussion_list.<locals>.<listcomp>)numtitleis_pull_requestr0   r2   
created_atr<   right)headersid_key
alignments)r   r   r   r   r   r   valueget_repo_discussions_CLIENT_SIDE_STATUSESr0   appendlenr   table)r/   r0   r1   r2   r3   r4   r5   api
api_statusapi_discussion_typediscussionsr:   itemsr   r   r   discussion_listS   s<   
!





rO   infoz'hf discussions info username/my-model 5z5hf discussions info username/my-model 5 --format jsonr<   c                 C   s*   t |d}|j| ||jd}t| dS )z,Get info about a discussion or pull request.r6   r/   discussion_numr4   N)r   get_discussion_detailsrD   r   dictr/   r<   r4   r5   rJ   detailsr   r   r   discussion_info   s   
rW   create)z<hf discussions create username/my-model --title "Bug report"zWhf discussions create username/my-model --title "Feature request" --body "Please add X"zIhf discussions create username/my-model --title "Fix typo" --pull-requestzUhf discussions create username/my-dataset --type dataset --title "Data quality issue"Fr=   z--titlez,The title of the discussion or pull request.z--bodyz$The description (supports Markdown).z--body-filez4Read the description from a file. Use '-' for stdin.r   z--pull-requestz--prz.Create a pull request instead of a discussion.c                 C   sx   t ||}t|d}|j| |||j|d}	|rdnd}
|r#d|	j nd}tjd|
 d|	j d	|  |	j|	j|d
 dS )z2Create a new discussion or pull request on a repo.r6   )r/   r=   descriptionr4   r   zpull requestr   zrefs/pr/NzCreated z #z on )r<   urlref)r,   r   create_discussionrD   r<   r   resultrZ   )r/   r=   r    r!   r   r4   r5   rY   rJ   r   r1   r[   r   r   r   discussion_create   s   
,
.r^   commentzIhf discussions comment username/my-model 5 --body "Thanks for reporting!"z9hf discussions comment username/my-model 5 --body "LGTM!"z%The comment text (supports Markdown).z0Read the comment from a file. Use '-' for stdin.c                 C   sZ   t ||}|du rtdt|d}|j| |||jd tjd| d|  || d dS )z(Comment on a discussion or pull request.Nz)Either --body or --body-file is required.r6   r/   rR   r_   r4   zCommented on # in r<   repo)r,   r%   r&   r   comment_discussionrD   r   r]   )r/   r<   r    r!   r4   r5   r_   rJ   r   r   r   discussion_comment   s   


 re   closez(hf discussions close username/my-model 5zIhf discussions close username/my-model 5 --comment "Closing as resolved."z	--commentz)An optional comment to post when closing.yesz--yesz-yzSkip confirmation prompt.c                 C   \   t jd| d|  d|d t|d}|j| |d||jd t jd| d	|  || d
 dS )z#Close a discussion or pull request.zClose # on ''?rg   r6   r   r/   rR   
new_statusr_   r4   zClosed #ra   rb   Nr   confirmr   change_discussion_statusrD   r]   r/   r<   r_   rg   r4   r5   rJ   r   r   r   discussion_close     
 rr   reopenz)hf discussions reopen username/my-model 5zZhf discussions reopen username/my-model 5 --comment "Reopening for further investigation."z+An optional comment to post when reopening.c                 C   rh   )z+Reopen a closed discussion or pull request.zReopen #ri   rj   rk   r6   r   rl   z
Reopened #ra   rb   Nrn   rq   r   r   r   discussion_reopen>  rs   ru   renamez9hf discussions rename username/my-model 5 "Updated title"	new_titlezThe new title.c                 C   s@   t |d}|j| |||jd tjd| d|  || |d dS )z$Rename a discussion or pull request.r6   )r/   rR   rw   r4   z	Renamed #ra   )r<   rc   r=   N)r   rename_discussionrD   r   r]   )r/   r<   rw   r4   r5   rJ   r   r   r   discussion_renameg  s   
"ry   mergez(hf discussions merge username/my-model 5zEhf discussions merge username/my-model 5 --comment "Merging, thanks!"z)An optional comment to post when merging.c                 C   sZ   t jd| d|  d|d t|d}|j| |||jd t jd| d|  || d	 d
S )zMerge a pull request.zMerge #ri   rj   rk   r6   r`   zMerged #ra   rb   N)r   ro   r   merge_pull_requestrD   r]   rq   r   r   r   discussion_merge  s   
 r|   diffz'hf discussions diff username/my-model 5c                 C   s@   t |d}|j| ||jd}|jrt|j dS td dS )z Show the diff of a pull request.r6   rQ   zNo diff available.N)r   rS   rD   r}   r   textrU   r   r   r   discussion_diff  s   
r   )/__doc__enumr(   pathlibr   typingr   r%   huggingface_hubr   
_cli_utilsr   r   r   r	   r
   r   r   r   r   _outputr   r'   Enumr   r   rF   intArgumentDiscussionNumArgr,   discussions_clicommandr   r   modelOptionrO   rW   boolr^   re   rr   ru   ry   r|   r   r   r   r   r   <module>   s  ,
"	

?
	 !1
"
"
"	

!	