ó
&9_[c           @  sè   d  Z  d d l m Z d d l m Z d d l Z d d l Z d d l m Z m	 Z	 d d l
 m Z d d l m Z m Z m Z d d l m Z d d	 l m Z m Z d d
 l m Z e j e d „ Z e d e f d „  ƒ  Yƒ Z d S(   s   Extra magics for terminal use.iÿÿÿÿ(   t   print_function(   t   errorN(   t   TryNextt
   UsageError(   t   IPythonInputSplitter(   t   Magicst   magics_classt
   line_magic(   t   ClipboardEmpty(   t   SListt   strip_email_quotes(   t	   py3compatc         c  s   | s t  d |  ƒ d } n d } xW t r| y. t j | | ƒ ƒ } | |  k rT d S| VWq& t k
 rx t  d ƒ d SXq& Wd S(   sH    Yield pasted lines until the user enters the given sentinel value.
    sA   Pasting code; enter '%s' alone on the line to stop or use Ctrl-D.t   :t    Ns   <EOF>(   t   printt   TrueR   t   str_to_unicodet   EOFError(   t   sentinelt   l_inputt   quiett   promptt   l(    (    s6   lib/python2.7/site-packages/IPython/terminal/magics.pyt   get_pasted_lines   s    			
t   TerminalMagicsc           B  s†   e  Z d  „  Z d „  Z d „  Z d d „ Z e d d „ ƒ Z e d d „ ƒ Z e d d „ ƒ Z	 e
 j d	 k r„ e d
 „  ƒ Z n  RS(   c         C  s&   t  t |  ƒ j | ƒ t ƒ  |  _ d  S(   N(   t   superR   t   __init__R   t   input_splitter(   t   selft   shell(    (    s6   lib/python2.7/site-packages/IPython/terminal/magics.pyR   *   s    c         C  s†   | r3 t  | j ƒ  ƒ |  j j | <t d | ƒ nO |  j | ƒ } | |  j j d <t |  j _ z |  j j | ƒ Wd t	 |  j _ Xd S(   sM    Execute a block, or store it in a variable, per the user's request.
        s   Block assigned to '%s't   pasted_blockN(
   R	   t
   splitlinesR   t   user_nsR   t   preclean_inputR   t   using_paste_magicst   run_cellt   False(   R   t   blockt   namet   b(    (    s6   lib/python2.7/site-packages/IPython/terminal/magics.pyt   store_or_execute.   s    c         C  sG   | j  ƒ  } x% | r3 | d j ƒ  r3 | d } q Wt d j | ƒ ƒ S(   Ni    i   s   
(   R   t   stripR
   t   join(   R   R%   t   lines(    (    s6   lib/python2.7/site-packages/IPython/terminal/magics.pyR!   >   s    R   c         C  s   |  j  j j | ƒ } | d k r0 t d ƒ ‚ n  t | t j ƒ sQ t d ƒ ‚ n  t d | j	 d d ƒ d t
 | ƒ f ƒ |  j  j | ƒ d S(   s,    Rerun a previously pasted command.
        s"   No previous pasted block availables6   Variable 'pasted_block' is not a string, can't executes   Re-executing '%s...' (%d chars)s   
i   i    N(   R   R    t   gett   NoneR   t
   isinstanceR   t   string_typesR   t   splitt   lenR#   (   R   R&   R'   (    (    s6   lib/python2.7/site-packages/IPython/terminal/magics.pyt   rerun_pastedD   s    *R   c         C  s8   t  d ƒ |  j j ƒ  t  d d d g |  j j ƒ d S(   s%   Toggle autoindent on/off (deprecated)sp   %autoindent is deprecated since IPython 5: you can now paste multiple lines without turning autoindentation off.s   Automatic indentation is:t   OFFt   ONN(   R   R   t   set_autoindentt
   autoindent(   R   t   parameter_s(    (    s6   lib/python2.7/site-packages/IPython/terminal/magics.pyR6   S   s    
c         C  s…   |  j  | d d d ƒ\ } } d | k r8 |  j ƒ  d Sd | k } | j d d ƒ } d	 j t | d
 | ƒƒ } |  j | | ƒ d S(   s–  Paste & execute a pre-formatted code block from clipboard.

        You must terminate the block with '--' (two minus-signs) or Ctrl-D
        alone on the line. You can also provide your own sentinel with '%paste
        -s %%' ('%%' is the new sentinel for this operation).

        The block is dedented prior to execution to enable execution of method
        definitions. '>' and '+' characters at the beginning of a line are
        ignored, to allow pasting directly from e-mails, diff files and
        doctests (the '...' continuation prompt is also stripped).  The
        executed block is also assigned to variable named 'pasted_block' for
        later editing with '%edit pasted_block'.

        You can also pass a variable name as an argument, e.g. '%cpaste foo'.
        This assigns the pasted block to variable 'foo' as string, without
        dedenting or executing it (preceding >>> and + is still stripped)

        '%cpaste -r' re-executes the block previously entered by cpaste.
        '%cpaste -q' suppresses any additional output messages.

        Do not be alarmed by garbled output on Windows (it's a readline bug).
        Just press enter and type -- (and press enter again) and the block
        will be what was just pasted.

        IPython statements (magics, shell escapes) are not supported (yet).

        See also
        --------
        paste: automatically pull code from clipboard.

        Examples
        --------
        ::

          In [8]: %cpaste
          Pasting code; enter '--' alone on the line to stop.
          :>>> a = ["world!", "Hello"]
          :>>> print " ".join(sorted(a))
          :--
          Hello world!
        s   rqs:t   modet   stringt   rNt   qt   su   --s   
R   (   t   parse_optionsR2   R,   R*   R   R(   (   R   R7   t   optsR&   R   R   R%   (    (    s6   lib/python2.7/site-packages/IPython/terminal/magics.pyt   cpaste[   s    +
c         C  s  |  j  | d d d ƒ\ } } d | k r8 |  j ƒ  d Sy |  j j j ƒ  } Wn` t k
 r” } t | d ƒ } | r† t | d ƒ n
 t d ƒ d St k
 r° t	 d	 ƒ ‚ n Xd
 | k r|  j j
 } | |  j j | ƒ ƒ | j d ƒ sû | d ƒ n  | d ƒ n  |  j | | ƒ d S(   sä  Paste & execute a pre-formatted code block from clipboard.

        The text is pulled directly from the clipboard without user
        intervention and printed back on the screen before execution (unless
        the -q flag is given to force quiet mode).

        The block is dedented prior to execution to enable execution of method
        definitions. '>' and '+' characters at the beginning of a line are
        ignored, to allow pasting directly from e-mails, diff files and
        doctests (the '...' continuation prompt is also stripped).  The
        executed block is also assigned to variable named 'pasted_block' for
        later editing with '%edit pasted_block'.

        You can also pass a variable name as an argument, e.g. '%paste foo'.
        This assigns the pasted block to variable 'foo' as string, without
        executing it (preceding >>> and + is still stripped).

        Options:

          -r: re-executes the block previously entered by cpaste.

          -q: quiet mode: do not echo the pasted text back to the terminal.

        IPython statements (magics, shell escapes) are not supported (yet).

        See also
        --------
        cpaste: manually paste code into terminal until you mark its end.
        t   rqR8   R9   R:   Nt   argsi    s&   Could not get text from the clipboard.s!   The clipboard appears to be emptyR;   s   
s   ## -- End pasted text --
(   R=   R2   R   t   hookst   clipboard_getR   t   getattrR   R   R   t   writet
   pycolorizet   endswithR(   (   R   R7   R>   R&   R%   t   clipboard_exct   messageRE   (    (    s6   lib/python2.7/site-packages/IPython/terminal/magics.pyt   paste‘   s*    

t   win32c         C  s   t  j d ƒ d S(   s   Clear screen.
            t   clsN(   t   ost   system(   R   R<   (    (    s6   lib/python2.7/site-packages/IPython/terminal/magics.pyRL   Ì   s    (   t   __name__t
   __module__R   R(   R!   R2   R   R6   R?   RJ   t   syst   platformRL   (    (    (    s6   lib/python2.7/site-packages/IPython/terminal/magics.pyR   (   s   			59(   t   __doc__t
   __future__R    t   loggingR   RM   RQ   t   IPython.core.errorR   R   t   IPython.core.inputsplitterR   t   IPython.core.magicR   R   R   t   IPython.lib.clipboardR   t   IPython.utils.textR	   R
   t   IPython.utilsR   t   inputR$   R   R   (    (    (    s6   lib/python2.7/site-packages/IPython/terminal/magics.pyt   <module>   s   