Skip to content

Commit

Permalink
Use signals emitted by Spyder to update plugin
Browse files Browse the repository at this point in the history
Use signals to update the widget's copy of the Spyder PYTHONPATH
  • Loading branch information
cdfredrick committed Nov 5, 2018
1 parent 6136dca commit 06d1af7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
15 changes: 14 additions & 1 deletion spyder_line_profiler/lineprofiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,16 @@ def __init__(self, parent=None):

# Initialize plugin
self.initialize_plugin()


def update_pythonpath(self):
"""
Update the PYTHONPATH used when running the line_profiler.
This function is called whenever the Python path set in Spyder changes.
It synchronizes the PYTHONPATH in the line_profiler widget with the
PYTHONPATH in Spyder.
"""
self.widget.spyder_pythonpath = self.main.get_spyder_pythonpath()

# --- SpyderPluginWidget API ----------------------------------------------
def get_plugin_title(self):
"""Return widget title."""
Expand Down Expand Up @@ -117,6 +126,10 @@ def on_first_registration(self):

def register_plugin(self):
"""Register plugin in Spyder's main window."""
# Spyder PYTHONPATH
self.update_pythonpath()
self.main.sig_pythonpath_changed.connect(self.update_pythonpath)

self.edit_goto.connect(self.main.editor.load)
self.widget.redirect_stdio.connect(self.main.redirect_internalshell_stdio)
self.main.add_dockwidget(self)
Expand Down
9 changes: 2 additions & 7 deletions spyder_line_profiler/widgets/lineprofiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,10 @@ class LineProfilerWidget(QWidget):

def __init__(self, parent):
QWidget.__init__(self, parent)
self.main = parent
if self.main is None:
# Fail gracefully if not passed the spyder MainWindow
self.main = type('main', (object,), {})
self.main.get_spyder_pythonpath = (lambda self: None)
self.main = self.main()
# Need running QApplication before importing runconfig
from spyder.plugins import runconfig
self.runconfig = runconfig
self.spyder_pythonpath = None

self.setWindowTitle("Line profiler")

Expand Down Expand Up @@ -225,7 +220,7 @@ def analyze(self, filename=None, wdir=None, args=None, pythonpath=None,
if wdir is None:
wdir = osp.dirname(filename)
if pythonpath is None:
pythonpath = self.main.get_spyder_pythonpath()
pythonpath = self.spyder_pythonpath
self.start(wdir, args, pythonpath)

def select_file(self):
Expand Down

0 comments on commit 06d1af7

Please sign in to comment.