From eb1ece7be134bbc6684b4bfd17358294fff4f4e4 2015-03-31 21:30:37 From: Thomas Kluyver Date: 2015-03-31 21:30:37 Subject: [PATCH] Move IPython specific functions from utils to IPython.paths --- diff --git a/IPython/paths.py b/IPython/paths.py new file mode 100644 index 0000000..11f45aa --- /dev/null +++ b/IPython/paths.py @@ -0,0 +1,149 @@ +import os.path +import shutil +import tempfile +from warnings import warn + +import IPython +from IPython.utils.importstring import import_item +from IPython.utils.path import ( + get_home_dir, get_xdg_dir, get_xdg_cache_dir, compress_user, _writable_dir, + ensure_dir_exists, fs_encoding, filefind +) +from IPython.utils import py3compat + +def get_ipython_dir(): + """Get the IPython directory for this platform and user. + + This uses the logic in `get_home_dir` to find the home directory + and then adds .ipython to the end of the path. + """ + + env = os.environ + pjoin = os.path.join + + + ipdir_def = '.ipython' + + home_dir = get_home_dir() + xdg_dir = get_xdg_dir() + + # import pdb; pdb.set_trace() # dbg + if 'IPYTHON_DIR' in env: + warn('The environment variable IPYTHON_DIR is deprecated. ' + 'Please use IPYTHONDIR instead.') + ipdir = env.get('IPYTHONDIR', env.get('IPYTHON_DIR', None)) + if ipdir is None: + # not set explicitly, use ~/.ipython + ipdir = pjoin(home_dir, ipdir_def) + if xdg_dir: + # Several IPython versions (up to 1.x) defaulted to .config/ipython + # on Linux. We have decided to go back to using .ipython everywhere + xdg_ipdir = pjoin(xdg_dir, 'ipython') + + if _writable_dir(xdg_ipdir): + cu = compress_user + if os.path.exists(ipdir): + warn(('Ignoring {0} in favour of {1}. Remove {0} to ' + 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir))) + elif os.path.islink(xdg_ipdir): + warn(('{0} is deprecated. Move link to {1} to ' + 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir))) + else: + warn('Moving {0} to {1}'.format(cu(xdg_ipdir), cu(ipdir))) + shutil.move(xdg_ipdir, ipdir) + + ipdir = os.path.normpath(os.path.expanduser(ipdir)) + + if os.path.exists(ipdir) and not _writable_dir(ipdir): + # ipdir exists, but is not writable + warn("IPython dir '{0}' is not a writable location," + " using a temp directory.".format(ipdir)) + ipdir = tempfile.mkdtemp() + elif not os.path.exists(ipdir): + parent = os.path.dirname(ipdir) + if not _writable_dir(parent): + # ipdir does not exist and parent isn't writable + warn("IPython parent '{0}' is not a writable location," + " using a temp directory.".format(parent)) + ipdir = tempfile.mkdtemp() + + return py3compat.cast_unicode(ipdir, fs_encoding) + + +def get_ipython_cache_dir(): + """Get the cache directory it is created if it does not exist.""" + xdgdir = get_xdg_cache_dir() + if xdgdir is None: + return get_ipython_dir() + ipdir = os.path.join(xdgdir, "ipython") + if not os.path.exists(ipdir) and _writable_dir(xdgdir): + ensure_dir_exists(ipdir) + elif not _writable_dir(xdgdir): + return get_ipython_dir() + + return py3compat.cast_unicode(ipdir, fs_encoding) + + +def get_ipython_package_dir(): + """Get the base directory where IPython itself is installed.""" + ipdir = os.path.dirname(IPython.__file__) + return py3compat.cast_unicode(ipdir, fs_encoding) + + +def get_ipython_module_path(module_str): + """Find the path to an IPython module in this version of IPython. + + This will always find the version of the module that is in this importable + IPython package. This will always return the path to the ``.py`` + version of the module. + """ + if module_str == 'IPython': + return os.path.join(get_ipython_package_dir(), '__init__.py') + mod = import_item(module_str) + the_path = mod.__file__.replace('.pyc', '.py') + the_path = the_path.replace('.pyo', '.py') + return py3compat.cast_unicode(the_path, fs_encoding) + +def locate_profile(profile='default'): + """Find the path to the folder associated with a given profile. + + I.e. find $IPYTHONDIR/profile_whatever. + """ + from IPython.core.profiledir import ProfileDir, ProfileDirError + try: + pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile) + except ProfileDirError: + # IOError makes more sense when people are expecting a path + raise IOError("Couldn't find profile %r" % profile) + return pd.location + +def get_security_file(filename, profile='default'): + """Return the absolute path of a security file given by filename and profile + + This allows users and developers to find security files without + knowledge of the IPython directory structure. The search path + will be ['.', profile.security_dir] + + Parameters + ---------- + + filename : str + The file to be found. If it is passed as an absolute path, it will + simply be returned. + profile : str [default: 'default'] + The name of the profile to search. Leaving this unspecified + The file to be found. If it is passed as an absolute path, fname will + simply be returned. + + Returns + ------- + Raises :exc:`IOError` if file not found or returns absolute path to file. + """ + # import here, because profiledir also imports from utils.path + from IPython.core.profiledir import ProfileDir + try: + pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile) + except Exception: + # will raise ProfileDirError if no such profile + raise IOError("Profile %r not found") + return filefind(filename, ['.', pd.security_dir]) diff --git a/IPython/utils/path.py b/IPython/utils/path.py index 5ad52e3..54f320a 100644 --- a/IPython/utils/path.py +++ b/IPython/utils/path.py @@ -16,10 +16,8 @@ import glob from warnings import warn from hashlib import md5 -import IPython from IPython.testing.skipdoctest import skip_doctest from IPython.utils.process import system -from IPython.utils.importstring import import_item from IPython.utils import py3compat from IPython.utils.decorators import undoc @@ -256,111 +254,35 @@ def get_xdg_cache_dir(): return None +@undoc def get_ipython_dir(): - """Get the IPython directory for this platform and user. - - This uses the logic in `get_home_dir` to find the home directory - and then adds .ipython to the end of the path. - """ - - env = os.environ - pjoin = os.path.join - - - ipdir_def = '.ipython' - - home_dir = get_home_dir() - xdg_dir = get_xdg_dir() - - # import pdb; pdb.set_trace() # dbg - if 'IPYTHON_DIR' in env: - warn('The environment variable IPYTHON_DIR is deprecated. ' - 'Please use IPYTHONDIR instead.') - ipdir = env.get('IPYTHONDIR', env.get('IPYTHON_DIR', None)) - if ipdir is None: - # not set explicitly, use ~/.ipython - ipdir = pjoin(home_dir, ipdir_def) - if xdg_dir: - # Several IPython versions (up to 1.x) defaulted to .config/ipython - # on Linux. We have decided to go back to using .ipython everywhere - xdg_ipdir = pjoin(xdg_dir, 'ipython') - - if _writable_dir(xdg_ipdir): - cu = compress_user - if os.path.exists(ipdir): - warn(('Ignoring {0} in favour of {1}. Remove {0} to ' - 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir))) - elif os.path.islink(xdg_ipdir): - warn(('{0} is deprecated. Move link to {1} to ' - 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir))) - else: - warn('Moving {0} to {1}'.format(cu(xdg_ipdir), cu(ipdir))) - shutil.move(xdg_ipdir, ipdir) - - ipdir = os.path.normpath(os.path.expanduser(ipdir)) - - if os.path.exists(ipdir) and not _writable_dir(ipdir): - # ipdir exists, but is not writable - warn("IPython dir '{0}' is not a writable location," - " using a temp directory.".format(ipdir)) - ipdir = tempfile.mkdtemp() - elif not os.path.exists(ipdir): - parent = os.path.dirname(ipdir) - if not _writable_dir(parent): - # ipdir does not exist and parent isn't writable - warn("IPython parent '{0}' is not a writable location," - " using a temp directory.".format(parent)) - ipdir = tempfile.mkdtemp() - - return py3compat.cast_unicode(ipdir, fs_encoding) - + warn("get_ipython_dir has moved to the IPython.paths module") + from IPython.paths import get_ipython_dir + return get_ipython_dir() +@undoc def get_ipython_cache_dir(): - """Get the cache directory it is created if it does not exist.""" - xdgdir = get_xdg_cache_dir() - if xdgdir is None: - return get_ipython_dir() - ipdir = os.path.join(xdgdir, "ipython") - if not os.path.exists(ipdir) and _writable_dir(xdgdir): - ensure_dir_exists(ipdir) - elif not _writable_dir(xdgdir): - return get_ipython_dir() - - return py3compat.cast_unicode(ipdir, fs_encoding) - + warn("get_ipython_cache_dir has moved to the IPython.paths module") + from IPython.paths import get_ipython_cache_dir + return get_ipython_cache_dir() +@undoc def get_ipython_package_dir(): - """Get the base directory where IPython itself is installed.""" - ipdir = os.path.dirname(IPython.__file__) - return py3compat.cast_unicode(ipdir, fs_encoding) - + warn("get_ipython_package_dir has moved to the IPython.paths module") + from IPython.paths import get_ipython_package_dir + return get_ipython_package_dir() +@undoc def get_ipython_module_path(module_str): - """Find the path to an IPython module in this version of IPython. - - This will always find the version of the module that is in this importable - IPython package. This will always return the path to the ``.py`` - version of the module. - """ - if module_str == 'IPython': - return os.path.join(get_ipython_package_dir(), '__init__.py') - mod = import_item(module_str) - the_path = mod.__file__.replace('.pyc', '.py') - the_path = the_path.replace('.pyo', '.py') - return py3compat.cast_unicode(the_path, fs_encoding) + warn("get_ipython_module_path has moved to the IPython.paths module") + from IPython.paths import get_ipython_module_path + return get_ipython_module_path(module_str) +@undoc def locate_profile(profile='default'): - """Find the path to the folder associated with a given profile. - - I.e. find $IPYTHONDIR/profile_whatever. - """ - from IPython.core.profiledir import ProfileDir, ProfileDirError - try: - pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile) - except ProfileDirError: - # IOError makes more sense when people are expecting a path - raise IOError("Couldn't find profile %r" % profile) - return pd.location + warn("locate_profile has moved to the IPython.paths module") + from IPython.paths import locate_profile + return locate_profile(profile=profile) def expand_path(s): """Expand $VARS and ~names in a string, like a shell @@ -453,36 +375,11 @@ def filehash(path): with open(path, "rU") as f: return md5(py3compat.str_to_bytes(f.read())).hexdigest() +@undoc def get_security_file(filename, profile='default'): - """Return the absolute path of a security file given by filename and profile - - This allows users and developers to find security files without - knowledge of the IPython directory structure. The search path - will be ['.', profile.security_dir] - - Parameters - ---------- - - filename : str - The file to be found. If it is passed as an absolute path, it will - simply be returned. - profile : str [default: 'default'] - The name of the profile to search. Leaving this unspecified - The file to be found. If it is passed as an absolute path, fname will - simply be returned. - - Returns - ------- - Raises :exc:`IOError` if file not found or returns absolute path to file. - """ - # import here, because profiledir also imports from utils.path - from IPython.core.profiledir import ProfileDir - try: - pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile) - except Exception: - # will raise ProfileDirError if no such profile - raise IOError("Profile %r not found") - return filefind(filename, ['.', pd.security_dir]) + warn("get_security_file has moved to the IPython.paths module") + from IPython.paths import get_security_file + return get_security_file(filename, profile=profile) ENOLINK = 1998