
    {i                    ~    S r SSKJr  SSKJrJr  SSKJr  SSKJ	r	  SSK
Jr  SS	KJr  \(       a  SS
Kr " S S5      rg
)z.Provide CommentForest for submission comments.    )annotations)heappopheappush)TYPE_CHECKING   )DuplicateReplaceException)_deprecate_args   )MoreCommentsNc                      \ rS rSrSrSS jrSS jrSS jr  SS jr\	SS.     SS	 jj5       r
 S   SS
 jjrSS jr\" SS5      SSS.     SS jj5       rSrg)CommentForest   zqA forest of comments starts with multiple top-level comments.

Each of these comments can be a tree of replies.

c                     U R                   U   $ )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)selfindexs     T/home/james-whalen/.local/lib/python3.13/site-packages/praw/models/comment_forest.py__getitem__CommentForest.__getitem__   s    $ ~~e$$    c                ,    [        U R                  5      $ )z6Return the number of top-level comments in the forest.)lenr   )r   s    r   __len__CommentForest.__len__+   s    4>>""r   c                   UR                   U R                  R                  ;   a  [        eU R                  Ul        [        U[        5      (       d  UR                  (       a  U R                  R                  U5        g UR                  U R                  R                  ;   d   S5       eU R                  R                  UR                     nUR                  R                  R                  U5        g )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   commentparents      r   _insert_commentCommentForest._insert_comment/   s    <<4++;;;++!--g|,,NN!!'*$$(8(8(H(HH *H %%55g6G6GHFNN$$++G4r   c                    / n[        U 5      nU(       a[  UR                  S5      nUR                  U5        [        U[        5      (       d  UR                  UR                  5        U(       a  M[  U$ )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%   s       r   r*   CommentForest.list=   sY     T
iilGOOG$g|44W__-	 e
 r   Nparent_treec               t   / nU  Vs/ s H  nSU4PM	     nnU(       a  UR                  S5      u  pV[        U[        5      (       a?  [        X&5        U(       a  UR                  R
                  Ul        O6U=(       d    U Ul        O%UR                   H  nUR                  Xg45        M     U(       a  M  U$ s  snf )zCReturn a list of :class:`.MoreComments` objects obtained from tree.Nr   )r+   r    r   r   r$   r   _remove_fromr"   )treer1   more_commentsxr.   r&   r%   items           r   _gather_more_comments#CommentForest._gather_more_commentsO   s     $()Dq$D)#iilOF'<000+1>>+C+CG(+6+>$G(#OODLL'1 , e  *s   B5c                    X l         Xl        g)zInitialize 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-   s      r   __init__CommentForest.__init__e   s     "%r   c                D    Xl         U H  nU R                  Ul        M     g N)r   r   r   )r   r-   r%   s      r   _updateCommentForest._updateu   s    !G!%!1!1G  r   limit	threshold    r   )rB   rC   c                  UnU R                  U R                  5      n/ nU(       a  [        U5      nUb  US::  d  UR                  U:  a.  UR	                  U5        UR
                  R                  U5        MY  UR                  SS9nUb  US-  nU R                  XpR                  S9 H  nU R                  Ul	        [        XH5        M!     U H  n	U R                  U	5        M     UR
                  R                  U5        U(       a  M  XE-   $ )a=  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   F)updater
   r0   )r8   r   r   countr"   r3   remover-   r   r   r   r'   )
r   rB   rC   	remainingr5   skippedr7   new_commentsmorer%   s
             r   replace_moreCommentForest.replace_morez   s   t 	224>>B =)D$a4::	;Qt$!!((.===6L$Q	 22.. 3  #'"2"2-	 ($$W- ( $$T*- m0 &&r   r;   )r   intreturnpraw.models.Comment)rP   rO   )r%   rQ   )rP   z4list[praw.models.Comment | praw.models.MoreComments])r4   list[praw.models.MoreComments]r1   z%list[praw.models.MoreComments] | NonerP   zlist[MoreComments]r?   )r   zpraw.models.Submissionr-   z list[praw.models.Comment] | None)r-   zlist[praw.models.Comment])rB   z
int | NonerC   rO   rP   rR   )__name__
__module____qualname____firstlineno____doc__r   r   r'   r*   staticmethodr8   r<   r@   r	   rM   __static_attributes__ r   r   r   r      s    %(#5	=$  >B, ; 
	 0 6:&*& 3& 2
 Wk*%'!V'"V'47V'	'V' +V'r   r   )rW   
__future__r   heapqr   r   typingr   
exceptionsr   utilr	   reddit.morer   praw.modelsprawr   rZ   r   r   <module>rc      s,    4 " #   2 " %A' A'r   