U
    ”Ê¹jM0  ã                   @   s  d Z ddlmZ ddlmZ ddlmZmZmZmZm	Z	 ddl
mZ ddlmZ ed ZeG d	d
„ d
ƒƒZeG dd„ deƒƒZG dd„ deƒZeG dd„ dƒƒZeG dd„ deƒƒZeG dd„ deƒƒZeG dd„ deƒƒZeG dd„ deƒƒZeedœdd„ZdS )a  
Data structures to interact with Discussions and Pull Requests on the Hub.

See [the Discussions and Pull Requests guide](https://huggingface.co/docs/hub/repositories-pull-requests-discussions)
for more information on Pull Requests, Discussions, and the community tab.
é    )Ú	dataclass)Údatetime)ÚListÚLiteralÚOptionalÚ	TypedDictÚUnioné   )Ú	constants)Úparse_datetime)ÚopenÚclosedZmergedZdraftc                   @   s‚   e Zd ZU dZeed< eed< eed< eed< eed< eed< eed< e	ed	< eed
< e
ee dœdd„ƒZe
edœdd„ƒZdS )Ú
Discussiona  
    A Discussion or Pull Request on the Hub.

    This dataclass is not intended to be instantiated directly.

    Attributes:
        title (`str`):
            The title of the Discussion / Pull Request
        status (`str`):
            The status of the Discussion / Pull Request.
            It must be one of:
                * `"open"`
                * `"closed"`
                * `"merged"` (only for Pull Requests )
                * `"draft"` (only for Pull Requests )
        num (`int`):
            The number of the Discussion / Pull Request.
        repo_id (`str`):
            The id (`"{namespace}/{repo_name}"`) of the repo on which
            the Discussion / Pull Request was open.
        repo_type (`str`):
            The type of the repo on which the Discussion / Pull Request was open.
            Possible values are: `"model"`, `"dataset"`, `"space"`.
        author (`str`):
            The username of the Discussion / Pull Request author.
            Can be `"deleted"` if the user has been deleted since.
        is_pull_request (`bool`):
            Whether or not this is a Pull Request.
        created_at (`datetime`):
            The `datetime` of creation of the Discussion / Pull Request.
        endpoint (`str`):
            Endpoint of the Hub. Default is https://huggingface.co.
        git_reference (`str`, *optional*):
            (property) Git reference to which changes can be pushed if this is a Pull Request, `None` otherwise.
        url (`str`):
            (property) URL of the discussion on the Hub.
    ÚtitleÚstatusÚnumÚrepo_idÚ	repo_typeÚauthorÚis_pull_requestÚ
created_atÚendpoint©Úreturnc                 C   s   | j rd| j› �S dS )z‰
        If this is a Pull Request , returns the git reference to which changes can be pushed.
        Returns `None` otherwise.
        zrefs/pr/N)r   r   ©Úself© r   úX/var/www/html/bwave/python/venv/lib/python3.8/site-packages/huggingface_hub/community.pyÚgit_referenceE   s    zDiscussion.git_referencec                 C   sR   | j dks| j tjkr0| j› d| j› d| j› �S | j› d| j › d| j› d| j› �S )z-Returns the URL of the discussion on the Hub.Nú/z/discussions/zs/)r   r
   ZREPO_TYPE_MODELr   r   r   r   r   r   r   ÚurlO   s    zDiscussion.urlN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__ÚDiscussionStatusÚintÚboolr   Úpropertyr   r   r    r   r   r   r   r      s   
