/opt/imh-python/lib/python3.9/site-packages/IPython/core
NameSizeModeActions
magics/-0755rm
profile/-0755rm
tests/-0755rm
__pycache__/-0755rm
alias.py100340644editdlrm
application.py189370644editdlrm
async_helpers.py42970644editdlrm
autocall.py19910644editdlrm
builtin_trap.py30090644editdlrm
compilerop.py77300644editdlrm
completer.py1178500644editdlrm
completerlib.py123650644editdlrm
crashhandler.py85080644editdlrm
debugger.py388720644editdlrm
display.py425030644editdlrm
displayhook.py129620644editdlrm
displaypub.py49440644editdlrm
display_functions.py129180644editdlrm
display_trap.py20980644editdlrm
error.py17340644editdlrm
events.py52510644editdlrm
excolors.py49280644editdlrm
extensions.py57800644editdlrm
formatters.py349810644editdlrm
getipython.py9120644editdlrm
guarded_eval.py250370644editdlrm
history.py344750644editdlrm
historyapp.py59090644editdlrm
hooks.py56630644editdlrm
inputsplitter.py290410644editdlrm
inputtransformer.py181840644editdlrm
inputtransformer2.py293930644editdlrm
interactiveshell.py1537610644editdlrm
latex_symbols.py312880644editdlrm
logger.py84410644editdlrm
macro.py17340644editdlrm
magic.py288990644editdlrm
magic_arguments.py97340644editdlrm
oinspect.py398980644editdlrm
page.py117530644editdlrm
payload.py17580644editdlrm
payloadpage.py14310644editdlrm
prefilter.py255880644editdlrm
profileapp.py106310644editdlrm
profiledir.py80290644editdlrm
prompts.py6070644editdlrm
pylabtools.py140180644editdlrm
release.py21780644editdlrm
shellapp.py178450644editdlrm
splitinput.py48360644editdlrm
ultratb.py558030644editdlrm
usage.py135420644editdlrm
__init__.py00644editdlrm
Edit: /opt/imh-python/lib/python3.9/site-packages/IPython/core/extensions.py (5780B)
# encoding: utf-8 """A class for managing IPython extensions.""" # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. import os import os.path import sys from importlib import import_module, reload from traitlets.config.configurable import Configurable from IPython.utils.path import ensure_dir_exists, compress_user from IPython.utils.decorators import undoc from traitlets import Instance #----------------------------------------------------------------------------- # Main class #----------------------------------------------------------------------------- BUILTINS_EXTS = {"storemagic": False, "autoreload": False} class ExtensionManager(Configurable): """A class to manage IPython extensions. An IPython extension is an importable Python module that has a function with the signature:: def load_ipython_extension(ipython): # Do things with ipython This function is called after your extension is imported and the currently active :class:`InteractiveShell` instance is passed as the only argument. You can do anything you want with IPython at that point, including defining new magic and aliases, adding new components, etc. You can also optionally define an :func:`unload_ipython_extension(ipython)` function, which will be called if the user unloads or reloads the extension. The extension manager will only call :func:`load_ipython_extension` again if the extension is reloaded. You can put your extension modules anywhere you want, as long as they can be imported by Python's standard import mechanism. However, to make it easy to write extensions, you can also put your extensions in ``os.path.join(self.ipython_dir, 'extensions')``. This directory is added to ``sys.path`` automatically. """ shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) def __init__(self, shell=None, **kwargs): super(ExtensionManager, self).__init__(shell=shell, **kwargs) self.shell.observe( self._on_ipython_dir_changed, names=('ipython_dir',) ) self.loaded = set() @property def ipython_extension_dir(self): return os.path.join(self.shell.ipython_dir, u'extensions') def _on_ipython_dir_changed(self, change): ensure_dir_exists(self.ipython_extension_dir) def load_extension(self, module_str: str): """Load an IPython extension by its module name. Returns the string "already loaded" if the extension is already loaded, "no load function" if the module doesn't have a load_ipython_extension function, or None if it succeeded. """ try: return self._load_extension(module_str) except ModuleNotFoundError: if module_str in BUILTINS_EXTS: BUILTINS_EXTS[module_str] = True return self._load_extension("IPython.extensions." + module_str) raise def _load_extension(self, module_str: str): if module_str in self.loaded: return "already loaded" from IPython.utils.syspathcontext import prepended_to_syspath with self.shell.builtin_trap: if module_str not in sys.modules: mod = import_module(module_str) mod = sys.modules[module_str] if self._call_load_ipython_extension(mod): self.loaded.add(module_str) else: return "no load function" def unload_extension(self, module_str: str): """Unload an IPython extension by its module name. This function looks up the extension's name in ``sys.modules`` and simply calls ``mod.unload_ipython_extension(self)``. Returns the string "no unload function" if the extension doesn't define a function to unload itself, "not loaded" if the extension isn't loaded, otherwise None. """ if BUILTINS_EXTS.get(module_str, False) is True: module_str = "IPython.extensions." + module_str if module_str not in self.loaded: return "not loaded" if module_str in sys.modules: mod = sys.modules[module_str] if self._call_unload_ipython_extension(mod): self.loaded.discard(module_str) else: return "no unload function" def reload_extension(self, module_str: str): """Reload an IPython extension by calling reload. If the module has not been loaded before, :meth:`InteractiveShell.load_extension` is called. Otherwise :func:`reload` is called and then the :func:`load_ipython_extension` function of the module, if it exists is called. """ from IPython.utils.syspathcontext import prepended_to_syspath if BUILTINS_EXTS.get(module_str, False) is True: module_str = "IPython.extensions." + module_str if (module_str in self.loaded) and (module_str in sys.modules): self.unload_extension(module_str) mod = sys.modules[module_str] with prepended_to_syspath(self.ipython_extension_dir): reload(mod) if self._call_load_ipython_extension(mod): self.loaded.add(module_str) else: self.load_extension(module_str) def _call_load_ipython_extension(self, mod): if hasattr(mod, 'load_ipython_extension'): mod.load_ipython_extension(self.shell) return True def _call_unload_ipython_extension(self, mod): if hasattr(mod, 'unload_ipython_extension'): mod.unload_ipython_extension(self.shell) return True