o
    0iA                     @   sP  d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	Z	ddl
mZ ddlmZ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mZmZmZmZmZmZ G d
d de ej!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e fddZ)dee  dee dee  fddZ*d|dede+ddfdd Z,ed!d"Z-e-j.d#g d$d%e"j/e#j0dd&ej1ej2ddfd'edee"e	j3d(d)d*d"f d+ee#e	j3d,d-d.d"f d/ed0ed1ed2ed3ed4eddfd5d6Z4e-j.d7g d8d%dddej1e$j5dfd'ed9e(d:ee+e	j3d;d<d"f d=ee+e	j3d>d?d"f d@ee+e	j3dAdBd"f d1ed2ee$e	j3dCd"f d4eddfdDdEZ6e-j.dFg dGd%dddej1dfd'edHee e	j3dIdJd"f deee  e	j3dKdLd"f deee e	j3dMdNd"f dOee+e	j3dPdQdRd"f d1ed4eddfdSdTZ7e-j.dUdVdWgd%ddej1dfd'ed9e(deee  e	j3dKdXd"f deee e	j3dMdYd"f d1ed4eddfdZd[Z8e-j.d\d]d^gd%ddej1dfd'ed9e(dUeee  e	j3d_d`d"f daee+e	j3dbdcddd"f d1ed4eddfdedfZ9e-j.dgdhdigd%ddej1dfd'ed9e(dUeee  e	j3d_djd"f daee+e	j3dbdcddd"f d1ed4eddfdkdlZ:e-j.dmdngd%ej1dfd'ed9e(doee e	j'dpd"f d1ed4eddfdqdrZ;e-j.dsdtdugd%ddej1dfd'ed9e(dUeee  e	j3d_dvd"f daee+e	j3dbdcddd"f d1ed4eddfdwdxZ<e-j.d=dygd%ej1dfd'ed9e(d1ed4eddf
dzd{Z=dS )}zYContains commands to interact with discussions and pull requests on the Hugging Face Hub.    N)Path)	AnnotatedOptional)	constants)DiscussionCommentDiscussionWithDetails)ANSI   )	AuthorOpt	FormatOptLimitOptOutputFormatQuietOpt	RepoIdArgRepoTypeRepoTypeOptTokenOpt_format_cellapi_object_to_dict
get_hf_apiprint_list_outputtyper_factoryc                   @   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!   Y/home/kim/smarthome/.venv/lib/python3.10/site-packages/huggingface_hub/cli/discussions.pyr   .   s    r   c                   @      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$   6   s    r$   c                   @   r#   )
InfoFormatz#Output format for the info command.textjsonN)r   r   r    __doc__r(   r)   r!   r!   r!   r"   r'   <   s    r'   r   r   z&The discussion or pull request number.)helpminstatusreturnc                 C   sL   | dkr	t dS | dkrt dS | dkrt dS | dkr$t dS | S )Nr   r   r   r   )r   greenredblueyellow)r-   r!   r!   r"   _format_statusS   s   



r3   body	body_filec                 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)r4   r5   r!   r!   r"   
_read_body_   s   

r?   Fdetailsshow_commentsc                 C   s  | j rdnd}tt| j dtd| j   t| j| j	| j
dg}| j r:| jr:|dt| j  t| dd|  | j rg| jrg| jd	u rYttd
 nttdd| j  d }g }| jD ]}t|tr|js|d u r|}qn|| qn|r|j rt  t|j  |r|rt  ttd |D ]}t  tt|j	 d|j
d  t|j  qn|rt  ttt| dt|dkrdnd d t  tdt| j  d S )NzPull RequestZ
Discussion #z%Y-%m-%d %H:%Mzinto z: u    · TzHas conflicting fileszConflicting files: z,    ────────────────────────────────────────────────────────────z commentr	   s z (use --comments to show)View on Hub: )is_pull_requestprintr   boldtitlegraynumr3   r-   author
created_atstrftimeZtarget_branchappendjoinZconflicting_filesr2   events
isinstancer   Zhiddencontentstriplenr1   url)r@   rA   kindpartsr4   commentseventcommentr!   r!   r"   _print_discussion_infoj   sD   (


".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)Zexamples   repo_idz-sz--statusz4Filter by status (open, closed, merged, draft, all).rY   z-kz--kindz/Filter by kind (discussion, pull_request, all).rN   limit	repo_typeformatquiet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|||dg d	d
d ddid dS )z-List discussions and pull requests on a repo.re   r   r   N)r`   rN   Zdiscussion_typeZdiscussion_statusrb   c                 S   s   g | ]}t |qS r!   )r   ).0dr!   r!   r"   
<listcomp>   s    z#discussion_list.<locals>.<listcomp>rM   )rM   rK   rH   r-   rN   rO   c              
   S   s`   d| d  t | dddd| drdndtt| d	dt| d