&	r   c                   @   sX   e Zd ZU dZed ed< eee edf ed< e	e ed< e	e ed< e	e ed< dS )	ÚDiscussionWithDetailsa7	  
    Subclass of [`Discussion`].

    Attributes:
        title (`str`):
            The title of the Discussion / Pull Request
        status (`str`):
            The status of the Discussion / Pull Request.
            It can be one of:
                * `"open"`
                * `"closed"`
                * `"merged"` (only for Pull Requests )
                * `"draft"` (only for Pull Requests )
        num (`int`):
            The number of the Discussion / Pull Request.
        repo_id (`str`):
            The id (`"{namespace}/{repo_name}"`) of the repo on which
            the Discussion / Pull Request was open.
        repo_type (`str`):
            The type of the repo on which the Discussion / Pull Request was open.
            Possible values are: `"model"`, `"dataset"`, `"space"`.
        author (`str`):
            The username of the Discussion / Pull Request author.
            Can be `"deleted"` if the user has been deleted since.
        is_pull_request (`bool`):
            Whether or not this is a Pull Request.
        created_at (`datetime`):
            The `datetime` of creation of the Discussion / Pull Request.
        events (`list` of [`DiscussionEvent`])
            The list of [`DiscussionEvents`] in this Discussion or Pull Request.
        conflicting_files (`Union[List[str], bool, None]`, *optional*):
            A list of conflicting files if this is a Pull Request.
            `None` if `self.is_pull_request` is `False`.
            `True` if there are conflicting files but the list can't be retrieved.
        target_branch (`str`, *optional*):
            The branch into which changes are to be merged if this is a
            Pull Request . `None`  if `self.is_pull_request` is `False`.
        merge_commit_oid (`str`, *optional*):
            If this is a merged Pull Request , this is set to the OID / SHA of
            the merge commit, `None` otherwise.
        diff (`str`, *optional*):
            The git diff if this is a Pull Request , `None` otherwise.
        endpoint (`str`):
            Endpoint of the Hub. Default is https://huggingface.co.
        git_reference (`str`, *optional*):
            (property) Git reference to which changes can be pushed if this is a Pull Request, `None` otherwise.
        url (`str`):
            (property) URL of the discussion on the Hub.
    ÚDiscussionEventÚeventsNZconflicting_filesZtarget_branchZmerge_commit_oidÚdiff)
r!   r"   r#   r$   r   r&   r   r%   r)   r   r   r   r   r   r+   W   s   
2r+   c                   @   s6   e Zd ZU eed< eed< eed< eed< eed< dS )ÚDiscussionEventArgsÚidÚtyper   r   Ú_eventN)r!   r"   r#   r%   r&   r   Údictr   r   r   r   r/   ’   s
   
r/   c                   @   s:   e Zd ZU dZeed< eed< eed< eed< eed< dS )r,   aÜ  
    An event in a Discussion or Pull Request.

    Use concrete classes:
        * [`DiscussionComment`]
        * [`DiscussionStatusChange`]
        * [`DiscussionCommit`]
        * [`DiscussionTitleChange`]

    Attributes:
        id (`str`):
            The ID of the event. An hexadecimal string.
        type (`str`):
            The type of the event.
        created_at (`datetime`):
            A [`datetime`](https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime)
            object holding the creation timestamp for the event.
        author (`str`):
            The username of the Discussion / Pull Request author.
            Can be `"deleted"` if the user has been deleted since.
    r0   r1   r   r   r2   N)r!   r"   r#   r$   r%   r&   r   r3   r   r   r   r   r,   š   s   
r,   c                   @   sˆ   e Zd ZU dZeed< eed< eed< eedœdd„ƒZee	dœdd	„ƒZ
eedœd
d„ƒZeee dœdd„ƒZeedœdd„ƒZdS )ÚDiscussionCommentaz  A comment in a Discussion / Pull Request.

    Subclass of [`DiscussionEvent`].


    Attributes:
        id (`str`):
            The ID of the event. An hexadecimal string.
        type (`str`):
            The type of the event.
        created_at (`datetime`):
            A [`datetime`](https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime)
            object holding the creation timestamp for the event.
        author (`str`):
            The username of the Discussion / Pull Request author.
            Can be `"deleted"` if the user has been deleted since.
        content (`str`):
            The raw markdown content of the comment. Mentions, links and images are not rendered.
        edited (`bool`):
            Whether or not this comment has been edited.
        hidden (`bool`):
            Whether or not this comment has been hidden.
    ÚcontentÚeditedÚhiddenr   c                 C   s   | j d d d S )z&The rendered comment, as a HTML stringÚdataÚlatestÚhtml©r2   r   r   r   r   ÚrenderedÙ   s    zDiscussionComment.renderedc                 C   s   t | jd d d ƒS )ú+The last edit time, as a `datetime` object.r8   r9   Z	updatedAt)r   r2   r   r   r   r   Úlast_edited_atÞ   s    z DiscussionComment.last_edited_atc                 C   s   | j d d  di ¡ dd¡S )r=   r8   r9   r   ÚnameÚdeleted)r2   Úgetr   r   r   r   Úlast_edited_byã   s    z DiscussionComment.last_edited_byc                 C   s   | j d d S )zThe edit history of the commentr8   Úhistoryr;   r   r   r   r   Úedit_historyè   s    zDiscussionComment.edit_historyc                 C   s
   t | jƒS )N)ÚlenrD   r   r   r   r   Únumber_of_editsí   s    z!DiscussionComment.number_of_editsN)r!   r"   r#   r$   r%   r&   r)   r*   r<   r   r>   rB   r   r3   rD   r(   rF   r   r   r   r   r4   »   s   
