Skip to content

Commit

Permalink
Merge pull request #3622 from ccordoba12/fix-cwd-toolbar
Browse files Browse the repository at this point in the history
Fix connection between the IPython Console and the Working Directory toolbar
  • Loading branch information
ccordoba12 authored Oct 31, 2016
2 parents 09f385d + 01d5ab5 commit 6fbed96
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
27 changes: 17 additions & 10 deletions spyder/plugins/ipythonconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
to_text_string)
from spyder.utils.qthelpers import create_action
from spyder.utils import icon_manager as ima
from spyder.utils import programs
from spyder.utils import encoding, programs
from spyder.utils.misc import (add_pathlist_to_PYTHONPATH, get_error_match,
get_python_executable, remove_backslashes)
from spyder.widgets.findreplace import FindReplace
Expand Down Expand Up @@ -769,15 +769,15 @@ def register_plugin(self):
self.editor = self.main.editor

self.focus_changed.connect(self.main.plugin_focus_changed)

if self.editor is not None:
self.edit_goto.connect(self.editor.load)
self.edit_goto[str, int, str, bool].connect(
lambda fname, lineno, word, processevents:
self.editor.load(fname, lineno, word,
processevents=processevents))
self.editor.run_in_current_ipyclient.connect(
self.run_script_in_current_client)
self.edit_goto.connect(self.editor.load)
self.edit_goto[str, int, str, bool].connect(
lambda fname, lineno, word, processevents:
self.editor.load(fname, lineno, word,
processevents=processevents))
self.editor.run_in_current_ipyclient.connect(
self.run_script_in_current_client)
self.main.workingdirectory.set_current_console_wd.connect(
self.set_current_client_working_directory)

#------ Public API (for clients) ------------------------------------------
def get_clients(self):
Expand Down Expand Up @@ -837,6 +837,13 @@ def run_script_in_current_client(self, filename, wdir, args, debug,
"<br><br>Please open a new one and try again."
) % osp.basename(filename), QMessageBox.Ok)

def set_current_client_working_directory(self, directory):
"""Set current client working directory."""
shellwidget = self.get_current_shellwidget()
if shellwidget is not None:
directory = encoding.to_unicode_from_fs(directory)
shellwidget.set_cwd(directory)

def execute_code(self, lines):
sw = self.get_current_shellwidget()
if sw is not None:
Expand Down
11 changes: 6 additions & 5 deletions spyder/plugins/workingdirectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,13 @@ def select_directory(self):
def previous_directory(self):
"""Back to previous directory"""
self.histindex -= 1
self.chdir(browsing_history=True)
self.chdir(directory='', browsing_history=True)

@Slot()
def next_directory(self):
"""Return to next directory"""
self.histindex += 1
self.chdir(browsing_history=True)
self.chdir(directory='', browsing_history=True)

@Slot()
def parent_directory(self):
Expand All @@ -317,8 +317,10 @@ def parent_directory(self):
def chdir(self, directory, browsing_history=False,
refresh_explorer=True):
"""Set directory as working directory"""
if directory:
directory = osp.abspath(to_text_string(directory))

# Working directory history management
directory = osp.abspath(to_text_string(directory))
if browsing_history:
directory = self.history[self.histindex]
elif directory in self.history:
Expand All @@ -338,8 +340,7 @@ def chdir(self, directory, browsing_history=False,
self.set_explorer_cwd.emit(directory)
self.set_as_current_console_wd()
self.refresh_findinfiles.emit()

@Slot()

def set_as_current_console_wd(self):
"""Set as current console working directory"""
self.set_current_console_wd.emit(getcwd())
4 changes: 4 additions & 0 deletions spyder/utils/ipython/spyder_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ def get_source(self, objtxt):
if valid:
return getsource(obj)

def set_cwd(self, dirname):
"""Set current working directory."""
return os.chdir(dirname)

# -- Private API ---------------------------------------------------
# --- For the Variable Explorer
def _get_current_namespace(self, with_magics=False):
Expand Down
5 changes: 5 additions & 0 deletions spyder/widgets/ipythonconsole/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ def is_running(self):
else:
return False

def set_cwd(self, dirname):
"""Set shell current working directory."""
return self.silent_execute(
"get_ipython().kernel.set_cwd(r'{}')".format(dirname))

# --- To handle the banner
def long_banner(self):
"""Banner for IPython widgets with pylab message"""
Expand Down

0 comments on commit 6fbed96

Please sign in to comment.