U
    h                     @  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                      sb   e Zd ZdZdd fddZdddd	d
Zdd ZddddZej	dddddZ
  ZS )_ParserBaseExceptionz7Base class for parser exceptions including normal exit.zSequence[str]msgsc                   s*   t  jd|f|| || _d | _d S )N )super__init__join_msgs_ipython_env)selfr   argskwargs	__class__ P/tmp/pip-unpacked-wheel-doshhd5e/google/generativeai/notebook/argument_parser.pyr
   '   s    z_ParserBaseException.__init__zipython_env.IPythonEnvNoneenvreturnc                 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_)r   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)__name__
__module____qualname____doc__r
   r   r   r   abcabstractmethodr   __classcell__r   r   r   r   r   $   s   r   )	metaclassc                   @  s    e Zd ZdZdddddZdS )ParserNormalExitzvException thrown when the parser exits normally.

  This is usually thrown when the user requests the help message.
  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.displayNr    r!   r"   r#   r   r   r   r   r   r(   >   s   r(   c                   @  s    e Zd ZdZdddddZdS )ParserErrorz(Exception thrown when there is an error.r   r   r   c                 C  s*   | j D ]}t| q|d k	r&|d d S )Nz,<b style='font-family:courier new'>ERROR</b>)r   r)   Zdisplay_htmlr*   r   r   r   r   L   s    

zParserError.displayNr,   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|| 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)r/   append)r   messagefiler   r   r   _print_messagea   s    zArgumentParser._print_messager   c                 C  s<   |r|  | | j}g | _|dkr.t|dn
t|ddS )z(Override ArgumentParser's exit() method.r   r   N)r3   r/   r(   r-   )r   statusr1   r   r   r   r   exitf   s    
zArgumentParser.exit)N)r   N)r    r!   r"   r#   r
   r3   r5   r&   r   r   r   r   r.   T   s   
r.   )r#   
__future__r   r$   argparsetypingr   Zgoogle.generativeai.notebookr   RuntimeErrorABCMetar   r(   r-   r.   r   r   r   r   <module>   s   