dt | ddgS )NrC   rM   rK   rF   2   )max_lenrH   ZPRr-   rN   rO   )r   getr3   r:   )itemr!   r!   r"   <lambda>   s   z!discussion_list.<locals>.<lambda>right)rc   rd   Zid_keyheadersZrow_fnZ
alignments)r   r   r   r   r$   r   valueZget_repo_discussions_CLIENT_SIDE_STATUSESr-   rQ   rW   r   )r`   r-   rY   rN   ra   rb   rc   rd   re   apiZ
api_statusZapi_discussion_typeZdiscussionsrh   itemsr!   r!   r"   discussion_list   sB   
#





ru   info)z'hf discussions info username/my-model 5z2hf discussions info username/my-model 5 --commentsz.hf discussions info username/my-model 5 --diffz5hf discussions info username/my-model 5 --format jsonrM   r[   z
--commentszShow all comments.diffz--diffz"Show the diff (for pull requests).no_colorz
--no-colorzDisable colored output.zOutput format (text or json).c                 C   s   ddl }|rd|jd< t|d}	|	j| ||jd}
|tjkr5t|
}|s*|dd t	tj
|dd	 dS t|
|d
 |rQ|
jrSt	  t	td t	|
j dS dS dS )z,Get info about a discussion or pull request.r   N1NO_COLORrf   r`   discussion_numrb   rw      )indent)rA   rD   )osenvironr   get_discussion_detailsrq   r'   r)   r   poprI   dumpsr^   rw   r   rL   )r`   rM   r[   rw   rx   rb   rc   re   r   rs   r@   resultr!   r!   r"   discussion_info   s*   +



r   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"rK   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   s   t ||}t|d}|j| |||j|d}	|rdnd}
td|
 dtd|	j  dt|   |rBtd	td
|	j   tdt|	j	  dS )z2Create a new discussion or pull request on a repo.rf   )r`   rK   descriptionrb   r&   zpull requestr%   zCreated rB   rC   z on zPush changes to: zrefs/pr/rG   N)
r?   r   Zcreate_discussionrq   rI   r   rJ   rM   r1   rX   )r`   rK   r4   r5   r&   rb   re   r   rs   r%   rY   r!   r!   r"   discussion_create3  s   
,
.r   r]   zIhf 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   sX   t ||}|du rtdt|d}|j| |||jd td| dt|   dS )z(Comment on a discussion or pull request.Nz)Either --body or --body-file is required.rf   r`   r|   r]   rb   zCommented on # in )	r?   r8   r9   r   Zcomment_discussionrq   rI   r   rJ   )r`   rM   r4   r5   rb   re   r]   rs   r!   r!   r"   discussion_commento  s   


r   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   n   |st d| d|  d}|std t  t|d}|j| |d||jd td| d	t|   d
S )z#Close a discussion or pull request.zClose # on ''?Aborted.rf   r   r`   r|   Z
new_statusr]   rb   zClosed #r   N	r8   confirmrI   Exitr   Zchange_discussion_statusrq   r   rJ   r`   rM   r]   r   rb   re   r   rs   r!   r!   r"   discussion_close     
r   Z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   r   )z+Reopen a closed discussion or pull request.zReopen #r   r   r   rf   r   r   z
Reopened #r   Nr   r   r!   r!   r"   discussion_reopen  r   r   renamez9hf discussions rename username/my-model 5 "Updated title"	new_titlezThe new title.c              	   C   sH   t |d}|j| |||jd td| dt| dt|   dS )z$Rename a discussion or pull request.rf   )r`   r|   r   rb   z	Renamed #z to r   N)r   Zrename_discussionrq   rI   r   rJ   )r`   rM   r   rb   re   rs   r!   r!   r"   discussion_rename  s   
*r   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   sl   |st d| d|  d}|std t  t|d}|j| |||jd td| dt|   d	S )
zMerge a pull request.zMerge #r   r   r   rf   r   zMerged #r   N)	r8   r   rI   r   r   Zmerge_pull_requestrq   r   rJ   r   r!   r!   r"   discussion_merge  s   
r   z'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.rf   r{   zNo diff available.N)r   r   rq   rw   rI   )r`   rM   rb   re   rs   r@   r!   r!   r"   discussion_diff;  s   
r   )F)>r*   enumr)   r;   pathlibr   typingr   r   r8   Zhuggingface_hubr   Zhuggingface_hub.communityr   r   Zhuggingface_hub.utilsr   Z
_cli_utilsr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r:   Enumr   r$   r'   rr   intZArgumentZDiscussionNumArgr3   r?   boolr^   Zdiscussions_clicommandr   r   modeltableOptionru   r(   r   r   r   r   r   r   r   r   r!   r!   r!   r"   <module>   s  @	"
+
L
 =	 !3
"
&
&	

%	