o
    û›h&  ã                   @   s
  d Z ddl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
 e d¡ZeG dd	„ d	ejƒƒZG d
d„ dejƒZdd„ Zdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZg d ¢ZdS )!a´   A decorator-based method of constructing IPython magics with `argparse`
option handling.

New magic functions can be defined like so::

    from IPython.core.magic_arguments import (argument, magic_arguments,
        parse_argstring)

    @magic_arguments()
    @argument('-o', '--option', help='An optional argument.')
    @argument('arg', type=int, help='An integer positional argument.')
    def magic_cool(self, arg):
        """ A really cool magic command.

    """
        args = parse_argstring(magic_cool, arg)
        ...

The `@magic_arguments` decorator marks the function as having argparse arguments.
The `@argument` decorator adds an argument using the same syntax as argparse's
`add_argument()` method. More sophisticated uses may also require the
`@argument_group` or `@kwds` decorator to customize the formatting and the
parsing.

Help text for the magic is automatically generated from the docstring and the
arguments::

    In[1]: %cool?
        %cool [-o OPTION] arg
        
        A really cool magic command.
        
        positional arguments:
          arg                   An integer positional argument.
        
        optional arguments:
          -o OPTION, --option OPTION
                                An optional argument.

Here is an elaborated example that uses default parameters in `argument` and calls the `args` in the cell magic::

    from IPython.core.magic import register_cell_magic
    from IPython.core.magic_arguments import (argument, magic_arguments,
                                            parse_argstring)


    @magic_arguments()
    @argument(
        "--option",
        "-o",
        help=("Add an option here"),
    )
    @argument(
        "--style",
        "-s",
        default="foo",
        help=("Add some style arguments"),
    )
    @register_cell_magic
    def my_cell_magic(line, cell):
        args = parse_argstring(my_cell_magic, line)
        print(f"{args.option=}")
        print(f"{args.style=}")
        print(f"{cell=}")

In a jupyter notebook, this cell magic can be executed like this::

    %%my_cell_magic -o Hello
    print("bar")
    i = 42

Inheritance diagram:

.. inheritance-diagram:: IPython.core.magic_arguments
   :parts: 3

é    N©Ú
UsageError)Úundoc)Ú	arg_split)Údedentz[a-zA-Z][a-zA-Z0-9_-]*$c                       s2   e Zd ZdZdd„ Zdd„ Zd	‡ fdd„	Z‡  ZS )
ÚMagicHelpFormatterz@A HelpFormatter with a couple of changes to meet our needs.
    c                 C   s   t j | t|ƒ||¡S ©N)ÚargparseÚRawDescriptionHelpFormatterÚ
_fill_textr   )ÚselfÚtextÚwidthÚindent© r   úRc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/magic_arguments.pyr   e   s   zMagicHelpFormatter._fill_textc                 C   sŠ   |j s|  ||j¡dƒ\}|S g }|jdkr| |j ¡ n#|j ¡ }|  ||¡}t |¡s1d| }|j D ]}| 	d||f ¡ q4d 
|¡S )Né   r   z<%s>z%s %sz, )Úoption_stringsZ_metavar_formatterÚdestÚnargsÚextendÚupperZ_format_argsÚNAME_REÚmatchÚappendÚjoin)r   ÚactionÚmetavarÚpartsÚdefaultZargs_stringZoption_stringr   r   r   Ú_format_action_invocationi   s   




z,MagicHelpFormatter._format_action_invocationú::

  %c                    s   t t| ƒ ||||¡ d S r   )Úsuperr   Ú	add_usage)r   ÚusageÚactionsÚgroupsÚprefix©Ú	__class__r   r   r#   †   s   zMagicHelpFormatter.add_usage)r!   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r#   Ú__classcell__r   r   r(   r   r   `   s
    r   c                
       sF   e Zd ZdZdddddeddddf
‡ fdd„	Zdd	„ Zd
d„ Z‡  ZS )ÚMagicArgumentParserz: An ArgumentParser tweaked for use by IPython magics.
    Nú-ÚerrorFc                    s4   |d u rg }t t| ƒj|||||||||	|
d�
 d S )N)
Úprogr$   ÚdescriptionÚepilogÚparentsÚformatter_classÚprefix_charsÚargument_defaultÚconflict_handlerÚadd_help)r"   r/   Ú__init__)r   r2   r$   r3   r4   r5   r6   r7   r8   r9   r:   r(   r   r   r;   Œ   s   
üzMagicArgumentParser.__init__c                 C   s   t |ƒ‚)z5 Raise a catchable error instead of exiting.
        r   )r   Úmessager   r   r   r1   Ÿ   s   zMagicArgumentParser.errorc                 C   s   t |ƒ}|  |¡S )zL Split a string into an argument list and parse that argument list.
        )r   Ú
