Show More
@@ -31,16 +31,28 b' ESC_SEQUENCES = [ESC_SHELL, ESC_SH_CAP, ESC_HELP ,\\' | |||
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | class InputTransformer(object): |
|
34 | """Abstract base class for line-based input transformers.""" | |
|
34 | 35 | __metaclass__ = abc.ABCMeta |
|
35 | 36 | |
|
36 | 37 | @abc.abstractmethod |
|
37 | 38 | def push(self, line): |
|
39 | """Send a line of input to the transformer, returning the transformed | |
|
40 | input or None if the transformer is waiting for more input. | |
|
41 | ||
|
42 | Must be overridden by subclasses. | |
|
43 | """ | |
|
38 | 44 | pass |
|
39 | 45 | |
|
40 | 46 | @abc.abstractmethod |
|
41 | 47 | def reset(self): |
|
48 | """Return, transformed any lines that the transformer has accumulated, | |
|
49 | and reset its internal state. | |
|
50 | ||
|
51 | Must be overridden by subclasses. | |
|
52 | """ | |
|
42 | 53 | pass |
|
43 | 54 | |
|
55 | # Set this to True to allow the transformer to act on lines inside strings. | |
|
44 | 56 | look_in_string = False |
|
45 | 57 | |
|
46 | 58 | def stateless_input_transformer(func): |
@@ -50,24 +62,35 b' def stateless_input_transformer(func):' | |||
|
50 | 62 | self.func = func |
|
51 | 63 | |
|
52 | 64 | def push(self, line): |
|
65 | """Send a line of input to the transformer, returning the | |
|
66 | transformed input.""" | |
|
53 | 67 | return self.func(line) |
|
54 | 68 | |
|
55 | 69 | def reset(self): |
|
70 | """No-op - exists for compatibility.""" | |
|
56 | 71 | pass |
|
57 | 72 | |
|
58 | 73 | return StatelessInputTransformer |
|
59 | 74 | |
|
60 | 75 | def coroutine_input_transformer(coro): |
|
61 | 76 | class CoroutineInputTransformer(InputTransformer): |
|
77 | """Wrapper for input transformers based on coroutines.""" | |
|
62 | 78 | def __init__(self): |
|
63 | 79 | # Prime it |
|
64 | 80 | self.coro = coro() |
|
65 | 81 | next(self.coro) |
|
66 | 82 | |
|
67 | 83 | def push(self, line): |
|
84 | """Send a line of input to the transformer, returning the | |
|
85 | transformed input or None if the transformer is waiting for more | |
|
86 | input. | |
|
87 | """ | |
|
68 | 88 | return self.coro.send(line) |
|
69 | 89 | |
|
70 | 90 | def reset(self): |
|
91 | """Return, transformed any lines that the transformer has | |
|
92 | accumulated, and reset its internal state. | |
|
93 | """ | |
|
71 | 94 | return self.coro.send(None) |
|
72 | 95 | |
|
73 | 96 | return CoroutineInputTransformer |
@@ -89,7 +112,11 b' def _make_help_call(target, esc, lspace, next_input=None):' | |||
|
89 | 112 | |
|
90 | 113 | @coroutine_input_transformer |
|
91 | 114 | def escaped_transformer(): |
|
92 |
"""Translate lines beginning with one of IPython's escape characters. |
|
|
115 | """Translate lines beginning with one of IPython's escape characters. | |
|
116 | ||
|
117 | This is stateful to allow magic commands etc. to be continued over several | |
|
118 | lines using explicit line continuations (\ at the end of a line). | |
|
119 | """ | |
|
93 | 120 | |
|
94 | 121 | # These define the transformations for the different escape characters. |
|
95 | 122 | def _tr_system(line_info): |
@@ -211,6 +238,11 b' def help_end(line):' | |||
|
211 | 238 | |
|
212 | 239 | @coroutine_input_transformer |
|
213 | 240 | def cellmagic(): |
|
241 | """Captures & transforms cell magics. | |
|
242 | ||
|
243 | After a cell magic is started, this stores up any lines it gets until it is | |
|
244 | reset (sent None). | |
|
245 | """ | |
|
214 | 246 | tpl = 'get_ipython().run_cell_magic(%r, %r, %r)' |
|
215 | 247 | cellmagic_help_re = re.compile('%%\w+\?') |
|
216 | 248 | line = '' |
@@ -259,6 +291,7 b' def _strip_prompts(prompt1_re, prompt2_re):' | |||
|
259 | 291 | |
|
260 | 292 | @coroutine_input_transformer |
|
261 | 293 | def classic_prompt(): |
|
294 | """Strip the >>>/... prompts of the Python interactive shell.""" | |
|
262 | 295 | prompt1_re = re.compile(r'^(>>> )') |
|
263 | 296 | prompt2_re = re.compile(r'^(>>> |^\.\.\. )') |
|
264 | 297 | return _strip_prompts(prompt1_re, prompt2_re) |
@@ -267,6 +300,7 b' classic_prompt.look_in_string = True' | |||
|
267 | 300 | |
|
268 | 301 | @coroutine_input_transformer |
|
269 | 302 | def ipy_prompt(): |
|
303 | """Strip IPython's In [1]:/...: prompts.""" | |
|
270 | 304 | prompt1_re = re.compile(r'^In \[\d+\]: ') |
|
271 | 305 | prompt2_re = re.compile(r'^(In \[\d+\]: |^\ \ \ \.\.\.+: )') |
|
272 | 306 | return _strip_prompts(prompt1_re, prompt2_re) |
@@ -276,6 +310,11 b' ipy_prompt.look_in_string = True' | |||
|
276 | 310 | |
|
277 | 311 | @coroutine_input_transformer |
|
278 | 312 | def leading_indent(): |
|
313 | """Remove leading indentation. | |
|
314 | ||
|
315 | If the first line starts with a spaces or tabs, the same whitespace will be | |
|
316 | removed from each following line until it is reset. | |
|
317 | """ | |
|
279 | 318 | space_re = re.compile(r'^[ \t]+') |
|
280 | 319 | line = '' |
|
281 | 320 | while True: |
@@ -300,6 +339,11 b' leading_indent.look_in_string = True' | |||
|
300 | 339 | |
|
301 | 340 | |
|
302 | 341 | def _special_assignment(assignment_re, template): |
|
342 | """Transform assignment from system & magic commands. | |
|
343 | ||
|
344 | This is stateful so that it can handle magic commands continued on several | |
|
345 | lines. | |
|
346 | """ | |
|
303 | 347 | line = '' |
|
304 | 348 | while True: |
|
305 | 349 | line = (yield line) |
@@ -323,6 +367,7 b' def _special_assignment(assignment_re, template):' | |||
|
323 | 367 | |
|
324 | 368 | @coroutine_input_transformer |
|
325 | 369 | def assign_from_system(): |
|
370 | """Transform assignment from system commands (e.g. files = !ls)""" | |
|
326 | 371 | assignment_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))' |
|
327 | 372 | r'\s*=\s*!\s*(?P<cmd>.*)') |
|
328 | 373 | template = '%s = get_ipython().getoutput(%r)' |
@@ -330,6 +375,7 b' def assign_from_system():' | |||
|
330 | 375 | |
|
331 | 376 | @coroutine_input_transformer |
|
332 | 377 | def assign_from_magic(): |
|
378 | """Transform assignment from magic commands (e.g. a = %who_ls)""" | |
|
333 | 379 | assignment_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))' |
|
334 | 380 | r'\s*=\s*%\s*(?P<cmd>.*)') |
|
335 | 381 | template = '%s = get_ipython().magic(%r)' |
General Comments 0
You need to be logged in to leave comments.
Login now