r4   c                   @   s   e Zd ZU dZeed< dS )ÚDiscussionStatusChangeaV  A change of status in a Discussion / Pull Request.

    Subclass of [`DiscussionEvent`].

    Attributes:
        id (`str`):
            The ID of the event. An hexadecimal string.
        type (`str`):
            The type of the event.
        created_at (`datetime`):
            A [`datetime`](https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime)
            object holding the creation timestamp for the event.
        author (`str`):
            The username of the Discussion / Pull Request author.
            Can be `"deleted"` if the user has been deleted since.
        new_status (`str`):
            The status of the Discussion / Pull Request after the change.
            It can be one of:
                * `"open"`
                * `"closed"`
                * `"merged"` (only for Pull Requests )
    Ú
new_statusN©r!   r"   r#   r$   r%   r&   r   r   r   r   rG   ò   s   
rG   c                   @   s"   e Zd ZU dZeed< eed< dS )ÚDiscussionCommitaã  A commit in a Pull Request.

    Subclass of [`DiscussionEvent`].

    Attributes:
        id (`str`):
            The ID of the event. An hexadecimal string.
        type (`str`):
            The type of the event.
        created_at (`datetime`):
            A [`datetime`](https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime)
            object holding the creation timestamp for the event.
        author (`str`):
            The username of the Discussion / Pull Request author.
            Can be `"deleted"` if the user has been deleted since.
        summary (`str`):
            The summary of the commit.
        oid (`str`):
            The OID / SHA of the commit, as a hexadecimal string.
    ÚsummaryÚoidNrI   r   r   r   r   rJ     s   
rJ   c                   @   s"   e Zd ZU dZeed< eed< dS )ÚDiscussionTitleChangeaò  A rename event in a Discussion / Pull Request.

    Subclass of [`DiscussionEvent`].

    Attributes:
        id (`str`):
            The ID of the event. An hexadecimal string.
        type (`str`):
            The type of the event.
        created_at (`datetime`):
            A [`datetime`](https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime)
            object holding the creation timestamp for the event.
        author (`str`):
            The username of the Discussion / Pull Request author.
            Can be `"deleted"` if the user has been deleted since.
        old_title (`str`):
            The previous title for the Discussion / Pull Request.
        new_title (`str`):
            The new title.
    Ú	old_titleÚ	new_titleNrI   r   r   r   r   rM   )  s   
rM   )Úeventr   c                 C   sú   | d }| d }t | d ƒ}||||  di ¡ dd¡| dœ}|dkrvtf || d	 d
 | d	 d | d	 d d dœ—ŽS |dkr˜tf |d| d	 d i—ŽS |dkrÄtf || d	 d | d	 d dœ—ŽS |dkrðtf || d	 d | d	 d dœ—ŽS tf |ŽS )z.Instantiates a [`DiscussionEvent`] from a dictr0   r1   Z	createdAtr   r?   r@   )r0   r1   r   r   r2   Úcommentr8   r6   r7   r9   Úraw)r6   r7   r5   zstatus-changerH   r   ÚcommitÚsubjectrL   )rK   rL   ztitle-changeÚfromÚto)rN   rO   )r   rA   r4   rG   rJ   rM   r,   )rP   Zevent_idZ
event_typer   Zcommon_argsr   r   r   Údeserialize_eventD  sF    û

ü
ÿ
þ

ý


ý
rW   N)r$   Údataclassesr   r   Útypingr   r   r   r   r   Ú r
   Úutilsr   r'   r   r+   r/   r,   r4   rG   rJ   rM   r3   rW   r   r   r   r   Ú<module>   s,   C: 6