From 252c9adc490738c120387ac2b3bc15f4eb77081f Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Fri, 25 Dec 2015 16:37:32 -0500 Subject: [PATCH] Move add_pathlist_to_PYTHONPATH function to utils/misc --- spyderlib/utils/misc.py | 25 ++++++++++++++++++- spyderlib/widgets/externalshell/baseshell.py | 22 ++-------------- .../widgets/externalshell/pythonshell.py | 4 +-- .../widgets/externalshell/systemshell.py | 4 +-- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/spyderlib/utils/misc.py b/spyderlib/utils/misc.py index fe4c8194aec..38c3b4073a9 100644 --- a/spyderlib/utils/misc.py +++ b/spyderlib/utils/misc.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright © 2009-2010 Pierre Raybaut +# Copyright © 2009- The Spyder Developmet Team # Licensed under the terms of the MIT License # (see spyderlib/__init__.py for details) @@ -11,6 +11,8 @@ import sys import stat +from spyderlib.py3compat import is_text_string + def __remove_pyc_pyo(fname): """Eventually remove .pyc and .pyo files associated to a Python script""" @@ -19,6 +21,7 @@ def __remove_pyc_pyo(fname): if osp.exists(fname+ending): os.remove(fname+ending) + def rename_file(source, dest): """ Rename file from *source* to *dest* @@ -27,6 +30,7 @@ def rename_file(source, dest): os.rename(source, dest) __remove_pyc_pyo(source) + def remove_file(fname): """ Remove file *fname* @@ -35,6 +39,7 @@ def remove_file(fname): os.remove(fname) __remove_pyc_pyo(fname) + def move_file(source, dest): """ Move file from *source* to *dest* @@ -227,6 +232,24 @@ def get_common_path(pathlist): else: return osp.abspath(common) + +def add_pathlist_to_PYTHONPATH(env, pathlist, drop_env=False): + # PyQt API 1/2 compatibility-related tests: + assert isinstance(env, list) + assert all([is_text_string(path) for path in env]) + + pypath = "PYTHONPATH" + pathstr = os.pathsep.join(pathlist) + if os.environ.get(pypath) is not None and not drop_env: + for index, var in enumerate(env[:]): + if var.startswith(pypath+'='): + env[index] = var.replace(pypath+'=', + pypath+'='+pathstr+os.pathsep) + env.append('OLD_PYTHONPATH='+os.environ[pypath]) + else: + env.append(pypath+'='+pathstr) + + if __name__ == '__main__': if os.name == 'nt': assert get_common_path([ diff --git a/spyderlib/widgets/externalshell/baseshell.py b/spyderlib/widgets/externalshell/baseshell.py index 807e48c77f3..4555f7e3347 100644 --- a/spyderlib/widgets/externalshell/baseshell.py +++ b/spyderlib/widgets/externalshell/baseshell.py @@ -9,7 +9,6 @@ # pylint: disable=R0911 # pylint: disable=R0201 -import os import os.path as osp from time import time, strftime, gmtime @@ -22,29 +21,12 @@ LOCALE_CODEC = QTextCodec.codecForLocale() # Local imports -from spyderlib.utils.qthelpers import (create_toolbutton, create_action, +from spyderlib.utils.qthelpers import (create_toolbutton, create_action, add_actions) from spyderlib.config.base import get_conf_path, _ -from spyderlib.py3compat import is_text_string, to_text_string +from spyderlib.py3compat import to_text_string -def add_pathlist_to_PYTHONPATH(env, pathlist, drop_env=False): - # PyQt API 1/2 compatibility-related tests: - assert isinstance(env, list) - assert all([is_text_string(path) for path in env]) - - pypath = "PYTHONPATH" - pathstr = os.pathsep.join(pathlist) - if os.environ.get(pypath) is not None and not drop_env: - for index, var in enumerate(env[:]): - if var.startswith(pypath+'='): - env[index] = var.replace(pypath+'=', - pypath+'='+pathstr+os.pathsep) - env.append('OLD_PYTHONPATH='+os.environ[pypath]) - else: - env.append(pypath+'='+pathstr) - - #TODO: code refactoring/cleaning (together with systemshell.py and pythonshell.py) class ExternalShellBase(QWidget): """External Shell widget: execute Python script in a separate process""" diff --git a/spyderlib/widgets/externalshell/pythonshell.py b/spyderlib/widgets/externalshell/pythonshell.py index 1667ad725e6..694b4989562 100644 --- a/spyderlib/widgets/externalshell/pythonshell.py +++ b/spyderlib/widgets/externalshell/pythonshell.py @@ -28,8 +28,8 @@ from spyderlib.widgets.shell import PythonShellWidget from spyderlib.widgets.variableexplorer.namespacebrowser import NamespaceBrowser from spyderlib.utils.bsdsocket import communicate, write_packet -from spyderlib.widgets.externalshell.baseshell import (ExternalShellBase, - add_pathlist_to_PYTHONPATH) +from spyderlib.utils.misc import add_pathlist_to_PYTHONPATH +from spyderlib.widgets.externalshell.baseshell import ExternalShellBase from spyderlib.widgets.variableexplorer.collectionseditor import CollectionsEditor from spyderlib.py3compat import (is_text_string, to_text_string, to_binary_string) diff --git a/spyderlib/widgets/externalshell/systemshell.py b/spyderlib/widgets/externalshell/systemshell.py index 48c1453e73e..cae05e800cb 100644 --- a/spyderlib/widgets/externalshell/systemshell.py +++ b/spyderlib/widgets/externalshell/systemshell.py @@ -18,10 +18,10 @@ # Local imports from spyderlib.utils.programs import shell_split from spyderlib.config.base import _ -from spyderlib.widgets.externalshell.baseshell import (ExternalShellBase, - add_pathlist_to_PYTHONPATH) +from spyderlib.widgets.externalshell.baseshell import ExternalShellBase from spyderlib.widgets.shell import TerminalWidget from spyderlib.py3compat import to_text_string, is_text_string +from spyderlib.utils.misc import add_pathlist_to_PYTHONPATH import spyderlib.utils.icon_manager as ima