Ë
    Fü´j÷  ã                  óp   — d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
 ddlmZ erddlmZ  G d„ d	«      Zy
)z.Provide CommentForest for submission comments.é    )Úannotations)ÚheappopÚheappush)ÚTYPE_CHECKINGÚcast)ÚDuplicateReplaceException)ÚMoreComments)Úmodelsc                  ó„   — e Zd ZdZeddœ	 	 	 	 	 dd„«       Zdd„Z	 d	 	 	 	 	 dd„Zdd„Zdd„Z	dd	„Z
	 	 dd
„Zdddœdd„Zy)ÚCommentForestzyA forest of comments starts with multiple top-level comments.

    Each of these comments can be a tree of replies.

    N©Úparent_treec               ó>  ‡— g }| D �cg c]  }d|f‘Œ }}|r�|j                  d«      \  }Št        ‰t        «      r6t        |‰«       |r|j                  j
                  ‰_        n0|xs | ‰_        n$|j                  ˆfd„‰j                  D «       «       |rŒ�|S c c}w )zCReturn a list of :class:`.MoreComments` objects obtained from tree.Nr   c              3  ó&   •K  — | ]  }‰|f–— Œ
 y ­w©N© )Ú.0ÚitemÚcomments     €úX/root/workspace/ytshorts/venv/lib/python3.12/site-packages/praw/models/comment_forest.pyú	<genexpr>z6CommentForest._gather_more_comments.<locals>.<genexpr>(   s   øè ø€ ÒI°˜g tœ_ÑIùs   ƒ)ÚpopÚ
isinstancer	   r   ÚrepliesÚ	_commentsÚ_remove_fromÚextend)Útreer   Úmore_commentsÚxÚqueueÚparentr   s         @r   Ú_gather_more_commentsz#CommentForest._gather_more_comments   s™   ø€ ð -/ˆØnrÖZsÐijÐ\`ÐbcÒ[dÐZsˆÐZsÙØ#Ÿi™i¨›l‰OˆF�GÜ˜'¤<Ô0Ü˜¨Ô0ÙØ+1¯>©>×+CÑ+C�GÕ(à+6Ò+>¸$�GÕ(à—‘ÓI¸¿¹ÔIÔIò ð Ðùò [ts   ˆBc                ó    — | 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)

        )r   )ÚselfÚindexs     r   Ú__getitem__zCommentForest.__getitem__+   s   € ð$ �~‰~˜eÑ$Ð$ó    c                ó(   — |�|ng | _         || _        y)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   Ú_submission)r%   Ú
submissionÚcommentss      r   Ú__init__zCommentForest.__init__?   s   € ð RZÐQeÁXÐkmˆŒØ%ˆÕr(   c                ó,   — t        | j                  «      S )z6Return the number of top-level comments in the forest.)Úlenr   )r%   s    r   Ú__len__zCommentForest.__len__O   s   € ä�4—>‘>Ó"Ð"r(   c                óÈ  — |j                   | j                  j                  v rt        ‚| j                  |_        t        |t        «      s|j                  r| j                  j                  |«       y |j                  | j                  j                  v sJ d«       ‚| j                  j                  |j                     }|j                  j                  j                  |«       y )NzYPRAW Error occurred. Please file a bug report and include the code that caused the error.)Únamer*   Ú_comments_by_idr   r+   r   r	   Úis_rootr   ÚappendÚ	parent_idr   )r%   r   r"   s      r   Ú_insert_commentzCommentForest._insert_commentS   s°   € Ø�<‰<˜4×+Ñ+×;Ñ;Ñ;Ü+Ð+Ø!×-Ñ-ˆÔÜ�gœ|Ô,°·²Ø�N‰N×!Ñ! 'Õ*à×$Ñ$¨×(8Ñ(8×(HÑ(HÑHð ØkóÐHð ×%Ñ%×5Ñ5°g×6GÑ6GÑHˆFØ�N‰N×$Ñ$×+Ñ+¨GÕ4r(   c                óB   — || _         |D ]  }| j                  |_        Œ y r   )r   r*   r+   )r%   r,   r   s      r   Ú_updatezCommentForest._update`   s&   € Ø!ˆŒØò 	2ˆGØ!%×!1Ñ!1ˆGÕñ	2r(   c                óì   — g }t        | j                  «      }|rZ|j                  d«      }|j                  |«       t	        |t
        «      s%|j                  |j                  j                  «       |rŒZ|S )z©Return a flattened list of all comments.

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

        r   )Úlistr   r   r5   r   r	   r   r   )r%   r,   r!   r   s       r   r;   zCommentForest.liste   sa   € ð @BˆÜ�T—^‘^Ó$ˆÙØ—i‘i “lˆGØ�O‰O˜GÔ$Ü˜g¤|Ô4Ø—‘˜WŸ_™_×6Ñ6Ô7ò	 ð
 ˆr(   é    r   )ÚlimitÚ	thresholdc               ó  — |}| j                  | j                  «      }g }|ræt        |«      }|�|dk  s|j                  |k  r-|j	                  |«       |j
                  j                  |«       ŒPt        d|j                  d¬«      «      }|�|dz  }| j                  || j                  ¬«      D ]  }| j                  |_
        t        ||«       Œ! |D ]  }	| j                  |	«       Œ |j
                  j                  |«       |rŒæ||z   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`.

        r   ú*list[models.Comment | models.MoreComments]F)Úupdateé   r   )r#   r   r   Úcountr5   r   Úremover   r,   r*   r+   r   r7   )
r%   r=   r>   Ú	remainingr   Úskippedr   Únew_commentsÚmorer   s
             r   Úreplace_morezCommentForest.replace_morew   s  € ðn ˆ	Ø×2Ñ2°4·>±>ÓBˆØˆñ Ü˜=Ó)ˆDØÐ%¨)°qª.¸T¿Z¹ZÈ)Ò=SØ—‘˜tÔ$Ø×!Ñ!×(Ñ(¨Ô.ØäØ<Ø—‘ U�Ó+óˆLð Ð$Ø˜Q‘�	ð ×2Ñ2°<ÈTÏ^É^Ð2Ó\ò .�Ø"&×"2Ñ"2�”Ü˜¨Õ-ð.ð (ò .�Ø×$Ñ$ WÕ-ð.ð ×Ñ×$Ñ$ TÔ*ò/ ð2 ˜wÑ&Ð&r(   )r   r@   r   ú1list[models.Comment | models.MoreComments] | NoneÚreturnzlist[MoreComments])r&   ÚintrK   ú$models.Comment | models.MoreCommentsr   )r+   zmodels.Submissionr,   rJ   rK   ÚNone)rK   rL   )r   rM   rK   rN   )r,   r@   rK   rN   )rK   r@   )r=   z
int | Noner>   rL   rK   zlist[models.MoreComments])Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr#   r'   r-   r0   r7   r9   r;   rI   r   r(   r   r   r      s–   „ ñð ð JNñØ8ðð Gðð 
ò	ó ðó(%ð. GKð&à%ð&ð Dð&ð 
ó	&ó #ó5ó2ð
à	3óð$ 35Àqö U'r(   r   N)rR   Ú
__future__r   Úheapqr   r   Útypingr   r   Úpraw.exceptionsr   Úpraw.models.reddit.morer	   Úprawr
   r   r   r(   r   ú<module>rZ      s)   ðÙ 4å "ç #ß &å 5Ý 0áÝ÷}'ò }'r(   