B
    nôb\ßI  ã               @   sÖ   d 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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mZmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" G dd„ deeƒZ#dS )zŽ
Projects Plugin

It handles closing, opening and switching among projetcs and also
updating the file tree explorer associated with a project
é    N)Úgetexistingdirectory)ÚSignalÚSlot)ÚQMenuÚQMessageBox)Ú_Úget_home_dir)ÚSpyderPluginMixin)Úis_text_string)Úencoding)Úicon_manager)Úadd_actionsÚcreate_actionÚMENU_SEPARATOR)Úgetcwd_or_home)ÚProjectExplorerWidget)ÚProjectDialog)ÚEmptyProjectc               @   s˜  e Zd ZdZdZeeƒZeƒ ZeeƒZ	eeƒZ
eeƒZeeƒZeeeƒZeeeƒZeeƒZeeƒZeeeeƒZeeƒZeeƒZeeƒZdFdd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ ZdGdd„Zdd„ Zdd„ Zdd„ Z dd„ Z!e"ƒ dd„ ƒZ#dd„ Z$dHd d!„Z%d"d#„ Z&d$d%„ Z'd&d'„ Z(d(d)„ Z)d*d+„ Z*d,d-„ Z+d.d/„ Z,d0d1„ Z-dId2d3„Z.d4d5„ Z/d6d7„ Z0d8d9„ Z1d:d;„ Z2d<d=„ Z3d>d?„ Z4d@dA„ Z5dBdC„ Z6dDdE„ Z7dS )JÚProjectszProjects pluginZproject_explorerNc             C   sh   t j| ||  d¡|  d¡|  d¡d t | |¡ | jdg d| _d | _d | _|  ¡  |  |  	¡ ¡ d S )NÚname_filtersÚshow_allÚshow_hscrollbar)Úparentr   r   r   Úrecent_projects)Údefault)
r   Ú__init__Ú
get_optionr	   r   Úcurrent_active_projectÚlatest_projectZinitialize_pluginÚsetup_projectÚget_active_project_path)Úselfr   © r"   ú6lib/python3.7/site-packages/spyder/plugins/projects.pyr   ;   s    zProjects.__init__c             C   s   t dƒS )zReturn widget titlezProject explorer)r   )r!   r"   r"   r#   Úget_plugin_titleK   s    zProjects.get_plugin_titlec             C   s   | j S )zq
        Return the widget to give focus to when
        this plugin's dockwidget is raised on top-level
        )Ú
treewidget)r!   r"   r"   r#   Úget_focus_widgetO   s    zProjects.get_focus_widgetc          
      sà   t ˆ tdƒˆ jdˆ _t ˆ tdƒ‡ fdd„dˆ _t ˆ tdƒˆ jdˆ _t ˆ tdƒˆ jdˆ _t ˆ tdƒˆ j	dˆ _
t ˆ td	ƒˆ jdˆ _ttd
ƒˆ ƒˆ _ˆ j}ˆ jdk	rÔˆ j jˆ jtˆ jˆ jˆ jtˆ j|g7  _ˆ  ¡  g S )z*Return a list of actions related to pluginzNew Project...)Ú	triggeredzOpen Project...c                s   ˆ   ¡ S )N)Úopen_project)Úv)r!   r"   r#   Ú<lambda>]   s    z-Projects.get_plugin_actions.<locals>.<lambda>zClose ProjectzDelete ProjectzClear this listzProject PreferenceszRecent ProjectsN)r   r   Úcreate_new_projectZnew_project_actionZopen_project_actionÚclose_projectÚclose_project_actionÚ_delete_projectÚdelete_project_actionÚclear_recent_projectsÚclear_recent_projects_actionÚedit_project_preferencesÚedit_project_preferences_actionr   Úrecent_project_menuÚtoggle_view_actionÚmainZprojects_menu_actionsr   Úsetup_menu_actions)r!   Zexplorer_actionr"   )r!   r#   Úget_plugin_actionsV   s:    



zProjects.get_plugin_actionsc                sr  ˆ j  ¡  ˆ j j ˆ j¡ ˆ j ˆ j j¡ ˆ j ˆ j jj¡ ˆ j ˆ j jj	¡ ˆ j
 ˆ j jj
