| 1 |
|
| 2 |
=========================== |
| 3 |
Custom input transformation |
| 4 |
=========================== |
| 5 |
|
| 6 |
IPython extends Python syntax to allow things like magic commands, and help with |
| 7 |
the ``?`` syntax. There are several ways to customise how the user's input is |
| 8 |
processed into Python code to be executed. |
| 9 |
|
| 10 |
These hooks are mainly for other projects using IPython as the core of their |
| 11 |
interactive interface. Using them carelessly can easily break IPython! |
| 12 |
|
| 13 |
String based transformations |
| 14 |
============================ |
| 15 |
|
| 16 |
.. currentmodule:: IPython.core.inputtransformers2 |
| 17 |
|
| 18 |
When the user enters code, it is first processed as a string. By the |
| 19 |
end of this stage, it must be valid Python syntax. |
| 20 |
|
| 21 |
.. versionchanged:: 7.0 |
| 22 |
|
| 23 |
The API for string and token-based transformations has been completely |
| 24 |
redesigned. Any third party code extending input transformation will need to |
| 25 |
be rewritten. The new API is, hopefully, simpler. |
| 26 |
|
| 27 |
String based transformations are functions which accept a list of strings: |
| 28 |
each string is a single line of the input cell, including its line ending. |
| 29 |
The transformation function should return output in the same structure. |
| 30 |
|
| 31 |
These transformations are in two groups, accessible as attributes of |
| 32 |
the :class:`~IPython.core.interactiveshell.InteractiveShell` instance. |
| 33 |
Each group is a list of transformation functions. |
| 34 |
|
| 35 |
* ``input_transformers_cleanup`` run first on input, to do things like stripping |
| 36 |
prompts and leading indents from copied code. It may not be possible at this |
| 37 |
stage to parse the input as valid Python code. |
| 38 |
* Then IPython runs its own transformations to handle its special syntax, like |
| 39 |
``%magics`` and ``!system`` commands. This part does not expose extension |
| 40 |
points. |
| 41 |
* ``input_transformers_post`` run as the last step, to do things like converting |
| 42 |
float literals into decimal objects. These may attempt to parse the input as |
| 43 |
Python code. |
| 44 |
|
| 45 |
These transformers may raise :exc:`SyntaxError` if the input code is invalid, but |
| 46 |
in most cases it is clearer to pass unrecognised code through unmodified and let |
| 47 |
Python's own parser decide whether it is valid. |
| 48 |
|
| 49 |
For example, imagine we want to obfuscate our code by reversing each line, so |
| 50 |
we'd write ``)5(f =+ a`` instead of ``a += f(5)``. Here's how we could swap it |
| 51 |
back the right way before IPython tries to run it:: |
| 52 |
|
| 53 |
def reverse_line_chars(lines): |
| 54 |
new_lines = [] |
| 55 |
for line in lines: |
| 56 |
chars = line[:-1] # the newline needs to stay at the end |
| 57 |
new_lines.append(chars[::-1] + '\n') |
| 58 |
return new_lines |
| 59 |
|
| 60 |
To start using this:: |
| 61 |
|
| 62 |
ip = get_ipython() |
| 63 |
ip.input_transformers_cleanup.append(reverse_line_chars) |
| 64 |
|
| 65 |
.. versionadded:: 7.17 |
| 66 |
|
| 67 |
input_transformers can now have an attribute ``has_side_effects`` set to |
| 68 |
`True`, which will prevent the transformers from being ran when IPython is |
| 69 |
trying to guess whether the user input is complete. |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
AST transformations |
| 74 |
=================== |
| 75 |
|
| 76 |
After the code has been parsed as Python syntax, you can use Python's powerful |
| 77 |
*Abstract Syntax Tree* tools to modify it. Subclass :class:`ast.NodeTransformer`, |
| 78 |
and add an instance to ``shell.ast_transformers``. |
| 79 |
|
| 80 |
This example wraps integer literals in an ``Integer`` class, which is useful for |
| 81 |
mathematical frameworks that want to handle e.g. ``1/3`` as a precise fraction:: |
| 82 |
|
| 83 |
|
| 84 |
class IntegerWrapper(ast.NodeTransformer): |
| 85 |
"""Wraps all integers in a call to Integer()""" |
| 86 |
def visit_Num(self, node): |
| 87 |
if isinstance(node.value, int): |
| 88 |
return ast.Call(func=ast.Name(id='Integer', ctx=ast.Load()), |
| 89 |
args=[node], keywords=[]) |
| 90 |
return node |