Skip to content

Commit

Permalink
IPython Console: Change Projects active project path handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dalthviz committed Sep 13, 2021
1 parent 0ffbc61 commit bcbc3e4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
18 changes: 15 additions & 3 deletions spyder/plugins/ipythonconsole/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,19 @@ def on_history_available(self):
history = self.get_plugin(Plugins.History)
history.add_history(get_conf_path('history.py'))

@on_plugin_available(plugin=Plugins.Projects)
def on_projects_available(self):
projects = self.get_plugin(Plugins.Projects)
widget = self.get_widget()
widget.projects_available = True
projects.sig_project_loaded.connect(
lambda:
widget.update_active_project_path(
projects.get_active_project_path()))
projects.sig_project_closed.connect(
lambda:
widget.update_active_project_path(None))

def update_font(self):
"""Update font from Preferences"""
font = self.get_font()
Expand Down Expand Up @@ -485,10 +498,9 @@ def update_working_directory(self):
"""Update working directory to console cwd."""
self.get_widget().update_working_directory()

def update_path(self, path_dict, new_path_dict, new_spyder_pythonpath):
def update_path(self, path_dict, new_path_dict):
"""Update path on consoles."""
self.get_widget().update_path(
path_dict, new_path_dict, new_spyder_pythonpath)
self.get_widget().update_path(path_dict, new_path_dict)

def set_spyder_breakpoints(self):
"""Set Spyder breakpoints into all clients"""
Expand Down
49 changes: 41 additions & 8 deletions spyder/plugins/ipythonconsole/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,13 @@ def __init__(
self.clients = []
self.filenames = []
self.mainwindow_close = False
self.spyder_pythonpath = []
self.projects_available = False
self.active_project_path = None
self.create_new_client_if_empty = True
self.css_path = self.get_conf('css_path', section='appearance')
self.run_cell_filename = None
self.interrupt_action = None
self.spyder_pythonpath = []
self.initial_conf_options = self.get_conf_options()

# Attrs for testing
Expand Down Expand Up @@ -1615,15 +1617,13 @@ def register_client(self, client, give_focus=True):
shellwidget.custom_edit_requested.connect(self.edit_file)

# Set shell cwd according to preferences
# TODO: Check dependency with Projects
cwd_path = ''
if self.get_conf(
'console/use_project_or_home_directory', section='workingdir'):
cwd_path = get_home_dir()
if (self._plugin.main.projects is not None and
self._plugin.main.projects.get_active_project()
is not None):
cwd_path = self._plugin.main.projects.get_active_project_path()
if (self.projects_available and
self.active_project_path is not None):
cwd_path = self.active_project_path
elif self.get_conf(
'startup/use_fixed_directory', section='workingdir'):
cwd_path = self.get_conf(
Expand Down Expand Up @@ -1957,6 +1957,8 @@ def connect_external_kernel(self, shellwidget):
lambda:
self._plugin.main.variableexplorer.remove_shellwidget(id(sw)))

# TODO: This will change after
# https://github.com/spyder-ide/spyder/pull/15922 gets merged
if self._plugin.main.plots is not None:
self._plugin.main.plots.add_shellwidget(sw)
kc.stopped_channels.connect(
Expand Down Expand Up @@ -2174,8 +2176,22 @@ def update_working_directory(self):
shellwidget.update_cwd()

@on_conf_change(section='main', option='spyder_pythonpath')
def update_spyder_pythonpath(self, value):
self.spyder_pythonpath = value
def update_spyder_pythonpath(self, spyder_pythonpath):
"""
Update the spyder_pythonpath value used to create kernelspecs
Parameters
----------
value : list
Path set in the spyder_pythonpath configuration. The value is the
PYTHONPATH that is used inside Spyder
Returns
-------
None.
"""
self.spyder_pythonpath = spyder_pythonpath

def update_path(self, path_dict, new_path_dict):
"""Update path on consoles."""
Expand All @@ -2184,6 +2200,23 @@ def update_path(self, path_dict, new_path_dict):
if shell is not None:
shell.update_syspath(path_dict, new_path_dict)

def update_active_project_path(self, active_project_path):
"""
Update the active project path attribute used to set the current
working directory on the shells in case a project is active
Parameters
----------
active_project_path : str
Root path of the active project if any.
Returns
-------
None.
"""
self.active_project_path = active_project_path

# ---- For execution
def execute_code_and_focus_editor(self, lines, focus_to_editor=True):
"""
Expand Down

0 comments on commit bcbc3e4

Please sign in to comment.