o
    ~riA                     @   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!   m/lsinfo/ai/hellotax_ai/llm_service/venv_embed/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 Request
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strftimetarget_branchappendjoinconflicting_filesr2   events
isinstancer   hiddencontentstriplenr1   url)r@   rA   kindpartsr4   commentseventcommentr!   r!   r"   _print_discussion_infoj   sD   (


".rb   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_idz-sz--statusz4Filter by status (open, closed, merged, draft, all).r]   z-kz--kindz/Filter by kind (discussion, pull_request, all).rO   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.rj   r   r   N)re   rO   discussion_typediscussion_statusrg   c                 S   s   g | ]}t |qS r!   )r   ).0dr!   r!   r"   
<listcomp>   s    z#discussion_list.<locals>.<listcomp>rN   )rN   rL   rI   r-   rO   rP   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 )NrD   rN   rL   rG   2   )max_lenrI   PRr-   rO   rP   )r   getr3   r:   )itemr!   r!   r"   <lambda>   s   z!discussion_list.<locals>.<lambda>right)rh   ri   id_keyheadersrow_fn
alignments)r   r   r   r   r$   r   valueget_repo_discussions_CLIENT_SIDE_STATUSESr-   rS   r[   r   )re   r-   r]   rO   rf   rg   rh   ri   rj   api
api_statusapi_discussion_typediscussionsro   itemsr!   r!   r"   discussion_list   sB   
#





r   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 jsonrN   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_COLORrk   re   discussion_numrg   r      )indent)rA   rE   )osenvironr   get_discussion_detailsr|   r'   r)   r   poprJ   dumpsrb   r   r   rM   )re   rN   r_   r   r   rg   rh   rj   r   r   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"rL   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.rk   )re   rL   descriptionrg   r&   zpull requestr%   zCreated rC   rD   z on zPush changes to: zrefs/pr/rH   N)
r?   r   create_discussionr|   rJ   r   rK   rN   r1   r\   )re   rL   r4   r5   r&   rg   rj   r   r   r%   r]   r!   r!   r"   discussion_create3  s   
,
.r   ra   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.rk   re   r   ra   rg   zCommented on # in )	r?   r8   r9   r   comment_discussionr|   rJ   r   rK   )re   rN   r4   r5   rg   rj   ra   r   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.rk   r   re   r   
new_statusra   rg   zClosed #r   N	r8   confirmrJ   Exitr   change_discussion_statusr|   r   rK   re   rN   ra   r   rg   rj   r   r   r!   r!   r"   discussion_close     
r   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   rk   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.rk   )re   r   r   rg   z	Renamed #z to r   N)r   rename_discussionr|   rJ   r   rK   )re   rN   r   rg   rj   r   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   rk   r   zMerged #r   N)	r8   r   rJ   r   r   merge_pull_requestr|   r   rK   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.rk   r   zNo diff available.N)r   r   r|   r   rJ   )re   rN   rg   rj   r   r@   r!   r!   r"   discussion_diff;  s   
r   )F)>r*   enumr)   r;   pathlibr   typingr   r   r8   huggingface_hubr   huggingface_hub.communityr   r   huggingface_hub.utilsr   
_cli_utilsr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r:   Enumr   r$   r'   r~   intArgumentDiscussionNumArgr3   r?   boolrb   discussions_clicommandr   r   modeltableOptionr   r(   r   r   r   r   r   r   r   r   r!   r!   r!   r"   <module>   s  @	"
+
L
 =	 !3
"
&
&	

%	