o
    ß­jy  ã                   @  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__© úi/var/www/html/CropPilot/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   