parse_args)r   Ú	argstringÚargvr   r   r   Úparse_argstring¤   s   
z#MagicArgumentParser.parse_argstring)	r*   r+   r,   r-   r   r;   r1   r@   r.   r   r   r(   r   r/   ‰   s    ör/   c                 C   s|   t | di ƒ}d|vrt | ddƒ|d< t| ƒ}t|fi |¤Ž}d}| jddd… D ]}| ||¡}|dur6|}q(| ¡ | _|S )zB Construct an argument parser using the function decorations.
    Úargcmd_kwdsr3   r-   Néÿÿÿÿ)ÚgetattrÚ	real_namer/   Ú
decoratorsÚadd_to_parserÚformat_helpr-   )Ú
magic_funcÚkwdsZarg_nameÚparserÚgroupÚdecoÚresultr   r   r   Úconstruct_parser«   s   €
rN   c                 C   s   | j  |¡S )zA Parse the string of arguments for the given magic function.
    )rJ   r@   )rH   r>   r   r   r   r@   Á   s   r@   c                 C   s,   | j }| d¡r|tdƒd… }t| d|ƒS )z& Find the real name of the magic.
    Zmagic_NÚargcmd_name)r*   Ú
startswithÚlenrC   )rH   Z
magic_namer   r   r   rD   Ç   s   
rD   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚArgDecoratorzN Base class for decorators to add ArgumentParser information to a method.
    c                 C   s(   t |ddƒsd|_g |_|j | ¡ |S ©NÚhas_argumentsFT)rC   rT   rE   r   ©r   Úfuncr   r   r   Ú__call__Ô   s
   zArgDecorator.__call__c                 C   s   dS )zD Add this object's information to the parser, if necessary.
        Nr   ©r   rJ   rK   r   r   r   rF   Û   s   zArgDecorator.add_to_parserN)r*   r+   r,   r-   rW   rF   r   r   r   r   rR   Ð   s    rR   c                   @   s"   e Zd ZdZddd„Zdd„ ZdS )Úmagic_argumentszS Mark the magic as having argparse arguments and possibly adjust the
    name.
    Nc                 C   ó
   || _ d S r   )Úname)r   r[   r   r   r   r;   æ   ó   
zmagic_arguments.__init__c                 C   s8   t |ddƒsd|_g |_| jd ur| j|_t|ƒ|_|S rS   )rC   rT   rE   r[   rO   rN   rJ   rU   r   r   r   rW   é   s   

zmagic_arguments.__call__r   )r*   r+   r,   r-   r;   rW   r   r   r   r   rY   á   s    
rY   c                   @   s*   e Zd ZU dZeed< dd„ Zdd„ ZdS )ÚArgMethodWrapperz‹
    Base class to define a wrapper for ArgumentParser method.

    Child class must define either `_method_name` or `add_to_parser`.

    Ú_method_namec                 O   s   || _ || _d S r   )ÚargsrI   )r   r_   rI   r   r   r   r;      s   
zArgMethodWrapper.__init__c                 C   s*   |dur|}t || jƒ| ji | j¤Ž dS )ú6 Add this object's information to the parser.
        N)rC   r^   r_   rI   rX   r   r   r   rF     s   zArgMethodWrapper.add_to_parserN)r*   r+   r,   r-   ÚstrÚ__annotations__r;   rF   r   r   r   r   r]   õ   s
   
 r]   c                   @   ó   e Zd ZdZdZdS )Úargumentzt Store arguments and keywords to pass to add_argument().

    Instances also serve to decorate command methods.
    Úadd_argumentN©r*   r+   r,   r-   r^   r   r   r   r   rd     ó    rd   c                   @   rc   )Údefaultszt Store arguments and keywords to pass to set_defaults().

    Instances also serve to decorate command methods.
    Úset_defaultsNrf   r   r   r   r   rh     rg   rh   c                   @   s   e Zd ZdZdd„ ZdS )Úargument_groupzz Store arguments and keywords to pass to add_argument_group().

    Instances also serve to decorate command methods.
    c                 C   s   |j | ji | j¤ŽS )r`   )Úadd_argument_groupr_   rI   rX   r   r   r   rF   #  s   zargument_group.add_to_parserN)r*   r+   r,   r-   rF   r   r   r   r   rj     s    rj   c                       s(   e Zd ZdZdd„ Z‡ fdd„Z‡  ZS )rI   z; Provide other keywords to the sub-parser constructor.
    c                 K   rZ   r   )rI   )r   rI   r   r   r   r;   ,  r\   zkwds.__init__c                    s   t t| ƒ |¡}| j|_|S r   )r"   rI   rW   rA   rU   r(   r   r   rW   /  s   zkwds.__call__)r*   r+   r,   r-   r;   rW   r.   r   r   r(   r   rI   )  s    rI   )rY   rd   rj   rI   r@   )r-   r	   ÚreZIPython.core.errorr   ZIPython.utils.decoratorsr   ZIPython.utils.processr   ZIPython.utils.textr   Úcompiler   r
   r   ÚArgumentParserr/   rN   r@   rD   ÚobjectrR   rY   r]   rd   rh   rj   rI   Ú__all__r   r   r   r   Ú<module>   s,    T
("	