
    Je                    p   d Z ddlZddlmZ ddlZddlZddlZddlm	Z	 ddl
mZmZ ddlmZmZmZmZmZmZmZ ddlmZ ddlmZmZmZmZmZmZ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+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1m2Z2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z: ddl7m;Z;m<Z<m=Z= ddl>m?Z? ddl@mAZAmBZBmCZC 	 ddlDmEZF  G d d      ZG G d d      ZH G d d      ZI G d d      ZJ G d d      ZK G d d       ZL G d! d"      ZM G d# d$      ZN G d% d&e       ZO G d' d(e$e+      ZP G d) d*eP      ZQy#  dZFY yxY w)+zFiles and Directories Explorer    N)PYQT5)getexistingdirectorygetsavefilename)QDir	QMimeDataQtQTimerQUrlSignalSlot)QDrag)QApplicationQDialogQDialogButtonBoxQFileSystemModelQInputDialogQLabel	QLineEditQMessageBoxQProxyStyleQStyle	QTextEditQToolTip	QTreeViewQVBoxLayout)on_conf_change)_)SpyderWidgetMixin)get_home_dir)NAME_FILTERS)create_scriptfixpathIconProvidershow_in_external_file_explorer)to_binary_string)encoding)ima)miscprogramsvcs)getcwd_or_home)file_urikeyevent_to_keysequence_str
start_file)PythonExporterc                       e Zd ZdZdZdZy)DirViewColumns         N)__name__
__module____qualname__SizeTypeDate     J/usr/lib/python3/dist-packages/spyder/plugins/explorer/widgets/explorer.pyr1   r1   7   s    DDDr<   r1   c                       e Zd ZdZy)DirViewOpenWithSubMenuSectionsMainNr5   r6   r7   r@   r;   r<   r=   r?   r?   =   s    Dr<   r?   c                   t    e 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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)DirViewActionstoggle_date_column_action"toggle_single_click_to_open_actiontoggle_size_column_actiontoggle_type_column_actiontoggle_show_hidden_actionedit_name_filters_actionnew_file_actionnew_module_actionnew_folder_actionnew_package_actionopen_with_spyder_actionopen_with_system_actionopen_with_system_2_actiondelete_actionrename_actionmove_actionz	copy filez
paste filezcopy absolute pathzcopy relative pathshow_system_explorer_actionversion_control_commit_actionversion_control_browse_actionconvert_notebook_actionopen_interpreter_action
run_actionN)r5   r6   r7   ToggleDateColumnToggleSingleClickToggleSizeColumnToggleTypeColumnToggleHiddenFilesEditNameFiltersNewFile	NewModule	NewFolder
NewPackageOpenWithSpyderOpenWithSystemOpenWithSystem2DeleteRenameMoveCopyPasteCopyAbsolutePathCopyRelativePathShowInSystemExplorerVersionControlCommitVersionControlBrowseConvertNotebookOpenInterpreterRunr;   r<   r=   rC   rC   A   s    2<223 1OG#I#I%J.N.N1OFFDDE++8::/O 0O
Cr<   rC   c                       e Zd ZdZdZdZdZy)DirViewMenuscontext_menuheader_menunew_menuopen_with_menuN)r5   r6   r7   ContextHeaderNewOpenWithr;   r<   r=   ru   ru   c   s    GF
CHr<   ru   c                       e Zd ZdZy)DirViewHeaderMenuSectionsmain_sectionNrA   r;   r<   r=   r   r   j   s    Dr<   r   c                       e Zd ZdZdZy)DirViewNewSubMenuSectionsgeneral_sectionlanguage_sectionN)r5   r6   r7   GeneralLanguager;   r<   r=   r   r   n   s    G!Hr<   r   c                        e Zd ZdZdZdZdZdZy)DirViewContextMenuSectionscopy_paste_sectionextras_sectionnew_sectionsystem_sectionversion_control_sectionN)r5   r6   r7   	CopyPasteExtrasr|   SystemVersionControlr;   r<   r=   r   r   s   s    $IF
CF.Nr<   r   c                       e Zd ZdZdZdZdZy)ExplorerTreeWidgetActionstoggle_filter_files_actionnext_actionparent_actionprevious_actionN)r5   r6   r7   ToggleFilterNextParentPreviousr;   r<   r=   r   r   {   s    /L DF Hr<   r   c                         e Zd Zd fd	Z xZS )DirViewStylec                 x    |t         j                  k(  ry|t         j                  k(  ryt        |   ||||      S )zp
        To show tooltips with longer delays.

        From https://stackoverflow.com/a/59059919/438386
        i  r   )r   SH_ToolTip_WakeUpDelaySH_ToolTip_FallAsleepDelaysuper	styleHint)selfhintoptionwidgetreturn_data	__class__s        r=   r   zDirViewStyle.styleHint   s=     6000V666w vv{CCr<   )NNN)r5   r6   r7   r   __classcell__r   s   @r=   r   r      s    D Dr<   r   c                   v    e Zd ZdZ ee      Z	  ee      Z	  ee      Z	  ee	      Z
	  ee      Z	  eee      Z	  ee      Z	  ee      Z	  eee      Z	  ee      Z	 dN fd	Zd Z eg d      d        Zd ZdNdZej0                  fd	Zd
 Zd Zd Z fdZ fdZ fdZ fdZ d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d Z+dNdZ,d Z- e.       d        Z/ e.       dNd       Z0 e.       d         Z1 e.       dNd!       Z2d" Z3d# Z4d$ Z5 e.       dNd%       Z6d& Z7 e.       dNd'       Z8 e.       dNd(       Z9 e.       dOd)       Z:d* Z;d+ Z< e.       dNd,       Z=d- Z> e.       dNd.       Z? e.       dNd/       Z@dPd0ZA e.       d1        ZB e.       d2        ZC e.       dNd3       ZD e.       dNd4       ZE e.       dNd5       ZFdNd6ZGd7 ZHd8 ZId9 ZJd: ZKd; ZLd< ZMd= ZNd> ZOd? ZPd@ ZQdA ZRdB ZSdC ZTdD ZUdE ZVdF ZWdG ZXdH ZYdI ZZ e.       dJ        Z[ e.       dNdK       Z\ e.       dNdL       Z]dM Z^ xZ_S )QDirViewzBase file/directory tree view.c                    t         rt        | 	  ||       n-t        j                  | |       t	        j                  | |       || _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        d| _        d| _        d| _        d| _        | j#                         }|j$                  j'                  | j(                         t+        d      | _        | j,                  j/                  |        | j1                  | j,                         | j3                          | j5                  | j6                         |j9                  t:        j<                         | j?                  d       y)zInitialize the DirView.

        Parameters
        ----------
        parent: QWidget
            Parent QWidget of the widget.
        )parentclass_parent)r   r   TNF) r   r   __init__r   r   _parent_last_column_last_order_scrollbar_positions_to_be_loaded_DirView__expanded_statecommon_actions	filter_onexpanded_or_colapsed_by_mousefsmodelmenurw   headercustomContextMenuRequestedconnectshow_header_menur   _style	setParentsetStylesetup_fs_modelsetSelectionModeExtendedSelectionsetContextMenuPolicyr   CustomContextMenusetMouseTracking)r   r   r   r   s      r=   r   zDirView.__init__  s7    GF@tV,&&t&A $(!! $"-2* 	 	))11$2G2GH #4(d#dkk" 	d445##B$8$89 	d#r<   c                      j                           j                  t        j                  t	        d       j                  d       j                        } j                  t        j                  t	        d       j                  d       j                        } j                  t        j                  t	        d       j                  d       j                        } j                  t        j                  t	        d       j                  d	       j                        } j                  t        j                  t	        d
       j                  d       j                         _         j                  t        j                   t	        d       j"                         _         j                  t        j&                  t	        d       j"                  d       _         j                  t        j*                  t	        d       j                  d       j,                        } j                  t        j.                  t	        d       j                  d       j0                        } j                  t        j2                  t	        d       j                  d       j4                         _         j                  t        j8                  t	        d       j                  d       j:                  d       _         j                  t        j>                  t	        d       j                  d       j@                  d       _!         j                  t        jD                  t	        d       jF                  d       _$         j                  t        jJ                  t	        d       jL                  d       _'        tP        jR                  dk(  rt	        d       }nt	        d!      } j                  t        jT                  | jV                        } j                  t        jX                  t	        d"       j                  d#       fd$       _-         j                  t        j\                  t	        d%       j                  d&       fd'       _/         j                  t        j`                  t	        d(      d jc                  d)      d)*       _2         j                  t        jf                  t	        d+       j                  d,       jh                         _5         j                  t        jl                  t	        d-      d jc                  d.      d.*        j                  t        jn                  t	        d/       j                  d0       jp                         _9         j                  t        jt                  t	        d1       j                  d2       jv                        }	 j                  t        jx                  t	        d3      t{        j|                  d       j~                  4      }
 j                  t        j                  t	        d5      d jc                  d6      dd67      } j                  t        j                  tP        jR                  dk(  rt	        d8      n
t	        d8      d jc                  d9      dd97      } j                  t        j                  t	        d:      d jc                  d;      dd;7      } j                  t        j                         _F        |||fD ].  } j                  | j                  t        j                  <       0  j                  t        j                  t	        d=            }||fD ]$  } j                  ||t        j                  <       & ||fD ]$  } j                  ||t        j                  <       &  j                  t        j                  t	        d>             _O         j                  t        j                         _Q        ||	 j                   j                   j$                  || j6                  fD ].  } j                  | j                  t        j                  <       0  j<                   jB                   jH                   jN                  fD ].  } j                  | j                  t        j                  <       0  j                  | j                  t        j                  <        jZ                   j^                  fD ].  } j                  | j                  t        j                  <       0  jr                  |
fD ].  } j                  | j                  t        j                  <       0  j                  j                  j                   j                         y )?NzFile...TextFileIcontexticon	triggeredzPython file...pythonz	Folder...
folder_newzPython Package...package_newzOpen in SpydereditzOpen externally)r   r   zDefault external applicationFr   r   register_shortcutz	Delete...
editdeletez	Rename...renamezMove...moverj   editcopyT)r   r   r   r   rk   	editpastezCopy Absolute PathzCopy Relative PathdarwinzShow in FinderzShow in FolderCommit
vcs_commitc                  &     j                  d      S )Ncommitvcs_commandr   s   r=   <lambda>zDirView.setup.<locals>.<lambda>      d..x8 r<   zBrowse repository
vcs_browsec                  &     j                  d      S )Nbrowser   r   s   r=   r   zDirView.setup.<locals>.<lambda>  r   r<   zShow hidden filesshow_hidden)r   toggledinitialr   zEdit filter settings...filterzSingle click to opensingle_click_to_openzOpen IPython console hereipython_consolers   runzConvert to Python file)r   r   r8   size_column)r   r   r   r   r   r9   type_columnzDate modifieddate_columnr   sectionr|   z	Open with)Z
setup_viewcreate_actionrC   r`   r   create_iconnew_filera   
new_modulerb   
new_folderrc   new_packagerd   openrN   re   open_externalopen_external_actionrf   open_external_action_2rg   deleterh   r   ri   r   rS   rj   copy_file_clipboardcopy_actionrk   save_file_clipboardpaste_actionrl   copy_absolute_pathcopy_absolute_path_actionrm   copy_relative_pathcopy_relative_path_actionsysplatformrn   r$   ro   vcs_commit_actionrp   vcs_log_actionr^   get_confhidden_actionr_   edit_filterfilters_actionr[   rr   open_interpreterrX   rs   r   rq   r'   r   convert_notebooksr\   r]   rZ   create_menuru   r{   rw   add_item_to_menur   r@   r|   r   r   r   r}   open_with_submenurz   rv   r   r   r   r   r   aboutToShowr   update_actions)r   rJ   rK   rL   rM   rQ   rR   show_in_finder_textshow_in_system_explorer_actionrY   ipynb_convert_actionsize_column_actiontype_column_actiondate_column_actionitemnew_submenus   `               r=   setupzDirView.setup8  sD	    ,,""9!!.1mm	 - 
 !..$$#$!!(+oo	 / 
 !..$$;!!,/oo	 / 
 "//%%&'!!-0&&	 0 
 (,'9'9))#$!!&)ii	 (: (