¡ ˆ j ˆ j jj¡ ˆ j ˆ j jj¡ ˆ j ˆ j jj¡ ˆ j j ˆ ¡ ˆ j  ˆ ¡ ˆ j ˆ j j¡ ˆ j ‡ fdd„¡ ˆ j ‡ fdd„¡ ˆ j ‡ fdd„¡ ˆ j ‡ fdd„¡ ˆ j ˆ j¡ ˆ jt  ‡ fdd„¡ ˆ j ‡ fdd„¡ ˆ j ‡ fdd„¡ ˆ jj ˆ j¡ d	S )
z'Register plugin in Spyder's main windowc                s   ˆ j jj| dS )N)Útext)r6   ÚeditorÚnew)Úx)r!   r"   r#   r*   ‹   s    z*Projects.register_plugin.<locals>.<lambda>c                s   ˆ j j | ¡S )N)r6   ÚworkingdirectoryÚchdir)r)   )r!   r"   r#   r*      s    c                s
   ˆ j  ¡ S )N)r6   Úset_window_title)r)   )r!   r"   r#   r*   ‘   s    c                s   ˆ j j ¡ S )N)r6   r:   Úsetup_open_files)r)   )r!   r"   r#   r*   “   s    c                s   ˆ j j ˆ  ¡ ¡S )N)r6   r=   r>   Úget_last_working_dir)r)   )r!   r"   r#   r*   –   s   c                s
   ˆ j  ¡ S )N)r6   r?   )r)   )r!   r"   r#   r*   ™   s    c                s   ˆ j j ¡ S )N)r6   r:   r@   )r)   )r!   r"   r#   r*   ›   s    N)r6   Zpythonpath_changedÚrestore_scrollbar_positionÚconnectÚsig_pythonpath_changedÚcreate_moduler:   r;   ÚeditÚloadÚremovedÚremoved_treeÚrenamedÚrenamed_treeZset_projectsZadd_dockwidgetZsig_open_fileZ	open_fileÚsig_new_fileÚsig_project_loadedÚupdate_explorerÚsig_project_closedÚobjectr4   ZaboutToShowr7   )r!   r"   )r!   r#   Úregister_pluginz   s8    

zProjects.register_pluginc             C   s   dS )zRefresh project explorer widgetNr"   )r!   r"   r"   r#   Úrefresh_pluginž   s    zProjects.refresh_pluginFc             C   s   |   ¡  |  ¡  dS )z3Perform actions before parent main window is closedT)Úsave_configZclosing_widget)r!   Z
cancelabler"   r"   r#   Úclosing_plugin¢   s    zProjects.closing_pluginc             C   sZ   | j jdk	r,| j jjr,| j j| k	r,| j  ¡  |  d¡rV| j ¡ sL| j d¡ |  d¡ dS )zSwitch to plugin.NÚvisible_if_project_openT)	r6   Zlast_pluginZismaximizedZmaximize_dockwidgetr   r5   Z	isCheckedZ
setCheckedZvisibility_changed)r!   r"   r"   r#   Úswitch_to_plugin¨   s    



zProjects.switch_to_pluginc          	      s´   ˆ j  ¡  g ˆ _ˆ jrxbˆ jD ]X}ˆ  |¡rj| tƒ d¡}tˆ |t 	d¡|f‡ fdd„	d}ˆ j 
|¡ qˆ j |¡ qW ˆ  jdˆ jg7  _n
ˆ jgˆ _tˆ j ˆ jƒ ˆ  ¡  dS )z"Setup and update the menu actions.ú~Úprojectc                s   ˆ j |dS )N)Úpath)r(   )r   Úp)r!   r"   r#   r*   Ä   s    z-Projects.setup_menu_actions.<locals>.<lambda>)Úiconr'   N)r4   ÚclearZrecent_projects_actionsr   Úis_valid_projectÚreplacer   r   Úimar[   ÚappendÚremover1   r   Úupdate_project_actions)r!   rX   ÚnameÚactionr"   )r!   r#   r7   ·   s$    


