
    ;OOf"?                         d dl mZmZ 	 d dlZd dlmZ  G d de      Z G d dej                        Z	 G d d	e
      Z G d
 de      Z G d dej                        Z G d dej                        Zy)    )divisionprint_functionN)SelectableIconc                       e Zd Zy)TreeWidgetErrorN)__name__
__module____qualname__     P/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/urwid/treetools.pyr   r   &   s    r   r   c                       e Zd ZdZdZ edd      Z edd      Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)
TreeWidgetz9A widget representing something in a nested tree display.   +r   -c                     || _         d | _        t        |d       | _        d| _        | j                         }| j                  j                  |       y )Nget_first_childT)_node_innerwidgethasattris_leafexpandedget_indented_widget_TreeWidget__super__init__)selfnodewidgets      r   r   zTreeWidget.__init__0   sJ    
 "4):;;))+f%r   c                     | j                    S )zS
        Allow selection of non-leaf nodes so children may be (un)expanded
        )r   r   s    r   
selectablezTreeWidget.selectable8   s     <<r   c                    | j                         }| j                  s?t        j                  dd| j                  | j
                  g| j                     f|gd      }| j                         }t        j                  |d|      S )Nfixed   )dividechars)relatived   )widthleft)	get_inner_widgetr   urwidColumnsunexpanded_iconexpanded_iconr   get_indent_colsPadding)r   r   indent_colss      r   r   zTreeWidget.get_indented_widget>   s    &&(||]]Wa%%t'9'9:4==I%K$%&(F **,}}V#+7 	7r   c                     | j                   | j                  g| j                     | j                  j                  j
                  d<   y)z-Update display widget text for parent widgetsr   N)r.   r/   r   _wbase_widgetwidget_listr!   s    r   update_expanded_iconzTreeWidget.update_expanded_iconH   s>       $"4"4.66:mm.E''*r   c                 X    | j                   | j                         j                         z  S N)r2   get_node	get_depthr!   s    r   r0   zTreeWidget.get_indent_colsN   s"    $--/";";"===r   c                 \    | j                   | j                         | _         | j                   S r9   )r   load_inner_widgetr!   s    r   r+   zTreeWidget.get_inner_widgetQ   s+    $ $ 6 6 8D   r   c                 H    t        j                  | j                               S r9   )r,   Textget_display_textr!   s    r   r=   zTreeWidget.load_inner_widgetV   s    zz$//122r   c                     | j                   S r9   )r   r!   s    r   r:   zTreeWidget.get_nodeY   s    zzr   c                     | j                         j                         dz   t        | j                         j                               z   S )Nz: )r:   get_keystr	get_valuer!   s    r   r@   zTreeWidget.get_display_text\   s:    '')D0DMMO--/01 	2r   c                 B   | j                         }||S | j                         }|j                         }|j                         }|G|dkD  rB|j	                         }|j                         }|dz  }||j                         k(  sJ ||dkD  rB|y|j                         S )z5Return the next TreeWidget depth first from this one.Nr   r%   )first_childr:   next_siblingr;   
