
    9 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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                   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                 `    t           j                            | t          |          ||          S N)argparseRawDescriptionHelpFormatter
_fill_textr   )selftextwidthindents       R/var/www/equiseq/venv/lib/python3.11/site-packages/IPython/core/magic_arguments.pyr   zMagicHelpFormatter._fill_texte   s'    3>>tVD\\SXZ`aaa    c                    |j         s( |                     ||j                  d          \  }|S g }|j        dk    r|                    |j                    nr|j                                        }|                     ||          }t                              |          sd|z  }|j         D ]}|	                    |d|           d
                    |          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{CCAFFHGN E |q  V23333
 !+++--"//@@ }}[11 7"(;"6K%+%: I IMLLMMM;;!GHHHH99U###r   ::

  %c                 ^    t          t          |                               ||||           d S r   )superr	   	add_usage)r   usageactionsgroupsprefix	__class__s        r   r-   zMagicHelpFormatter.add_usage   s.     $''11%&&QQQQQr   )r*   )__name__
__module____qualname____doc__r   r)   r-   __classcell__r2   s   @r   r	   r	   `   sr         b b b$ $ $:R R R R R R R R 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                 t    |g }t          t          |                               |||||||||	|

  
         d S )N)
progr.   descriptionepilogparentsformatter_classprefix_charsargument_defaultconflict_handleradd_help)r,   r:   __init__)r   r>   r.   r?   r@   rA   rB   rC   rD   rE   rF   r2   s              r   rG   zMagicArgumentParser.__init__   s_     ?G!4((11t5#F_%8H-	 	2 	B 	B 	B 	B 	Br   c                      t          |          )z5 Raise a catchable error instead of exiting.
        r   )r   messages     r   r<   zMagicArgumentParser.error   s     !!!r   c                 J    t          |          }|                     |          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   )	r3   r4   r5   r6   r	   rG   r<   rN   r7   r8   s   @r   r:   r:      s          !!3!"&")B B B B B B&" " "
% % % % % % %r   r:   c                     t          | di           }d|vrt          | dd          |d<   t          |           }t          |fi |}d}| j        ddd         D ]}|                    ||          }||}|                                | _        |S )zB Construct an argument parser using the function decorations.
    argcmd_kwdsr?   r6   N)getattr	real_namer:   
decoratorsadd_to_parserformat_helpr6   )
magic_funckwdsarg_nameparsergroupdecoresults          r   construct_parserr^      s     :}b11DD  %j)TBB]$$H 22T22F E%ddd+  ##FE22E  ++--JMr   c                 6    | j                             |          S )zA Parse the string of arguments for the given magic function.
    )rZ   rN   )rW   rL   s     r   rN   rN      s     ,,Y777r   c                     | j         }|                    d          r|t          d          d         }t          | d|          S )z& Find the real name of the magic.
    magic_Nargcmd_name)r3   
startswithlenrR   )rW   
magic_names     r   rS   rS      sH     $JX&& 0H/
:}j999r   c                       e Zd ZdZd Zd ZdS )ArgDecoratorzN Base class for decorators to add ArgumentParser information to a method.
    c                 x    t          |dd          sd|_        g |_        |j                            |            |S Nhas_argumentsFT)rR   rj   rT   r!   r   funcs     r   __call__zArgDecorator.__call__   s@    t_e44 	!!%D DOt$$$r   c                     dS )zD Add this object's information to the parser, if necessary.
        N r   rZ   r[   s      r   rU   zArgDecorator.add_to_parser   s	     	r   N)r3   r4   r5   r6   rm   rU   ro   r   r   rg   rg      s<               r   rg   c                        e Zd ZdZddZd ZdS )magic_argumentszS Mark the magic as having argparse arguments and possibly adjust the
    name.
    Nc                     || _         d S r   )name)r   rt   s     r   rG   zmagic_arguments.__init__       			r   c                     t          |dd          sd|_        g |_        | j        | j        |_        t          |          |_        |S ri   )rR   rj   rT   rt   rb   r^   rZ   rk   s     r   rm   zmagic_arguments.__call__   sM    t_e44 	!!%D DO9 #yD 't,,r   r   )r3   r4   r5   r6   rG   rm   ro   r   r   rr   rr      sA            	 	 	 	 	r   rr   c                   *    e Zd ZU dZeed<   d Z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                 "    || _         || _        d S r   )argsrX   )r   r{   rX   s      r   rG   zArgMethodWrapper.__init__   s    				r   c                 X    ||} t          || j                  | j        i | j         dS )6 Add this object's information to the parser.
        N)rR   ry   r{   rX   rp   s      r   rU   zArgMethodWrapper.add_to_parser  s:     F*)**DICCCCtr   N)r3   r4   r5   r6   str__annotations__rG   rU   ro   r   r   rx   rx      sL                 r   rx   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r3   r4   r5   r6   ry   ro   r   r   r   r               "LLLr   r   c                       e Zd ZdZdZdS )defaultszt Store arguments and keywords to pass to set_defaults().

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

    Instances also serve to decorate command methods.
    c                 0     |j         | j        i | j        S )r}   )add_argument_groupr{   rX   rp   s      r   rU   zargument_group.add_to_parser#  s      )v($)AtyAAAr   N)r3   r4   r5   r6   rU   ro   r   r   r   r     s2         
B B B B Br   r   c                   (     e Zd ZdZd Z fdZ xZS )rX   z; Provide other keywords to the sub-parser constructor.
    c                     || _         d S r   )rX   )r   rX   s     r   rG   zkwds.__init__,  ru   r   c                 p    t          t          |                               |          }| j        |_        |S r   )r,   rX   rm   rP   )r   rl   r2   s     r   rm   zkwds.__call__/  s/    T4  ))$//9r   )r3   r4   r5   r6   rG   rm   r7   r8   s   @r   rX   rX   )  sQ                   r   rX   )rr   r   r   rX   rN   )r6   r   reIPython.core.errorr   IPython.utils.decoratorsr   IPython.utils.processr   IPython.utils.textr   compiler   r   r	   ArgumentParserr:   r^   rN   rS   objectrg   rr   rx   r   r   r   rX   __all__ro   r   r   <module>r      sc  L Lh  				 * ) ) ) ) ) * * * * * * + + + + + + % % % % % %
"*/
0
0&R &R &R &R &R= &R &R &RP% % % % %(1 % % %D  ,8 8 8: : :    6   "    l   (    |   0" " " " " " " "" " " " " " " "	B 	B 	B 	B 	B% 	B 	B 	B	 	 	 	 	< 	 	 	  r   