splitinput.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # encoding: utf-8
  2. """
  3. Simple utility for splitting user input. This is used by both inputsplitter and
  4. prefilter.
  5. Authors:
  6. * Brian Granger
  7. * Fernando Perez
  8. """
  9. #-----------------------------------------------------------------------------
  10. # Copyright (C) 2008-2011 The IPython Development Team
  11. #
  12. # Distributed under the terms of the BSD License. The full license is in
  13. # the file COPYING, distributed as part of this software.
  14. #-----------------------------------------------------------------------------
  15. #-----------------------------------------------------------------------------
  16. # Imports
  17. #-----------------------------------------------------------------------------
  18. import re
  19. import sys
  20. from IPython.utils import py3compat
  21. from IPython.utils.encoding import get_stream_enc
  22. #-----------------------------------------------------------------------------
  23. # Main function
  24. #-----------------------------------------------------------------------------
  25. # RegExp for splitting line contents into pre-char//first word-method//rest.
  26. # For clarity, each group in on one line.
  27. # WARNING: update the regexp if the escapes in interactiveshell are changed, as
  28. # they are hardwired in.
  29. # Although it's not solely driven by the regex, note that:
  30. # ,;/% only trigger if they are the first character on the line
  31. # ! and !! trigger if they are first char(s) *or* follow an indent
  32. # ? triggers as first or last char.
  33. line_split = re.compile("""
  34. ^(\s*) # any leading space
  35. ([,;/%]|!!?|\?\??)? # escape character or characters
  36. \s*(%{0,2}[\w\.\*]*) # function/method, possibly with leading %
  37. # to correctly treat things like '?%magic'
  38. (.*?$|$) # rest of line
  39. """, re.VERBOSE)
  40. def split_user_input(line, pattern=None):
  41. """Split user input into initial whitespace, escape character, function part
  42. and the rest.
  43. """
  44. # We need to ensure that the rest of this routine deals only with unicode
  45. encoding = get_stream_enc(sys.stdin, 'utf-8')
  46. line = py3compat.cast_unicode(line, encoding)
  47. if pattern is None:
  48. pattern = line_split
  49. match = pattern.match(line)
  50. if not match:
  51. # print "match failed for line '%s'" % line
  52. try:
  53. ifun, the_rest = line.split(None,1)
  54. except ValueError:
  55. # print "split failed for line '%s'" % line
  56. ifun, the_rest = line, u''
  57. pre = re.match('^(\s*)(.*)',line).groups()[0]
  58. esc = ""
  59. else:
  60. pre, esc, ifun, the_rest = match.groups()
  61. #print 'line:<%s>' % line # dbg
  62. #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg
  63. return pre, esc or '', ifun.strip(), the_rest.lstrip()
  64. class LineInfo(object):
  65. """A single line of input and associated info.
  66. Includes the following as properties:
  67. line
  68. The original, raw line
  69. continue_prompt
  70. Is this line a continuation in a sequence of multiline input?
  71. pre
  72. Any leading whitespace.
  73. esc
  74. The escape character(s) in pre or the empty string if there isn't one.
  75. Note that '!!' and '??' are possible values for esc. Otherwise it will
  76. always be a single character.
  77. ifun
  78. The 'function part', which is basically the maximal initial sequence
  79. of valid python identifiers and the '.' character. This is what is
  80. checked for alias and magic transformations, used for auto-calling,
  81. etc. In contrast to Python identifiers, it may start with "%" and contain
  82. "*".
  83. the_rest
  84. Everything else on the line.
  85. """
  86. def __init__(self, line, continue_prompt=False):
  87. self.line = line
  88. self.continue_prompt = continue_prompt
  89. self.pre, self.esc, self.ifun, self.the_rest = split_user_input(line)
  90. self.pre_char = self.pre.strip()
  91. if self.pre_char:
  92. self.pre_whitespace = '' # No whitespace allowd before esc chars
  93. else:
  94. self.pre_whitespace = self.pre
  95. def ofind(self, ip):
  96. """Do a full, attribute-walking lookup of the ifun in the various
  97. namespaces for the given IPython InteractiveShell instance.
  98. Return a dict with keys: {found, obj, ospace, ismagic}
  99. Note: can cause state changes because of calling getattr, but should
  100. only be run if autocall is on and if the line hasn't matched any
  101. other, less dangerous handlers.
  102. Does cache the results of the call, so can be called multiple times
  103. without worrying about *further* damaging state.
  104. """
  105. return ip._ofind(self.ifun)
  106. def __str__(self):
  107. return "LineInfo [%s|%s|%s|%s]" %(self.pre, self.esc, self.ifun, self.the_rest)