ó
æNXc        	   @   s¶  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m	 Z	 m
 Z
 m Z m Z m Z m Z m Z m Z m Z m Z m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z e j d j d d d	 d
 d d d g ƒ ƒ Z  e j d ƒ Z! e j d ƒ Z" d „  Z# d „  Z$ e j d e j% ƒ Z& e j d e j% ƒ Z' d „  Z( d „  Z) d „  Z* d e+ f d „  ƒ  YZ, d e, f d „  ƒ  YZ- d S(   sÞ  Input handling and transformation machinery.

The first class in this module, :class:`InputSplitter`, is designed to tell when
input from a line-oriented frontend is complete and should be executed, and when
the user should be prompted for another line of code instead. The name 'input
splitter' is largely for historical reasons.

A companion, :class:`IPythonInputSplitter`, provides the same functionality but
with full support for the extended IPython syntax (magics, system calls, etc).
The code to actually do these transformations is in :mod:`IPython.core.inputtransformer`.
:class:`IPythonInputSplitter` feeds the raw code to the transformers in order
and stores the results.

For more details, see the class docstrings below.
iÿÿÿÿN(   t   cast_unicode(   t   leading_indentt   classic_promptt
   ipy_promptt   strip_encoding_cookiet	   cellmagict   assemble_logical_linest   help_endt   escaped_commandst   assign_from_magict   assign_from_systemt   assemble_python_lines(
   t	   ESC_SHELLt
   ESC_SH_CAPt   ESC_HELPt	   ESC_HELP2t	   ESC_MAGICt
   ESC_MAGIC2t	   ESC_QUOTEt
   ESC_QUOTE2t	   ESC_PARENt   ESC_SEQUENCESt   |s   ^\s+raise(\s.*)?$s   ^\s+raise\([^\)]*\).*$s   ^\s+return(\s.*)?$s   ^\s+return\([^\)]*\).*$s   ^\s+pass\s*$s   ^\s+break\s*$s   ^\s+continue\s*$s   ^([ \t\r\f\v]+)s   ^\s*\#c         C   s'   t  j |  ƒ } | r | j ƒ  Sd Sd S(   s  Return the number of initial spaces in a string.

    Note that tabs are counted as a single space.  For now, we do *not* support
    mixing of tabs and spaces in the user's input.

    Parameters
    ----------
    s : string

    Returns
    -------
    n : int
    i    N(   t   ini_spaces_ret   matcht   end(   t   st
   ini_spaces(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   num_ini_spacesF   s    
c         C   s0   |  s
 t  S|  j ƒ  d } | d k p/ | j ƒ  S(   sÌ   Determine if the input source ends in a blank.

    A blank is either a newline or a line consisting of whitespace.

    Parameters
    ----------
    src : string
      A single or multiline string.
    iÿÿÿÿt    (   t   Falset
   splitlinest   isspace(   t   srct   ll(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt
   last_blank[   s    
 s   \n\s*\n\s*$s   .+\n\s*\n\s+$c         C   sR   |  s
 t  Sd j d g |  j ƒ  d ƒ } t t j | ƒ ƒ pQ t t j | ƒ ƒ S(   sÏ   Determine if the input source ends in two blanks.

    A blank is either a newline or a line consisting of whitespace.

    Parameters
    ----------
    src : string
      A single or multiline string.
    s   
s   ###
iþÿÿÿ(   R   t   joinR   t   boolt   last_two_blanks_reR   t   last_two_blanks_re2(   R!   t   new_src(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   last_two_blanksm   s
    
  c         C   s   t  j d d |  ƒ S(   s  Remove all comments from input source.

    Note: comments are NOT recognized inside of strings!

    Parameters
    ----------
    src : string
      A single or multiline input string.

    Returns
    -------
    String with all Python comments removed.
    s   #.*R   (   t   ret   sub(   R!   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   remove_comments„   s    c          C   s.   t  t j d d ƒ }  |  d k r* d }  n  |  S(   sc   Return the default standard input encoding.

    If sys.stdin has no encoding, 'ascii' is returned.t   encodingt   asciiN(   t   getattrt   syst   stdint   None(   R-   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   get_input_encoding–   s    	t   InputSplitterc           B   s¤   e  Z d  Z d Z d Z d Z d Z d Z d Z	 e
 Z d Z e
 Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d d d „ Z d „  Z RS(   s$  An object that can accumulate lines of Python source before execution.

    This object is designed to be fed python source line-by-line, using
    :meth:`push`. It will return on each push whether the currently pushed
    code could be executed already. In addition, it provides a method called
    :meth:`push_accepts_more` that can be used to query whether more input
    can be pushed into a single interactive block.

    This is a simple example of how an interactive terminal-based client can use
    this tool::

        isp = InputSplitter()
        while isp.push_accepts_more():
            indent = ' '*isp.indent_spaces
            prompt = '>>> ' + indent
            line = indent + raw_input(prompt)
            isp.push(line)
        print 'Input source was:\n', isp.source_reset(),
    i    R   c         C   s(   g  |  _  t j ƒ  |  _ t ƒ  |  _ d S(   s-   Create a new InputSplitter instance.
        N(   t   _buffert   codeopt   CommandCompilert   _compileR3   R-   (   t   self(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   __init__×   s    	c         C   sD   d |  _  g  |  j (d |  _ d |  _ t |  _ t |  _ t |  _ d S(   s,   Reset the input buffer and associated state.i    R   N(	   t   indent_spacesR5   t   sourceR2   t   codeR   t   _is_completet   _is_invalidt   _full_dedent(   R9   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   resetÞ   s    	
				c         C   s   |  j  } |  j ƒ  | S(   s:   Return the input source and perform a full reset.
        (   R<   RA   (   R9   t   out(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   source_resetè   s    	
c         C   sp   |  j  ƒ  zT y |  j | ƒ Wn t k
 r2 d SX|  j r@ d S|  j ƒ  rY d |  j f Sd SWd |  j  ƒ  Xd S(   sy  Return whether a block of code is ready to execute, or should be continued
        
        This is a non-stateful API, and will reset the state of this InputSplitter.
        
        Parameters
        ----------
        source : string
          Python input code, which can be multiline.
        
        Returns
        -------
        status : str
          One of 'complete', 'incomplete', or 'invalid' if source is not a
          prefix of valid code.
        indent_spaces : int or None
          The number of spaces by which to indent the next line of code. If
          status is not 'incomplete', this is None.
        t   invalidt
   incompletet   completeN(   s   invalidN(   s   invalidN(   RF   N(   RA   t   pusht   SyntaxErrorR2   R?   t   push_accepts_moreR;   (   R9   R<   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   check_completeï   s    
 	c      
   C   sá   |  j  | ƒ |  j } d \ |  _ |  _ t |  _ | j d ƒ rD t S|  j | ƒ y? t	 j
 ƒ  - t	 j d t ƒ |  j | d d ƒ|  _ Wd QXWn5 t t t t t t f k
 rÇ t |  _ t |  _ n X|  j d k	 |  _ |  j S(   s÷  Push one or more lines of input.

        This stores the given lines and returns a status code indicating
        whether the code forms a complete Python block or not.

        Any exceptions generated in compilation are swallowed, but if an
        exception was produced, the method returns True.

        Parameters
        ----------
        lines : string
          One or more lines of Python input.

        Returns
        -------
        is_complete : boolean
          True if the current input source (the result of the current input
          plus prior inputs) forms a complete Python execution block.  Note that
          this value is also stored as a private attribute (``_is_complete``), so it
          can be queried at any time.
        s   \
t   errort   symbolt   execN(   NN(   t   _storeR<   R2   R=   R>   R   R?   t   endswitht   _update_indentt   warningst   catch_warningst   simplefiltert   SyntaxWarningR8   RH   t   OverflowErrort
   ValueErrort	   TypeErrort   MemoryErrort   True(   R9   t   linesR<   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyRG     s"    		"	c         C   sÐ   |  j  s t S|  j j ƒ  d } | s3 | j ƒ  r7 t S|  j d k rÌ t |  j j ƒ  ƒ d k re t Sy t j	 d j
 |  j ƒ ƒ } Wn t k
 r˜ t SXt | j ƒ d k rÌ t | j d d ƒ rÌ t Sn  t S(   s¯  Return whether a block of interactive input can accept more input.

        This method is meant to be used by line-oriented frontends, who need to
        guess whether a block is complete or not based solely on prior and
        current input lines.  The InputSplitter considers it has a complete
        interactive block and will not accept more input when either:
        
        * A SyntaxError is raised

        * The code is complete and consists of a single line or a single
          non-compound statement

        * The code is complete and has a blank line at the end

        If the current input produces a syntax error, this method immediately
        returns False but does *not* raise the syntax error exception, as
        typically clients will want to send invalid syntax to an execution
        backend which might convert the invalid syntax into valid Python via
        one of the dynamic IPython mechanisms.
        iÿÿÿÿi    i   u    t   body(   R>   RY   R<   R   R    R   R;   t   lent   astt   parseR$   R5   t	   ExceptionR[   t   hasattr(   R9   t	   last_linet   code_ast(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyRI   K  s     	c         C   s»   |  j  } |  j } t | ƒ } | | k  rH | } | d k rH t } qH n  | j ƒ  d d k rk | d 7} n1 t j | ƒ rœ | d 8} | d k rœ t } qœ n  | d k  r± d } n  | | f S(   sÓ  Compute the new indentation level for a single line.

        Parameters
        ----------
        line : str
          A single new line of non-whitespace, non-comment Python input.

        Returns
        -------
        indent_spaces : int
          New value for the indent level (it may be equal to self.indent_spaces
        if indentation doesn't change.

        full_dedent : boolean
          Whether the new line causes a full flush-left dedent.
        i    iÿÿÿÿt   :i   (   R;   R@   R   RY   t   rstript	   dedent_reR   (   R9   t   lineR;   t   full_dedentt   inisp(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   _find_indent†  s     		
	c         C   sR   xK t  | ƒ j ƒ  D]7 } | r | j ƒ  r |  j | ƒ \ |  _ |  _ q q Wd  S(   N(   R,   R   R    Ri   R;   R@   (   R9   RZ   Rf   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyRP   ¯  s    R<   c         C   se   | d k r |  j } n  | j d ƒ r7 | j | ƒ n | j | d ƒ t |  | |  j | ƒ ƒ d S(   s‚   Store one or more lines of input.

        If input lines are not newline-terminated, a newline is automatically
        appended.s   
N(   R2   R5   RO   t   appendt   setattrt   _set_source(   R9   RZ   t   buffert   store(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyRN   ´  s    c         C   s   d j  | ƒ S(   Nu    (   R$   (   R9   Rm   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyRl   Ã  s    N(   t   __name__t
   __module__t   __doc__R;   R-   R<   R2   R=   R5   R8   R   R@   R>   R?   R:   RA   RC   RJ   RG   RI   Ri   RP   RN   Rl   (    (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyR4   ¥   s(   		
		$	8	;	)	t   IPythonInputSplitterc           B   s¡   e  Z d  Z d Z e Z e Z d Z e	 d d d d „ Z
 e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z RS(   sB   An input splitter that recognizes all of IPython's special syntax.R   c         C   sà   t  t |  ƒ j ƒ  g  |  _ t |  _ | d  k	 r= | |  _ n- t ƒ  t	 ƒ  t
 ƒ  t d | ƒ t ƒ  g |  _ t ƒ  |  _ | d  k	 rŽ | |  _ n! t ƒ  t ƒ  t ƒ  t ƒ  g |  _ t ƒ  |  _ | d  k	 rÓ | |  _ n	 g  |  _ d  S(   Nt   end_on_blank_line(   t   superRr   R:   t   _buffer_rawRY   t	   _validateR2   t   physical_line_transformsR   R   R   R   R   R   t   logical_line_transformsR   R   R	   R
   R   t   python_line_transforms(   R9   t   line_input_checkerRw   Rx   Ry   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyR:   Ú  s*    		c         C   s)   |  j  |  j g |  j |  j g |  j S(   s!   Quick access to all transformers.(   Rw   R   Rx   R   Ry   (   R9   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt
   transformsý  s    c         C   sB   |  j  } |  j s- | |  j g |  j 7} n  | |  j g |  j S(   sT   Transformers, excluding logical line transformers if we're in a
        Python line.(   Rw   t   within_python_lineR   Rx   R   Ry   (   R9   t   t(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   transforms_in_use  s    
	c         C   sr   t  t |  ƒ j ƒ  g  |  j (d |  _ t |  _ t |  _ x3 |  j D]( } y | j ƒ  WqB t	 k
 ri qB XqB Wd S(   s,   Reset the input buffer and associated state.R   N(
   Rt   Rr   RA   Ru   t
   source_rawR   t   transformer_accumulatingR|   R{   RH   (   R9   R}   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyRA     s    
			c         C   sa   d „  } g  } x  |  j  D] } | | | ƒ } q Wt | ƒ } | r] |  j d j | ƒ ƒ n  d  S(   Nc         s   so   xH | D]@ } x7 | j  ƒ  D]) } |  j | ƒ } | d k	 r | Vq q Wq W|  j ƒ  } | d k	 rk | Vn  d S(   sA  yield transformed lines
            
            always strings, never None
            
            transform: the current transform
            outs: an iterable of previously transformed inputs.
                 Each may be multiline, which will be passed
                 one line at a time to transform.
            N(   R   RG   R2   RA   (   t	   transformt   outsRB   Rf   t   tmp(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   _flush  s    
s   
(   R~   t   listRN   R$   (   R9   R„   RB   R}   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   flush_transformers  s    	c         C   s   |  j  } |  j ƒ  | S(   s8   Return raw input only and perform a full reset.
        (   R   RA   (   R9   RB   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt	   raw_reset<  s    	
c         C   s'   z |  j  ƒ  |  j SWd  |  j ƒ  Xd  S(   N(   R†   R<   RA   (   R9   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyRC   C  s    
c         C   s$   |  j  r t St t |  ƒ j ƒ  Sd  S(   N(   R€   RY   Rt   Rr   RI   (   R9   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyRI   J  s    	c         C   s>   |  j  ƒ  z" |  j | ƒ |  j ƒ  |  j SWd |  j  ƒ  Xd S(   s/   Process and translate a cell of input.
        N(   RA   RG   R†   R<   (   R9   t   cell(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   transform_cellP  s    

c         C   sj   t  | |  j ƒ } | j ƒ  } | s0 d g } n  |  j | |  j d ƒ x | D] } |  j | ƒ } qM W| S(   s@  Push one or more lines of IPython input.

        This stores the given lines and returns a status code indicating
        whether the code forms a complete Python block or not, after processing
        all input lines for special IPython syntax.

        Any exceptions generated in compilation are swallowed, but if an
        exception was produced, the method returns True.

        Parameters
        ----------
        lines : string
          One or more lines of Python input.

        Returns
        -------
        is_complete : boolean
          True if the current input source (the result of the current input
          plus prior inputs) forms a complete Python execution block.  Note that
          this value is also stored as a private attribute (_is_complete), so it
          can be queried at any time.
        R   R   (   R    R-   R   RN   Ru   t	   push_line(   R9   RZ   t
   lines_listRf   RB   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyRG   [  s    c            sP  ˆ  j  } ‡  f d †  } x6 ˆ  j D]+ } | j | ƒ } | d  k r" | | ƒ Sq" Wˆ  j s¾ ˆ  j j | ƒ } | d  k r‚ | d ƒ Sx9 ˆ  j D]+ } | j | ƒ } | d  k rŒ | | ƒ SqŒ Wn  ˆ  j j | ƒ } | d  k rï t ˆ  _ | d ƒ St	 ˆ  _ x6 ˆ  j
 D]+ } | j | ƒ } | d  k r| | ƒ SqWt	 ˆ  _ t t ˆ  ƒ j | ƒ S(   Nc            s   t  ˆ  _ t S(   N(   RY   R€   R   (   t   dbg(   R9   (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   _accumulatingˆ  s    	s   acc logical lines   acc python line(   R5   Rw   RG   R2   R|   R   Rx   R   RY   R   Ry   R€   Rt   Rr   (   R9   Rf   t   bufR   t   transformer(    (   R9   sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyRŠ   …  s2    		
	
		N(   Ro   Rp   Rq   R   R   R€   R|   R2   Ru   RY   R:   t   propertyR{   R~   RA   R†   R‡   RC   RI   R‰   RG   RŠ   (    (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyRr   Ç  s"   "								*(.   Rq   R]   R6   R*   R0   RQ   t   IPython.utils.py3compatR    t   IPython.core.inputtransformerR   R   R   R   R   R   R   R   R	   R
   R   R   R   R   R   R   R   R   R   R   R   t   compileR$   Re   R   t   comment_line_reR   R#   t	   MULTILINER&   R'   R)   R,   R3   t   objectR4   Rr   (    (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/inputsplitter.pyt   <module>   s8   LF					ÿ #