ó
æNXc           @   ss  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 d e j f d	 „  ƒ  Yƒ Z d
 e j f d „  ƒ  YZ d „  Z d „  Z d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d d d d d g Z d S(   sM   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.

Inheritance diagram:

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

iÿÿÿÿN(   t
   UsageError(   t   undoc(   t	   arg_split(   t   dedents   [a-zA-Z][a-zA-Z0-9_-]*$t   MagicHelpFormatterc           B   s,   e  Z d  Z d „  Z d „  Z d d „ Z RS(   s@   A HelpFormatter with a couple of changes to meet our needs.
    c         C   s   t  j j |  t | ƒ | | ƒ S(   N(   t   argparset   RawDescriptionHelpFormattert
   _fill_textR   (   t   selft   textt   widtht   indent(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR   E   s    c         C   sÌ   | j  s+ |  j | | j ƒ d ƒ \ } | Sg  } | j d k rS | j | j  ƒ nh | j j ƒ  } |  j | | ƒ } t j | ƒ s d | } n  x( | j  D] } | j	 d | | f ƒ qš Wd j
 | ƒ Sd  S(   Ni   i    s   <%s>s   %s %ss   , (   t   option_stringst   _metavar_formattert   destt   nargst   extendt   uppert   _format_argst   NAME_REt   matcht   appendt   join(   R   t   actiont   metavart   partst   defaultt   args_stringt   option_string(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyt   _format_action_invocationI   s    	s   ::

  %c         C   s#   t  t |  ƒ j | | | | ƒ d  S(   N(   t   superR   t	   add_usage(   R   t   usaget   actionst   groupst   prefix(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR   f   s    (   t   __name__t
   __module__t   __doc__R   R   R   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR   @   s   		t   MagicArgumentParserc           B   sG   e  Z d  Z d d d d d e d d d e d „
 Z d „  Z d „  Z RS(   s:    An ArgumentParser tweaked for use by IPython magics.
    t   -t   errorc         C   sh   | d  k r g  } n  t t |  ƒ j d | d | d | d | d | d | d | d | d	 |	 d
 |
 ƒ 
d  S(   Nt   progR    t   descriptiont   epilogt   parentst   formatter_classt   prefix_charst   argument_defaultt   conflict_handlert   add_help(   t   NoneR   R'   t   __init__(   R   R*   R    R+   R,   R-   R.   R/   R0   R1   R2   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR4   l   s    	c         C   s   t  | ƒ ‚ d S(   s5    Raise a catchable error instead of exiting.
        N(   R    (   R   t   message(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR)      s    c         C   s   t  | ƒ } |  j | ƒ S(   sL    Split a string into an argument list and parse that argument list.
        (   R   t
   parse_args(   R   t	   argstringt   argv(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyt   parse_argstring„   s    N(	   R$   R%   R&   R3   R   t   FalseR4   R)   R9   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR'   i   s   		c         C   s³   t  |  d i  ƒ } d | k r7 t  |  d d ƒ | d <n  t |  ƒ } t | |  } d } xE |  j d d d … D]- } | j | | ƒ } | d k	 ro | } qo qo W| j ƒ  |  _ | S(   sB    Construct an argument parser using the function decorations.
    t   argcmd_kwdsR+   R&   Niÿÿÿÿ(   t   getattrR3   t	   real_nameR'   t
   decoratorst   add_to_parsert   format_helpR&   (   t
   magic_funct   kwdst   arg_namet   parsert   groupt   decot   result(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyt   construct_parser‹   s    c         C   s   |  j  j | ƒ S(   sA    Parse the string of arguments for the given magic function.
    (   RD   R9   (   RA   R7   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR9   ¡   s    c         C   s;   |  j  } | j d ƒ r+ | t d ƒ } n  t |  d | ƒ S(   s&    Find the real name of the magic.
    t   magic_t   argcmd_name(   R$   t
   startswitht   lenR<   (   RA   t
   magic_name(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR=   §   s    	t   ArgDecoratorc           B   s    e  Z d  Z d „  Z d „  Z RS(   sN    Base class for decorators to add ArgumentParser information to a method.
    c         C   s;   t  | d t ƒ s' t | _ g  | _ n  | j j |  ƒ | S(   Nt   has_arguments(   R<   R:   t   TrueRO   R>   R   (   R   t   func(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyt   __call__´   s
    	c         C   s   d S(   sD    Add this object's information to the parser, if necessary.
        N(    (   R   RD   RE   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR?   »   s    (   R$   R%   R&   RR   R?   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyRN   °   s   	t   magic_argumentsc           B   s#   e  Z d  Z d d „ Z d „  Z RS(   sS    Mark the magic as having argparse arguments and possibly adjust the
    name.
    c         C   s   | |  _  d  S(   N(   t   name(   R   RT   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR4   Æ   s    c         C   sX   t  | d t ƒ s' t | _ g  | _ n  |  j d  k	 rE |  j | _ n  t | ƒ | _	 | S(   NRO   (
   R<   R:   RP   RO   R>   RT   R3   RJ   RH   RD   (   R   RQ   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyRR   É   s    	N(   R$   R%   R&   R3   R4   RR   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyRS   Á   s   t   ArgMethodWrapperc           B   s&   e  Z d  Z d Z d „  Z d „  Z RS(   s‹   
    Base class to define a wrapper for ArgumentParser method.

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

    c         O   s   | |  _  | |  _ d  S(   N(   t   argsRB   (   R   RV   RB   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR4   à   s    	c         C   s8   | d k	 r | } n  t | |  j ƒ |  j |  j Ž  d S(   s6    Add this object's information to the parser.
        N(   R3   R<   t   _method_nameRV   RB   (   R   RD   RE   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR?   ä   s    	N(   R$   R%   R&   R3   RW   R4   R?   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyRU   Õ   s   	t   argumentc           B   s   e  Z d  Z d Z RS(   st    Store arguments and keywords to pass to add_argument().

    Instances also serve to decorate command methods.
    t   add_argument(   R$   R%   R&   RW   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyRX   í   s   t   defaultsc           B   s   e  Z d  Z d Z RS(   st    Store arguments and keywords to pass to set_defaults().

    Instances also serve to decorate command methods.
    t   set_defaults(   R$   R%   R&   RW   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyRZ   õ   s   t   argument_groupc           B   s   e  Z d  Z d „  Z RS(   sz    Store arguments and keywords to pass to add_argument_group().

    Instances also serve to decorate command methods.
    c         C   s   | j  |  j |  j Ž  S(   s6    Add this object's information to the parser.
        (   t   add_argument_groupRV   RB   (   R   RD   RE   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR?     s    (   R$   R%   R&   R?   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR\   ý   s   RB   c           B   s    e  Z d  Z d „  Z d „  Z RS(   s;    Provide other keywords to the sub-parser constructor.
    c         K   s   | |  _  d  S(   N(   RB   (   R   RB   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyR4     s    c         C   s(   t  t |  ƒ j | ƒ } |  j | _ | S(   N(   R   RB   RR   R;   (   R   RQ   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyRR     s    (   R$   R%   R&   R4   RR   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyRB   	  s   	R9   (   R&   R   t   ret   IPython.core.errorR    t   IPython.utils.decoratorsR   t   IPython.utils.processR   t   IPython.utils.textR   t   compileR   R   R   t   ArgumentParserR'   RH   R9   R=   t   objectRN   RS   RU   RX   RZ   R\   RB   t   __all__(    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magic_arguments.pyt   <module>-   s,   ("				