##// END OF EJS Templates
Enforce some more types since we are Py3only
Matthias Bussonnier -
Show More
@@ -1,119 +1,118 b''
1 """Find files and directories which IPython uses.
1 """Find files and directories which IPython uses.
2 """
2 """
3 import os.path
3 import os.path
4 import shutil
4 import shutil
5 import tempfile
5 import tempfile
6 from warnings import warn
6 from warnings import warn
7
7
8 import IPython
8 import IPython
9 from IPython.utils.importstring import import_item
9 from IPython.utils.importstring import import_item
10 from IPython.utils.path import (
10 from IPython.utils.path import (
11 get_home_dir, get_xdg_dir, get_xdg_cache_dir, compress_user, _writable_dir,
11 get_home_dir, get_xdg_dir, get_xdg_cache_dir, compress_user, _writable_dir,
12 ensure_dir_exists, fs_encoding)
12 ensure_dir_exists, fs_encoding)
13 from IPython.utils import py3compat
13 from IPython.utils import py3compat
14
14
15 def get_ipython_dir():
15 def get_ipython_dir() -> str:
16 """Get the IPython directory for this platform and user.
16 """Get the IPython directory for this platform and user.
17
17
18 This uses the logic in `get_home_dir` to find the home directory
18 This uses the logic in `get_home_dir` to find the home directory
19 and then adds .ipython to the end of the path.
19 and then adds .ipython to the end of the path.
20 """
20 """
21
21
22 env = os.environ
22 env = os.environ
23 pjoin = os.path.join
23 pjoin = os.path.join
24
24
25
25
26 ipdir_def = '.ipython'
26 ipdir_def = '.ipython'
27
27
28 home_dir = get_home_dir()
28 home_dir = get_home_dir()
29 xdg_dir = get_xdg_dir()
29 xdg_dir = get_xdg_dir()
30
30
31 # import pdb; pdb.set_trace() # dbg
32 if 'IPYTHON_DIR' in env:
31 if 'IPYTHON_DIR' in env:
33 warn('The environment variable IPYTHON_DIR is deprecated. '
32 warn('The environment variable IPYTHON_DIR is deprecated since IPython 3.0. '
34 'Please use IPYTHONDIR instead.')
33 'Please use IPYTHONDIR instead.', DeprecationWarning)
35 ipdir = env.get('IPYTHONDIR', env.get('IPYTHON_DIR', None))
34 ipdir = env.get('IPYTHONDIR', env.get('IPYTHON_DIR', None))
36 if ipdir is None:
35 if ipdir is None:
37 # not set explicitly, use ~/.ipython
36 # not set explicitly, use ~/.ipython
38 ipdir = pjoin(home_dir, ipdir_def)
37 ipdir = pjoin(home_dir, ipdir_def)
39 if xdg_dir:
38 if xdg_dir:
40 # Several IPython versions (up to 1.x) defaulted to .config/ipython
39 # Several IPython versions (up to 1.x) defaulted to .config/ipython
41 # on Linux. We have decided to go back to using .ipython everywhere
40 # on Linux. We have decided to go back to using .ipython everywhere
42 xdg_ipdir = pjoin(xdg_dir, 'ipython')
41 xdg_ipdir = pjoin(xdg_dir, 'ipython')
43
42
44 if _writable_dir(xdg_ipdir):
43 if _writable_dir(xdg_ipdir):
45 cu = compress_user
44 cu = compress_user
46 if os.path.exists(ipdir):
45 if os.path.exists(ipdir):
47 warn(('Ignoring {0} in favour of {1}. Remove {0} to '
46 warn(('Ignoring {0} in favour of {1}. Remove {0} to '
48 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
47 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
49 elif os.path.islink(xdg_ipdir):
48 elif os.path.islink(xdg_ipdir):
50 warn(('{0} is deprecated. Move link to {1} to '
49 warn(('{0} is deprecated. Move link to {1} to '
51 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
50 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
52 else:
51 else:
53 warn('Moving {0} to {1}'.format(cu(xdg_ipdir), cu(ipdir)))
52 warn('Moving {0} to {1}'.format(cu(xdg_ipdir), cu(ipdir)))
54 shutil.move(xdg_ipdir, ipdir)
53 shutil.move(xdg_ipdir, ipdir)
55
54
56 ipdir = os.path.normpath(os.path.expanduser(ipdir))
55 ipdir = os.path.normpath(os.path.expanduser(ipdir))
57
56
58 if os.path.exists(ipdir) and not _writable_dir(ipdir):
57 if os.path.exists(ipdir) and not _writable_dir(ipdir):
59 # ipdir exists, but is not writable
58 # ipdir exists, but is not writable
60 warn("IPython dir '{0}' is not a writable location,"
59 warn("IPython dir '{0}' is not a writable location,"
61 " using a temp directory.".format(ipdir))
60 " using a temp directory.".format(ipdir))
62 ipdir = tempfile.mkdtemp()
61 ipdir = tempfile.mkdtemp()
63 elif not os.path.exists(ipdir):
62 elif not os.path.exists(ipdir):
64 parent = os.path.dirname(ipdir)
63 parent = os.path.dirname(ipdir)
65 if not _writable_dir(parent):
64 if not _writable_dir(parent):
66 # ipdir does not exist and parent isn't writable
65 # ipdir does not exist and parent isn't writable
67 warn("IPython parent '{0}' is not a writable location,"
66 warn("IPython parent '{0}' is not a writable location,"
68 " using a temp directory.".format(parent))
67 " using a temp directory.".format(parent))
69 ipdir = tempfile.mkdtemp()
68 ipdir = tempfile.mkdtemp()
70
69 assert isinstance(ipdir, str), "all path manipulation should be str(unicode), but are not."
71 return py3compat.cast_unicode(ipdir, fs_encoding)
70 return ipdir
72
71
73
72
74 def get_ipython_cache_dir():
73 def get_ipython_cache_dir():
75 """Get the cache directory it is created if it does not exist."""
74 """Get the cache directory it is created if it does not exist."""
76 xdgdir = get_xdg_cache_dir()
75 xdgdir = get_xdg_cache_dir()
77 if xdgdir is None:
76 if xdgdir is None:
78 return get_ipython_dir()
77 return get_ipython_dir()
79 ipdir = os.path.join(xdgdir, "ipython")
78 ipdir = os.path.join(xdgdir, "ipython")
80 if not os.path.exists(ipdir) and _writable_dir(xdgdir):
79 if not os.path.exists(ipdir) and _writable_dir(xdgdir):
81 ensure_dir_exists(ipdir)
80 ensure_dir_exists(ipdir)
82 elif not _writable_dir(xdgdir):
81 elif not _writable_dir(xdgdir):
83 return get_ipython_dir()
82 return get_ipython_dir()
84
83
85 return py3compat.cast_unicode(ipdir, fs_encoding)
84 return py3compat.cast_unicode(ipdir, fs_encoding)
86
85
87
86
88 def get_ipython_package_dir():
87 def get_ipython_package_dir():
89 """Get the base directory where IPython itself is installed."""
88 """Get the base directory where IPython itself is installed."""
90 ipdir = os.path.dirname(IPython.__file__)
89 ipdir = os.path.dirname(IPython.__file__)
91 return py3compat.cast_unicode(ipdir, fs_encoding)
90 return py3compat.cast_unicode(ipdir, fs_encoding)
92
91
93
92
94 def get_ipython_module_path(module_str):
93 def get_ipython_module_path(module_str):
95 """Find the path to an IPython module in this version of IPython.
94 """Find the path to an IPython module in this version of IPython.
96
95
97 This will always find the version of the module that is in this importable
96 This will always find the version of the module that is in this importable
98 IPython package. This will always return the path to the ``.py``
97 IPython package. This will always return the path to the ``.py``
99 version of the module.
98 version of the module.
100 """
99 """
101 if module_str == 'IPython':
100 if module_str == 'IPython':
102 return os.path.join(get_ipython_package_dir(), '__init__.py')
101 return os.path.join(get_ipython_package_dir(), '__init__.py')
103 mod = import_item(module_str)
102 mod = import_item(module_str)
104 the_path = mod.__file__.replace('.pyc', '.py')
103 the_path = mod.__file__.replace('.pyc', '.py')
105 the_path = the_path.replace('.pyo', '.py')
104 the_path = the_path.replace('.pyo', '.py')
106 return py3compat.cast_unicode(the_path, fs_encoding)
105 return py3compat.cast_unicode(the_path, fs_encoding)
107
106
108 def locate_profile(profile='default'):
107 def locate_profile(profile='default'):
109 """Find the path to the folder associated with a given profile.
108 """Find the path to the folder associated with a given profile.
110
109
111 I.e. find $IPYTHONDIR/profile_whatever.
110 I.e. find $IPYTHONDIR/profile_whatever.
112 """
111 """
113 from IPython.core.profiledir import ProfileDir, ProfileDirError
112 from IPython.core.profiledir import ProfileDir, ProfileDirError
114 try:
113 try:
115 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
114 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
116 except ProfileDirError:
115 except ProfileDirError:
117 # IOError makes more sense when people are expecting a path
116 # IOError makes more sense when people are expecting a path
118 raise IOError("Couldn't find profile %r" % profile)
117 raise IOError("Couldn't find profile %r" % profile)
119 return pd.location
118 return pd.location
General Comments 0
You need to be logged in to leave comments. Login now