def synchronize(self): """ Synchronize Spyder's path list with PYTHONPATH environment variable Only apply to: current user, on Windows platforms """ answer = QMessageBox.question(self, _("Synchronize"), _("This will synchronize Spyder's path list with " "<b>PYTHONPATH</b> environment variable for current user, " "allowing you to run your Python modules outside Spyder " "without having to configure sys.path. " "<br>Do you want to clear contents of PYTHONPATH before " "adding Spyder's path list?"), QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) if answer == QMessageBox.Cancel: return elif answer == QMessageBox.Yes: remove = True else: remove = False from spyder.utils.environ import (get_user_env, set_user_env, listdict2envdict) env = get_user_env() if remove: ppath = self.active_pathlist+self.ro_pathlist else: ppath = env.get('PYTHONPATH', []) if not isinstance(ppath, list): ppath = [ppath] ppath = [path for path in ppath if path not in (self.active_pathlist+self.ro_pathlist)] ppath.extend(self.active_pathlist+self.ro_pathlist) env['PYTHONPATH'] = ppath set_user_env(listdict2envdict(env), parent=self)
def synchronize(self): """ Synchronize Spyder's path list with PYTHONPATH environment variable Only apply to: current user, on Windows platforms """ answer = QMessageBox.question(self, _("Synchronize"), _("This will synchronize Spyder's path list with " "<b>PYTHONPATH</b> environment variable for current user, " "allowing you to run your Python modules outside Spyder " "without having to configure sys.path. " "<br>Do you want to clear contents of PYTHONPATH before " "adding Spyder's path list?"), QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) if answer == QMessageBox.Cancel: return elif answer == QMessageBox.Yes: remove = True else: remove = False from spyder.utils.environ import (get_user_env, set_user_env, listdict2envdict) env = get_user_env() if remove: ppath = self.pathlist+self.ro_pathlist else: ppath = env.get('PYTHONPATH', []) if not isinstance(ppath, list): ppath = [ppath] ppath = [path for path in ppath if path not in (self.pathlist+self.ro_pathlist)] ppath.extend(self.pathlist+self.ro_pathlist) env['PYTHONPATH'] = ppath set_user_env( listdict2envdict(env), parent=self )
def test_synchronize_with_PYTHONPATH(qtbot, mocker): pathmanager = setup_pathmanager(qtbot, None, pathlist=['path1', 'path2', 'path3'], ro_pathlist=['path4', 'path5', 'path6']) # Import here to prevent an ImportError when testing on unix systems from spyder.utils.environ import (get_user_env, set_user_env, listdict2envdict) # Store PYTHONPATH original state env = get_user_env() original_pathlist = env.get('PYTHONPATH', []) # Mock the dialog window and answer "Yes" to clear contents of PYTHONPATH # before adding Spyder's path list mocker.patch.object(pathmanager_mod.QMessageBox, 'question', return_value=pathmanager_mod.QMessageBox.Yes) # Assert that PYTHONPATH is synchronized correctly with Spyder's path list pathmanager.synchronize() expected_pathlist = ['path1', 'path2', 'path3', 'path4', 'path5', 'path6'] env = get_user_env() assert env['PYTHONPATH'] == expected_pathlist # Uncheck 'path2' and assert that it is removed from PYTHONPATH when it # is synchronized with Spyder's path list pathmanager.listwidget.item(1).setCheckState(Qt.Unchecked) pathmanager.synchronize() expected_pathlist = ['path1', 'path3', 'path4', 'path5', 'path6'] env = get_user_env() assert env['PYTHONPATH'] == expected_pathlist # Mock the dialog window and answer "No" to clear contents of PYTHONPATH # before adding Spyder's path list mocker.patch.object(pathmanager_mod.QMessageBox, 'question', return_value=pathmanager_mod.QMessageBox.No) # Uncheck 'path3' and assert that it is kept in PYTHONPATH when it # is synchronized with Spyder's path list pathmanager.listwidget.item(2).setCheckState(Qt.Unchecked) pathmanager.synchronize() expected_pathlist = ['path3', 'path1', 'path4', 'path5', 'path6'] env = get_user_env() assert env['PYTHONPATH'] == expected_pathlist # Restore PYTHONPATH to its original state env['PYTHONPATH'] = original_pathlist set_user_env(listdict2envdict(env))
def test_synchronize_with_PYTHONPATH(qtbot, mocker): pathmanager = setup_pathmanager(qtbot, None, pathlist=['path1', 'path2', 'path3'], ro_pathlist=['path4', 'path5', 'path6']) # Import here to prevent an ImportError when testing on unix systems from spyder.utils.environ import (get_user_env, set_user_env, listdict2envdict) # Store PYTHONPATH original state env = get_user_env() original_pathlist = env.get('PYTHONPATH', []) # Mock the dialog window and answer "Yes" to clear contents of PYTHONPATH # before adding Spyder's path list mocker.patch.object(pathmanager_mod.QMessageBox, 'question', return_value=pathmanager_mod.QMessageBox.Yes) # Assert that PYTHONPATH is synchronized correctly with Spyder's path list pathmanager.synchronize() expected_pathlist = ['path1', 'path2', 'path3', 'path4', 'path5', 'path6'] env = get_user_env() assert env['PYTHONPATH'] == expected_pathlist # Uncheck 'path2' and assert that it is removed from PYTHONPATH when it # is synchronized with Spyder's path list pathmanager.listwidget.item(1).setCheckState(Qt.Unchecked) pathmanager.synchronize() expected_pathlist = ['path1', 'path3', 'path4', 'path5', 'path6'] env = get_user_env() assert env['PYTHONPATH'] == expected_pathlist # Mock the dialog window and answer "No" to clear contents of PYTHONPATH # before adding Spyder's path list mocker.patch.object(pathmanager_mod.QMessageBox, 'question', return_value=pathmanager_mod.QMessageBox.No) # Uncheck 'path3' and assert that it is kept in PYTHONPATH when it # is synchronized with Spyder's path list pathmanager.listwidget.item(2).setCheckState(Qt.Unchecked) pathmanager.synchronize() expected_pathlist = ['path3', 'path1', 'path4', 'path5', 'path6'] env = get_user_env() assert env['PYTHONPATH'] == expected_pathlist # Restore PYTHONPATH to its original state env['PYTHONPATH'] = original_pathlist set_user_env(listdict2envdict(env))
def synchronize(self): """ Synchronize Spyder's path list with PYTHONPATH environment variable Only apply to: current user, on Windows platforms. """ answer = QMessageBox.question( self, _("Synchronize"), _("This will synchronize Spyder's path list with " "<b>PYTHONPATH</b> environment variable for the current user, " "allowing you to run your Python modules outside Spyder " "without having to configure sys.path. " "<br>" "Do you want to clear contents of PYTHONPATH before " "adding Spyder's path list?"), QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) if answer == QMessageBox.Cancel: return elif answer == QMessageBox.Yes: remove = True else: remove = False from spyder.utils.environ import (get_user_env, listdict2envdict, set_user_env) env = get_user_env() # Includes read only paths active_path = tuple(k for k, v in self.get_path_dict(True).items() if v) if remove: ppath = active_path else: ppath = env.get('PYTHONPATH', []) if not isinstance(ppath, list): ppath = [ppath] ppath = tuple(p for p in ppath if p not in active_path) ppath = ppath + active_path env['PYTHONPATH'] = list(ppath) set_user_env(listdict2envdict(env), parent=self)