splitinput.py
138 lines
| 4.7 KiB
| text/x-python
|
PythonLexer
|
r2244 | # encoding: utf-8 | ||
""" | ||||
|
r4746 | Simple utility for splitting user input. This is used by both inputsplitter and | ||
prefilter. | ||||
|
r2244 | |||
Authors: | ||||
* Brian Granger | ||||
* Fernando Perez | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
|
r5390 | # Copyright (C) 2008-2011 The IPython Development Team | ||
|
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 | ||||
|
r3038 | import sys | ||
|
r2244 | |||
|
r4731 | from IPython.utils import py3compat | ||
|
r6655 | from IPython.utils.encoding import get_stream_enc | ||
|
r28165 | from IPython.core.oinspect import OInfo | ||
|
r4731 | |||
|
r2244 | #----------------------------------------------------------------------------- | ||
# Main function | ||||
#----------------------------------------------------------------------------- | ||||
# RegExp for splitting line contents into pre-char//first word-method//rest. | ||||
# For clarity, each group in on one line. | ||||
|
r4746 | # WARNING: update the regexp if the escapes in interactiveshell are changed, as | ||
# they are hardwired in. | ||||
|
r2244 | |||
# Although it's not solely driven by the regex, note that: | ||||
# ,;/% only trigger if they are the first character on the line | ||||
|
r4872 | # ! and !! trigger if they are first char(s) *or* follow an indent | ||
|
r2244 | # ? triggers as first or last char. | ||
|
r24452 | line_split = re.compile(r""" | ||
|
r4746 | ^(\s*) # any leading space | ||
([,;/%]|!!?|\?\??)? # escape character or characters | ||||
|
r6998 | \s*(%{0,2}[\w\.\*]*) # function/method, possibly with leading % | ||
|
r4746 | # to correctly treat things like '?%magic' | ||
(.*?$|$) # rest of line | ||||
""", re.VERBOSE) | ||||
|
r2244 | |||
|
r6912 | |||
|
r2244 | def split_user_input(line, pattern=None): | ||
|
r4746 | """Split user input into initial whitespace, escape character, function part | ||
and the rest. | ||||
|
r2256 | """ | ||
|
r3038 | # We need to ensure that the rest of this routine deals only with unicode | ||
|
r6655 | encoding = get_stream_enc(sys.stdin, 'utf-8') | ||
|
r6651 | line = py3compat.cast_unicode(line, encoding) | ||
|
r4872 | |||
|
r2244 | if pattern is None: | ||
pattern = line_split | ||||
match = pattern.match(line) | ||||
if not match: | ||||
|
r28756 | # print("match failed for line '%s'" % line) | ||
|
r2244 | try: | ||
ifun, the_rest = line.split(None,1) | ||||
except ValueError: | ||||
|
r28756 | # print("split failed for line '%s'" % line) | ||
|
r3038 | ifun, the_rest = line, u'' | ||
|
r24452 | pre = re.match(r'^(\s*)(.*)',line).groups()[0] | ||
|
r4744 | esc = "" | ||
|
r2244 | else: | ||
|
r4744 | pre, esc, ifun, the_rest = match.groups() | ||
|
r2244 | |||
|
r28756 | # print('line:<%s>' % line) # dbg | ||
# print('pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest)) # dbg | ||||
|
r4746 | return pre, esc or '', ifun.strip(), the_rest.lstrip() | ||
|
r6912 | |||
|
r4746 | class LineInfo(object): | ||
"""A single line of input and associated info. | ||||
|
r4872 | Includes the following as properties: | ||
|
r4746 | |||
line | ||||
The original, raw line | ||||
|
r4872 | |||
|
r4746 | continue_prompt | ||
Is this line a continuation in a sequence of multiline input? | ||||
|
r4872 | |||
|
r4746 | pre | ||
Any leading whitespace. | ||||
|
r4872 | |||
|
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. | ||||
|
r4872 | |||
|
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 | ||||
"*". | ||||
|
r4872 | |||
|
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: | ||||
|
r24132 | self.pre_whitespace = '' # No whitespace allowed before esc chars | ||
|
r4872 | else: | ||
|
r4746 | self.pre_whitespace = self.pre | ||
|
r28165 | def ofind(self, ip) -> OInfo: | ||
|
r4746 | """Do a full, attribute-walking lookup of the ifun in the various | ||
namespaces for the given IPython InteractiveShell instance. | ||||
|
r6912 | Return a dict with keys: {found, obj, ospace, ismagic} | ||
|
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. | ||||
""" | ||||
|
r6913 | return ip._ofind(self.ifun) | ||
|
r4746 | |||
|
r4872 | def __str__(self): | ||
return "LineInfo [%s|%s|%s|%s]" %(self.pre, self.esc, self.ifun, self.the_rest) | ||||