Format: application/x-python-code
⬇ Download File (9.3 KB)
   1  o
   2  
   3  ��h&�@s
   4  dZddlZddlZddlmZddlmZddlmZddl	m
   5  Z
   6  e�d�ZeGdd	�d	ej
   7  ��ZGd
   8  d�dej�Zdd
   9  �Zdd�Zdd�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�Zgd �ZdS)!a� A decorator-based method of constructing IPython magics with `argparse`
  10  option handling.
  11  
  12  New magic functions can be defined like so::
  13  
  14      from IPython.core.magic_arguments import (argument, magic_arguments,
  15          parse_argstring)
  16  
  17      @magic_arguments()
  18      @argument('-o', '--option', help='An optional argument.')
  19      @argument('arg', type=int, help='An integer positional argument.')
  20      def magic_cool(self, arg):
  21          """ A really cool magic command.
  22  
  23      """
  24          args = parse_argstring(magic_cool, arg)
  25          ...
  26  
  27  The `@magic_arguments` decorator marks the function as having argparse arguments.
  28  The `@argument` decorator adds an argument using the same syntax as argparse's
  29  `add_argument()` method. More sophisticated uses may also require the
  30  `@argument_group` or `@kwds` decorator to customize the formatting and the
  31  parsing.
  32  
  33  Help text for the magic is automatically generated from the docstring and the
  34  arguments::
  35  
  36      In[1]: %cool?
  37          %cool [-o OPTION] arg
  38          
  39          A really cool magic command.
  40          
  41          positional arguments:
  42            arg                   An integer positional argument.
  43          
  44          optional arguments:
  45            -o OPTION, --option OPTION
  46                                  An optional argument.
  47  
  48  Here is an elaborated example that uses default parameters in `argument` and calls the `args` in the cell magic::
  49  
  50      from IPython.core.magic import register_cell_magic
  51      from IPython.core.magic_arguments import (argument, magic_arguments,
  52                                              parse_argstring)
  53  
  54  
  55      @magic_arguments()
  56      @argument(
  57          "--option",
  58          "-o",
  59          help=("Add an option here"),
  60      )
  61      @argument(
  62          "--style",
  63          "-s",
  64          default="foo",
  65          help=("Add some style arguments"),
  66      )
  67      @register_cell_magic
  68      def my_cell_magic(line, cell):
  69          args = parse_argstring(my_cell_magic, line)
  70          print(f"{args.option=}")
  71          print(f"{args.style=}")
  72          print(f"{cell=}")
  73  
  74  In a jupyter notebook, this cell magic can be executed like this::
  75  
  76      %%my_cell_magic -o Hello
  77      print("bar")
  78      i = 42
  79  
  80  Inheritance diagram:
  81  
  82  .. inheritance-diagram:: IPython.core.magic_arguments
  83     :parts: 3
  84  
  85  �N��
  86  UsageError)�undoc)�	arg_split)�dedentz[a-zA-Z][a-zA-Z0-9_-]*$cs2eZdZdZdd�Zdd�Zd	�fdd�	Z�ZS)
  87  �MagicHelpFormatterz@A HelpFormatter with a couple of changes to meet our needs.
  88      cCstj�|t|�||�S�N)�argparse�RawDescriptionHelpFormatter�
  89  _fill_textr)�self�text�width�indent�r�Rc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/magic_arguments.pyreszMagicHelpFormatter._fill_textcCs�|js|�||j�d�\}|Sg}|jdkr|�|j�n#|j��}|�||�}t�|�s1d|}|jD]}|�	d||f�q4d�
  90  |�S)N�rz<%s>z%s %sz, )�option_stringsZ_metavar_formatter�dest�nargs�extend�upperZ_format_args�NAME_RE�match�append�join)r�action�metavar�parts�defaultZargs_stringZ
  91  option_stringrrr�_format_action_invocationis
  92  
  93  
  94  
  95  
  96  z,MagicHelpFormatter._format_action_invocation�::
  97  
  98    %cstt|��||||�dSr)�superr�	add_usage)r�usage�actions�groups�prefix��	__class__rrr#�szMagicHelpFormatter.add_usage)r!)�__name__�
  99  __module__�__qualname__�__doc__rr r#�
 100  __classcell__rrr(rr`s
 101  rc
 102  sFeZdZdZdddddeddddf
 103  �fdd�	Zdd	�Zd
 104  d�Z�ZS)�MagicArgumentParserz: An ArgumentParser tweaked for use by IPython magics.
 105      N�-�errorFcs4|durg}tt|�j|||||||||	|
 106  d�
 107  dS)N)
 108  �progr$�description�epilog�parents�formatter_class�prefix_chars�argument_default�conflict_handler�add_help)r"r/�__init__)rr2r$r3r4r5r6r7r8r9r:r(rrr;�s
 109  �zMagicArgumentParser.__init__cCst|��)z5 Raise a catchable error instead of exiting.
 110          r)r�messagerrrr1�szMagicArgumentParser.errorcCst|�}|�|�S)zL Split a string into an argument list and parse that argument list.
 111          )r�
 112  parse_args)r�	argstring�argvrrr�parse_argstring�s
 113  z#MagicArgumentParser.parse_argstring)	r*r+r,r-rr;r1r@r.rrr(rr/�s�r/cCs|t|di�}d|vrt|dd�|d<t|�}t|fi|��}d}|jddd�D]}|�||�}|dur6|}q(|��|_|S)zB Construct an argument parser using the function decorations.
 114      �argcmd_kwdsr3r-N�����)�getattr�	real_namer/�
 115  decorators�
 116  add_to_parser�format_helpr-)�
 117  magic_func�kwdsZarg_name�parser�group�deco�resultrrr�construct_parser�s�
 118  rNcCs|j�|�S)zA Parse the string of arguments for the given magic function.
 119      )rJr@)rHr>rrrr@�sr@cCs,|j}|�d�r|td�d�}t|d|�S)z& Find the real name of the magic.
 120      Zmagic_N�argcmd_name)r*�
 121  startswith�lenrC)rHZ
 122  magic_namerrrrD�s
 123  rDc@s eZdZdZdd�Zdd�ZdS)�ArgDecoratorzN Base class for decorators to add ArgumentParser information to a method.
 124      cCs(t|dd�sd|_g|_|j�|�|S�N�
 125  has_argumentsFT)rCrTrEr�r�funcrrr�__call__�s
 126  zArgDecorator.__call__cCsdS)zD Add this object's information to the parser, if necessary.
 127          Nr�rrJrKrrrrF�szArgDecorator.add_to_parserN)r*r+r,r-rWrFrrrrrR�srRc@s"eZdZdZddd�Zdd�ZdS)�magic_argumentszS Mark the magic as having argparse arguments and possibly adjust the
 128      name.
 129      NcC�
 130  ||_dSr)�name)rr[rrrr;��
 131  zmagic_arguments.__init__cCs8t|dd�sd|_g|_|jdur|j|_t|�|_|SrS)rCrTrEr[rOrNrJrUrrrrW�s
 132  
 133  zmagic_arguments.__call__r)r*r+r,r-r;rWrrrrrY�s
 134  rYc@s*eZdZUdZeed<dd�Zdd�ZdS)�ArgMethodWrapperz�
 135      Base class to define a wrapper for ArgumentParser method.
 136  
 137      Child class must define either `_method_name` or `add_to_parser`.
 138  
 139      �_method_namecOs||_||_dSr)�argsrI)rr_rIrrrr;s
 140  zArgMethodWrapper.__init__cCs*|dur|}t||j�|ji|j��dS)�6 Add this object's information to the parser.
 141          N)rCr^r_rIrXrrrrFszArgMethodWrapper.add_to_parserN)r*r+r,r-�str�__annotations__r;rFrrrrr]�s
 142  
 143  r]c@�eZdZdZdZdS)�argumentzt Store arguments and keywords to pass to add_argument().
 144  
 145      Instances also serve to decorate command methods.
 146      �add_argumentN�r*r+r,r-r^rrrrrd
 147  �rdc@rc)�defaultszt Store arguments and keywords to pass to set_defaults().
 148  
 149      Instances also serve to decorate command methods.
 150      �set_defaultsNrfrrrrrhrgrhc@seZdZdZdd�ZdS)�argument_groupzz Store arguments and keywords to pass to add_argument_group().
 151  
 152      Instances also serve to decorate command methods.
 153      cCs|j|ji|j��S)r`)�add_argument_groupr_rIrXrrrrF#szargument_group.add_to_parserN)r*r+r,r-rFrrrrrjsrjcs(eZdZdZdd�Z�fdd�Z�ZS)rIz; Provide other keywords to the sub-parser constructor.
 154      cKrZr)rI)rrIrrrr;,r\z
 155  kwds.__init__cstt|��|�}|j|_|Sr)r"rIrWrArUr(rrrW/sz
 156  kwds.__call__)r*r+r,r-r;rWr.rrr(rrI)srI)rYrdrjrIr@)r-r	�reZIPython.core.errorrZIPython.utils.decoratorsrZIPython.utils.processrZIPython.utils.textr�compilerr
 157  r�ArgumentParserr/rNr@rD�objectrRrYr]rdrhrjrI�__all__rrrr�<module>s,T
 158  ("