inputtransformer.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. """Input transformer classes to support IPython special syntax.
  2. This includes the machinery to recognise and transform ``%magic`` commands,
  3. ``!system`` commands, ``help?`` querying, prompt stripping, and so forth.
  4. """
  5. import abc
  6. import functools
  7. import re
  8. from IPython.core.splitinput import LineInfo
  9. from IPython.utils import tokenize2
  10. from IPython.utils.openpy import cookie_comment_re
  11. from IPython.utils.py3compat import with_metaclass, PY3
  12. from IPython.utils.tokenize2 import generate_tokens, untokenize, TokenError
  13. if PY3:
  14. from io import StringIO
  15. else:
  16. from StringIO import StringIO
  17. #-----------------------------------------------------------------------------
  18. # Globals
  19. #-----------------------------------------------------------------------------
  20. # The escape sequences that define the syntax transformations IPython will
  21. # apply to user input. These can NOT be just changed here: many regular
  22. # expressions and other parts of the code may use their hardcoded values, and
  23. # for all intents and purposes they constitute the 'IPython syntax', so they
  24. # should be considered fixed.
  25. ESC_SHELL = '!' # Send line to underlying system shell
  26. ESC_SH_CAP = '!!' # Send line to system shell and capture output
  27. ESC_HELP = '?' # Find information about object
  28. ESC_HELP2 = '??' # Find extra-detailed information about object
  29. ESC_MAGIC = '%' # Call magic function
  30. ESC_MAGIC2 = '%%' # Call cell-magic function
  31. ESC_QUOTE = ',' # Split args on whitespace, quote each as string and call
  32. ESC_QUOTE2 = ';' # Quote all args as a single string, call
  33. ESC_PAREN = '/' # Call first argument with rest of line as arguments
  34. ESC_SEQUENCES = [ESC_SHELL, ESC_SH_CAP, ESC_HELP ,\
  35. ESC_HELP2, ESC_MAGIC, ESC_MAGIC2,\
  36. ESC_QUOTE, ESC_QUOTE2, ESC_PAREN ]
  37. class InputTransformer(with_metaclass(abc.ABCMeta, object)):
  38. """Abstract base class for line-based input transformers."""
  39. @abc.abstractmethod
  40. def push(self, line):
  41. """Send a line of input to the transformer, returning the transformed
  42. input or None if the transformer is waiting for more input.
  43. Must be overridden by subclasses.
  44. Implementations may raise ``SyntaxError`` if the input is invalid. No
  45. other exceptions may be raised.
  46. """
  47. pass
  48. @abc.abstractmethod
  49. def reset(self):
  50. """Return, transformed any lines that the transformer has accumulated,
  51. and reset its internal state.
  52. Must be overridden by subclasses.
  53. """
  54. pass
  55. @classmethod
  56. def wrap(cls, func):
  57. """Can be used by subclasses as a decorator, to return a factory that
  58. will allow instantiation with the decorated object.
  59. """
  60. @functools.wraps(func)
  61. def transformer_factory(**kwargs):
  62. return cls(func, **kwargs)
  63. return transformer_factory
  64. class StatelessInputTransformer(InputTransformer):
  65. """Wrapper for a stateless input transformer implemented as a function."""
  66. def __init__(self, func):
  67. self.func = func
  68. def __repr__(self):
  69. return "StatelessInputTransformer(func={0!r})".format(self.func)
  70. def push(self, line):
  71. """Send a line of input to the transformer, returning the
  72. transformed input."""
  73. return self.func(line)
  74. def reset(self):
  75. """No-op - exists for compatibility."""
  76. pass
  77. class CoroutineInputTransformer(InputTransformer):
  78. """Wrapper for an input transformer implemented as a coroutine."""
  79. def __init__(self, coro, **kwargs):
  80. # Prime it
  81. self.coro = coro(**kwargs)
  82. next(self.coro)
  83. def __repr__(self):
  84. return "CoroutineInputTransformer(coro={0!r})".format(self.coro)
  85. def push(self, line):
  86. """Send a line of input to the transformer, returning the
  87. transformed input or None if the transformer is waiting for more
  88. input.
  89. """
  90. return self.coro.send(line)
  91. def reset(self):
  92. """Return, transformed any lines that the transformer has
  93. accumulated, and reset its internal state.
  94. """
  95. return self.coro.send(None)
  96. class TokenInputTransformer(InputTransformer):
  97. """Wrapper for a token-based input transformer.
  98. func should accept a list of tokens (5-tuples, see tokenize docs), and
  99. return an iterable which can be passed to tokenize.untokenize().
  100. """
  101. def __init__(self, func):
  102. self.func = func
  103. self.current_line = ""
  104. self.line_used = False
  105. self.reset_tokenizer()
  106. def reset_tokenizer(self):
  107. self.tokenizer = generate_tokens(self.get_line)
  108. def get_line(self):
  109. if self.line_used:
  110. raise TokenError
  111. self.line_used = True
  112. return self.current_line
  113. def push(self, line):
  114. self.current_line += line + "\n"
  115. if self.current_line.isspace():
  116. return self.reset()
  117. self.line_used = False
  118. tokens = []
  119. stop_at_NL = False
  120. try:
  121. for intok in self.tokenizer:
  122. tokens.append(intok)
  123. t = intok[0]
  124. if t == tokenize2.NEWLINE or (stop_at_NL and t == tokenize2.NL):
  125. # Stop before we try to pull a line we don't have yet
  126. break
  127. elif t == tokenize2.ERRORTOKEN:
  128. stop_at_NL = True
  129. except TokenError:
  130. # Multi-line statement - stop and try again with the next line
  131. self.reset_tokenizer()
  132. return None
  133. return self.output(tokens)
  134. def output(self, tokens):
  135. self.current_line = ""
  136. self.reset_tokenizer()
  137. return untokenize(self.func(tokens)).rstrip('\n')
  138. def reset(self):
  139. l = self.current_line
  140. self.current_line = ""
  141. self.reset_tokenizer()
  142. if l:
  143. return l.rstrip('\n')
  144. class assemble_python_lines(TokenInputTransformer):
  145. def __init__(self):
  146. super(assemble_python_lines, self).__init__(None)
  147. def output(self, tokens):
  148. return self.reset()
  149. @CoroutineInputTransformer.wrap
  150. def assemble_logical_lines():
  151. """Join lines following explicit line continuations (\)"""
  152. line = ''
  153. while True:
  154. line = (yield line)
  155. if not line or line.isspace():
  156. continue
  157. parts = []
  158. while line is not None:
  159. if line.endswith('\\') and (not has_comment(line)):
  160. parts.append(line[:-1])
  161. line = (yield None) # Get another line
  162. else:
  163. parts.append(line)
  164. break
  165. # Output
  166. line = ''.join(parts)
  167. # Utilities
  168. def _make_help_call(target, esc, lspace, next_input=None):
  169. """Prepares a pinfo(2)/psearch call from a target name and the escape
  170. (i.e. ? or ??)"""
  171. method = 'pinfo2' if esc == '??' \
  172. else 'psearch' if '*' in target \
  173. else 'pinfo'
  174. arg = " ".join([method, target])
  175. if next_input is None:
  176. return '%sget_ipython().magic(%r)' % (lspace, arg)
  177. else:
  178. return '%sget_ipython().set_next_input(%r);get_ipython().magic(%r)' % \
  179. (lspace, next_input, arg)
  180. # These define the transformations for the different escape characters.
  181. def _tr_system(line_info):
  182. "Translate lines escaped with: !"
  183. cmd = line_info.line.lstrip().lstrip(ESC_SHELL)
  184. return '%sget_ipython().system(%r)' % (line_info.pre, cmd)
  185. def _tr_system2(line_info):
  186. "Translate lines escaped with: !!"
  187. cmd = line_info.line.lstrip()[2:]
  188. return '%sget_ipython().getoutput(%r)' % (line_info.pre, cmd)
  189. def _tr_help(line_info):
  190. "Translate lines escaped with: ?/??"
  191. # A naked help line should just fire the intro help screen
  192. if not line_info.line[1:]:
  193. return 'get_ipython().show_usage()'
  194. return _make_help_call(line_info.ifun, line_info.esc, line_info.pre)
  195. def _tr_magic(line_info):
  196. "Translate lines escaped with: %"
  197. tpl = '%sget_ipython().magic(%r)'
  198. if line_info.line.startswith(ESC_MAGIC2):
  199. return line_info.line
  200. cmd = ' '.join([line_info.ifun, line_info.the_rest]).strip()
  201. return tpl % (line_info.pre, cmd)
  202. def _tr_quote(line_info):
  203. "Translate lines escaped with: ,"
  204. return '%s%s("%s")' % (line_info.pre, line_info.ifun,
  205. '", "'.join(line_info.the_rest.split()) )
  206. def _tr_quote2(line_info):
  207. "Translate lines escaped with: ;"
  208. return '%s%s("%s")' % (line_info.pre, line_info.ifun,
  209. line_info.the_rest)
  210. def _tr_paren(line_info):
  211. "Translate lines escaped with: /"
  212. return '%s%s(%s)' % (line_info.pre, line_info.ifun,
  213. ", ".join(line_info.the_rest.split()))
  214. tr = { ESC_SHELL : _tr_system,
  215. ESC_SH_CAP : _tr_system2,
  216. ESC_HELP : _tr_help,
  217. ESC_HELP2 : _tr_help,
  218. ESC_MAGIC : _tr_magic,
  219. ESC_QUOTE : _tr_quote,
  220. ESC_QUOTE2 : _tr_quote2,
  221. ESC_PAREN : _tr_paren }
  222. @StatelessInputTransformer.wrap
  223. def escaped_commands(line):
  224. """Transform escaped commands - %magic, !system, ?help + various autocalls.
  225. """
  226. if not line or line.isspace():
  227. return line
  228. lineinf = LineInfo(line)
  229. if lineinf.esc not in tr:
  230. return line
  231. return tr[lineinf.esc](lineinf)
  232. _initial_space_re = re.compile(r'\s*')
  233. _help_end_re = re.compile(r"""(%{0,2}
  234. [a-zA-Z_*][\w*]* # Variable name
  235. (\.[a-zA-Z_*][\w*]*)* # .etc.etc
  236. )
  237. (\?\??)$ # ? or ??
  238. """,
  239. re.VERBOSE)
  240. # Extra pseudotokens for multiline strings and data structures
  241. _MULTILINE_STRING = object()
  242. _MULTILINE_STRUCTURE = object()
  243. def _line_tokens(line):
  244. """Helper for has_comment and ends_in_comment_or_string."""
  245. readline = StringIO(line).readline
  246. toktypes = set()
  247. try:
  248. for t in generate_tokens(readline):
  249. toktypes.add(t[0])
  250. except TokenError as e:
  251. # There are only two cases where a TokenError is raised.
  252. if 'multi-line string' in e.args[0]:
  253. toktypes.add(_MULTILINE_STRING)
  254. else:
  255. toktypes.add(_MULTILINE_STRUCTURE)
  256. return toktypes
  257. def has_comment(src):
  258. """Indicate whether an input line has (i.e. ends in, or is) a comment.
  259. This uses tokenize, so it can distinguish comments from # inside strings.
  260. Parameters
  261. ----------
  262. src : string
  263. A single line input string.
  264. Returns
  265. -------
  266. comment : bool
  267. True if source has a comment.
  268. """
  269. return (tokenize2.COMMENT in _line_tokens(src))
  270. def ends_in_comment_or_string(src):
  271. """Indicates whether or not an input line ends in a comment or within
  272. a multiline string.
  273. Parameters
  274. ----------
  275. src : string
  276. A single line input string.
  277. Returns
  278. -------
  279. comment : bool
  280. True if source ends in a comment or multiline string.
  281. """
  282. toktypes = _line_tokens(src)
  283. return (tokenize2.COMMENT in toktypes) or (_MULTILINE_STRING in toktypes)
  284. @StatelessInputTransformer.wrap
  285. def help_end(line):
  286. """Translate lines with ?/?? at the end"""
  287. m = _help_end_re.search(line)
  288. if m is None or ends_in_comment_or_string(line):
  289. return line
  290. target = m.group(1)
  291. esc = m.group(3)
  292. lspace = _initial_space_re.match(line).group(0)
  293. # If we're mid-command, put it back on the next prompt for the user.
  294. next_input = line.rstrip('?') if line.strip() != m.group(0) else None
  295. return _make_help_call(target, esc, lspace, next_input)
  296. @CoroutineInputTransformer.wrap
  297. def cellmagic(end_on_blank_line=False):
  298. """Captures & transforms cell magics.
  299. After a cell magic is started, this stores up any lines it gets until it is
  300. reset (sent None).
  301. """
  302. tpl = 'get_ipython().run_cell_magic(%r, %r, %r)'
  303. cellmagic_help_re = re.compile('%%\w+\?')
  304. line = ''
  305. while True:
  306. line = (yield line)
  307. # consume leading empty lines
  308. while not line:
  309. line = (yield line)
  310. if not line.startswith(ESC_MAGIC2):
  311. # This isn't a cell magic, idle waiting for reset then start over
  312. while line is not None:
  313. line = (yield line)
  314. continue
  315. if cellmagic_help_re.match(line):
  316. # This case will be handled by help_end
  317. continue
  318. first = line
  319. body = []
  320. line = (yield None)
  321. while (line is not None) and \
  322. ((line.strip() != '') or not end_on_blank_line):
  323. body.append(line)
  324. line = (yield None)
  325. # Output
  326. magic_name, _, first = first.partition(' ')
  327. magic_name = magic_name.lstrip(ESC_MAGIC2)
  328. line = tpl % (magic_name, first, u'\n'.join(body))
  329. def _strip_prompts(prompt_re, initial_re=None, turnoff_re=None):
  330. """Remove matching input prompts from a block of input.
  331. Parameters
  332. ----------
  333. prompt_re : regular expression
  334. A regular expression matching any input prompt (including continuation)
  335. initial_re : regular expression, optional
  336. A regular expression matching only the initial prompt, but not continuation.
  337. If no initial expression is given, prompt_re will be used everywhere.
  338. Used mainly for plain Python prompts, where the continuation prompt
  339. ``...`` is a valid Python expression in Python 3, so shouldn't be stripped.
  340. If initial_re and prompt_re differ,
  341. only initial_re will be tested against the first line.
  342. If any prompt is found on the first two lines,
  343. prompts will be stripped from the rest of the block.
  344. """
  345. if initial_re is None:
  346. initial_re = prompt_re
  347. line = ''
  348. while True:
  349. line = (yield line)
  350. # First line of cell
  351. if line is None:
  352. continue
  353. out, n1 = initial_re.subn('', line, count=1)
  354. if turnoff_re and not n1:
  355. if turnoff_re.match(line):
  356. # We're in e.g. a cell magic; disable this transformer for
  357. # the rest of the cell.
  358. while line is not None:
  359. line = (yield line)
  360. continue
  361. line = (yield out)
  362. if line is None:
  363. continue
  364. # check for any prompt on the second line of the cell,
  365. # because people often copy from just after the first prompt,
  366. # so we might not see it in the first line.
  367. out, n2 = prompt_re.subn('', line, count=1)
  368. line = (yield out)
  369. if n1 or n2:
  370. # Found a prompt in the first two lines - check for it in
  371. # the rest of the cell as well.
  372. while line is not None:
  373. line = (yield prompt_re.sub('', line, count=1))
  374. else:
  375. # Prompts not in input - wait for reset
  376. while line is not None:
  377. line = (yield line)
  378. @CoroutineInputTransformer.wrap
  379. def classic_prompt():
  380. """Strip the >>>/... prompts of the Python interactive shell."""
  381. # FIXME: non-capturing version (?:...) usable?
  382. prompt_re = re.compile(r'^(>>>|\.\.\.)( |$)')
  383. initial_re = re.compile(r'^>>>( |$)')
  384. # Any %magic/!system is IPython syntax, so we needn't look for >>> prompts
  385. turnoff_re = re.compile(r'^[%!]')
  386. return _strip_prompts(prompt_re, initial_re, turnoff_re)
  387. @CoroutineInputTransformer.wrap
  388. def ipy_prompt():
  389. """Strip IPython's In [1]:/...: prompts."""
  390. # FIXME: non-capturing version (?:...) usable?
  391. prompt_re = re.compile(r'^(In \[\d+\]: |\s*\.{3,}: ?)')
  392. # Disable prompt stripping inside cell magics
  393. turnoff_re = re.compile(r'^%%')
  394. return _strip_prompts(prompt_re, turnoff_re=turnoff_re)
  395. @CoroutineInputTransformer.wrap
  396. def leading_indent():
  397. """Remove leading indentation.
  398. If the first line starts with a spaces or tabs, the same whitespace will be
  399. removed from each following line until it is reset.
  400. """
  401. space_re = re.compile(r'^[ \t]+')
  402. line = ''
  403. while True:
  404. line = (yield line)
  405. if line is None:
  406. continue
  407. m = space_re.match(line)
  408. if m:
  409. space = m.group(0)
  410. while line is not None:
  411. if line.startswith(space):
  412. line = line[len(space):]
  413. line = (yield line)
  414. else:
  415. # No leading spaces - wait for reset
  416. while line is not None:
  417. line = (yield line)
  418. @CoroutineInputTransformer.wrap
  419. def strip_encoding_cookie():
  420. """Remove encoding comment if found in first two lines
  421. If the first or second line has the `# coding: utf-8` comment,
  422. it will be removed.
  423. """
  424. line = ''
  425. while True:
  426. line = (yield line)
  427. # check comment on first two lines
  428. for i in range(2):
  429. if line is None:
  430. break
  431. if cookie_comment_re.match(line):
  432. line = (yield "")
  433. else:
  434. line = (yield line)
  435. # no-op on the rest of the cell
  436. while line is not None:
  437. line = (yield line)
  438. _assign_pat = \
  439. r'''(?P<lhs>(\s*)
  440. ([\w\.]+) # Initial identifier
  441. (\s*,\s*
  442. \*?[\w\.]+)* # Further identifiers for unpacking
  443. \s*?,? # Trailing comma
  444. )
  445. \s*=\s*
  446. '''
  447. assign_system_re = re.compile(r'{}!\s*(?P<cmd>.*)'.format(_assign_pat), re.VERBOSE)
  448. assign_system_template = '%s = get_ipython().getoutput(%r)'
  449. @StatelessInputTransformer.wrap
  450. def assign_from_system(line):
  451. """Transform assignment from system commands (e.g. files = !ls)"""
  452. m = assign_system_re.match(line)
  453. if m is None:
  454. return line
  455. return assign_system_template % m.group('lhs', 'cmd')
  456. assign_magic_re = re.compile(r'{}%\s*(?P<cmd>.*)'.format(_assign_pat), re.VERBOSE)
  457. assign_magic_template = '%s = get_ipython().magic(%r)'
  458. @StatelessInputTransformer.wrap
  459. def assign_from_magic(line):
  460. """Transform assignment from magic commands (e.g. a = %who_ls)"""
  461. m = assign_magic_re.match(line)
  462. if m is None:
  463. return line
  464. return assign_magic_template % m.group('lhs', 'cmd')