$ %)$6$6))$%(( %7 %
! '+&8&8**12((#	 '9 '
# **!!;!!,/kk	 + 
 **!!;!!(+kk	 + 
  --9!!&)ii	 . 
  --6!!*-.." . 
 !..  7!!+..." / 
 *.););++'(--"	 *< *
& *.););++'(--"	 *< *
& <<8#"#$4"5"#$4"5)-););//$99 *< *
& "&!3!3//8!!,/8	 "4 "
 #00//&'!!,/8	 1 
 "//,,&'MM-0  0 
 #00**,-!!(+&&	 1 
 	,,)*MM"89) 	 	
 (,'9'9**./!!"34++	 (: (
$ ''5!!%(hh	 ( 

  $11**&'(#,,	  2  
 "//++6MM-0#  0 
 "//++!llh66AfIMM-0#  0 
 "//++?#MM-0#  0 
  ++L,?,?@');') 	D!!%%166 " 	 &&eH
 %&78 	D!! 199 " 	 '(:; 	D!! 1:: " 	 "&!1!1!!kN"
 !,,\-A-AB *11++.."M43C3C	E 		D
 !!&&266 " 		 %%t'8'833335 	D !!&&2<< " 	 	*"".55 	 	
 ++T-@-@A 	D!!&&2AA " 	 113GH 	D!!&&299 " 	 	%%--d.A.ABr<   )r   r   r   name_filtersr   r   )r   c                    |dk(  r"| j                  t        j                  |        y |dk(  r"| j                  t        j                  |        y |dk(  r"| j                  t        j                  |        y |dk(  r| j
                  r| j                  |       y y |dk(  r| j                  |       y |dk(  r| j                  |       y y )Nr   r   r   r  r   r   )	setColumnHiddenr1   r8   r9   r:   r   filter_filesset_show_hiddenset_single_click_to_open)r   r   values      r=   on_conf_updatezDirView.on_conf_updateY  s     ]"  !4!4%i@}$  !4!4%i@}$  !4!4%i@~%~~!!%( }$  '--))%0 .r<   c                    | j                         }|rt        j                  |d         r|d   }nt        j                  |d         }t	        t        j                  |d               }|xr, t        |D cg c]  }t        j                  |       c}      }t        |D cg c]  }t        j                  |       c}      }t        |D cg c]  }t        j                  |       c}      }n
d}d}d}d}d}t        j                  |      }	| j                  j                  t        |D cg c]#  }t	        t        j                  |            |k(  % c}             | j                  j                  d       | j                  j                  |       | j                  j                  |xr |       | j                   j#                         j                  d       t%        j&                         }
|
j)                         j+                         }| j,                  j/                  |        | j0                  j                  |	       | j2                  j                  |	       |rt5        |      dk(  r| j7                  |d         }nt5        |      dkD  r| j9                  |      }t5              dk\  r| j;                         }| j                   j#                         j                  d       | j                   j=                          |D ].  }| j?                  || j                   t@        jB                         0 n| j                  j                  d       | j                         }t        |D cg c]  }t        jD                  |      d   dk(   c}      }t        |D cg c]  }t        jD                  |      d   dv  c}      }|xr tF        d u}| jI                  tJ        jL                        j                  |       | jI                  tJ        jN                        j                  |       y c c}w c c}w c c}w c c}w c c}w c c}w )	Nr   F r2   Tr   z.ipynbz.pyz.pywz.ipy)(get_selected_filenamesospisdirdirnamer"   allisfiler&   is_text_filer*   is_vcs_repositoryrS   
setVisibler   rX   rN   r  
menuActionr   	clipboardmimeDatahasUrlsr  setDisabledr	  r
  lenget_file_associationsget_common_file_associations!_create_file_associations_actionsclear_actionsr  r?   r@   splitext
