o
    2g                     @  sn   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ dd	lmZ er.dd
lZG dd dZd
S )z.Provide CommentForest for submission comments.    )annotations)heappopheappush)TYPE_CHECKING   )DuplicateReplaceException)_deprecate_args   )MoreCommentsNc                   @  s   e Zd ZdZd*ddZd+dd	Zd,ddZd-ddZeddd.ddZ		d/d0ddZ
d1d d!Zed"d#d$d%d&d2d(d)ZdS )3CommentForestzyA forest of comments starts with multiple top-level comments.

    Each of these comments can be a tree of replies.

    indexintreturnpraw.models.Commentc                 C  s
   | j | S )a  Return the comment at position ``index`` in the list.

        This method is to be used like an array access, such as:

        .. code-block:: python

            first_comment = submission.comments[0]

        Alternatively, the presence of this method enables one to iterate over all top
        level comments, like so:

        .. code-block:: python

            for comment in submission.comments:
                print(comment.body)

        )	_comments)selfr    r   R/home/garg/my-data/venv/lib/python3.10/site-packages/praw/models/comment_forest.py__getitem__   s   
zCommentForest.__getitem__c                 C  s
   t | jS )z6Return the number of top-level comments in the forest.)lenr   )r   r   r   r   __len__+   s   
zCommentForest.__len__commentc                 C  sp   |j | jjv r	t| j|_t|ts|jr| j	| d S |j
| jjv s(J d| jj|j
 }|jj	| d S )NzYPRAW Error occurred. Please file a bug report and include the code that caused the error.)name_submission_comments_by_idr   
submission
isinstancer
   is_rootr   append	parent_idreplies)r   r   parentr   r   r   _insert_comment/   s   zCommentForest._insert_comment4list[praw.models.Comment | praw.models.MoreComments]c                 C  sB   g }t | }|r|d}|| t|ts||j |s|S )zReturn a flattened list of all comments.

        This list may contain :class:`.MoreComments` instances if :meth:`.replace_more`
        was not called first.

        r   )listpopr   r   r
   extendr    )r   commentsqueuer   r   r   r   r$   =   s   	


zCommentForest.listNparent_treetreelist[praw.models.MoreComments]r*   %list[praw.models.MoreComments] | Nonelist[MoreComments]c                C  sv   g }dd | D }|r9| d\}}t|tr*t|| |r$|jj|_n|p'| |_n|jD ]	}|||f q-|s|S )zCReturn a list of :class:`.MoreComments` objects obtained from tree.c                 S  s   g | ]}d |fqS Nr   ).0xr   r   r   
<listcomp>W   s    z7CommentForest._gather_more_comments.<locals>.<listcomp>r   )r%   r   r
   r   r    r   _remove_fromr   )r+   r*   more_commentsr(   r!   r   itemr   r   r   _gather_more_commentsO   s   


z#CommentForest._gather_more_commentsr   praw.models.Submissionr'    list[praw.models.Comment] | Nonec                 C  s   || _ || _dS )a  Initialize a :class:`.CommentForest` instance.

        :param submission: An instance of :class:`.Submission` that is the parent of the
            comments.
        :param comments: Initialize the forest with a list of comments (default:
            ``None``).

        N)r   r   )r   r   r'   r   r   r   __init__e   s   
zCommentForest.__init__list[praw.models.Comment]c                 C  s   || _ |D ]}| j|_qd S r/   )r   r   r   )r   r'   r   r   r   r   _updateu   s   
zCommentForest._updatelimit	threshold    r   )r<   r=   
int | Nonec          
      C  s   |}|  | j}g }|r]t|}|dur|dks|j|k r)|| |j| q
|jdd}|dur7|d8 }| j || jdD ]}| j|_	t
|| q?|D ]}	| |	 qM|j| |s|| S )ae  Update the comment forest by resolving instances of :class:`.MoreComments`.

        :param limit: The maximum number of :class:`.MoreComments` instances to replace.
            Each replacement requires 1 API request. Set to ``None`` to have no limit,
            or to ``0`` to remove all :class:`.MoreComments` instances without
            additional requests (default: ``32``).
        :param threshold: The minimum number of children comments a
            :class:`.MoreComments` instance must have in order to be replaced.
            :class:`.MoreComments` instances that represent "continue this thread" links
            unfortunately appear to have 0 children (default: ``0``).

        :returns: A list of :class:`.MoreComments` instances that were not replaced.

        :raises: ``prawcore.TooManyRequests`` when used concurrently.

        For example, to replace up to 32 :class:`.MoreComments` instances of a
        submission try:

        .. code-block:: python

            submission = reddit.submission("3hahrw")
            submission.comments.replace_more()

        Alternatively, to replace :class:`.MoreComments` instances within the replies of
        a single comment try:

        .. code-block:: python

            comment = reddit.comment("d8r4im1")
            comment.refresh()
            comment.replies.replace_more()

        .. note::

            This method can take a long time as each replacement will discover at most
            100 new :class:`.Comment` instances. As a result, consider looping and
            handling exceptions until the method returns successfully. For example:

            .. code-block:: python

                while True:
                    try:
                        submission.comments.replace_more()
                        break
                    except PossibleExceptions:
                        print("Handling replace_more exception")
                        sleep(1)

        .. warning::

            If this method is called, and the comments are refreshed, calling this
            method again will result in a :class:`.DuplicateReplaceException`.

        Nr   F)updater	   r)   )r6   r   r   countr   r3   remover'   r   r   r   r"   )
r   r<   r=   	remainingr4   skippedr5   new_commentsmorer   r   r   r   replace_morez   s,   :

zCommentForest.replace_more)r   r   r   r   )r   r   )r   r   )r   r#   )r+   r,   r*   r-   r   r.   r/   )r   r7   r'   r8   )r'   r:   )r<   r?   r=   r   r   r,   )__name__
__module____qualname____doc__r   r   r"   r$   staticmethodr6   r9   r;   r   rG   r   r   r   r   r      s    




r   )rK   
__future__r   heapqr   r   typingr   
exceptionsr   utilr   reddit.morer
   praw.modelsprawr   r   r   r   r   <module>   s    