
    G@dWR                     D   d Z ddlmZ ddlZddlmZ ddlZddlm	Z	 ddl
mZ ddlmZmZmZ ddlmZmZmZmZmZmZmZmZ ddlm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& ddl'm(Z(  G d d          Z) G d dee          Z*d Z+e,dk    r e+             dS dS )zSpyder path manager.    )OrderedDictN)PYQT5)getexistingdirectory)QtSignalSlot)QDialogQDialogButtonBoxQHBoxLayoutQListWidgetQListWidgetItemQMessageBoxQVBoxLayoutQLabel)SpyderWidgetMixin)_)
check_pathget_system_pythonpath)get_user_envset_user_env)ima)getcwd_or_home)PANES_TOOLBAR_STYLESHEETc                   *    e Zd ZdZdZdZdZdZdZdZ	dS )	PathManagerToolbuttonsmove_topmove_up	move_downmove_to_bottomadd_pathremove_pathexport_pathsN)
__name__
__module____qualname__MoveTopMoveUpMoveDownMoveToBottomAddPath
RemovePathExportPaths     Mlib/python3.11/site-packages/spyder/plugins/pythonpath/widgets/pathmanager.pyr   r   !   s1        GFH#LGJ KKKr.   r   c                   ~    e Zd ZdZ ee          Z ee          ZdZ		 	 d fd	Z
d Zd Zd Zd	 Zed
             Zed             Zd Z e            d             Zd dZd Zd!dZd Z e            d"d            Z e            d d            Zd#dZd Zd Zd Zd Zd Z d Z! fdZ" fdZ# fdZ$ xZ%S )$PathManagerPath manager dialog.pythonpath_managerNTc                 t    t           r$t                                          ||           n+t          j         |           t	          j         |           t          |t          t          d          f          sJ                      t          t                               |pd _        |pd _        |pd _        t                       _        d _        d _        g  _         j        r"                     t)                                 d _        d _        d _        d _        d _        d _        d _        d _        d _        d _        g  _        g  _          !                                 _"        tG                      _$        tK          tJ          j&        tJ          j'        z             _(         j(        )                    tJ          j&                   _*         +                    tY          d                      -                    t]          j/        d                      0                    dd            j        1                    td          j3        d	k    o|           ti          tY          d
                    }|5                    d           tm                      } 7                     j"        |           |8                    d           ts                      }|:                     j$                   |;                    |           tm                      }	|	:                    |           |	<                    d           |	;                    |           |	:                     j(                    =                    |	            j$        j>        ?                     fd            j$        j@        ?                     fd            j(        jA        ?                     jB                    j(        jC        ?                     jD                    E                                 dS )r2   )class_parentNr-   system_pathzPYTHONPATH manager
pythonpathi  i  ntzThe paths listed below will be passed to IPython consoles and the Python language server as additional locations to search for Python modules.T      c                 ,                                     S Nrefreshxselfs    r/   <lambda>z&PathManager.__init__.<locals>.<lambda>   s    DLLNN r.   c                 ,                                     S r=   r>   r@   s    r/   rC   z&PathManager.__init__.<locals>.<lambda>   s    dllnn r.   )Fr   super__init__r	   r   
isinstancetupletypesetStyleSheetstrr   pathproject_pathnot_active_pathr   	last_pathoriginal_path_dictr7   	user_pathupdate_pathsr   
add_buttonremove_buttonmovetop_buttonmoveup_buttonmovedown_buttonmovebottom_buttonexport_buttonuser_headerproject_headersystem_headerheadersselection_widgets_setup_right_toolbarright_buttonsr   
listwidgetr
   OkCancelbboxbutton	button_oksetWindowTitler   setWindowIconr   iconresize