nbexporter
get_actionrC   rq   rs   )r   fnamesr-  basedirfname	only_dirs
only_filesfna
only_validvcs_visibler4  has_urlsassocactionsactiononly_notebooksonly_modules
nb_visibles                     r=   r  zDirView.update_actionsk  s   ,,.yy# )++fQi0ckk&)45GN3f'MU		%(8'M#NIVDEcjj/DEJFKSh33C8KLJJJIGG++G4 	##VLEU+,7LM	O!!,,U3$$//	:$$//
0IzJ))+66u= **,	%%'//1%%(l3 	))+6&&{36{a226!9=Vq99&A5zQ@@B&&113>>tD&&446% F))!33 > C C *  ))44T:,,.+13"' "ll51!4@ 3 4)/1 %  LL/26MM 1 2 $>
$(>
667BB:N**+66|Do (NDK MD31s$   7P5#P:P?0(Q2"Q	$!Qc                    
 | j                         }g }t        |D cg c]  }t        j                  |       c}      }|rJt	        |      dk(  r j                  |d         }nt	        |      dkD  r j                  |      }t	              dk\  r|D ]  \  }}|}t        j                  j                  |      s-t        j                  j                  |      s|t        d      z  }	  j                  
 j                  |||f
fd	d      }	t        j                  j                  |      s0t        j                  j                  |      s|	j%                  d       |j'                  |	        |j'                   j(                         |S c c}w # t        $ r_  j                  |      }		 |	j                  j!                          n# t        $ r Y nw xY w|	j                  j#                  |f fd	       Y w xY w)	z2
        Create file association actions.
        r2   r   z (Application not found!)c                      |      S Nr;   )xy
open_assocs     r=   r   z;DirView._create_file_associations_actions.<locals>.<lambda>  s    A r<   Fr   c                 &    j                  |      S rQ  )open_association)rR  rS  r   s     r=   r   z;DirView._create_file_associations_actions.<locals>.<lambda>  s    t/D/DQ/G r<   T)r*  r.  r+  r/  r8  r9  r:  ospathr,  r   rV  r   	Exceptionr?  r   
disconnectr   r7  appendr   )r   r@  rJ  rB  rD  rI  app_namefpathr   open_with_actionrT  s   `         @r=   r;  z)DirView._create_file_associations_actions  s    >002F@#**U+@A
6{a226!9=Vq99&A5zQ',  5OHe#DGGNN51RWW]]55I"= >>%)%:%:
+/+=+=$!%27&F.3	 ,> ,(* GGNN51RWW]]55I(44T:NN#34A 5D t::;Y A, % +/??8+D(!,66AAC( ! ! )22::(-Gs;   F-&FG?2GG?	GG?G#G?>G?c                     | j                         }|j                  d       t        j                  | ||       |j	                  d|       || _        | j                   | _        y)zOverride Qt method.Tr   N)r   setSortIndicatorShownr   sortByColumnsetSortIndicatorr   r   )r   columnorderr   s       r=   ra  zDirView.sortByColumn  sV    $$T*tVU35)"#///r<   c                 N    | j                          t        j                  | |      S )zReimplement Qt method)executeDelayedItemsLayoutr   viewportEventr   events     r=   rg  zDirView.viewportEvent  s#     	&&(&&tU33r<   c                     	 | j                         }t        |      dk7  r*| j                  j                  |j	                                yy# t
        $ r Y yw xY w)zOverride Qt methodr   N)r*  r8  rv   popup	globalPosAttributeError)r   ri  r@  s      r=   contextMenuEventzDirView.contextMenuEvent  sS    	002F6{a!!''(9:   		s   AA 	AAc                    t        |      }|j                         t        j                  t        j                  fv r| j                          y|j                         t        j                  k(  r| j                          y|j                         t        j                  k(  r| j                          y|j                         t        j                  k(  r| j                          y|| j                  j                         j                         k(  r| j                          y|| j                   j                         j                         k(  r| j#                          y|| j$                  j                         j                         k(  r| j'                          y|| j(                  j                         j                         k(  r| j+                          yt-        j.                  | |       y)z+Handle keyboard shortcuts and special keys.N)r-   keyr   	Key_Enter
Key_ReturnclickedKey_F2r   
Key_Deleter   Key_Backspacego_to_parent_directoryr   shortcuttoStringr   r  r  r  r  r  r  r   keyPressEvent)r   ri  key_seqs      r=   rz  zDirView.keyPressEvent	  sF   -e499;2<<77LLNYY[BII%KKMYY[BMM)KKMYY[B,,,'')((113<<>>$$&))224==??$$&66??AJJLL##%66??AJJLL##%##D%0r<   c                     t         |   |       | j                  d      s0| j                  | j	                  |j                                      yy)zHandle double clicks.r   indexN)r   mouseDoubleClickEventr  rs  indexAtposr   ri  r   s     r=   r  zDirView.mouseDoubleClickEvent   s?    %e,}}34LLt||EIIK8L9 5r<   c                    | j                  |j                               }|j                         r| j                  |      }|j	                         | j                  | j                               j	                         z
  }|j                         j	                         |k  rd| _        nd| _        t        | !  |       y)z
        Detect when a directory was expanded or collapsed by clicking
        on its arrow.

        Taken from https://stackoverflow.com/a/13142586/438386
        TFN)	r  r  isValid
visualRectrR  	rootIndexr   r   mousePressEvent)r   ri  clicked_indexvrectitem_identationr   s        r=   r  zDirView.mousePressEvent&  s     UYY[1  "OOM2E#ggi$//$..:J*K*M*M*OOOyy{}}05925:2&r<   c                     t         |   |       | j                  d      r0| j                  | j	                  |j                                      yy)zHandle single clicks.r   r}  N)r   mouseReleaseEventr  rs  r  r  r  s     r=   r  zDirView.mouseReleaseEvent7  s?    !%(==/0LLt||EIIK8L9 1r<   c                 j   t        j                          | j                  |j                               }|j	                         r| j                  d      r| j                  |      }|j                         | j                  | j                               j                         z
  }|j                         j                         |kD  r | j                  t        j                         n| j                  t        j                         | j                  | j                  |             t        | A  |       y)z%Actions to take with mouse movements.r   N)r   hideTextr  r  r  r  r  rR  r  	setCursorr   PointingHandCursorArrowCursor
setToolTipget_filenamer   mouseMoveEvent)r   ri  r~  r  r  r   s        r=   r  zDirView.mouseMoveEvent=  s     	UYY[)==?}}34.GGI0@ A C C EE   99;==?_4NN2#8#89 NN2>>2OOD--e45u%r<   c                 `    |j                  |j                         j                  d             y)zDrag and Drop - Enter event
text/plainN)setAcceptedr5  	hasFormatrh  s     r=   dragEnterEventzDirView.dragEnterEventU  s"    %..*44\BCr<   c                     |j                         j                  d      r0|j                  t        j                         |j                          y|j                          y)zDrag and Drop - Move eventr  N)r5  r  setDropActionr   
MoveActionacceptignorerh  s     r=   dragMoveEventzDirView.dragMoveEventY  s;    NN&&|4.LLNLLNr<   c                     t               }|j                  | j                         D cg c]  }t        |       c}       t	        |       }|j                  |       |j                          yc c}w )z)Reimplement Qt Method - handle drag eventN)r   setUrlsr*  r
   r   setMimeDataexec_)r   dropActionsdatarB  drags        r=   	startDragzDirView.startDraga  sS    {t/J/J/LMed5kMNT{

 Ns   A.c                 Z    t        |       | _        | j                  j                  d       y)zSetup filesystem modelFN)r   r   setNameFilterDisablesr   s    r=   r   zDirView.setup_fs_modelk  s     '-**51r<   c                 :    | j                  | j                         y)zInstall filesystem modelN)setModelr   r   s    r=   install_modelzDirView.install_modelp  s    dll#r<   c                 x     j                           j                  j                  j                   fd        j	                  d        j                  d        j                  dt        j                          j                  j                  j                   j                          j                          y)z