zProjects.setup_menu_actionsc             C   sT   | j r| j d¡ n| j d¡ t|  ¡ ƒ}| j |¡ | j |¡ | j |¡ dS )z#Update actions of the Projects menuTFN)r   r1   Z
setEnabledÚboolr    r-   r/   r3   )r!   Zactiver"   r"   r#   rb   Ð   s    zProjects.update_project_actionsc             C   s:   ddl m} | jr6| jd }|| |ƒ}| ¡  | ¡  dS )z&Edit Spyder active project preferencesr   )ÚProjectPreferencesN)Z$spyder.widgets.projects.configdialogrf   Zproject_activeZproject_listÚshowÚexec_)r!   rf   Úactive_projectÚdlgr"   r"   r#   r2   Ü   s    

z!Projects.edit_project_preferencesc             C   sj   |   ¡  | j}t| ƒ}|j | j¡ |j | j¡ | ¡ rf|dkrT|  d¡rT|  	¡  | j
 ¡  |  ¡  dS )zCreate new projectNrU   )rV   r   r   Zsig_project_creation_requestedrC   Ú_create_projectÚsig_project_createdrh   r   Úshow_explorerrD   ÚemitÚrestart_consoles)r!   ri   rj   r"   r"   r#   r+   ê   s    

zProjects.create_new_projectc             C   s"   | j |d |  ¡  |  |¡ dS )zCreate a new project.)rY   N)r(   r7   Úadd_to_recent)r!   rY   r"   r"   r#   rk   ù   s    zProjects._create_projectTc             C   s>  |   ¡  |dkr`tƒ }t| tdƒ|d}t |¡}|  |¡sj|rZt | tdƒtdƒ| ¡ dS n
t |¡}|  	|¡ | j
dkrÌ|rš| jjdk	rš| jj ¡  | jjdk	r¸| jj dtƒ ¡ |  d¡rê|  ¡  n| jjdk	rê|  | jj ¡ ¡ t|ƒ| _
t|ƒ| _|  d|  ¡ ¡ |  ¡  | j |¡ | j ¡  |r:|  ¡  dS )	z"Open the project located in `path`NzOpen project)r   ZcaptionÚbasedirÚErrorz"<b>%s</b> is not a Spyder project!Úlast_working_dirrU   Úcurrent_project_path)rV   r   r   r   r   Zto_unicode_from_fsr]   r   Zcriticalrp   r   r6   r:   Zsave_open_filesÚ
set_optionr   r   rm   Úset_project_filenamesÚget_open_filenamesr   r   r    r7   rM   rn   rD   ro   )r!   rY   ro   Úsave_previous_filesrq   r"   r"   r#   r(   ÿ   sB    










