U
    h7                     @  s  U d Z ddlmZ ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlZddlmZ ejZejZejZejZdaded< ddddZdddddZG dd dejZG dd deZejG dd dejZe e dS )z-Colab Magics class.

Installs %%palm magics.
    )annotationsN)credentials)gspread_client)ipython_env)ipython_env_impl)magics_engine)post_process_utils)sheets_utils)magiczipython_env.IPythonEnv | None_ipython_envzipython_env.IPythonEnvreturnc                   C  s   t dkrt a t S )z;Lazily constructs and returns a global IPythonEnv instance.N)r   r   ZIPythonEnvImpl r   r   G/tmp/pip-unpacked-wheel-doshhd5e/google/generativeai/notebook/magics.py_get_ipython_env.   s    r   zcredentials.CredentialsNone)credsr   c                 C  s   t j| t d dS )zSets up credentials.

  This is used for interacting Google APIs, such as Google Sheets.

  Args:
    creds: The credentials that will be used (e.g. to read from Google Sheets.)
  )r   envN)r   	authorizer   )r   r   r   r   r   6   s    r   c                   @  s&   e Zd ZdZejdddddZdS )AbstractMagicsz"Defines interface to Magics class.
str | None	cell_line	cell_bodyc                 C  s
   t  dS )zPerform various LLM-related operations.

    Args:
      cell_line: String to pass to the MagicsEngine.
      cell_body: Contents of the cell body.
    N)NotImplementedErrorselfr   r   r   r   r   palmD   s    zAbstractMagics.palmN)__name__
__module____qualname____doc__abcabstractmethodr   r   r   r   r   r   A   s   r   c                   @  s(   e Zd ZdZdd ZdddddZdS )	
MagicsImplzActual class implementing the magics functionality.

  We use a separate class to ensure a single, global instance
  of the magics class.
  c                 C  s   t jt d| _d S )N)r   )r   ZMagicsEnginer   _engine)r   r   r   r   __init__V   s    zMagicsImpl.__init__r   r   c                 C  s   |pd}|pd}| j ||S )Perform various LLM-related operations.

    Args:
      cell_line: String to pass to the MagicsEngine.
      cell_body: Contents of the cell body.

    Returns:
      Results from running MagicsEngine.
     )r%   Zexecute_cellr   r   r   r   r   Y   s    
zMagicsImpl.palmN)r   r   r    r!   r&   r   r   r   r   r   r$   O   s   r$   c                   @  s<   e Zd ZdZdZeddddZejdddd	d
Z	dS )MagicszvClass to register the magic with Colab.

  Objects of this class delegate all calls to a single,
  global instance.
  Nr   r   c                 C  s   | j dkrt | _ | j S )z.Retrieve global instance of the Magics object.N)	_instancer$   )clsr   r   r   get_instances   s    
zMagics.get_instancer   r   c                 C  s   t  j||dS )r'   r   )r)   r,   r   r   r   r   r   r   z   s    zMagics.palm)
r   r   r    r!   r*   classmethodr,   r
   Zline_cell_magicr   r   r   r   r   r)   h   s   r)   )r!   
__future__r   r"   Zgoogle.authr   Zgoogle.generativeai.notebookr   r   r   r   r   r	   ZIPythonZIPython.corer
   ZSheetsInputsZSheetsOutputsZpost_process_add_fnZpost_process_replace_fnr   __annotations__r   r   ABCr   r$   Zmagics_classr)   Zget_ipythonZregister_magicsr   r   r   r   <module>   s.   