Setup viewc                  &     j                  d      S )Nr   )resizeColumnToContentsr   s   r=   r   z$DirView.setup_view.<locals>.<lambda>x  s    D//2 r<   FTr   N)r  r   directoryLoadedr   setAnimatedsetSortingEnabledra  r   AscendingOrder
modelResetreset_icon_providerr   s   `r=   r   zDirView.setup_viewt  s    $$,,2	4t$!R../''(@(@A  "r<   c                 v    |r7t        j                  t        | j                  j	                  |                  S y)z'Return filename associated with *index*N)r+  normpathstrr   filePath)r   r~  s     r=   r  zDirView.get_filename  s-    <<DLL$9$9%$@ ABB r<   c                 8    | j                   j                  |      S )z%Return index associated with filename)r   r~  )r   filenames     r=   	get_indexzDirView.get_index  s    ||!!(++r<   c                 &   g }| j                         | j                  k(  rL| j                         :| j                         j                         D cg c]  }| j	                  |       }}|S | j	                  | j                               g}|S c c}w )zReturn selected filenames)selectionModer   selectionModelselectedRowsr  currentIndex)r   r@  idxs      r=   r*  zDirView.get_selected_filenames  s    4#9#99""$0--/<<>@S$++C0 @ @
  ''(9(9(;<=F@s   Bc                     | j                  |      }|r,t        j                  |      r|S t        j                  |      S y)z&Return dirname associated with *index*N)r  r+  r,  r-  )r   r~  rB  s      r=   get_dirnamezDirView.get_dirname  s:    !!%(yy{{5))	 r<   c                 X    | j                   j                  | j                  |             y)zDisplay header menu.N)rw   rk  mapToGlobal)r   r  s     r=   r   zDirView.show_header_menu  s!    t//45r<   c                    | j                         }|rF|j                         r6| j                  |      }t        j                  |      r| j
                  ry|g}|D ]  }t        j                  |      r| j                  ||       +t        |      dk(  r| j                  |d         }nt        |      dkD  r| j                  |      }r| j                  |d   d          | j                  |g        y)zV
        Selected item was single/double-clicked or enter/return was pressed.
        Nr2   r   )r*  r  r  r+  r,  r   directory_clickedr8  r9  r:  rV  r   )r   r~  r@  rB  rI  s        r=   rs  zDirView.clicked  s     ,,. U]]_%%e,Eyy55#WF  	'Eyy&&ue4v;!# 66vayAE[1_ ==fEE))%(2,7IIug&	'r<   c                     t        d      )z
        Handle directories being clicked.

        Parameters
        ----------
        dirname: str
            Path to the clicked directory.
        index: QModelIndex
            Index of the directory.
        zTo be implemented by subclasses)NotImplementedErrorr   r-  r~  s      r=   r  zDirView.directory_clicked  s     ""CDDr<   c                 v    t               j                  dd       j                  t        d             t	        t        d            }|j                  d       |j                  d       t        dj                   j                  d                   t               }|j                  |       |j                          fd	} fd
}t        t        j                  t        j                  z  t        j                  z        }|j                   j#                  |       |j$                  j#                  j&                         |j)                  t        j                        j*                  j#                  |       |j                  |       j-                  |       j/                          y)zEdit name filters.i  i,  zEdit filter settingszFilter files by name, extension, or more using <a href="https://en.wikipedia.org/wiki/Glob_(programming)">glob patterns.</a> Please enter the glob patterns of the files you want to show, separated by commas.T, r  r   c                      j                         } t        |       j                  d      D cg c]  }|j                          } }j	                  |        j                          y c c}w )N,)toPlainTextr  splitstripset_name_filtersr  )filter_textfdialogfiltersr   s     r=   	handle_okz&DirView.edit_filter.<locals>.handle_ok  sW    !--/K.1+.>.D.DS.IJ1779JKJ!!+.MMO Ks   A)c                      j                  t                j                  dj                  j	                  d                   y )Nr  r  )r  r    setPlainTextjoinr  )r  r   s   r=   handle_resetz)DirView.edit_filter.<locals>.handle_reset  s1    !!,/  4==+H!IJr<   N)r   resizesetWindowTitler   r   setOpenExternalLinkssetWordWrapr   r  r  r   	addWidgetr   ResetOkCancelacceptedr   rejectedrejectbuttonrs  	setLayoutshow)r   description_labellayoutr  r  
button_boxr  r  s   `     @@r=   r  zDirView.edit_filter  sb    c3a 678 # 3 45
 	..t4%%d+DIIdmmN&CD#')*+!		K
 &&6&<&<&6&9&9':&6&=&='> ?
 	##I.##FMM2*00199AA,O$ r<   c                     || j                         }|D ]Z  }t        j                  |      r1t        j                  |      r| j
                  j                  |       I| j                  |g       \ y)z+Open files with the appropriate applicationN)r*  r+  r/  r&   r0  sig_open_file_requestedemitopen_outside_spyderr   r@  rB  s      r=   r   zDirView.open  sc     >002F 	2Ezz% X%:%:5%A,,11%8((%1		2r<   c                    t         j                  j                  |      s&t         j                  j                  |      s|di}d}ni }|r&| j	                         }t        j                  ||      }| j                  |       y)z2Open files with given application executable path.r2   N)rW  rX  r,  r/  r*  r)   open_files_with_applicationcheck_launch_error_codes)r   app_pathreturn_codesr@  s       r=   rV  zDirView.open_association  sk     h'277>>(+C$a=LHL002F#??@FHL%%l3r<   c                 Z    || j                         }|D ]  }| j                  |g        y)z#Open files with default applicationN)r*  r  r  s      r=   r   zDirView.open_external  s6     >002F 	.E$$eW-	.r<   c                     t        |      D ]L  }t        |      }t        |      }|rt        j                  |      s2| j
                  j                  |       N y)z
        Open file outside Spyder with the appropriate application.

        If this does not work, opening unknown file in Spyder, as text file.
        N)sortedr,   r.   r&   r0  r  r  )r   r@  rX  oks       r=   r  zDirView.open_outside_spyder  sN     6N 	8DD>DD!B(//5,,11$7		8r<   c                 \   t        j                  |      r=	 t        j                  |t        j
                         t        j                  |      r<yy# t        $ rN}t        |      j                  dk(  r,t        |j                        }t        j                  |d       Y d}~jd}~ww xY w)z_
        Remove whole directory tree

        Reimplemented in project explorer widget
        )onerrorOSErrorT)ignore_errorsN)r+  existsshutilrmtreer(   r  rY  typer5   r  r  )r   r-  e