get_parent
get_widget)r   
firstchildthisnodenextnodedepths        r   next_inorderzTreeWidget.next_inorder`   s     %%'
! ==?((*""$519**,H,,.HQJEH..0000 519 &&((r   c                     | j                   }|j                         }|&|j                         }|j                         }||S |S |j	                         }||dk(  ry||j                         }|j                         S )z9Return the previous TreeWidget depth first from this one.Nr   )r   prev_siblingrJ   
last_childr;   rI   )r   rL   prevnode
prevwidget	lastchildrN   s         r   prev_inorderzTreeWidget.prev_inorderw   s    ::((* ",,.J"--/I !!   &&(EEQJ!#..0&&((r   c                     | j                   r|S |dv rd| _        | j                          y|dk(  rd| _        | j                          y| j                  j	                         r| j
                  j                  ||      S |S )z2Handle expand & collapse requests (non-leaf nodes))r   rightTr   FN)r   r   r7   r4   r"   r   keypressr   sizekeys      r   rY   zTreeWidget.keypress   so    <<J.  DM%%'CZ!DM%%'WW!<<((s33Jr   c                     | j                   s
|dk7  s|dk7  ry|dk(  r6|| j                         k(  r#| j                   | _        | j                          yy)Nzmouse pressr%   Fr   T)r   r0   r   r7   )r   r[   eventbuttoncolrowfocuss          r   mouse_eventzTreeWidget.mouse_event   sP    <<5M1VQY!8t3355 $-DM%%'r   c                     | j                   s| j                  sy| j                  j                         r*| j                  j	                         }|j                         S y)zReturn first child if expanded.N)r   r   r   has_childrenr   rJ   )r   	firstnodes     r   rG   zTreeWidget.first_child   sF    <<t}}zz&&( JJ668	 ++--r   c                     | j                   s| j                  sy| j                  j                         r)| j                  j	                         j                         }ny|j                         }||S |S )zReturn last child if expanded.N)r   r   r   re   get_last_childrJ   rR   )r   rU   lastdescendants      r   rR   zTreeWidget.last_child   s`    <<t}}zz&&( JJ557BBD	&113N%  %%r   N)r   r	   r
   __doc__r2   r   r.   r/   r   r"   r   r7   r0   r+   r=   r:   r@   rO   rV   rY   rc   rG   rR   r   r   r   r   r   *   sn    CK$S!,O"3*M& 7E>!
32).), 		&r   r   c                   n    e Zd ZdZddZddZd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zy)TreeNodea:  
    Store tree contents and cache TreeWidget objects.
    A TreeNode consists of the following elements:
    *  key: accessor token for parent nodes
    *  value: subclass-specific data
    *  parent: a TreeNode which contains a pointer back to this object
    *  widget: The widget used to render the object
    Nc                 J    || _         || _        || _        || _        d | _        y r9   )_key_parent_value_depth_widgetr   valueparentr\   rN   s        r   r   zTreeNode.__init__   s%    	r   c                 f    | j                   |dk(  r| j                         | _         | j                   S )z! Return the widget for this node.T)rr   load_widgetr   reloads     r   rJ   zTreeNode.get_widget   s,    <<6T>++-DL||r   c                     t        |       S r9   )r   r!   s    r   rw   zTreeNode.load_widget   s    $r   c                     | j                   | j                  d| _         | j                   S | j                   "| j                  j                         dz   | _         | j                   S )Nr   r%   )rq   ro   r;   r!   s    r   r;   zTreeNode.get_depth   sW    ;;4<<#7DK {{ [[ ,,002Q6DK{{r   c                     | j                         dk(  ry | j                         }| j                         }|j                  |      S Nr   )r;   rC   rI   get_child_index)r   r\   ru   s      r   	get_indexzTreeNode.get_index   s<    >>q ,,.C__&F))#..r   c                     | j                   S r9   rn   r!   s    r   rC   zTreeNode.get_key   s    yyr   c                     || _         y r9   r   r   r\   s     r   set_keyzTreeNode.set_key   s	    	r   c                 X    | j                         j                  | j                  |       y r9   )rI   change_child_keyrn   r   s     r   
change_keyzTreeNode.change_key   s    **499c:r   c                     | j                   d k(  r(| j                         dkD  r| j                         | _         | j                   S r}   )ro   r;   load_parentr!   s    r   rI   zTreeNode.get_parent   s6    <<4DNN$4q$8++-DL||r   c                     t        d      )zProvide TreeNode with a parent for the current node.  This function
        is only required if the tree was instantiated from a child node
        (virtual function)(virtual function.  Implement in subclassr   r!   s    r   r   zTreeNode.load_parent   s     HIIr   c                     | j                   S r9   )rp   r!   s    r   rE   zTreeNode.get_value   s    {{r   c                 (    | j                         dk(  S r}   )r;   r!   s    r   is_rootzTreeNode.is_root  s    ~~1$$r   c                     | j                         dkD  r-| j                         j                  | j                               S y r}   )r;   rI   
