Skip to content

Commit

Permalink
Merge from 5.x: PR #17934
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed May 20, 2022
2 parents fc22be3 + d06d05f commit c953763
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
2 changes: 1 addition & 1 deletion spyder/plugins/maininterpreter/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def update_actions(self):
pass

@on_conf_change(option=['default', 'custom_interpreter', 'custom'])
def section_conf_update(self, option, value):
def on_interpreter_changed(self, option, value):
if ((option == 'default' and value) or
(option == 'custom' and not value)):
executable = get_python_executable()
Expand Down
73 changes: 41 additions & 32 deletions spyder/plugins/maininterpreter/widgets/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from spyder.api.translations import get_translation
from spyder.api.widgets.status import BaseTimerStatus
from spyder.utils.conda import get_list_conda_envs
from spyder.utils.misc import get_python_executable
from spyder.utils.programs import get_interpreter_info
from spyder.utils.pyenv import get_list_pyenv_envs
from spyder.utils.workers import WorkerManager
Expand All @@ -32,7 +31,6 @@
class InterpreterStatus(BaseTimerStatus):
"""Status bar widget for displaying the current conda environment."""
ID = 'interpreter_status'

CONF_SECTION = 'main_interpreter'

sig_open_preferences_requested = Signal()
Expand Down Expand Up @@ -72,17 +70,13 @@ def get_value(self):

if not osp.isdir(env_dir):
# Env was removed on Mac or Linux
self.set_conf('custom', False)
self.set_conf('default', True)
self.update_interpreter(get_python_executable())
self._on_interpreter_removed()
elif not osp.isfile(self._interpreter):
# This can happen on Windows because the interpreter was
# renamed to .conda_trash
if not osp.isdir(osp.join(env_dir, 'conda-meta')):
# If conda-meta is missing, it means the env was removed
self.set_conf('custom', False)
self.set_conf('default', True)
self.update_interpreter(get_python_executable())
self._on_interpreter_removed()
else:
# If not, it means the interpreter is being updated so
# we need to update its version
Expand All @@ -95,13 +89,13 @@ def get_value(self):

return self.value

# ---- Qt reimplemented
# ---- Qt reimplemented methods
def closeEvent(self, event):
self._get_envs_timer.stop()
self._worker_manager.terminate_all()
super().closeEvent(event)

# ---- Widget API
# ---- Private API
def _get_env_dir(self, interpreter):
"""Get env directory from interpreter executable."""
if os.name == 'nt':
Expand All @@ -122,6 +116,43 @@ def _get_envs(self):
pyenv_env = get_list_pyenv_envs()
return {**conda_env, **pyenv_env}

def _get_env_info(self, path):
"""Get environment information."""
path = path.lower() if os.name == 'nt' else path
try:
name = self.path_to_env[path]
except KeyError:
win_app_path = osp.join(
'AppData', 'Local', 'Programs', 'spyder')
if 'Spyder.app' in path or win_app_path in path:
name = 'internal'
elif 'conda' in path:
name = 'conda'
elif 'pyenv' in path:
name = 'pyenv'
else:
name = 'custom'
version = get_interpreter_info(path)
self.path_to_env[path] = name
self.envs[name] = (path, version)
__, version = self.envs[name]
return f'{name} ({version})'

def _on_interpreter_removed(self):
"""
Actions to take when the current custom interpreter is removed
outside Spyder.
"""
# NOTES:
# 1. The interpreter will be updated when the option changes below
# generate a change in the 'executable' one in the container.
# 2. *Do not* change the order in which these options are set or the
# interpreter won't be updated correctly.
self.set_conf('custom_interpreter', ' ')
self.set_conf('custom', False)
self.set_conf('default', True)

# ---- Public API
def get_envs(self):
"""
Get the list of environments in a thread to keep them up to
Expand All @@ -148,28 +179,6 @@ def open_interpreter_preferences(self):
"""Request to open the main interpreter preferences."""
self.sig_open_preferences_requested.emit()

def _get_env_info(self, path):
"""Get environment information."""
path = path.lower() if os.name == 'nt' else path
try:
name = self.path_to_env[path]
except KeyError:
win_app_path = osp.join(
'AppData', 'Local', 'Programs', 'spyder')
if 'Spyder.app' in path or win_app_path in path:
name = 'internal'
elif 'conda' in path:
name = 'conda'
elif 'pyenv' in path:
name = 'pyenv'
else:
name = 'custom'
version = get_interpreter_info(path)
self.path_to_env[path] = name
self.envs[name] = (path, version)
__, version = self.envs[name]
return f'{name} ({version})'

def update_interpreter(self, interpreter=None):
"""Set main interpreter and update information."""
if interpreter:
Expand Down

0 comments on commit c953763

Please sign in to comment.