setVisibleosnamer   setWordWrapr   _add_buttons_to_layout
addStretchr   	addWidget	addLayout
addSpacing	setLayoutcurrentRowChangedconnectitemChangedacceptedacceptrejectedrejectsetup)rB   parentrL   rM   rN   syncdescriptionbuttons_layoutmiddle_layoutlayout	__class__s   `         r/   rF   zPathManager.__init__4   s     	BGGV&9999T6***&t&AAAA$T

 344444 	3788999JB	(.B.4"'))"& 9 	C*?*A*ABBB !"!#!%!"!!#!6688%d++$%5%8'7'>&? @ @	))*:*=>> 	A23344438L11222C%%bgo&>$???  $ % %
 

 	%%% %##D$6GGG!!!$$$ $000/// %%%"'''###v 	)112J2J2J2JKKK#++,D,D,D,DEEE	""4;///	""4;/// 	

r.   c                 :    |D ]}|                     |           dS )z$Helper to add buttons to its layout.N)rq   )rB   widgetsr   widgets       r/   ro   z"PathManager._add_buttons_to_layout   s2     	% 	%FV$$$$	% 	%r.   c                                           t          j        t          d                               d           fd           _                              t          j        t          d                               d           fd           _                              t          j        t          d	                               d
           fd           _	                              t          j
        t          d                               d           fd           _                              t          j        t          d                               d           fd           _                              t          j        t          d                               d           fd           _                              t          j                             d           j        t          d                     _         j         j         j	         j        g _         j         j        g j        z    j        gz   S )zCreate top toolbar and actions.zMove path to the top2uparrowc                  0                          d          S )Nr   absolutemove_torB   s   r/   rC   z2PathManager._setup_right_toolbar.<locals>.<lambda>       dllAl66 r.   )textri   	triggeredzMove path up1uparrowc                  0                          d          S )Nrelativer   r   s   r/   rC   z2PathManager._setup_right_toolbar.<locals>.<lambda>   s    dllBl77 r.   )tipri   r   zMove path down
1downarrowc                  0                          d          S )Nr:   r   r   r   s   r/   rC   z2PathManager._setup_right_toolbar.<locals>.<lambda>   r   r.   zMove path to the bottom
2downarrowc                  0                          d          S )Nr:   r   r   r   s   r/   rC   z2PathManager._setup_right_toolbar.<locals>.<lambda>   r   r.   Add pathedit_addc                 ,                                     S r=   )r    r@   s    r/   rC   z2PathManager._setup_right_toolbar.<locals>.<lambda>   s     r.   Remove path	editclearc                 ,                                     S r=   )r!   r@   s    r/   rC   z2PathManager._setup_right_toolbar.<locals>.<lambda>   s     0 0 2 2 r.   
fileexportz)Export to PYTHONPATH environment variable)ri   r   r   )create_toolbuttonr   r&   r   create_iconrU   r'   rV   r(   rW   r)   rX   r*   rS   r+   rT   r,   export_pythonpathrY   r^   r   s   `r/   r_   z PathManager._setup_right_toolbar   si   "44"*)**!!*--6666	 5 8 8
 "33").!!!!*--7777	 4 9 9
  $55"+"##!!,//6666	  6  8  8
 "&!7!7"/,--!!,//6666	 "8 "8 "8
 00"**!!*--////	 1 1 1
 "33"--  !!+..2222	 4 4 4
 "33".!!,//,=>>	 4 @ @ #'"5t7I"&"68N"P _d01"#&*&8%9:	
r.   c                 |   t          |          }|                    t          j        d                     || j        v rL|                    t          j        t          j        z             |	                    t          j
                   n|| j        v rT|                    |                                t          j        z             |	                    t          j                   nS|                    |                                t          j        z             |	                    t          j
                   |S )z!Helper to create a new list item.DirClosedIcon)r   setIconr   ri   rM   setFlagsr   NoItemFlagsItemIsUserCheckablesetCheckStateCheckedrN   flags	UncheckedrB   rL   items      r/   _create_itemzPathManager._create_item   s    t$$SXo..///4$$$MM".2+AABBBrz****T)))MM$**,,)??@@@r|,,,,MM$**,,)??@@@rz***r.   c                    t          |          }|                    t          j                   |                    t          j                   |                                }|                    d           |                    |           |S )z)Create a header for a given path section.T)	r   setTextAlignmentr   AlignHCenterr   ItemIsEnabledfontsetBoldsetFont)rB   r   headerr   s       r/   _create_headerzPathManager._create_header   st     && 	000())) {{}}Ttr.   c                     d}| j         r|t          | j                  dz   z  }| j        r|t          | j                  z  }|S )z*Maximum bottom row count that is editable.r   r:   )r[   lenrM   rZ   rQ   )rB   