next_childrC   r!   s    r   rH   zTreeNode.next_sibling  2    >>a??$//??r   c                     | j                         dkD  r-| j                         j                  | j                               S y r}   )r;   rI   
prev_childrC   r!   s    r   rQ   zTreeNode.prev_sibling  r   r   c                 l    | }|j                         !|j                         }|j                         !|S r9   )rI   )r   roots     r   get_rootzTreeNode.get_root  s4    oo+??$D oo+r   NNNF)r   r	   r
   rj   r   rJ   rw   r;   r   rC   r   r   rI   r   rE   r   rH   rQ   r   r   r   r   rl   rl      sS     /;
J%r   rl   c                   j    e Zd ZdZddZddZd Zd ZddZd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zy)
ParentNodez"Maintain sort order for TreeNodes.Nc                 T    t         j                  | ||||       d | _        i | _        y )N)ru   r\   rN   )rl   r   _child_keys	_childrenrs   s        r   r   zParentNode.__init__  s*    $f#UKr   c                 f    | j                   |dk(  r| j                         | _         | j                   S )z,Return a possibly ordered list of child keysT)r   load_child_keysrx   s     r   get_child_keyszParentNode.get_child_keys!  s1    #v~#335Dr   c                     t        d      )zPProvide ParentNode with an ordered list of child keys (virtual
        function)r   r   r!   s    r   r   zParentNode.load_child_keys'  s     HIIr   c                 D    | j                  |      }|j                         S )z8Return the widget for a given key.  Create if necessary.)get_child_noderJ   )r   r\   childs      r   get_child_widgetzParentNode.get_child_widget,  s#     ##C(!!r   c                     || j                   vs|dk(  r| j                  |      | j                   |<   | j                   |   S )z<Return the child node for a given key.  Create if necessary.T)r   load_child_node)r   r\   ry   s      r   r   zParentNode.get_child_node2  s<    dnn$$"&"6"6s";DNN3~~c""r   c                     t        d      )z6Load the child node for a given key (virtual function)r   r   r   s     r   r   zParentNode.load_child_node8  s    HIIr   c                 "    || j                   |<   y)z]Set the child node for a given key.  Useful for bottom-up, lazy
        population of a tree.N)r   )r   r\   r   s      r   set_child_nodezParentNode.set_child_node<  s     #sr   c                     || j                   v rt        d|z        | j                   j                  |      | j                   |<   | j                   |   j                  |       y )Nz%s is already in use)r   r   popr   )r   oldkeynewkeys      r   r   zParentNode.change_child_keyA  sS    T^^#!"86"ABB!%!3!3F!;vv&&v.r   c           
          	 | j                         j                  |      S # t        $ r9 d}t        ||| j	                         t        | j                               fz        w xY w)Nz7Can't find key %s in ParentNode %s
ParentNode items: %s)r   index
ValueErrorr   rC   rD   )r   r\   errorstrings      r   r~   zParentNode.get_child_indexG  sk    	?&&(..s33 	?2K!+dlln"%d&9&9&;"<1> #> ? ?	?s
   ! AA#c                     | j                  |      }|y|dz  }| j                         }|t        |      k  r| j                  ||         S y)z=Return the next child node in index order from the given key.Nr%   )r~   r   lenr   r   r\   r   
child_keyss       r   r   zParentNode.next_childP  sX     $$S)=
((*
3z?"&&z%'899r   c                     | j                  |      }|y| j                         }|dz  }|dk\  r| j                  ||         S y)zAReturn the previous child node in index order from the given key.Nr%   r   )r~   r   r   r   s       r   r   zParentNode.prev_child`  sQ    $$S)=((*

A:&&z%'899r   c                 J    | j                         }| j                  |d         S )z+Return the first TreeNode in the directory.r   r   r   r   r   s     r   r   zParentNode.get_first_childo  s%    ((*
"":a=11r   c                 J    | j                         }| j                  |d         S )z*Return the last TreeNode in the directory.r   r   s     r   rh   zParentNode.get_last_childt  s%    ((*
"":b>22r   c                 :    t        | j                               dkD  S )z!Does this node have any children?r   )r   r   r!   s    r   re   zParentNode.has_childreny  s    4&&()!++r   r   r   )r   r	   r
   rj   r   r   r   r   r   r   r   r   r~   r   r   r   rh   re   r   r   r   r   r     sM    , J
