##// END OF EJS Templates
BUG: ipython_directive now supports cython magic (remove dangling space) GH4791
BUG: ipython_directive now supports cython magic (remove dangling space) GH4791

File last commit:

r6998:d2a11a76
r14213:01fc30e9
Show More
splitinput.py
137 lines | 4.7 KiB | text/x-python | PythonLexer
Brian Granger
More work on refactoring things into components....
r2244 # encoding: utf-8
"""
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746 Simple utility for splitting user input. This is used by both inputsplitter and
prefilter.
Brian Granger
More work on refactoring things into components....
r2244
Authors:
* Brian Granger
* Fernando Perez
"""
#-----------------------------------------------------------------------------
Matthias BUSSONNIER
update copyright to 2011/20xx-2011...
r5390 # Copyright (C) 2008-2011 The IPython Development Team
Brian Granger
More work on refactoring things into components....
r2244 #
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import re
Fernando Perez
Unicode fixes, basic input/printing of unicode works.
r3038 import sys
Brian Granger
More work on refactoring things into components....
r2244
Thomas Kluyver
Start using py3compat module.
r4731 from IPython.utils import py3compat
Brandon Parsons
Feedback from pull request #1245
r6655 from IPython.utils.encoding import get_stream_enc
Thomas Kluyver
Start using py3compat module.
r4731
Brian Granger
More work on refactoring things into components....
r2244 #-----------------------------------------------------------------------------
# Main function
#-----------------------------------------------------------------------------
# RegExp for splitting line contents into pre-char//first word-method//rest.
# For clarity, each group in on one line.
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746 # WARNING: update the regexp if the escapes in interactiveshell are changed, as
# they are hardwired in.
Brian Granger
More work on refactoring things into components....
r2244
# Although it's not solely driven by the regex, note that:
# ,;/% only trigger if they are the first character on the line
Bernardo B. Marques
remove all trailling spaces
r4872 # ! and !! trigger if they are first char(s) *or* follow an indent
Brian Granger
More work on refactoring things into components....
r2244 # ? triggers as first or last char.
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746 line_split = re.compile("""
^(\s*) # any leading space
([,;/%]|!!?|\?\??)? # escape character or characters
Fernando Perez
Fix split_user_input to correctly handle %% escape for cell magics.
r6998 \s*(%{0,2}[\w\.\*]*) # function/method, possibly with leading %
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746 # to correctly treat things like '?%magic'
(.*?$|$) # rest of line
""", re.VERBOSE)
Brian Granger
More work on refactoring things into components....
r2244
Fernando Perez
Fix more failures due to direct shell access in magics.
r6912
Brian Granger
More work on refactoring things into components....
r2244 def split_user_input(line, pattern=None):
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746 """Split user input into initial whitespace, escape character, function part
and the rest.
Brian Granger
First go an implementing a=!ls and a=%who syntax....
r2256 """
Fernando Perez
Unicode fixes, basic input/printing of unicode works.
r3038 # We need to ensure that the rest of this routine deals only with unicode
Brandon Parsons
Feedback from pull request #1245
r6655 encoding = get_stream_enc(sys.stdin, 'utf-8')
Brandon Parsons
pythonw in py3k sets std{in,out,err} to None...
r6651 line = py3compat.cast_unicode(line, encoding)
Bernardo B. Marques
remove all trailling spaces
r4872
Brian Granger
More work on refactoring things into components....
r2244 if pattern is None:
pattern = line_split
match = pattern.match(line)
if not match:
Brian Granger
First go an implementing a=!ls and a=%who syntax....
r2256 # print "match failed for line '%s'" % line
Brian Granger
More work on refactoring things into components....
r2244 try:
ifun, the_rest = line.split(None,1)
except ValueError:
Brian Granger
First go an implementing a=!ls and a=%who syntax....
r2256 # print "split failed for line '%s'" % line
Fernando Perez
Unicode fixes, basic input/printing of unicode works.
r3038 ifun, the_rest = line, u''
Brian Granger
More work on refactoring things into components....
r2244 pre = re.match('^(\s*)(.*)',line).groups()[0]
Thomas Kluyver
Improvements in the code that breaks up user input.
r4744 esc = ""
Brian Granger
More work on refactoring things into components....
r2244 else:
Thomas Kluyver
Improvements in the code that breaks up user input.
r4744 pre, esc, ifun, the_rest = match.groups()
Brian Granger
More work on refactoring things into components....
r2244
#print 'line:<%s>' % line # dbg
#print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746 return pre, esc or '', ifun.strip(), the_rest.lstrip()
Fernando Perez
Fix more failures due to direct shell access in magics.
r6912
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746 class LineInfo(object):
"""A single line of input and associated info.
Bernardo B. Marques
remove all trailling spaces
r4872 Includes the following as properties:
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746
line
The original, raw line
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746 continue_prompt
Is this line a continuation in a sequence of multiline input?
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746 pre
Any leading whitespace.
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746 esc
The escape character(s) in pre or the empty string if there isn't one.
Note that '!!' and '??' are possible values for esc. Otherwise it will
always be a single character.
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746 ifun
The 'function part', which is basically the maximal initial sequence
of valid python identifiers and the '.' character. This is what is
checked for alias and magic transformations, used for auto-calling,
etc. In contrast to Python identifiers, it may start with "%" and contain
"*".
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746 the_rest
Everything else on the line.
"""
def __init__(self, line, continue_prompt=False):
self.line = line
self.continue_prompt = continue_prompt
self.pre, self.esc, self.ifun, self.the_rest = split_user_input(line)
self.pre_char = self.pre.strip()
if self.pre_char:
self.pre_whitespace = '' # No whitespace allowd before esc chars
Bernardo B. Marques
remove all trailling spaces
r4872 else:
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746 self.pre_whitespace = self.pre
def ofind(self, ip):
"""Do a full, attribute-walking lookup of the ifun in the various
namespaces for the given IPython InteractiveShell instance.
Fernando Perez
Fix more failures due to direct shell access in magics.
r6912 Return a dict with keys: {found, obj, ospace, ismagic}
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746
Note: can cause state changes because of calling getattr, but should
only be run if autocall is on and if the line hasn't matched any
other, less dangerous handlers.
Does cache the results of the call, so can be called multiple times
without worrying about *further* damaging state.
"""
Fernando Perez
Simplify line info code.
r6913 return ip._ofind(self.ifun)
Thomas Kluyver
Reuse common code for inputsplitter and prefilter.
r4746
Bernardo B. Marques
remove all trailling spaces
r4872 def __str__(self):
return "LineInfo [%s|%s|%s|%s]" %(self.pre, self.esc, self.ifun, self.the_rest)