error_paths       r=   remove_treezDirView.remove_tree*  sz     jj!Bgt||< jj!  B 7##y0!$QZZJMM*DABs   %A 	B+AB&&B+c                 n   |rDt         j                  t         j                  z  t         j                  z  t         j                  z  }n!t         j                  t         j                  z  }|~t        j
                  | t        d      t        d      t        j                  |      z  |      }|t         j                  k(  r|S |t         j                  k(  ry|t         j                  k(  rd}	 t        j                  |      r2t        j                  |       | j                  j                  |       |S | j                  |       | j                  j                  |       |S # t         $ rM}t        d      }t        j"                  | t        d      t        d      ||t%        |      fz         Y d}~yd}~ww xY w)	zDelete fileNrg   z'Do you really want to delete <b>%s</b>?FTr   zProject Explorerz9<b>Unable to %s <i>%s</i></b><br><br>Error message:<br>%s)r   YesYesToAllNor  warningr   r+  basenamer/  r(   remove_filesig_removedr  r  sig_tree_removedEnvironmentErrorcriticalr  )r   rB  multiple
yes_to_allbuttonsanswererror
action_strs           r=   delete_filezDirView.delete_file:  s|   ")=)=="~~&(3(:(:;G "oo6G ((ak; ll5)*+24F '!!;---;///!
	7zz%   '  %%e,    '%%**51 	78J  a*+M !5#e*567 7 	7s    )AE 0-E 	F4'AF//F4c                 ^   || j                         }t        |      dkD  }d}|D ]  }t        j                  |d      }t        j                  |      r?t        j
                  |      r*t        j                  | t        d      t        d             m| j                  |||      }||r y y)zDelete filesNr2   z.spyprojectzFile ExplorerzThe current directory contains a project.<br><br>If you want to delete the project, please go to <b>Projects</b> &raquo; <b>Delete Project</b>)
r*  r8  r+  r  r,  r  r   informationr   r  )r   r@  r  r  rB  spyproject_paths         r=   r   zDirView.delete\  s     >002Fv;?
 	E!hhum<OyyCJJ$?''!O, F GH "--eXzJ
)*	r<   c           
         t        j                  | t        d      t        d      t        j                  t        j                  |            \  }}|r=t        j                  t        j                  |      t        |            }||k(  ryt        j                  |      rt        j                  | t        d      t        d      t        j                  |      t        j                  |      fz  t        j                  t        j                  z        }|t        j                  k(  ry	 t        j                   ||       t        j"                  |      r| j$                  j'                  ||       |S | j(                  j'                  ||       |S y# t*        $ rT}t        j,                  | t        d      t        d      t        j                  |      t        |      fz         Y d}~yd}~ww xY w)zRename filerh   z	New name:NzQDo you really want to rename <b>%s</b> and overwrite the existing file <b>%s</b>?zB<b>Unable to rename file <i>%s</i></b><br><br>Error message:<br>%s)r   getTextr   r   Normalr+  r  r  r-  r  r  r   r  r
  r  r(   rename_filer/  sig_renamedr  sig_tree_renamedr  r  )r   rB  rX  validr  r  s         r=   r!  zDirView.rename_fileq  s   "**!H+q~y/?/?LL!e 88CKK.D	:Du}zz$$,,!H+ ? <<.T0BCD  OOknn46 [^^+=  -::d#$$))%6  ))..ud;' ( $ =$$!H+ 5 <<.E
;<= ==s    )AF 2F 	G.A
G))G.c                 >    || j                         }t        |       y)z#Show file in external file explorerN)r*  r$   )r   r@  s     r=   r$   z&DirView.show_in_external_file_explorer  s     >002F&v.r<   c                     || j                         }t        |t        t        f      s|g}|D ]  }| j	                  |        y)zRename filesN)r*  
isinstancetuplelistr!  r  s      r=   r   zDirView.rename  sI     >002F&5$-0XF 	$EU#	$r<   c                 `   || j                         }t        t        j                  |d               }	 | j                  j                  d       |t        | t        d      |      }n|}| j                  j                  d       |rt        |      }||k7  rnyf|D ]B  }t        j                  |      }	 t        j                  |t        j                  ||             D y# t        $ rA}t        j                  | t        d      t        d      |t        |      fz         Y d}~d}~ww xY w)zMove files/directoriesNr   TFzSelect directoryErrorz;<b>Unable to move <i>%s</i></b><br><br>Error message:<br>%s)r*  r"   r+  r-  sig_redirect_stdio_requestedr  r   r   r  r(   	move_filer  r  r   r  r  )r   r@  	directoryorigfolderrB  r  r  s           r=   r   zDirView.move  s$    >002Fs{{6!9-.--2259 -!./7 #--2248 T>   		2E||E*H2uchhvx&@A		2 $ 2$$!G* 5 #SZ012 22s   6*C##	D-,7D((D-c                 *   |d}t        j                  |      rt        j                  |      }t        j                  | ||t
        j                  d      \  }}|rt        j                  |t        |            }	 t        j                  |       |rHt        j                  |d      }		 t        |	d      5 }
|
j!                  t#        d             ddd       |S yy# t        $ r8}t        j                  | |t        d      |t        |      fz         Y d}~d}~ww xY w# 1 sw Y   |S xY w# t        $ r8}t        j                  | |t        d      |	t        |      fz         Y d}~yd}~ww xY w# |rt        j                  |d      }		 t        |	d      5 }
|
j!                  t#        d             ddd       n# 1 sw Y   nxY w|c cY S # t        $ r8}t        j                  | |t        d      |	t        |      fz         Y d}~w d}~ww xY ww xY w)zCreate new folderNr(  zD<b>Unable to create folder <i>%s</i></b><br><br>Error message:<br>%sz__init__.pywb#B<b>Unable to create file <i>%s</i></b><br><br>Error message:<br>%s)r+  r/  r-  r   r  r   r   r  r  rW  mkdirr  r   r  r   r   writer%   )r   current_pathtitlesubtitle
is_packagenamer$  r-  r  rB  r  s              r=   create_new_folderzDirView.create_new_folder  s   L::l#;;|4L"**4+4+;+;RAehh|SY7G7! HHWm<E	7!%. ;!GG$4S$9:;&   $ 1$$% 5 "CJ/01 11;&+ 7#,, % =  #(#e*"567 77 HHWm<E	7!%. ;!GG$4S$9:; ; ;&+ 7#,, % =  #(#e*"567 77 s   ;C )D- 5D 	D- 	D%.DE1 DE1  D*%D- *D- -	E.6.E))E.1HGF;2	G;G GH	H.H	H	HHc                     | j                         d   }t        j                  |      rt        j                  |      }t	        |      S )z Get selected dir
        If file is selected the directory containing file is returned.
        If multiple items are selected, first item is chosen.
        r   )r*  r+  r/  r-  r"   )r   selected_paths     r=   get_selected_dirzDirView.get_selected_dir  s<    
 335a8::m$KK6M}%%r<   c                 ~    || j                         }t        d      }t        d      }| j                  |||d       y)zNew folder.Nz
New folderzFolder name:Fr:  r?  r   r<  r   rA  r8  r9  s       r=   r   zDirView.new_folder  sA     ?++-G,^$wxEJr<   c           
         |d}t        j                  |      rt        j                  |      }| j                  j	                  d       t        | |||      \  }}| j                  j	                  d       |r	  ||       |S y# t        $ rA}t        j                  | t        d      t        d      |t        |      fz         Y d}~yd}~ww xY w)z2Create new file
        Returns True if successfulNr(  FTNew filer4  )r+  r/  r-  r,  r  r   r  r   r  r   r  )r   r7  r8  r  create_funcrB  
_selfilterr  s           r=   create_new_filezDirView.create_new_file  s     L::l#;;|4L))..u5+D%wOz))..t4/E"  $ /$$!J- 9  #e*-./ //s   9	B 	C7C		Cc                     || j                         }t        d       }t        d      dz   }d }| j                  ||||      }|| j                  |g       yy)rE  Nz	All filesz (*)c                     t        j                  |       d   dv rt        |        yt        | d      5 }|j	                  t        d             ddd       y# 1 sw Y   yxY w)zFile creation callbackr2   r)  r2  r(  N)r+  r=  r!   r   r6  r%   )rB  r  s     r=   rF  z%DirView.new_file.<locals>.create_func  sT    ||E"1%)@@e$%& 2!GG,R012 2 2s   AA )r?  r   rH  r   )r   rA  r8  r  rF  rB  s         r=   r   zDirView.new_file  sd     ?++-G*K.'	2 $$WeWkJIIug r<   c                 l    || j                         }|D ]  }| j                  j                  |        y)zRun Python scriptsN)r*  sig_run_requestedr  r  s      r=   r   zDirView.run!  s:     >002F 	/E""''.	/r<   c                    t        j                         }| j                  j                         || j	                         }t        |t        t        f      s|g}|D cg c]"  }|j                  t        j                  d      $ }}t        |      dkD  r>|dk(  rdj                  d |D              }ng|dk(  rbdj                  fd|D              }nG|dk(  r|d	   }n<|dk(  r7t        j                  |d	         j                  t        j                  d      }| j                  j                   j"                  }|d
k(  r|dk(  rj%                         D cg c]  }|j'                  d       }}|D cg c]4  }dj                  |j'                  d      j)                  d      dd       6 }}t        |      dkD  rdj                  d |D              }n|d	   }|j+                  |j,                         yc c}w c c}w c c}w )z;Copy absolute or relative path to given file(s)/folders(s).N/r2   absolutez,
c              3   ,   K   | ]  }d |z   d z     yw"Nr;   .0_fns     r=   	<genexpr>z$DirView.copy_path.<locals>.<genexpr>4  s     ,OS3Y_,O   relativec              3      K   | ]>  }d t        j                  |      j                  t        j                  d      z   d z    @ yw)rR  rN  N)r+  relpathreplacerW  sep)rT  rU  explorer_dirs     r=   rV  z$DirView.copy_path.<locals>.<genexpr>6  sJ      -@ 25 .1-0[[l-K-4WRVVS-A.BDG.H -@s   AAr   ProjectExplorerWidgetz,"c              3   ,   K   | ]  }d |z   d z     ywrQ  r;   rS  s     r=   rV  z$DirView.copy_path.<locals>.<genexpr>G  s      ->S3Y_ ->rW  mode)r   r4  r   rootPathr*  r'  r(  r)  r[  rW  r\  r8  r  r+  rZ  r   r   r5   
splitlinesr  r  setText	Clipboard)	r   r@  methodcbrU  clipboard_filescopied_fromrX  r]  s	           @r=   	copy_pathzDirView.copy_path)  s   ##%||,,.>002F&5$-0XF6<=s#++bffc*==v;?#"'**,O,O"O:%"'** -@ 9?-@ #@
 #"():%#&;;vay,#G#*72663#7  ll,,5511f