bottom_rows     r/   editable_bottom_rowzPathManager.editable_bottom_row   sS     
 	5#d/00144J 	.#dn---Jr.   c                 d    d}| j         r|t          | j                  dz   z  }| j        r|dz  }|S )z'Maximum top row count that is editable.r   r:   )r[   r   rM   rZ   )rB   top_rows     r/   editable_top_rowzPathManager.editable_top_row   sG      	2s4,--11G 	qLGr.   c                     | j                                          | j                                         d| _        d| _        d| _        | j        r|                     t          d                    | _        | j        	                    | j                   | j         
                    | j                   | j        D ]1}|                     |          }| j         
                    |           2| j        r|                     t          d                    | _        | j        	                    | j                   | j         
                    | j                   | j        D ]1}|                     |          }| j         
                    |           2| j        r|                     t          d                    | _        | j        	                    | j                   | j         
                    | j                   | j        D ]1}|                     |          }| j         
                    |           2| j                             d           |                                 | _        |                                  dS )zPopulate list widget.NzProject path
User pathszSystem PYTHONPATHr   )ra   clearr]   r[   rZ   r\   rM   r   r   appendaddItemr   rQ   r7   setCurrentRowget_path_dictrP   r?   r   s      r/   r|   zPathManager.setup   sK   "!  	."&"5"5a6G6G"H"HDL 3444O##D$7888) . .((..''---- > 	.#221\??CCDL 0111O##D$4555 . .((..''----  	.!%!4!4Q7J5K5K!L!LDL 2333O##D$6777( . .((..''----%%a((("&"4"4"6"6r.   c                 L   t          j        | t          d          t          d          t           j        t           j        z  t           j        z            }|t           j        k    rdS t                      }d |                                                                 D             |t           j        k    r}nA|	                    dg           }t          |t                    s|g}fd|D             }|z   }t          j                            |          t          j        d<   |                     t#          |                     |                     dt#          |                     |                                  t          |          |d<   t)          || 	           dS )
z`
        Export to PYTHONPATH environment variable
        Only apply to: current user.
        Exporta  This will export Spyder's path list to the <b>PYTHONPATH</b> environment variable for the current user, allowing you to run your Python modules outside Spyder without having to configure sys.path. <br><br>Do you want to clear the contents of PYTHONPATH before adding Spyder's path list?Nc                     g | ]	\  }}||
S r-   r-   ).0kvs      r/   
<listcomp>z1PathManager.export_pythonpath.<locals>.<listcomp>9  s!    GGGTQQGqGGGr.   
PYTHONPATHc                     g | ]}|v|	S r-   r-   )r   pactive_paths     r/   r   z1PathManager.export_pythonpath.<locals>.<listcomp>B  s#    >>>1+)=)=Q)=)=)=r.   r6   r7   )r}   )r   questionr   YesNorc   r   r   itemsgetrG   listrl   pathsepjoinenvironrR   rH   set_confr|   r   )rB   answerenvppathr   s       @r/   r   zPathManager.export_pythonpath  s    %hKK + , , Okn,{/AA
 
 ['''Fnn
 HGT%7%7%9%9%?%?%A%AGGG[_$$EEGGL"--EeT**  >>>>>>>EK'E#%:??5#9#9
<  	eEll333mU5\\222

 KKLS&&&&&&r.   Fc                 D   t                      }t          | j                                                  D ]j}| j                            |          }|                                }|| j        vr1|| j        v r|sE|                                t          j
        k    ||<   k|S )z
        Return an ordered dict with the path entries as keys and the active
        state as the value.

        If `project_path` is True, its entries are also included.
        )r   rangera   countr   r   r]   rM   
checkStater   r   )rB   rM   odictrowr   rL   s         r/   r   zPathManager.get_path_dictO  s     ..0011 	> 	>C?'',,D99;;D4<''4,,,\,"oo//2:=dr.   c                    g }t          | j                                                  D ]_}| j                            |          }|                                }|| j        vr&|| j        | j        z   vr|                    |           `|S )z1Get current user path as displayed on listwidget.)	r   ra   r   r   r   r]   rM   r7   r   )rB   rQ   r   r   rL   s        r/   get_user_pathzPathManager.get_user_path`  s    	..0011 	+ 	+C?'',,D99;;D4<'' 1D4D DEE$$T***r.   c                      || _         || _        || _                             dd           fd j         D              _        dS )zUpdate path attributes.Nr7   r-   c                 ,    g | ]}|j         z   v|S r-   r6   )r   rL   previous_system_pathrB   s     r/   r   z,PathManager.update_paths.<locals>.<listcomp>u  s7     
 
 
D,/CCDD DDDr.   )rL   rN   r7   get_confrQ   )rB   rL   rN   r7   r   s   `   @r/   rR   zPathManager.update_pathsk  sv    DI&#2D "*D#}}]B??
 
 
 
 
!Y
 
 
r.   c                 d   | j                                         }|du}| j        D ]}|                    |           | j                                         }g }|| j        k    r!|                    | j        | j        g           || j	        k    r!|                    | j
        | j        g           || j        v s|| j	        k    r-|                    | j        | j        | j
        | j        g           |D ]}|                    d           | j                            || j        vo| j        |cxk    o
