
    f&                     ~   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j                  d      Ze G d d	ej                               Z G d
 dej                        Zd Zd Zd Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zg dZy)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                   0     e Zd ZdZd Zd Zd fd	Z xZS )MagicHelpFormatterz@A HelpFormatter with a couple of changes to meet our needs.
    c                 X    t         j                  j                  | t        |      ||      S N)argparseRawDescriptionHelpFormatter
_fill_textr   )selftextwidthindents       W/var/www/cvtools/html/venv/lib/python3.12/site-packages/IPython/core/magic_arguments.pyr   zMagicHelpFormatter._fill_texte   s%    33>>tVD\SXZ`aa    c                    |j                   s& | j                  ||j                        d      \  }|S g }|j                  dk(  r|j	                  |j                          nm|j                  j                         }| j                  ||      }t        j                  |      sd|z  }|j                   D ]  }|j                  |d|        dj                  |      S )N   r   z<%s> z, )option_strings_metavar_formatterdestnargsextendupper_format_argsNAME_REmatchappendjoin)r   actionmetavarpartsdefaultargs_stringoption_strings          r   _format_action_invocationz,MagicHelpFormatter._format_action_invocationi   s    $$Ct..vv{{CAFHGN E ||q V223
 !++++-"//@ }}[1"(;"6K%+%:%: IMLLM;!GHI 99U##r   c                 2    t         t        |   ||||       y r   )superr	   	add_usage)r   usageactionsgroupsprefix	__class__s        r   r,   zMagicHelpFormatter.add_usage   s     $1%&&Qr   )z::

  %)__name__
__module____qualname____doc__r   r)   r,   __classcell__r1   s   @r   r	   r	   `   s    b$:R Rr   r	   c            
       D     e Zd ZdZdddddeddddf
 fd	Zd Zd Z xZS )	MagicArgumentParserz: An ArgumentParser tweaked for use by IPython magics.
    N-errorFc                 H    |g }t         t        |   |||||||||	|

       y )N)
progr-   descriptionepilogparentsformatter_classprefix_charsargument_defaultconflict_handleradd_help)r+   r9   __init__)r   r=   r-   r>   r?   r@   rA   rB   rC   rD   rE   r1   s              r   rF   zMagicArgumentParser.__init__   s>     ?G!41t5#F_%8H-	 	2 	Br   c                     t        |      )z5 Raise a catchable error instead of exiting.
        r   )r   messages     r   r;   zMagicArgumentParser.error   s     !!r   c                 :    t        |      }| j                  |      S )zL Split a string into an argument list and parse that argument list.
        )r   
parse_args)r   	argstringargvs      r   parse_argstringz#MagicArgumentParser.parse_argstring   s     #t$$r   )	r2   r3   r4   r5   r	   rF   r;   rM   r6   r7   s   @r   r9   r9      s8     !!3!"&")B&"
%r   r9   c                     t        | di       }d|vrt        | dd      |d<   t        |       }t        |fi |}d}| j                  ddd   D ]  }|j	                  ||      }||} |j                         | _        |S )zB Construct an argument parser using the function decorations.
    argcmd_kwdsr>   r5   N)getattr	real_namer9   
decoratorsadd_to_parserformat_helpr5   )
magic_funckwdsarg_nameparsergroupdecoresults          r   construct_parserr]      s     :}b1DD %j)TB]$H 2T2F E%%dd+ ##FE2E  ++-JMr   c                 8    | j                   j                  |      S )zA Parse the string of arguments for the given magic function.
    )rY   rM   )rV   rK   s     r   rM   rM      s     ,,Y77r   c                 r    | j                   }|j                  d      r|t        d      d }t        | d|      S )z& Find the real name of the magic.
    magic_Nargcmd_name)r2   
startswithlenrQ   )rV   
magic_names     r   rR   rR      s<     $$JX&H/
:}j99r   c                       e Zd ZdZd Zd Zy)ArgDecoratorzN Base class for decorators to add ArgumentParser information to a method.
    c                 r    t        |dd      sd|_        g |_        |j                  j                  |        |S Nhas_argumentsFT)rQ   ri   rS   r!   r   funcs     r   __call__zArgDecorator.__call__   s4    t_e4!%D DOt$r   c                      y)zD Add this object's information to the parser, if necessary.
        N r   rY   rZ   s      r   rT   zArgDecorator.add_to_parser   s     	r   N)r2   r3   r4   r5   rl   rT   rn   r   r   rf   rf      s    r   rf   c                       e Zd ZdZddZd Zy)magic_argumentszS Mark the magic as having argparse arguments and possibly adjust the
    name.
    Nc                     || _         y r   )name)r   rs   s     r   rF   zmagic_arguments.__init__   	    	r   c                     t        |dd      sd|_        g |_        | j                  | j                  |_        t        |      |_        |S rh   )rQ   ri   rS   rs   ra   r]   rY   rj   s     r   rl   zmagic_arguments.__call__   sF    t_e4!%D DO99 #yyD 't,r   r   )r2   r3   r4   r5   rF   rl   rn   r   r   rq   rq      s    	r   rq   c                   (    e Zd ZU dZeed<   d Zd Zy)ArgMethodWrapperz
    Base class to define a wrapper for ArgumentParser method.

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

    _method_namec                      || _         || _        y r   )argsrW   )r   rz   rW   s      r   rF   zArgMethodWrapper.__init__   s    		r   c                 l    ||} t        || j                        | j                  i | j                   y)6 Add this object's information to the parser.
        N)rQ   rx   rz   rW   ro   s      r   rT   zArgMethodWrapper.add_to_parser  s6     F*))*DIICCr   N)r2   r3   r4   r5   str__annotations__rF   rT   rn   r   r   rw   rw      s     r   rw   c                       e Zd ZdZdZy)argumentzt Store arguments and keywords to pass to add_argument().

    Instances also serve to decorate command methods.
    add_argumentNr2   r3   r4   r5   rx   rn   r   r   r   r          "Lr   r   c                       e Zd ZdZdZy)defaultszt Store arguments and keywords to pass to set_defaults().

    Instances also serve to decorate command methods.
    set_defaultsNr   rn   r   r   r   r     r   r   r   c                       e Zd ZdZd Zy)argument_groupzz Store arguments and keywords to pass to add_argument_group().

    Instances also serve to decorate command methods.
    c                 N     |j                   | j                  i | j                  S )r|   )add_argument_grouprz   rW   ro   s      r   rT   zargument_group.add_to_parser#  s$     )v(($))AtyyAAr   N)r2   r3   r4   r5   rT   rn   r   r   r   r     s    
Br   r   c                   (     e Zd ZdZd Z fdZ xZS )rW   z; Provide other keywords to the sub-parser constructor.
    c                     || _         y r   )rW   )r   rW   s     r   rF   zkwds.__init__,  rt   r   c                 P    t         t        |   |      }| j                  |_        |S r   )r+   rW   rl   rO   )r   rk   r1   s     r   rl   zkwds.__call__/  s%    T4)$/99r   )r2   r3   r4   r5   rF   rl   r6   r7   s   @r   rW   rW   )  s     r   rW   )rq   r   r   rW   rM   )r5   r   reIPython.core.errorr   IPython.utils.decoratorsr   IPython.utils.processr   IPython.utils.textr   compiler   r   r	   ArgumentParserr9   r]   rM   rR   objectrf   rq   rw   r   r   r   rW   __all__rn   r   r   <module>r      s   Lh  	 * * + %
"**/
0&R== &R &RP%(11 %D,8:6 "l (| 0" "" "	B% 	B	< 	r   