6J.99;=Dtzz$/ =O = (78#  #xx

3(=(=c(B12(FG 8O 8?#a'"'** ->-<-> #> #2!"4


?
65 >"=8s   'G:G?59Hc                 (    | j                  d       y)z@Copy absolute paths of named files/directories to the clipboard.rO  rf  Nrj  r   s    r=   r  zDirView.copy_absolute_pathM       	j)r<   c                 (    | j                  d       y)z@Copy relative paths of named files/directories to the clipboard.rX  rl  Nrm  r   s    r=   r  zDirView.copy_relative_pathR  rn  r<   c           	         || j                         }t        |t        t        f      s|g}	 t	               }|j                  |D cg c]  }t        j                  |       c}       t        j                         }|j                  ||j                         yc c}w # t        $ r?}t        j                  | t        d      t        d      t!        |      z          Y d}~yd}~ww xY w)z%Copy file(s)/folders(s) to clipboard.Nr`  zFile/Folder copy errorz?Cannot copy this type of file(s) or folder(s). The error was:

)r*  r'  r(  r)  r   r  r
   fromLocalFiler   r4  r  re  rY  r   r  r   r  )r   r@  file_contentrU  rg  r  s         r=   r   zDirView.copy_file_clipboardW  s     >002F&5$-0XF		=$;L  V!Lc$"4"4S"9!LM'')BNN<bllN; "M  	=  a01 2 358V<= =	=s)   B  B"8B  B   	C()5C##C(c           
      	   || j                         }t        |t        t        f      s|g}t	        |      dk\  r5	 t        j                  |      }t        j                  |      rt        j                  |      }nt        j                  |      }t        j                         j                         }|j                         r|j!                         }|D ]k  }|j#                         }t        j$                  |      }t        j                  |      r	 |t'        j(                  |      v rt        j*                  |      \  }	}
t-        j.                  d|	      }|rt1        |j3                               dz   }nd}t-        j4                  dd|	      }|t7        |      z  }||
z   }t        j8                  ||      }|t'        j(                  |      v rt        j8                  ||      }t;        j<                  ||       +	 |t'        j(                  |      v rt-        j.                  d|      }|rt1        |j3                               dz   }nd}t-        j4                  dd|      }|t7        |      z   }t        j8                  ||      }|t'        j(                  |      v rt        j8                  ||      }t        jF                  |      jI                  t        jF                  |      t&        jJ                  z         r+tA        jB                  | tE        d      tE        d	             Vt;        jL                  ||       n ytA        jB                  | tE        d      tE        d             yt        j                         j                         j                         r*tA        jB                  | tE        d      tE        d             yy# t        $ rS t	        |      dkD  r<t        j                  t        j                  t        j                  |                  }n|d   }Y w xY w# t>        $ r@}tA        jB                  | tE        d      tE        d      t7        |      z          Y d}~d}~ww xY w# t>        $ r@}tA        jB                  | tE        d
      tE        d      t7        |      z          Y d}~d}~ww xY w)z?Paste file from clipboard into file/project explorer directory.Nr2   r   z\d+$r(  zError pasting filez,Unsupported copy operation. The error was:

zRecursive copyz,Source is an ancestor of destination folder.zError pasting folderzNo file in clipboardzDNo file in the clipboard. Please copy a file to the clipboard first.z
Blank areazCannot paste in the blank area.)'r*  r'  r(  r)  r8  r+  
commonpathrm  r  r-  commonprefixr/  r   r4  r5  r6  urlstoLocalFiler  rW  listdirr=  researchintgroupsubr  r  r  copyrY  r   r  r   realpath
startswithr\  copytree)r   r@  selected_itemparent_pathcb_datarv  urlsource_name	base_namefile_no_extfile_ext
end_number
new_numberleft_stringdestinationr  s                   r=   r  zDirView.save_file_clipboardi  s    >002F&5$-0XFv;!. #v 6 zz-(!kk-8!ll=9",,.779G ||~ 3EC"%//"3K #[ 9Izz+.;"+rzz+/F"F8;Y8O 5X-/YYw-L
#-14Z5E5E5G1H11LJ12J.0ffWb+.N +s: >,7(,B	.1hh{I.N #,rzz+/F"F /2hh{I.N"KK[AE"+rzz+/F"F-/YYw	-J
#-14Z5E5E5G1H11LJ12J.0ffWb).L,7#j/,I	.1hh{I.N #,rzz+/F"F /2hh{I.N"||K8CC$'LL$=$F H + 4 4T567G5H56 8B 6C!D
 !)"OOKE[3Ej $$T1-C+D%& (I &JK %%'002::<$$T1\?%&'H%IK W " .v;?$'LLKK(8(8(@A%CM %+1IM.B  ) ;'00q9M7N12 4J 2K36q62:; ;;6  ) E'00123I1J12 4: 2;=@V2DE EEsW   O B=P*,P*B Q6#BQ6,Q6AP'&P'*	Q335Q..Q36	R??5R::R?c                 ~    || j                         }t        |      D ]  }| j                  j                  |        y)zOpen interpreterN)r*  r  sig_open_interpreter_requestedr  )r   r@  rX  s      r=   r  zDirView.open_interpreter  s>     >002F6N 	;D//44T:	;r<   c                     || j                  d      }| j                  r| j                  j                  |       y| j                  j                  g        y)z/Filter files given the defined list of filters.Nr  )r  r   r   setNameFiltersr   r  s     r=   r"  zDirView.filter_files  sA    ==8L>>LL''5LL''+r<   c                     g }|D ]$  }| j                  |      }|j                  |       & t        |d         }t        dt	        |            D ]  }|j                  ||         } t        t        |            S )zV
        Return the list of common matching file associations for all fnames.
        r   r2   )r9  r[  setranger8  intersectionr)  r  )r   r@  
all_valuesrB  valuescommonr~  s          r=   r:  z$DirView.get_common_file_associations  s     
 	&E//6Ff%	& Z]#1c*o. 	<E((E):;F	<F6N##r<   c                    | j                  di       j                         D ]T  \  }}|j                  d      D cg c]  }|j                          }}|D ]  }|j	                  ||dd f      s|}  |S  V g }|S c c}w )z:Return the list of matching file associations for `fname`.file_associationsr  r2   N)r  itemsr  r  endswith)r   rB  extsr  ext
