Show More
@@ -31,16 +31,28 b' ESC_SEQUENCES = [ESC_SHELL, ESC_SH_CAP, ESC_HELP ,\\' | |||||
31 |
|
31 | |||
32 |
|
32 | |||
33 | class InputTransformer(object): |
|
33 | class InputTransformer(object): | |
|
34 | """Abstract base class for line-based input transformers.""" | |||
34 | __metaclass__ = abc.ABCMeta |
|
35 | __metaclass__ = abc.ABCMeta | |
35 |
|
36 | |||
36 | @abc.abstractmethod |
|
37 | @abc.abstractmethod | |
37 | def push(self, line): |
|
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 | pass |
|
44 | pass | |
39 |
|
45 | |||
40 | @abc.abstractmethod |
|
46 | @abc.abstractmethod | |
41 | def reset(self): |
|
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 | pass |
|
53 | pass | |
43 |
|
54 | |||
|
55 | # Set this to True to allow the transformer to act on lines inside strings. | |||
44 | look_in_string = False |
|
56 | look_in_string = False | |
45 |
|
57 | |||
46 | def stateless_input_transformer(func): |
|
58 | def stateless_input_transformer(func): | |
@@ -50,24 +62,35 b' def stateless_input_transformer(func):' | |||||
50 | self.func = func |
|
62 | self.func = func | |
51 |
|
63 | |||
52 | def push(self, line): |
|
64 | def push(self, line): | |
|
65 | """Send a line of input to the transformer, returning the | |||
|
66 | transformed input.""" | |||
53 | return self.func(line) |
|
67 | return self.func(line) | |
54 |
|
68 | |||
55 | def reset(self): |
|
69 | def reset(self): | |
|
70 | """No-op - exists for compatibility.""" | |||
56 | pass |
|
71 | pass | |
57 |
|
72 | |||
58 | return StatelessInputTransformer |
|
73 | return StatelessInputTransformer | |
59 |
|
74 | |||
60 | def coroutine_input_transformer(coro): |
|
75 | def coroutine_input_transformer(coro): | |
61 | class CoroutineInputTransformer(InputTransformer): |
|
76 | class CoroutineInputTransformer(InputTransformer): | |
|
77 | """Wrapper for input transformers based on coroutines.""" | |||
62 | def __init__(self): |
|
78 | def __init__(self): | |
63 | # Prime it |
|
79 | # Prime it | |
64 | self.coro = coro() |
|
80 | self.coro = coro() | |
65 | next(self.coro) |
|
81 | next(self.coro) | |
66 |
|
82 | |||
67 | def push(self, line): |
|
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 | return self.coro.send(line) |
|
88 | return self.coro.send(line) | |
69 |
|
89 | |||
70 | def reset(self): |
|
90 | def reset(self): | |
|
91 | """Return, transformed any lines that the transformer has | |||
|
92 | accumulated, and reset its internal state. | |||
|
93 | """ | |||
71 | return self.coro.send(None) |
|
94 | return self.coro.send(None) | |
72 |
|
95 | |||
73 | return CoroutineInputTransformer |
|
96 | return CoroutineInputTransformer | |
@@ -89,7 +112,11 b' def _make_help_call(target, esc, lspace, next_input=None):' | |||||
89 |
|
112 | |||
90 | @coroutine_input_transformer |
|
113 | @coroutine_input_transformer | |
91 | def escaped_transformer(): |
|
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 | # These define the transformations for the different escape characters. |
|
121 | # These define the transformations for the different escape characters. | |
95 | def _tr_system(line_info): |
|
122 | def _tr_system(line_info): | |
@@ -211,6 +238,11 b' def help_end(line):' | |||||
211 |
|
238 | |||
212 | @coroutine_input_transformer |
|
239 | @coroutine_input_transformer | |
213 | def cellmagic(): |
|
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 | tpl = 'get_ipython().run_cell_magic(%r, %r, %r)' |
|
246 | tpl = 'get_ipython().run_cell_magic(%r, %r, %r)' | |
215 | cellmagic_help_re = re.compile('%%\w+\?') |
|
247 | cellmagic_help_re = re.compile('%%\w+\?') | |
216 | line = '' |
|
248 | line = '' | |
@@ -259,6 +291,7 b' def _strip_prompts(prompt1_re, prompt2_re):' | |||||
259 |
|
291 | |||
260 | @coroutine_input_transformer |
|
292 | @coroutine_input_transformer | |
261 | def classic_prompt(): |
|
293 | def classic_prompt(): | |
|
294 | """Strip the >>>/... prompts of the Python interactive shell.""" | |||
262 | prompt1_re = re.compile(r'^(>>> )') |
|
295 | prompt1_re = re.compile(r'^(>>> )') | |
263 | prompt2_re = re.compile(r'^(>>> |^\.\.\. )') |
|
296 | prompt2_re = re.compile(r'^(>>> |^\.\.\. )') | |
264 | return _strip_prompts(prompt1_re, prompt2_re) |
|
297 | return _strip_prompts(prompt1_re, prompt2_re) | |
@@ -267,6 +300,7 b' classic_prompt.look_in_string = True' | |||||
267 |
|
300 | |||
268 | @coroutine_input_transformer |
|
301 | @coroutine_input_transformer | |
269 | def ipy_prompt(): |
|
302 | def ipy_prompt(): | |
|
303 | """Strip IPython's In [1]:/...: prompts.""" | |||
270 | prompt1_re = re.compile(r'^In \[\d+\]: ') |
|
304 | prompt1_re = re.compile(r'^In \[\d+\]: ') | |
271 | prompt2_re = re.compile(r'^(In \[\d+\]: |^\ \ \ \.\.\.+: )') |
|
305 | prompt2_re = re.compile(r'^(In \[\d+\]: |^\ \ \ \.\.\.+: )') | |
272 | return _strip_prompts(prompt1_re, prompt2_re) |
|
306 | return _strip_prompts(prompt1_re, prompt2_re) | |
@@ -276,6 +310,11 b' ipy_prompt.look_in_string = True' | |||||
276 |
|
310 | |||
277 | @coroutine_input_transformer |
|
311 | @coroutine_input_transformer | |
278 | def leading_indent(): |
|
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 | space_re = re.compile(r'^[ \t]+') |
|
318 | space_re = re.compile(r'^[ \t]+') | |
280 | line = '' |
|
319 | line = '' | |
281 | while True: |
|
320 | while True: | |
@@ -300,6 +339,11 b' leading_indent.look_in_string = True' | |||||
300 |
|
339 | |||
301 |
|
340 | |||
302 | def _special_assignment(assignment_re, template): |
|
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 | line = '' |
|
347 | line = '' | |
304 | while True: |
|
348 | while True: | |
305 | line = (yield line) |
|
349 | line = (yield line) | |
@@ -323,6 +367,7 b' def _special_assignment(assignment_re, template):' | |||||
323 |
|
367 | |||
324 | @coroutine_input_transformer |
|
368 | @coroutine_input_transformer | |
325 | def assign_from_system(): |
|
369 | def assign_from_system(): | |
|
370 | """Transform assignment from system commands (e.g. files = !ls)""" | |||
326 | assignment_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))' |
|
371 | assignment_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))' | |
327 | r'\s*=\s*!\s*(?P<cmd>.*)') |
|
372 | r'\s*=\s*!\s*(?P<cmd>.*)') | |
328 | template = '%s = get_ipython().getoutput(%r)' |
|
373 | template = '%s = get_ipython().getoutput(%r)' | |
@@ -330,6 +375,7 b' def assign_from_system():' | |||||
330 |
|
375 | |||
331 | @coroutine_input_transformer |
|
376 | @coroutine_input_transformer | |
332 | def assign_from_magic(): |
|
377 | def assign_from_magic(): | |
|
378 | """Transform assignment from magic commands (e.g. a = %who_ls)""" | |||
333 | assignment_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))' |
|
379 | assignment_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))' | |
334 | r'\s*=\s*%\s*(?P<cmd>.*)') |
|
380 | r'\s*=\s*%\s*(?P<cmd>.*)') | |
335 | template = '%s = get_ipython().magic(%r)' |
|
381 | template = '%s = get_ipython().magic(%r)' |
General Comments 0
You need to be logged in to leave comments.
Login now