| j	        k    nc            | j                            | j                                         dk               | j                            | j        |                                 k               dS )zRefresh toolbar widgets.NFr   )ra   currentItemr^   
setEnabled
currentRowr   extendrU   rV   r   rX   rW   r]   rT   rY   r   rf   rP   r   )rB   current_itemenabledr   r   disable_widgetss         r/   r?   zPathManager.refreshz  s   2244d*, 	' 	'Fg&&&& o((** $'''""D$79K#LMMM $***""D$:$($8$: ; ; ; 4<''31I+I+I""$d&8')=?  
 & 	% 	%Fe$$$$ 	%%, K&#IIII1IIIII	
 	
 	

 	%%do&;&;&=&=&ABBB 	!!#t'9'9';';;	= 	= 	= 	= 	=r.   c                 (   |[| j                             d           t          | t          d          | j                  }| j                             d           |sdS t          j        |          }|| _        ||                                 v r| j        	                    |t          j                  d         }|                    t          j                   t          j        | t          d          t          d          t          j        t          j        z            }|t          j        k    rg| j                            | j                            |                    }| j                            d|           | j                            d           nct-          |          r| j        sF|                     t          d	                    | _        | j                            | j                   | j                            | j                  dk     r8| j        dk    r| j        dz
  }nd}| j                            || j                   |                     |          }| j                            | j        |           | j                            | j                   | j                            d|           n;t          j        | t          d          t          d
          t          j                   }| !                                 dS )zp
        Add path to list widget.

        If `directory` is provided, the folder dialog is overridden.
        NFzSelect directoryTr   r   z\This directory is already included in the list.<br> Do you want to move it to the top of it?r:   r   zThis directory cannot be added to the path!<br><br>If you want to set a different Python interpreter, please go to <tt>Preferences > Main interpreter</tt>.)"redirect_stdioemitr   r   rO   ospabspathr   ra   	findItemsr   MatchExactlyr   r   r   r   r   r   takeItemr   
insertItemr   r   rZ   r   r]   r   r   r   rQ   insertwarningrb   r?   )rB   	directoryr   r   
header_rows        r/   r    zPathManager.add_path  s    $$U+++,T15G3H3H-1^= =I$$T*** K	**	"**,,,,?,,YHHKDrz*** )* = > > +.02 2F ((//0C0CD0I0IJJ**1d333--a000)$$ $' :'+':':1\??'K'KD$L''(8999 ?&&t'7881<<,q00%)%:Q%>

%&
O..z/3/?A A A ((33**4+@$GGG--d.CDDD%%a3333$,jMM   
  N$ $ 	r.   c                    | j                                         r7|sHt          j        | t	          d          t	          d          t          j        t          j        z            }|s|t          j        k    r| j                                         }| j                            |	                                           | j         
                    | j                                                    t          | j                  dk    r7| j         
                    | j                             | j                             |                                  dS dS dS )zk
        Remove path from list widget.

        If `force` is True, the message box is overridden.
        r   z/Do you really want to remove the selected path?r   N)ra   r   r   r   r   r   r   rQ   remover   r   r   r   r   rZ   r?   )rB   forcer   r   s       r/   r!   zPathManager.remove_path  s3    ?&&(( 	 6$,m$$GHHOkn4	6 6  +/112244%%diikk222 (()C)C)E)EFFF t~&&!++O,,++D,<==? ? ? -	 	 21r.   c                    | j                                         }||r| j        }n| j        }n||z   }t	          dt          | j        |                    }| j                             |          }| j                             ||           | j                             |           | 	                                | _
        |                                  dS )zMove items of list widget.Nr:   )ra   r   r   r   maxminr   r   r   r   rQ   r?   )rB   r   r   index	new_indexr   s         r/   r   zPathManager.move_to  s    **,, 2 4		 1		(I3t7CCDD	''..""9d333%%i000++--r.   c                 4    | j                                         S )z$Returns the current row of the list.)ra   r   r   s    r/   current_rowzPathManager.current_row  s    ))+++r.   c                 :    | j                             |           dS )z Set the current row of the list.N)ra   r   )rB   r   s     r/   set_current_rowzPathManager.set_current_row  s    %%c*****r.   c                 ^    | j                             |          }|                                S )z)Return the checked state for item in row.)ra   r   r   )rB   r   r   s      r/   row_check_statezPathManager.row_check_state!  s'    ##C((   r.   c                 d    | j                             |          }|                    |           dS )z.Set the current checked state for item in row.N)ra   r   r   )rB   r   valuer   s       r/   set_row_check_statezPathManager.set_row_check_state&  s1    ##C((5!!!!!r.   c                 4    | j                                         S )zReturn the number of items.)ra   r   r   s    r/   r   zPathManager.count+  s    $$&&&r.   c                     | j         |                     dd          k    r,| j                            |                                            |                     d| j                    dS )zz
        Request to update path values on main window if current and previous
        system paths are different.
        r7   r-   )defaultN)r7   r   sig_path_changedr   r   r   r   s    r/   _update_system_pathzPathManager._update_system_path1  sb    
 t}}]B}GGGG!&&t'9'9';';<<<mT%566666r.   c                     |                                  }| j        |k    r| j                            |           t	                                                       dS )zOverride Qt method.N)r   rP   r  r   rE   ry   )rB   	path_dictr   s     r/   ry   zPathManager.accept:  sS    &&((	"i//!&&y111r.   c                 p    |                                   t                                                       d S r=   )r  rE   r{   )rB   r   s    r/   r{   zPathManager.rejectA  s/      """r.   c                 r    |                                   t                                          |           d S r=   )r  rE   
closeEvent)rB   eventr   s     r/   r  zPathManager.closeEventE  s3      """5!!!!!r.   )NNNT)F)NNNr=   )NN)&r#   r$   r%   __doc__r   boolr   objectr  CONF_SECTIONrF   ro   r_   r   r   propertyr   r   r|   r   r   r   r   rR   r?   r    r!   r   r  r  r  r  r   r  ry   r{   r  __classcell__)r   s   @r/   r1   r1   +   sh       VD\\Nvf~~ (L7;,0T T T T T Tl% % %
+
 +
 +
Z  "   	 	 X	 	 	 X	( ( (T 
TVV.' .' V.'`   "	 	 	
 
 
 
(= (= (=T 
TVV@ @ @ V@D 
TVV   V<   &, , ,+ + +! ! !
" " "
' ' '7 7 7        " " " " " " " " "r.   r1   c            	      L   ddl m}   |             }t          dt          t          j        dd                   t          t          j        dd                             }d }|j                            |           t	          j        |	                                           dS )zRun path manager test.r   )qapplicationNr:   )rL   rM   c                 ^    t           j                            t          |                      d S r=   )sysstdoutwriterK   )r  s    r/   callbackztest.<locals>.callbackU  s$    
Y(((((r.   )
spyder.utils.qthelpersr&  r1   rH   r)  rL   r  rv   exitexec_)r&  r   dlgr,  s       r/   testr1  J  s    333333A
38BQB<  38BCC=))  C) ) )   ***HSYY[[r.   __main__)-r  collectionsr   rl   os.pathrL   r   r)  qtpyr   qtpy.compatr   qtpy.QtCorer   r   r   qtpy.QtWidgetsr	   r
   r   r   r   r   r   r   spyder.api.widgets.mixinsr   spyder.config.baser   spyder.plugins.pythonpath.utilsr   r   spyder.utils.environr   r   spyder.utils.icon_managerr   spyder.utils.miscr   spyder.utils.stylesheetr   r   r1   r1  r#   r-   r.   r/   <module>r@     s
     $ # # # # # 				       



       , , , , , , ( ( ( ( ( ( ( ( ( (1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 8 7 7 7 7 7             M M M M M M M M ; ; ; ; ; ; ; ; ) ) ) ) ) ) , , , , , , < < < < < <! ! ! ! ! ! ! !\" \" \" \" \"', \" \" \"~  $ zDFFFFF r.   