clean_extss         r=   r9  zDirView.get_file_associations  s     MM*=rBHHJ 
	LD&15CA##))+AJA! >>3AB.1#F  
 
	 F Bs   A<c                    |j                         D cg c]  \  }}|dk7  s| }}}|rt        |      dk(  rt        d      }nt        d      }|t        |      dk(  rdndz  }|D ]9  }t        |      dk(  r|dj                  |      z  }&|dj                  |      z  }; |t        |      dk(  rd	nd
z  }t	        j
                  | d|t        j                         t        |       S c c}}w )z;Check return codes and display message box if errors found.r   r2   z2The following command did not launch successfully:z3The following commands did not launch successfully:z<br><br>z<br><br><ul>z<code>{}</code>z<li><code>{}</code></li>r(  z</ul>Application)r  r8  r   formatr   r  r  bool)r   r  cmdcodeerrorsmsgr  s          r=   r  z DirView.check_launch_error_codes  s    '3'9'9';I)#ttqy#II6{aLMMNV!1:~EC Dv;!#,33E::C5<<UCCC	D
 V)2w6CmS+..I<# Js
   C*C*c           	         | j                         }t        j                  |d         r|d   }nt        j                  |d         }	 t	        |      D ]  }t        j                  ||        y# t
        j                  $ rr}t        d      |j                  dj                  |j                        fz  }t        j                  | t        d      t        d      t        |      z         Y d}~yd}~ww xY w)zVCS action (commit, browse)r   zMFor %s support, please install one of the<br/> following tools:<br/><br/>  %sr  r+  z1<b>Unable to find external program.</b><br><br>%sN)r*  r+  r,  r-  r  r*   run_vcs_toolActionToolNotFoundr   vcsnamer  toolsr   r  r  )r   rK  r@  r-  rX  r  r  s          r=   r   zDirView.vcs_command  s    ,,. 99VAYQiGkk&),G
	 v 2  &12%% 	  5 6 ==$))EKK*@ABC   ajI #h   		 s   &A/ /C4A(C//C4c                 z    | j                         j                         | j                         j                         fS )zReturn scrollbar positions)horizontalScrollBarr%  verticalScrollBarr   s    r=   get_scrollbar_positionzDirView.get_scrollbar_position$  s6    ((*002&&(..02 	2r<   c                 ~    || _         | j                  *t        | j                        dk(  r| j                          yyy)zSet scrollbar positionsNr   )r   r   r8  restore_scrollbar_positions)r   positions     r=   set_scrollbar_positionzDirView.set_scrollbar_position)  s?     %-!)c$2D2D.E.J,,. /K)r<   c                     | j                   \  }}| j                         j                  |       | j                         j                  |       y)z/Restore scrollbar positions once tree is loadedN)r   r  setValuer  )r   horvers      r=   r  z#DirView.restore_scrollbar_positions0  s?    ,,S  "++C0 ))#.r<   c                 :    | j                          | j                  S )zReturn expanded state)save_expanded_stater   r   s    r=   get_expanded_statezDirView.get_expanded_state6  s      "$$$r<   c                 2    || _         | j                          y)zSet expanded stateN)r   restore_expanded_state)r   states     r=   set_expanded_statezDirView.set_expanded_state;  s     %##%r<   c                     | j                         }|Yg | _        |j                         D ]>  }| j                  |      s| j                  j	                  | j                  |             @ yy)zSave all items expanded stateN)modelr   persistentIndexList
isExpandedr[  r  )r   r  r  s      r=   r  zDirView.save_expanded_state@  sg    

 $&D!002 I??3'))001B1B31GHI r<   c                    t        j                  t        |            }t        j                  |      syt	        j
                  |      D ]  }t        j                  t        j                  ||            }t        j                  |      sB|| j                  v sQ| j                  j                  | j                  j                  |             | j                  g | _        | j                  j                  |       | j                  | j                  |      d        | j                  s0| j                  j                   j#                  | j$                         yy)z Restore directory expanded stateNT)r+  r  r  r  rW  rx  r  r,  r   popr~  r   r[  setExpandedr  r   r  rZ  restore_directory_state)r   rB  rootr  rX  s        r=   r  zDirView.restore_directory_stateK  s   ||CJ'zz$

4( 	=H<<x 89Dyy44+@+@#@%%))$*?*?*E*Ed*KL%%-)+D&""))$/  !5t<	= $$LL((33,,. %r<   c                    | j                   yt        j                  t        |            }|| j                   v r| j                   j	                  |       | j                   vt        | j                         dk(  r]| j                  j                  j                  | j                         | j                  !t        j                  d| j                         yyyy)z(Follow directories loaded during startupNr   2   )r   r+  r  r  remover8  r   r  rZ  follow_directories_loadedr   r	   
singleShotr  )r   rB  rX  s      r=   r  z!DirView.follow_directories_loaded]  s    %||CJ'4%%%%%d+)c$2D2D.E.JLL((33..0((4!!"d&F&FG 5 /K)r<   c                    | j                   zt        | j                   t              r_| j                  j                  j                  | j                         | j                  j                  j                  | j                         yyy)z Restore all items expanded stateN)r   r'  r)  r   r  r   r  r  r   s    r=   r  zDirView.restore_expanded_statek  sl      , $//6,,44002,,44224 7 -r<   c                 *    |s| j                          yy)zSet single click to open items.N)unsetCursorr   r%  s     r=   r$  z DirView.set_single_click_to_openx  s      r<   c                 (    | j                  d|       y)z!Set file associations open items.r  N)set_confr  s     r=   set_file_associationszDirView.set_file_associations~  s    )51r<   c                 x    | j                  d      dgk(  r| j                  dg        y| j                  d|       y)zSet name filtersr  r(  N)r  r  r  s     r=   r  zDirView.set_name_filters  s1    ==(RD0MM."-MM.,7r<   c                 l   t         j                  t         j                  z  t         j                  z  t         j                  z  }|rTt         j                  t         j                  z  t         j                  z  t         j                  z  t         j
                  z  }| j                  j                  |       y)z Toggle 'show hidden files' stateN)r   AllDirsFilesDrivesNoDotAndDotDotHiddenr   	setFilter)r   r  r  s      r=   r#  zDirView.set_show_hidden  su    <<$**,t{{:&&'||djj04;;>**+-1[[9Gw'r<   c                 L    | j                   j                  t        |              y)zgReset file system model icon provider
        The purpose of this is to refresh files/directories iconsN)r   setIconProviderr#   r   s    r=   r  zDirView.reset_icon_provider  s     	$$\$%78r<   c           	      
   	 t               j                  |      d   }| j                  j                  |       y# t        $ r?}t        j                  | t        d      t        d      t        |      z          Y d}~yd}~ww xY w)z8Convert an IPython notebook to a Python script in editorr   zConversion errorz=It was not possible to convert this notebook. The error is:

N)	r>  from_filenamerY  r   r  r   r  sig_file_createdr  )r   rB  scriptr  s       r=   convert_notebookzDirView.convert_notebook  s}    	\//6q9F 	""6*  	  a*+ 0 136q6:; 	s   : 	B5A==Bc                     | j                         }t        |t        t        f      s|g}|D ]  }| j	                  |        y)z5Convert IPython notebooks to Python scripts in editorN)r*  r'  r(  r)  r  r  s      r=   r  zDirView.convert_notebooks  sD     ,,.&5$-0XF 	)E!!%(	)r<   c                 ~    || j                         }t        d       }t        d      }| j                  |||d       y)zNew packageNzPackage name:TrA  rB  rC  s       r=   r   zDirView.new_package  sB     ?++-G- _%wxDIr<   c                      | j                         }t        d       }t        d      dz   } fd} j                  ||||       y)z
New moduleNzPython filesz (*.py *.pyw *.ipy)c                 <    j                   j                  |        y rQ  )sig_module_createdr  )rB  r   s    r=   rF  z'DirView.new_module.<locals>.create_func  s    ##((/r<   )r?  r   rH  )r   rA  r8  r  rF  s   `    r=   r   zDirView.new_module  sL     ?++-G,N#$99	0 	WeWkBr<   c                      y rQ  r;   r   s    r=   rw  zDirView.go_to_parent_directory  s    r<   rQ  )NN)NrO  )`r5   r6   r7   __doc__r   r  r  r  r  r  r,  r  r"  rL  r  r#  r  r   r  r   r&  r  r;  r   r  ra  rg  rn  rz  r  r  r  r  r  r  r  r   r  r   r  r  r*  r  r   rs  r  r   r  r   rV  r   r  r  r  r   r!  r$   r   r   r<  r?  r   rH  r   r   rj  r  r  r   r  r  r"  r:  r9  r  r   r  r  r  r  r  r  r  r  r  r$  r  r  r#  r  r  r  r   r   rw  r   r   s   @r=   r   r      s   ( c{ &,C["   $*$<  +K c"K	 s c{ c3'	 %Sk.$d_CB	  4 5151@ED4p *,):): 04 	1.:'":&0D2
$	#C
,
*6'>E 
V( (T 
V2 2 
V4 4 
V. .
8B  D 
V (=B 
V/ / 
V$ $ 
V2 2<7@& 
VK K/* 
V ( 
V/ /"7H 
V* * 
V* * 
V= =" 
VT Tl 
V; ;,$$ . 22
//%
&
	I.$H	428(9

+ 
V) ) 
VJ J 
VC Cr<   r   c                        e Zd ZdZ ee      Z	 d fd	Z fdZ fdZ	d Z
d Zd ZddZd	 Z e       d
        Z e       d        Z e       d        Zd ZddZ xZS )ExplorerTreeWidgetz.
    File/directory explorer tree widget.
    c                     t         |   |       || _        d| _        d| _        g | _        d| _        | j                  d       y)zInitialize the widget.

        Parameters
        ----------
        parent: PluginMainWidget, optional
            Parent widget of the explorer tree widget.
        r  NT)r   r   r    _ExplorerTreeWidget__last_folder(_ExplorerTreeWidget__original_root_indexhistory	histindexsetDragEnabled)r   r   r   s     r=   r   zExplorerTreeWidget.__init__  sL     	' !%)" 	D!r<   c                    t         |           | j                  t        j                  t        d      | j                  d      | j                        | _        | j                  t        j                  t        d      | j                  d      | j                        | _        | j                  t        j                  t        d      | j                  d      | j                         | j                  t        j                  dt        j                   d	      | j"                  
      | _        | j$                  j'                  d       y)z2
        Perform the setup of the widget.
        r   previousr   r   nextr   upr(  r   )r   r   r   TN)r   r  r   r   r   r   r   go_to_previous_directoryr   r   go_to_next_directoryr   r   rw  r   r'   r   change_filter_statefilter_buttonsetCheckabler   r   s    r=   r  zExplorerTreeWidget.setup  s    	  $11%..:!!*-33	  2  
  --%**6!!&)//	 . 
 	%,,8!!$'11	 	 	
 "//%22(#,,	 0 
 	''-r<   c                 "    t         |           y)zUpdate the widget actions.N)r   r  r  s    r=   r  z!ExplorerTreeWidget.update_actions  s     r<   c                     | j                    | _         | j                  j                  | j                          | j                  j                  t	        d             | j                          y)z&Handle the change of the filter state.zFilter filenamesN)r   r  
setCheckedr  r   r"  r   s    r=   r  z&ExplorerTreeWidget.change_filter_state  sN    !^^+%%dnn5%%a(:&;<r<   c                 l    | j                   j                  |      }|| _        | j                  |       |S )z
        Set current folder and return associated model index

        Parameters
        ----------
        folder: str
            New path to the selected folder.
        )r   setRootPathr  setRootIndex)r   r0  r~  s      r=   set_current_folderz%ExplorerTreeWidget.set_current_folder&  s3     ((0#% r<   c                     | j                   S rQ  )r  r   s    r=   get_current_folderz%ExplorerTreeWidget.get_current_folder4  s    !!!r<   c                    |
t               }|r3| j                  |      }| j                  |       | j                  |       | j                  j                  d       | j                  j                  d       | j                  g| j                  j                  | j                  dkD         | j                  j                  | j                  t        | j                        dz
  k         yy)a   
        Refresh widget

        Parameters
        ----------
        new_path: str, optional
            New path to refresh the widget.
        force_current: bool, optional
            If False, it won't refresh widget if path has not changed.
        NFr   r2   )
r+   r  expandsetCurrentIndexr   
setEnabledr   r  r8  r  )r   new_pathforce_currentr~  s       r=   refreshzExplorerTreeWidget.refresh7  s     %'H++H5EKK  '''.##E*>>%  ++DNNQ,>?''T\\9JQ9N(NO &r<   c                 .    |r| j                  |       y y )N)r.  )chdirr  s      r=   r  z$ExplorerTreeWidget.directory_clickedQ  s    JJJ) r<   c                     | j                  t        j                  t        j                  t	               t
        j                                     y)zGo to parent directoryN)r  r+  abspathr  r+   rW  pardirr   s    r=   rw  z)ExplorerTreeWidget.go_to_parent_directoryV  s+     	

3;;sxx(8"))DEFr<   c                 R    | xj                   dz  c_         | j                  d       y)zBack to previous directoryr2   Tbrowsing_historyNr  r  r   s    r=   r  z+ExplorerTreeWidget.go_to_previous_directory[        	!

D
)r<   c                 R    | xj                   dz  c_         | j                  d       y)zReturn to next directoryr2   Tr  Nr  r   s    r=   r  z'ExplorerTreeWidget.go_to_next_directorya  r  r<   c                     	 t        j                  t        |            }|| j                  v r!| j                  j	                  |      | _        yy# t        $ r  t               }| j                  |d       Y yw xY w)z
        Update browse history.

        Parameters
        ----------
        directory: str
            The new working directory.
        T)r.  r  N)	r+  r  r  r  r~  r  rY  r   r  )r   r.  user_directorys      r=   update_historyz!ExplorerTreeWidget.update_historyg  si    	HC	N3IDLL(!%!3!3I!> ) 	H)^NJJ$JG	Hs   AA &A98A9c                    |t        j                  t        |            }|r| j                  | j                     }n|| j                  v r!| j                  j                  |      | _        n| j                  g | _        n!| j                  d| j                  dz    | _        t        | j                        dk(  s| j                  r-| j                  d   |k7  r| j                  j                  |       t        | j                        dz
  | _        t        |      }	 t        j                  |       | j                  |d       |r| j                  j                  |       yy# t        $ r- t        j                  | j                   dt#        d             Y yt$        $ r( | j                  j'                  | j                         Y yw xY w)	a  
        Set directory as working directory.

        Parameters
        ----------
        directory: str
            The new working directory.
        browsing_history: bool, optional
            Add the new `directory`to the browsing history. Default is False.
        emit: bool, optional
            Emit a signal when changing the working directpory.
            Default is True.
        Nr2   r   r  T)r  r  r+  z;You don't have the right permissions to open this directory)r+  r  r  r  r  r~  r8  r[  rW  r  r  sig_dir_openedr  PermissionErrorr   r  r   r   FileNotFoundErrorr  )r   r.  r  r  s       r=   r  zExplorerTreeWidget.chdirx  si     C	N3IT^^4I$,,&!\\//	:DN~~%!#||,=T^^A-=>4<< A%b!1Y!>##I. .q0DN	N		-HHYLL)4L@##((3  	;  w!" $9 ":; ! 	- LLT^^,	-s   AE" "3G-GGrQ  )NF)NFT)r5   r6   r7   r  r   r  r  r   r  r  r  r  r  r  r  r   rw  r  r  r  r  r   r   s   @r=   r  r    s     C[N",!.F!"P4*
 
VG G 
V* *
 
V* *
H"+-r<   r  )Rr  rW  os.pathrX  r+  ry  r  r  qtpyr   qtpy.compatr   r   qtpy.QtCorer   r   r   r	   r
   r   r   
qtpy.QtGuir   qtpy.QtWidgetsr   r   r   r   r   r   r   r   r   r   r   r   r   r   spyder.api.config.decoratorsr   spyder.api.translationsr   spyder.api.widgets.mixinsr   spyder.config.baser   spyder.config.mainr    %spyder.plugins.explorer.widgets.utilsr!   r"   r#   r$   spyder.py3compatr%   spyder.utilsr&   spyder.utils.icon_managerr'   r(   r)   r*   spyder.utils.miscr+   spyder.utils.qthelpersr,   r-   r.   	nbconvertr/   r>  r1   r?   rC   ru   r   r   r   r   r   r   r  r;   r<   r=   <module>r2     s   % 
  	  
  = G G G > > > > 8 % 7 + +J J - ! ) , , ,7 76   D    " "
/ /! !D; D&mi* m`1Y- Y-s4Js   6D/ /D5