##// END OF EJS Templates
Transformers for assignment from %magic and \!system calls
Thomas Kluyver -
Show More
@@ -173,3 +173,39 b' def leading_indent():'
173 line = (yield line)
173 line = (yield line)
174
174
175 leading_indent.look_in_string = True
175 leading_indent.look_in_string = True
176
177 def _special_assignment(assignment_re, template):
178 line = ''
179 while True:
180 line = (yield line)
181 if not line or line.isspace():
182 continue
183
184 m = assignment_re.match(line)
185 if not m:
186 continue
187
188 parts = []
189 while line is not None:
190 parts.append(line.rstrip('\\'))
191 if not line.endswith('\\'):
192 break
193 line = (yield None)
194
195 # Output
196 whole = assignment_re.match(' '.join(parts))
197 line = template % (whole.group('lhs'), whole.group('cmd'))
198
199 @CoroutineInputTransformer
200 def assign_from_system():
201 assignment_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))'
202 r'\s*=\s*!\s*(?P<cmd>.*)')
203 template = '%s = get_ipython().getoutput(%r)'
204 return _special_assignment(assignment_re, template)
205
206 @CoroutineInputTransformer
207 def assign_from_magic():
208 assignment_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))'
209 r'\s*=\s*%\s*(?P<cmd>.*)')
210 template = '%s = get_ipython().magic(%r)'
211 return _special_assignment(assignment_re, template)
@@ -66,3 +66,19 b' leading_indent_tests = ['
66
66
67 def test_leading_indent():
67 def test_leading_indent():
68 tt.check_pairs(wrap_transform(inputtransformer.leading_indent), leading_indent_tests)
68 tt.check_pairs(wrap_transform(inputtransformer.leading_indent), leading_indent_tests)
69
70 assign_magic_tests = [(i, [py3compat.u_format(ol) for ol in o]) for i,o in [
71 (['a = %bc de \\', 'fg'], ["a = get_ipython().magic('bc de fg')"]),
72 (['a = %bc de \\', 'fg\\', None], ["a = get_ipython().magic('bc de fg')"]),
73 ]]
74
75 def test_assign_magic():
76 tt.check_pairs(wrap_transform(inputtransformer.assign_from_magic), assign_magic_tests)
77
78 assign_system_tests = [(i, [py3compat.u_format(ol) for ol in o]) for i,o in [
79 (['a = !bc de \\', 'fg'], ["a = get_ipython().getoutput('bc de fg')"]),
80 (['a = !bc de \\', 'fg\\', None], ["a = get_ipython().getoutput('bc de fg')"]),
81 ]]
82
83 def test_assign_system():
84 tt.check_pairs(wrap_transform(inputtransformer.assign_from_system), assign_system_tests)
General Comments 0
You need to be logged in to leave comments. Login now