"#J#
/? 2
3
,r   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)
TreeWalkerzTListWalker-compatible class for displaying TreeWidgets

    positions are TreeNodes.c                     || _         y)z,start_from: TreeNode with the initial focus.N)rb   )r   
start_froms     r   r   zTreeWalker.__init__  s	    
r   c                 R    | j                   j                         }|| j                   fS r9   )rb   rJ   )r   r   s     r   	get_focuszTreeWalker.get_focus  s#    &&(tzz!!r   c                 2    || _         | j                          y r9   )rb   	_modified)r   rb   s     r   	set_focuszTreeWalker.set_focus  s    
r   c                 l    |j                         }|j                         }|y||j                         fS N)NN)rJ   rO   r:   r   r   r   targets       r   get_nextzTreeWalker.get_next  8    &&($$&>6??,,,r   c                 l    |j                         }|j                         }|y||j                         fS r   )rJ   rV   r:   r   s       r   get_prevzTreeWalker.get_prev  r   r   N)	r   r	   r
   rj   r   r   r   r   r   r   r   r   r   r   ~  s       "--r   r   c                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)TreeListBoxzPA ListBox with special handling for navigation and
    collapsing of TreeWidgetsc                 ^    | j                   j                  ||      }| j                  ||      S r9   )_TreeListBox__superrY   unhandled_inputrZ   s      r   rY   zTreeListBox.keypress  s+    ll##D#.##D#..r   c                 b    |dk(  r| j                  |       y|dk(  r| j                  |       y|S )zHandle macro-navigation keysr*   r   N)move_focus_to_parentcollapse_focus_parent)r   r[   inputs      r   r   zTreeListBox.unhandled_input  s1    F?%%d+c\&&t,Lr   c                     | j                   j                         \  }}| j                  |       | j                   j                         \  }}||k7  r| j                  |d       yy)zCollapse parent directory.r   N)bodyr   r   rY   )r   r[   r   pospwidgetpposs         r   r   z!TreeListBox.collapse_focus_parent  sX     ii))+!!$'		++-$;MM$$ r   c                 <   | j                   j                         \  }}|j                         }|y| j                  |      \  }}}|\  }}	}
}}|\  }}|D ]%  \  }}}||z  }||k(  s| j	                  |||        y | j	                  ||j                                y)z(Move focus to parent of widget in focus.N)r   r   rI   calculate_visiblechange_focus)r   r[   r   r   	parentposmiddletopbottom
row_offsetfocus_widget	focus_pos
focus_rowscursortrim_top
fill_aboverowss                   r   r   z TreeListBox.move_focus_to_parent  s     ii))+NN$	"44d<VBH?
L)Z"*!+ 	FC$Ji!!$Z8		 	$ 01r   c                 $    | j                  |      S r9   )
focus_homer   r[   s     r   _keypress_max_leftzTreeListBox._keypress_max_left  s    t$$r   c                 $    | j                  |      S r9   )	focus_endr   s     r   _keypress_max_rightzTreeListBox._keypress_max_right  s    ~~d##r   c                     | j                   j                         \  }}|j                         }| j                  ||       y)zMove focus to very top.N)r   r   r   r   )r   r[   r   r   rootnodes        r   r   zTreeListBox.focus_home  s5     ii))+<<>$)r   c                     |\  }}| j                   j                         \  }}|j                         }|j                         }|j	                         }|r'|j                         }	| j                  ||	|dz
         yy)zMove focus to far bottom.r%   N)r   r   r   rJ   rR   r:   r   )
r   r[   maxrowmaxcolr   r   r   
rootwidget
lastwidgetlastnodes
             r   r   zTreeListBox.focus_end  sv     ii))+<<>((*
**,
!**,HdHfQh7 r   N)r   r	   r
   rj   rY   r   r   r   r   r   r   r   r   r   r   r   r     s/    !/%2.%$*8r   r   )
__future__r   r   r,   
urwid.wimpr   RuntimeErrorr   
WidgetWrapr   objectrl   r   
ListWalkerr   ListBoxr   r   r   r   <module>r     s{   . 0  %	l 	W&!! W&tRv Rjb, b,J-!! -DJ8%-- J8r   