##// END OF EJS Templates
Improve typing and MIME hook API for inspector (#14342)...
Improve typing and MIME hook API for inspector (#14342) Fixes https://github.com/ipython/ipython/issues/14339 ### Additions Adds `InfoDict` type to improve the typing of `info()` result. Adds missing `"subclasses"` to `info_fields` list (these were added to the field list in https://github.com/ipython/ipython/pull/11486 but we forgot to update `info_fields` variable at the time) - the newly added `InfoDict` type will ensure that this won't happen again. Adds `InspectorHookData` dataclass which is passed to the MIME hooks which now should expect a single argument. Having a single dataclass argument enables us to deprecate individual fields, or add new fields without breaking the existing hooks. The old hooks will still work (if any are out there since this mechanism got just added in the previous point version). ### Deletions A comment over `info_fields` gets deleted: - Contrarily to the comment (which is getting deleted in this PR), `info_fields` were not defining the order of display since at least 2015 (https://github.com/ipython/ipython/pull/7903 - I did not feel the need to go further in the history to find when exactly it happened). - Also contrarily to this comment, current Jupyter messaging spec does not define the contents of `info_fields` (I guess this was lost during IPython/Jupyter split), but the newly added `InfoDict` at least properly annotates their type (if you know where I can find the old IPython messaging spec with the descriptions I can add these as doc comments). Unused `cast_unicode` import gets deleted. If someone imported it from here... well they really should not have. ### Deprecations - mime hooks taking two arguments (`obj, info`)

File last commit:

r27764:aefe51c6
r28661:2084e7f3 merge
Show More
paths.py
125 lines | 4.2 KiB | text/x-python | PythonLexer
"""Find files and directories which IPython uses.
"""
import os.path
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,
)
def get_ipython_dir() -> str:
"""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()
if 'IPYTHON_DIR' in env:
warn('The environment variable IPYTHON_DIR is deprecated since IPython 3.0. '
'Please use IPYTHONDIR instead.', DeprecationWarning)
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:
ipdir = xdg_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()
else:
os.makedirs(ipdir, exist_ok=True)
assert isinstance(ipdir, str), "all path manipulation should be str(unicode), but are not."
return ipdir
def get_ipython_cache_dir() -> str:
"""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 ipdir
def get_ipython_package_dir() -> str:
"""Get the base directory where IPython itself is installed."""
ipdir = os.path.dirname(IPython.__file__)
assert isinstance(ipdir, str)
return ipdir
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 the_path
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 as e:
# IOError makes more sense when people are expecting a path
raise IOError("Couldn't find profile %r" % profile) from e
return pd.location