Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/IPython/paths.py: 23%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Find files and directories which IPython uses.
2"""
3from __future__ import annotations
5import os.path
6import tempfile
7from warnings import warn
9from IPython.utils.importstring import import_item
10from IPython.utils.path import (
11 get_home_dir,
12 get_xdg_dir,
13 get_xdg_cache_dir,
14 compress_user,
15 _writable_dir,
16 ensure_dir_exists,
17)
20def get_ipython_dir() -> str:
21 """Get the IPython directory for this platform and user.
23 This uses the logic in `get_home_dir` to find the home directory
24 and then adds .ipython to the end of the path.
25 """
27 env = os.environ
28 pjoin = os.path.join
31 ipdir_def = '.ipython'
33 home_dir = get_home_dir()
34 xdg_dir = get_xdg_dir()
36 ipdir = env.get("IPYTHONDIR", None)
37 if ipdir is None:
38 # not set explicitly, use ~/.ipython
39 ipdir = pjoin(home_dir, ipdir_def)
40 if xdg_dir:
41 # Several IPython versions (up to 1.x) defaulted to .config/ipython
42 # on Linux. We have decided to go back to using .ipython everywhere
43 xdg_ipdir = pjoin(xdg_dir, 'ipython')
45 if _writable_dir(xdg_ipdir):
46 cu = compress_user
47 if os.path.exists(ipdir):
48 warn(('Ignoring {0} in favour of {1}. Remove {0} to '
49 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
50 elif os.path.islink(xdg_ipdir):
51 warn(('{} is deprecated. Move link to {} to '
52 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
53 else:
54 ipdir = xdg_ipdir
56 ipdir = os.path.normpath(os.path.expanduser(ipdir))
58 if os.path.exists(ipdir) and not _writable_dir(ipdir):
59 # ipdir exists, but is not writable
60 warn("IPython dir '{}' is not a writable location,"
61 " using a temp directory.".format(ipdir))
62 ipdir = tempfile.mkdtemp()
63 elif not os.path.exists(ipdir):
64 parent = os.path.dirname(ipdir)
65 if not _writable_dir(parent):
66 # ipdir does not exist and parent isn't writable
67 warn("IPython parent '{}' is not a writable location,"
68 " using a temp directory.".format(parent))
69 ipdir = tempfile.mkdtemp()
70 else:
71 os.makedirs(ipdir, exist_ok=True)
72 assert isinstance(ipdir, str), "all path manipulation should be str(unicode), but are not."
73 return ipdir
76def get_ipython_cache_dir() -> str:
77 """Get the cache directory it is created if it does not exist."""
78 xdgdir = get_xdg_cache_dir()
79 if xdgdir is None:
80 return get_ipython_dir()
81 ipdir = os.path.join(xdgdir, "ipython")
82 if not os.path.exists(ipdir) and _writable_dir(xdgdir):
83 ensure_dir_exists(ipdir)
84 elif not _writable_dir(xdgdir):
85 return get_ipython_dir()
87 return ipdir
90def get_ipython_package_dir() -> str:
91 """Get the base directory where IPython itself is installed."""
92 ipdir = os.path.dirname(__file__)
93 assert isinstance(ipdir, str)
94 return ipdir
97def get_ipython_module_path(module_str):
98 """Find the path to an IPython module in this version of IPython.
100 This will always find the version of the module that is in this importable
101 IPython package. This will always return the path to the ``.py``
102 version of the module.
103 """
104 if module_str == 'IPython':
105 return os.path.join(get_ipython_package_dir(), '__init__.py')
106 mod = import_item(module_str)
107 the_path = mod.__file__.replace('.pyc', '.py')
108 the_path = the_path.replace('.pyo', '.py')
109 return the_path
112def locate_profile(profile: str = "default") -> str:
113 """Find the path to the folder associated with a given profile.
115 I.e. find $IPYTHONDIR/profile_whatever.
116 """
117 from IPython.core.profiledir import ProfileDir, ProfileDirError
118 try:
119 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
120 except ProfileDirError as e:
121 # IOError makes more sense when people are expecting a path
122 raise OSError("Couldn't find profile %r" % profile) from e
123 return pd.location