Skip to content

Commit

Permalink
Add import path functionality.
Browse files Browse the repository at this point in the history
Rather than automatically updating the system paths, provide mechanism for user to do so.
  • Loading branch information
mrclary committed Nov 21, 2024
1 parent a6f7038 commit c6f8e16
Showing 1 changed file with 54 additions and 14 deletions.
68 changes: 54 additions & 14 deletions spyder/plugins/pythonpath/widgets/pathmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from spyder.api.widgets.dialogs import SpyderDialogButtonBox
from spyder.api.widgets.mixins import SpyderWidgetMixin
from spyder.config.base import _
from spyder.plugins.pythonpath.utils import check_path
from spyder.plugins.pythonpath.utils import check_path, get_system_pythonpath
from spyder.utils.environ import get_user_env, set_user_env
from spyder.utils.misc import getcwd_or_home
from spyder.utils.stylesheet import (
Expand All @@ -43,6 +43,7 @@ class PathManagerToolbuttons:
MoveToBottom = 'move_to_bottom'
AddPath = 'add_path'
RemovePath = 'remove_path'
ImportPaths = 'import_paths'
ExportPaths = 'export_paths'
Prioritize = 'prioritize'

Expand Down Expand Up @@ -170,6 +171,11 @@ def _setup_right_toolbar(self):
tip=_('Remove path'),
icon=self.create_icon('editclear'),
triggered=lambda x: self.remove_path())
self.import_button = self.create_toolbutton(
PathManagerToolbuttons.ImportPaths,
tip=_('Import from PYTHONPATH environment variable'),
icon=self.create_icon('fileimport'),
triggered=lambda x: self.import_paths())
self.export_button = self.create_toolbutton(
PathManagerToolbuttons.ExportPaths,
icon=self.create_icon('fileexport'),
Expand All @@ -186,7 +192,7 @@ def _setup_right_toolbar(self):
self.movedown_button, self.movebottom_button]
return (
[self.add_button, self.remove_button] +
self.selection_widgets + [self.export_button] +
self.selection_widgets + [self.import_button, self.export_button] +
[self.prioritize_button]
)

Expand Down Expand Up @@ -248,6 +254,23 @@ def _stylesheet(self):

return css.toString()

def _setup_system_paths(self, paths):
"""Add system paths, creating system header if necessary"""
if not paths:
return

if not self.system_header:
self.system_header, system_widget = (
self._create_header(_("System PYTHONPATH"))
)
self.headers.append(self.system_header)
self.listwidget.addItem(self.system_header)
self.listwidget.setItemWidget(self.system_header, system_widget)

for path, active in paths.items():
item = self._create_item(path, active)
self.listwidget.addItem(item)

# ---- Public methods
# -------------------------------------------------------------------------
@property
Expand Down Expand Up @@ -308,18 +331,8 @@ def setup(self):
item = self._create_item(path, active)
self.listwidget.addItem(item)

# System path
if self.system_paths:
self.system_header, system_widget = (
self._create_header(_("System PYTHONPATH"))
)
self.headers.append(self.system_header)
self.listwidget.addItem(self.system_header)
self.listwidget.setItemWidget(self.system_header, system_widget)

for path, active in self.system_paths.items():
item = self._create_item(path, active)
self.listwidget.addItem(item)
# System paths
self._setup_system_paths(self.system_paths)

# Prioritize
self.prioritize_button.setChecked(self.prioritize)
Expand Down Expand Up @@ -596,6 +609,33 @@ def remove_path(self, force=False):
# Refresh widget
self.refresh()

@Slot()
def import_paths(self):
"""Import PYTHONPATH from environment."""
current_system_paths = self.get_system_paths()
system_paths = get_system_pythonpath()

# Inherit active state from current system paths
system_paths = OrderedDict(
{p: current_system_paths.get(p, True) for p in system_paths}
)

# Remove system paths
if self.system_header:
header_row = self.listwidget.row(self.system_header)
for row in range(self.listwidget.count(), header_row, -1):
self.listwidget.takeItem(row)

# Also remove system header
if not system_paths:
self.listwidget.takeItem(header_row)
self.headers.remove(self.system_header)
self.system_header = None

self._setup_system_paths(system_paths)

self.refresh()

def move_to(self, absolute=None, relative=None):
"""Move items of list widget."""
index = self.listwidget.currentRow()
Expand Down

0 comments on commit c6f8e16

Please sign in to comment.