Skip to content

Commit

Permalink
Update export_pythonpath
Browse files Browse the repository at this point in the history
  • Loading branch information
mrclary committed Mar 4, 2024
1 parent b6f8da4 commit 5f68078
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
55 changes: 42 additions & 13 deletions spyder/plugins/pythonpath/widgets/pathmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from spyder.api.widgets.mixins import SpyderWidgetMixin
from spyder.config.base import _
from spyder.plugins.pythonpath.utils import check_path
from spyder.utils.environ import get_user_env, set_user_env
from spyder.utils.misc import getcwd_or_home
from spyder.utils.stylesheet import (
AppStyle,
Expand All @@ -50,7 +51,6 @@ class PathManager(QDialog, SpyderWidgetMixin):

redirect_stdio = Signal(bool)
sig_path_changed = Signal(object, object, bool)
sig_export_pythonpath = Signal(object, object, bool)

# This is required for our tests
CONF_SECTION = 'pythonpath_manager'
Expand Down Expand Up @@ -332,6 +332,15 @@ def export_pythonpath(self):
"""
Export to PYTHONPATH environment variable
Only apply to: current user.
If the user chooses to clear the contents of the system PYTHONPATH,
then the active user paths are prepended to active system paths and
the resulting list is saved to the system PYTHONPATH. Inactive system
paths are discarded. If the user chooses not to clear the contents of
the system PYTHONPATH, then the new system PYTHONPATH comprises the
inactive system paths + active user paths + active system paths, and
inactive system paths remain inactive. With either choice, inactive
user paths are retained in the user paths and remain inactive.
"""
answer = QMessageBox.question(
self,
Expand All @@ -349,9 +358,24 @@ def export_pythonpath(self):
if answer == QMessageBox.Cancel:
return

self.sig_export_pythonpath(
self.get_user_paths(), self.get_system_paths(),
answer == QMessageBox.Yes
user_paths = self.get_user_paths()
active_user_paths = OrderedDict({p: v for p, v in user_paths.items() if v})
new_user_paths = OrderedDict({p: v for p, v in user_paths.items() if not v})

system_paths = self.get_system_paths()
active_system_paths = OrderedDict({p: v for p, v in system_paths.items() if v})
inactive_system_paths = OrderedDict({p: v for p, v in system_paths.items() if not v})

new_system_paths = active_user_paths | active_system_paths
if answer == QMessageBox.No:
new_system_paths = inactive_system_paths | new_system_paths

env = get_user_env()
env['PYTHONPATH'] = list(new_system_paths.keys())
set_user_env(env, parent=self)

self.update_paths(
user_paths=new_user_paths, system_paths=new_system_paths
)

def get_user_paths(self):
Expand Down Expand Up @@ -388,10 +412,10 @@ def get_system_paths(self):

def update_paths(
self,
project_paths=OrderedDict(),
user_paths=OrderedDict(),
system_paths=OrderedDict(),
prioritize=False
project_paths=None,
user_paths=None,
system_paths=None,
prioritize=None
):
"""Update path attributes.
Expand All @@ -400,10 +424,14 @@ def update_paths(
used to compare with what is shown in the listwidget in order to detect
changes.
"""
self.project_paths = project_paths
self.user_paths = user_paths
self.system_paths = system_paths
self.prioritize = prioritize
if project_paths is not None:
self.project_paths = project_paths
if user_paths is not None:
self.user_paths = user_paths
if system_paths is not None:
self.system_paths = system_paths
if prioritize is not None:
self.prioritize = prioritize

self.setup()

Expand Down Expand Up @@ -632,7 +660,8 @@ def test():
dlg.update_paths(
user_paths={p: True for p in sys.path[1:-2]},
project_paths={p: True for p in sys.path[:1]},
system_paths={p: True for p in sys.path[-2:]}
system_paths={p: True for p in sys.path[-2:]},
prioritize=False
)

def callback(user_paths, system_paths, prioritize):
Expand Down
3 changes: 2 additions & 1 deletion spyder/plugins/pythonpath/widgets/tests/test_pathmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def pathmanager(qtbot, request):
widget.update_paths(
user_paths=OrderedDict({p: True for p in user_paths}),
project_paths=OrderedDict({p: True for p in project_paths}),
system_paths=OrderedDict({p: True for p in system_paths})
system_paths=OrderedDict({p: True for p in system_paths}),
prioritize=False
)
widget.show()
qtbot.addWidget(widget)
Expand Down

0 comments on commit 5f68078

Please sign in to comment.