o
    Ďi                     @   s   d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
mZ dd Zdd Zh d	Zh d
Zh dZddhZe
G dd de	ZdS )z5Implementation of packaging-related magic functions.
    N)Path)Magicsmagics_class
line_magicc                    s   t   fdd}|S )Nc                     s(   t tjdd std | i |S )z>Return True if the current Python executable is in a conda env
conda-metahistoryzbThe python kernel does not appear to be a conda environment.  Please use ``%pip install`` instead.)r   sysprefixexists
ValueError)argskwargsfunc Y/home/jeff/fluffinator/venv/lib/python3.10/site-packages/IPython/core/magics/packaging.pywrapper   s
   z%is_conda_environment.<locals>.wrapper)	functoolswraps)r   r   r   r   r   is_conda_environment   s   
r   c                 C   s   d}d|   v r
d}|tjv r ttj| }| r t| S ttjj	|  }| r0t|S ttj
ddjdd}tjd|  d	|tjd
}|rP| d S | S )zFind the path to the given executable

    Parameters
    ----------

    executable: string
        Value should be: conda, mamba or micromamba
    	CONDA_EXEmamba	MAMBA_EXEr   r   zutf-8)encodingz^#\s*cmd:\s*(?P<command>.*z)\s[create|install])flagscommand)lowerosenvironr   is_filestrresolver   
executableparentr	   	read_textresearch	MULTILINE	groupdict)r   base_executabler"   r   matchr   r   r   _get_conda_like_executable$   s&   

r+   >   listremoveupdateinstallupgrade	uninstall>   r-   r.   r/   r0   r1   >   -n-p--name--prefixz-yz--yc                   @   s`   e Zd ZdZedd Zdd Zeedd Zeedd	 Z	eed
d Z
edd ZdS )PackagingMagicsz*Magics related to packaging & installationc                 C   N   t j}t jdkrd| d }nt|}| jd|dd|g td dS )zmRun the pip package manager within the current kernel.

        Usage:
          %pip install [pkgs]
        win32" -mpipANote: you may need to restart the kernel to use updated packages.N	r   r"   platformshlexquoteshellsystemjoinprintselflinepythonr   r   r   r<   \      

zPackagingMagics.pipc                 C   s   t |}t|dkr|d nd}t|dkr|dd  ndg}g }t| jdd d u}|tv }t|t}|rA|rA|sA|	d |t
v }	t|t}
|	rX|
sX|dtjg | jd||g| |  td d S )	Nr       kernelz--yesr5   r:   zB
Note: you may need to restart the kernel to use updated packages.)r@   splitlengetattrrB   CONDA_COMMANDS_REQUIRING_YESsetintersectionCONDA_YES_FLAGSappendCONDA_COMMANDS_REQUIRING_PREFIXCONDA_ENV_FLAGSextendr   r	   rC   rD   rE   )rG   cmdrH   r   r   
extra_argsstdin_disabled	needs_yeshas_yesneeds_prefix
has_prefixr   r   r   _run_commandm   s   

zPackagingMagics._run_commandc                 C      t d}| || dS )zqRun the conda package manager within the current kernel.

        Usage:
          %conda install [pkgs]
        condaNr+   r`   )rG   rH   rb   r   r   r   rb         zPackagingMagics.condac                 C   ra   )zqRun the mamba package manager within the current kernel.

        Usage:
          %mamba install [pkgs]
        r   Nrc   )rG   rH   r   r   r   r   r      rd   zPackagingMagics.mambac                 C   ra   )zvRun the conda package manager within the current kernel.

        Usage:
          %micromamba install [pkgs]
        
micromambaNrc   )rG   rH   re   r   r   r   re      rd   zPackagingMagics.micromambac                 C   r7   )zoRun the uv package manager within the current kernel.

        Usage:
          %uv pip install [pkgs]
        r8   r9   r:   r;   uvr=   Nr>   rF   r   r   r   rf      rJ   zPackagingMagics.uvN)__name__
__module____qualname____doc__r   r<   r`   r   rb   r   re   rf   r   r   r   r   r6   X   s     
			r6   )rj   r   r   r%   r@   r   pathlibr   IPython.core.magicr   r   r   r   r+   rV   rQ   rW   rT   r6   r   r   r   r   <module>   s     
*