##// END OF EJS Templates
More Python 3 compatibility fixes in core.
More Python 3 compatibility fixes in core.

File last commit:

r4744:17afbbef
r4745:ef3358bc
Show More
splitinput.py
86 lines | 2.8 KiB | text/x-python | PythonLexer
Brian Granger
More work on refactoring things into components....
r2244 # encoding: utf-8
"""
Simple utility for splitting user input.
Authors:
* Brian Granger
* Fernando Perez
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2009 The IPython Development Team
#
# 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
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.
Brian Granger
Moving and renaming in preparation of subclassing InteractiveShell....
r2760 # WARNING: update the regexp if the escapes in interactiveshell are changed, as they
Brian Granger
More work on refactoring things into components....
r2244 # are hardwired in.
# Although it's not solely driven by the regex, note that:
# ,;/% only trigger if they are the first character on the line
# ! and !! trigger if they are first char(s) *or* follow an indent
# ? triggers as first or last char.
Thomas Kluyver
Improvements in the code that breaks up user input.
r4744 # The four parts of the regex are:
# 1) pre: initial whitespace
# 2) esc: escape character
# 3) ifun: first word/method (mix of \w and '.')
# 4) the_rest: rest of line (separated from ifun by space if non-empty)
line_split = re.compile(r'^(\s*)'
r'([,;/%?]|!!?)?'
Brian Granger
More work on refactoring things into components....
r2244 r'\s*([\w\.]+)'
Thomas Kluyver
Improvements in the code that breaks up user input.
r4744 r'(.*$|$)')
Brian Granger
More work on refactoring things into components....
r2244
Brian Granger
First go an implementing a=!ls and a=%who syntax....
r2256 # r'[\w\.]+'
# r'\s*=\s*%.*'
Brian Granger
More work on refactoring things into components....
r2244
def split_user_input(line, pattern=None):
Brian Granger
First go an implementing a=!ls and a=%who syntax....
r2256 """Split user input into pre-char/whitespace, function part and rest.
This is currently handles lines with '=' in them in a very inconsistent
manner.
"""
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
Thomas Kluyver
Start using py3compat module.
r4731 line = py3compat.cast_unicode(line, sys.stdin.encoding or 'utf-8')
Fernando Perez
Unicode fixes, basic input/printing of unicode works.
r3038
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()
Thomas Kluyver
Start using py3compat module.
r4731
Thomas Kluyver
Improvements in the code that breaks up user input.
r4744 if not py3compat.isidentifier(ifun, dotted=True):
the_rest = ifun + u' ' + the_rest
ifun = u''
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
Improvements in the code that breaks up user input.
r4744 return pre, esc, ifun.strip(), the_rest.lstrip()