o
    j6hy                     @  s   d Z ddlmZ ddlZddlZddlmZ ddlmZ G dd de	ej
dZG d	d
 d
eZG dd deZG dd dejZdS )a  Customized ArgumentParser.

The default behvaior of argparse.ArgumentParser's parse_args() method  is to
exit with a SystemExit exception in the following cases:
1. When the user requests a help message (with the --help or -h flags), or
2. When there's a parsing error (e.g. missing required flags or mistyped flags)

To make the errors more user-friendly, this class customizes
argparse.ArgumentParser and raises either ParserNormalExit for (1) or
ParserError for (2); this way the caller has control over how to display them
to the user.
    )annotationsN)Sequence)ipython_envc                      sN   e Zd ZdZd fddZdd
dZdd ZdddZej	dddZ
  ZS )_ParserBaseExceptionz7Base class for parser exceptions including normal exit.msgsSequence[str]c                   s0   t  jd|g|R i | || _d | _d S )N )super__init__join_msgs_ipython_env)selfr   argskwargs	__class__ {/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/google/generativeai/notebook/argument_parser.pyr
   '   s    
z_ParserBaseException.__init__envipython_env.IPythonEnvreturnNonec                 C  s
   || _ d S N)r   r   r   r   r   r   set_ipython_env,   s   
z$_ParserBaseException.set_ipython_envc                 C  s   |  | j d S r   )displayr   r   r   r   r   _ipython_display_/   s   z&_ParserBaseException._ipython_display_c                 C  s   | j S r   )r   r   r   r   r   r   2   s   z_ParserBaseException.msgsipython_env.IPythonEnv | Nonec                 C  s   dS )z-Display this exception on an IPython console.Nr   r   r   r   r   r   5   s    z_ParserBaseException.display)r   r   )r   r   r   r   )r   r   r   r   r   r   )__name__
__module____qualname____doc__r
   r   r   r   abcabstractmethodr   __classcell__r   r   r   r   r   $   s    

r   )	metaclassc                   @     e Zd ZdZd	ddZdS )
ParserNormalExitzzException thrown when the parser exits normally.

    This is usually thrown when the user requests the help message.
    r   r   r   r   c                 C  s   | j D ]}t| qd S r   )r   printr   r   msgr   r   r   r   D   s   

zParserNormalExit.displayNr    r!   r"   r#   r$   r   r   r   r   r   r*   >   s    r*   c                   @  r)   )
ParserErrorz(Exception thrown when there is an error.r   r   r   r   c                 C  s.   | j D ]}t| q|d ur|d d S d S )Nz,<b style='font-family:courier new'>ERROR</b>)r   r+   display_htmlr,   r   r   r   r   L   s
   

zParserError.displayNr    r.   r   r   r   r   r/   I   s    r/   c                      s4   e Zd ZdZ fddZd
ddZddd	Z  ZS )ArgumentParsera!  Customized ArgumentParser for LLM Magics.

    This class overrides the parent argparse.ArgumentParser's error-handling
    methods to avoid side-effects like printing to stderr. The messages are
    accumulated and passed into the raised exceptions for the caller to
    handle them.
    c                   s   t  j|i | g | _d S r   )r	   r
   	_messages)r   r   r   r   r   r   r
   ]   s   
zArgumentParser.__init__Nc                 C  s   ~| j | dS )z2Override ArgumentParser's _print_message() method.N)r2   append)r   messagefiler   r   r   _print_messagea   s   zArgumentParser._print_messager   c                 C  s6   |r|  | | j}g | _|dkrt|dt|d)z(Override ArgumentParser's exit() method.r   )r   )r6   r2   r*   r/   )r   statusr4   r   r   r   r   exitf   s   


zArgumentParser.exitr   )r   N)r!   r"   r#   r$   r
   r6   r8   r'   r   r   r   r   r1   T   s
    
r1   )r$   
__future__r   r%   argparsetypingr   google.generativeai.notebookr   RuntimeErrorABCMetar   r*   r/   r1   r   r   r   r   <module>   s   