zProjects.open_projectc             C   sž   | j rš|  ¡  | j j}d| _ |  dd¡ |  ¡  | j |¡ | j ¡  | jdk	rl|  d| j 	¡ ¡ | j 
¡  |  ¡  |  ¡  | jjdk	rš|  | jj ¡ ¡ dS )z`
        Close current project and return to a window without an active
        project
        Nrt   rU   )r   rV   Ú	root_pathru   r7   rO   rn   rD   Ú
dockwidgetÚ	isVisibleÚcloser\   ro   r6   r:   rv   rw   )r!   rY   r"   r"   r#   r,   -  s"    


zProjects.close_projectc             C   s   | j r|  ¡  |  ¡  dS )zDelete current project.N)r   rV   Zdelete_project)r!   r"   r"   r#   r.   H  s    zProjects._delete_projectc             C   s   g | _ |  ¡  dS )z!Clear the list of recent projectsN)r   r7   )r!   r"   r"   r#   r0   N  s    zProjects.clear_recent_projectsc             C   s   | j S )zGet the active project)r   )r!   r"   r"   r#   Úget_active_projectS  s    zProjects.get_active_projectc             C   s8   | j ddd}|r4|  |¡r4| j|ddd |  ¡  dS )zT
        Reopen the active project when Spyder was closed last time, if any
        rt   N)r   F)rY   ro   rx   )r   r]   r(   Úload_config)r!   rt   r"   r"   r#   Úreopen_last_projectW  s    
zProjects.reopen_last_projectc             C   s*   g }| j r| j  ¡ }n| jr&| j ¡ }|S )z-Get the list of recent filenames of a project)r   Zget_recent_filesr   )r!   Úrecent_filesr"   r"   r#   Úget_project_filenamesf  s    
zProjects.get_project_filenamesc             C   s$   | j r |  | j j¡r | j  |¡ dS )z,Set the list of open file names in a projectN)r   r]   ry   Zset_recent_files)r!   r€   r"   r"   r#   rv   o  s    
zProjects.set_project_filenamesc             C   s   d}| j r| j j}|S )zGet path of the active projectN)r   ry   )r!   Zactive_project_pathr"   r"   r#   r    v  s    z Projects.get_active_project_pathc             C   s2   |r| j ddd}n|  ¡ }|dkr(g S |gS dS )z4Get project path as a list to be added to PYTHONPATHrt   N)r   )r   r    )r!   Zat_startZcurrent_pathr"   r"   r#   Úget_pythonpath}  s    
zProjects.get_pythonpathc             C   s   | j jjdtƒ dS )z*Get the path of the last working directoryrs   )r   )r6   r:   r   r   )r!   r"   r"   r#   rA   ‰  s    
zProjects.get_last_working_dirc             C   sT   |   d| j¡ |   d| j ¡ ¡ |   d| j ¡ ¡ | jrP| jrP|   d| j ¡ ¡ dS )z’
        Save configuration: opened projects & tree widget state.

        Also save whether dock widget is visible if a project is open.
        r   Úexpanded_stateÚscrollbar_positionrU   N)ru   r   r%   Zget_expanded_stateZget_scrollbar_positionr   rz   r{   )r!   r"   r"   r#   rS   Ž  s    zProjects.save_configc             C   s0   |   dd¡}t|ƒrd}|dk	r,| j |¡ dS )z7Load configuration: opened projects & tree widget staterƒ   N)r   r
   r%   Zset_expanded_state)r!   rƒ   r"   r"   r#   r~   œ  s
    zProjects.load_configc             C   s$   |   dd¡}|dk	r | j |¡ dS )z9Restoring scrollbar position after main window is visibler„   N)r   r%   Zset_scrollbar_position)r!   Zscrollbar_posr"   r"   r#   rB   §  s    z#Projects.restore_scrollbar_positionc             C   s   |   |  ¡ ¡ dS )zUpdate explorer treeN)r   r    )r!   r"   r"   r#   rN   ­  s    zProjects.update_explorerc             C   s6   | j dk	r2| j  ¡ r| j  ¡  | j  ¡  | j  ¡  dS )zShow the explorerN)rz   ZisHiddenrg   Zraise_Úupdate)r!   r"   r"   r#   rm   ±  s
    



zProjects.show_explorerc             C   s   | j jdk	r| j j ¡  dS )z=Restart consoles when closing, opening and switching projectsN)r6   Z
ipyconsoleZrestart)r!   r"   r"   r#   ro   ¹  s    zProjects.restart_consolesc             C   s,   t  |d¡}t  |¡r$t  |¡r$dS dS dS )z.Check if a directory is a valid Spyder projectz.spyprojectTFN)ÚospÚjoinÚisdir)r!   rY   Zspy_project_dirr"   r"   r#   r]   ¾  s    zProjects.is_valid_projectc             C   s,   || j kr(| j  d|¡ | j dd… | _ dS )zs
        Add an entry to recent projetcs

        We only maintain the list of the 10 most recent projects
        r   Né
   )r   Úinsert)r!   rX   r"   r"   r#   rp   Æ  s    
zProjects.add_to_recent)N)F)NTT)F)8Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZCONF_SECTIONr   ÚstrZopen_interpreterrD   rE   rF   rH   rI   rJ   rK   re   Zredirect_stdioÚrunrP   rl   rM   rO   rL   r   r$   r&   r8   rQ   rR   rT   rV   r7   rb   r2   r   r+   rk   r(   r,   r.   r0   r}   r   r   rv   r    r‚   rA   rS   r~   rB   rN   rm   ro   r]   rp   r"   r"   r"   r#   r   #   sb   


$$
 
-	
r   )$rŽ   Úos.pathrY   r†   Zqtpy.compatr   Zqtpy.QtCorer   r   Zqtpy.QtWidgetsr   r   Zspyder.config.baser   r   Zspyder.pluginsr	   Zspyder.py3compatr
   Zspyder.utilsr   r   r_   Zspyder.utils.qthelpersr   r   r   Zspyder.utils.miscr   Z spyder.widgets.projects.explorerr   Z%spyder.widgets.projects.projectdialogr   Zspyder.widgets.projectsr   r   r"   r"   r"   r#   Ú<module>   s   