##// END OF EJS Templates
More input transformers
Thomas Kluyver -
Show More
@@ -17,6 +17,8 b' class InputTransformer(object):'
17 @abc.abstractmethod
17 @abc.abstractmethod
18 def reset(self):
18 def reset(self):
19 pass
19 pass
20
21 look_in_string = False
20
22
21 class StatelessInputTransformer(InputTransformer):
23 class StatelessInputTransformer(InputTransformer):
22 """Decorator for a stateless input transformer implemented as a function."""
24 """Decorator for a stateless input transformer implemented as a function."""
@@ -96,7 +98,7 b' def cellmagic():'
96 line = ''
98 line = ''
97 while True:
99 while True:
98 line = (yield line)
100 line = (yield line)
99 if not line.startswith(ESC_MAGIC2):
101 if (not line) or (not line.startswith(ESC_MAGIC2)):
100 continue
102 continue
101
103
102 first = line
104 first = line
@@ -110,3 +112,64 b' def cellmagic():'
110 magic_name, _, first = first.partition(' ')
112 magic_name, _, first = first.partition(' ')
111 magic_name = magic_name.lstrip(ESC_MAGIC2)
113 magic_name = magic_name.lstrip(ESC_MAGIC2)
112 line = tpl % (magic_name, first, '\n'.join(body))
114 line = tpl % (magic_name, first, '\n'.join(body))
115
116 def _strip_prompts(prompt1_re, prompt2_re):
117 """Remove matching input prompts from a block of input."""
118 line = ''
119 while True:
120 line = (yield line)
121
122 if line is None:
123 continue
124
125 m = prompt1_re.match(line)
126 if m:
127 while m:
128 line = (yield line[len(m.group(0)):])
129 if line is None:
130 break
131 m = prompt2_re.match(line)
132 else:
133 # Prompts not in input - wait for reset
134 while line is not None:
135 line = (yield line)
136
137 @CoroutineInputTransformer
138 def classic_prompt():
139 prompt1_re = re.compile(r'^(>>> )')
140 prompt2_re = re.compile(r'^(>>> |^\.\.\. )')
141 return _strip_prompts(prompt1_re, prompt2_re)
142
143 classic_prompt.look_in_string = True
144
145 @CoroutineInputTransformer
146 def ipy_prompt():
147 prompt1_re = re.compile(r'^In \[\d+\]: ')
148 prompt2_re = re.compile(r'^(In \[\d+\]: |^\ \ \ \.\.\.+: )')
149 return _strip_prompts(prompt1_re, prompt2_re)
150
151 ipy_prompt.look_in_string = True
152
153 @CoroutineInputTransformer
154 def leading_indent():
155 space_re = re.compile(r'^[ \t]+')
156 line = ''
157 while True:
158 line = (yield line)
159
160 if line is None:
161 continue
162
163 m = space_re.match(line)
164 if m:
165 space = m.group(0)
166 while line is not None:
167 if line.startswith(space):
168 line = line[len(space):]
169 line = (yield line)
170 else:
171 # No leading spaces - wait for reset
172 while line is not None:
173 line = (yield line)
174
175 leading_indent.look_in_string = True
@@ -9,25 +9,28 b' from IPython.core.tests.test_inputsplitter import syntax'
9
9
10 def wrap_transform(transformer):
10 def wrap_transform(transformer):
11 def transform(inp):
11 def transform(inp):
12 results = []
12 for line in inp:
13 for line in inp:
13 res = transformer.push(line)
14 res = transformer.push(line)
14 if res is not None:
15 if res is not None:
15 return res
16 results.append(res)
16 return transformer.push(None)
17 transformer.reset()
18 return results
17
19
18 return transform
20 return transform
19
21
20 cellmagic_tests = [
22 cellmagic_tests = [
21 (['%%foo a'], "get_ipython().run_cell_magic('foo', 'a', '')"),
23 (['%%foo a', None], ["get_ipython().run_cell_magic('foo', 'a', '')"]),
22 (['%%bar 123', 'hello', ''], "get_ipython().run_cell_magic('bar', '123', 'hello')"),
24 (['%%bar 123', 'hello', ''], ["get_ipython().run_cell_magic('bar', '123', 'hello')"]),
23 ]
25 ]
24
26
25 def test_transform_cellmagic():
27 def test_transform_cellmagic():
26 tt.check_pairs(wrap_transform(inputtransformer.cellmagic), cellmagic_tests)
28 tt.check_pairs(wrap_transform(inputtransformer.cellmagic), cellmagic_tests)
27
29
28 esctransform_tests = [(i, py3compat.u_format(o)) for i,o in [
30 esctransform_tests = [(i, [py3compat.u_format(ol) for ol in o]) for i,o in [
29 (['%pdef zip'], "get_ipython().magic({u}'pdef zip')"),
31 (['%pdef zip'], ["get_ipython().magic({u}'pdef zip')"]),
30 (['%abc def \\', 'ghi'], "get_ipython().magic({u}'abc def ghi')"),
32 (['%abc def \\', 'ghi'], ["get_ipython().magic({u}'abc def ghi')"]),
33 (['%abc def \\', 'ghi\\', None], ["get_ipython().magic({u}'abc def ghi')"]),
31 ]]
34 ]]
32
35
33 def test_transform_escaped():
36 def test_transform_escaped():
@@ -35,3 +38,31 b' def test_transform_escaped():'
35
38
36 def endhelp_test():
39 def endhelp_test():
37 tt.check_pairs(inputtransformer.transform_help_end.push, syntax['end_help'])
40 tt.check_pairs(inputtransformer.transform_help_end.push, syntax['end_help'])
41
42 classic_prompt_tests = [
43 (['>>> a=1'], ['a=1']),
44 (['>>> a="""','... 123"""'], ['a="""', '123"""']),
45 (['a="""','... 123"""'], ['a="""', '... 123"""']),
46 ]
47
48 def test_classic_prompt():
49 tt.check_pairs(wrap_transform(inputtransformer.classic_prompt), classic_prompt_tests)
50
51 ipy_prompt_tests = [
52 (['In [1]: a=1'], ['a=1']),
53 (['In [2]: a="""',' ...: 123"""'], ['a="""', '123"""']),
54 (['a="""',' ...: 123"""'], ['a="""', ' ...: 123"""']),
55 ]
56
57 def test_ipy_prompt():
58 tt.check_pairs(wrap_transform(inputtransformer.ipy_prompt), ipy_prompt_tests)
59
60 leading_indent_tests = [
61 ([' print "hi"'], ['print "hi"']),
62 ([' for a in range(5):', ' a*2'], ['for a in range(5):', ' a*2']),
63 ([' a="""',' 123"""'], ['a="""', '123"""']),
64 (['a="""',' 123"""'], ['a="""', ' 123"""']),
65 ]
66
67 def test_leading_indent():
68 tt.check_pairs(wrap_transform(inputtransformer.leading_indent), leading_indent_tests)
General Comments 0
You need to be logged in to leave comments. Login now