o
    e                     @   sn   d Z ddlZddlZddlZddlmZmZ ddlmZ ddl	m
Z
 ddlmZ dddZG d	d
 d
eZdS )z(A class for managing IPython extensions.    N)import_modulereload)Configurable)ensure_dir_exists)InstanceF)Z
storemagicZ
autoreloadc                       sv   e Zd ZdZedddZd fdd	Zdefd	d
ZdefddZ	defddZ
defddZdd Zdd Z  ZS )ExtensionManagera  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.
    z1IPython.core.interactiveshell.InteractiveShellABCT)
allow_noneNc                    s&   t t| jdd|i| t | _d S )Nshell )superr   __init__setloaded)selfr	   kwargs	__class__r
   W/root/Voice_Assist/myprojectenv/lib/python3.10/site-packages/IPython/core/extensions.pyr   2   s   zExtensionManager.__init__
module_strc                 C   s@   z|  |W S  ty   |tv rdt|< |  d|  Y S  w )a  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.
        TIPython.extensions.)_load_extensionModuleNotFoundErrorBUILTINS_EXTS)r   r   r
   r
   r   load_extension6   s   zExtensionManager.load_extensionc                 C   s   || j v rdS | jd usJ | jj, |tjvrt|}tj| }| |r-| j | n		 W d    dS W d    d S 1 sAw   Y  d S )Nzalready loadedzno load function)r   r	   Zbuiltin_trapsysmodulesr   _call_load_ipython_extensionaddr   r   modr
   r
   r   r   E   s   




"z ExtensionManager._load_extensionc                 C   s\   t |ddu rd| }|| jvrdS |tjv r,tj| }| |r*| j| dS dS dS )a{  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.
        FTr   z
not loadedzno unload functionN)r   getr   r   r   _call_unload_ipython_extensiondiscardr   r
   r
   r   unload_extensionT   s   




z!ExtensionManager.unload_extensionc                 C   st   t |ddu rd| }|| jv r3|tjv r3| | tj| }t| | |r1| j| dS dS | 	| dS )a4  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.
        FTr   N)
r   r    r   r   r   r#   r   r   r   r   r   r
   r
   r   reload_extensionj   s   	


z!ExtensionManager.reload_extensionc                 C      t |dr|| j dS d S )Nload_ipython_extensionT)hasattrr&   r	   r   r   r
   r
   r   r         
z-ExtensionManager._call_load_ipython_extensionc                 C   r%   )Nunload_ipython_extensionT)r'   r*   r	   r(   r
   r
   r   r!      r)   z/ExtensionManager._call_unload_ipython_extension)N)__name__
__module____qualname____doc__r   r	   r   strr   r   r#   r$   r   r!   __classcell__r
   r
   r   r   r      s    r   )r.   osZos.pathr   	importlibr   r   Ztraitlets.config.configurabler   ZIPython.utils.pathr   Z	traitletsr   r   r   r
   r
   r
   r   <module>   s   
