Show More
@@ -1,444 +1,444 b'' | |||||
1 | import abc |
|
1 | import abc | |
2 | import functools |
|
2 | import functools | |
3 | import re |
|
3 | import re | |
4 | from StringIO import StringIO |
|
4 | from StringIO import StringIO | |
5 |
|
5 | |||
6 | from IPython.core.splitinput import LineInfo |
|
6 | from IPython.core.splitinput import LineInfo | |
7 | from IPython.utils import tokenize2 |
|
7 | from IPython.utils import tokenize2 | |
8 | from IPython.utils.tokenize2 import generate_tokens, untokenize, TokenError |
|
8 | from IPython.utils.tokenize2 import generate_tokens, untokenize, TokenError | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Globals |
|
11 | # Globals | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | # The escape sequences that define the syntax transformations IPython will |
|
14 | # The escape sequences that define the syntax transformations IPython will | |
15 | # apply to user input. These can NOT be just changed here: many regular |
|
15 | # apply to user input. These can NOT be just changed here: many regular | |
16 | # expressions and other parts of the code may use their hardcoded values, and |
|
16 | # expressions and other parts of the code may use their hardcoded values, and | |
17 | # for all intents and purposes they constitute the 'IPython syntax', so they |
|
17 | # for all intents and purposes they constitute the 'IPython syntax', so they | |
18 | # should be considered fixed. |
|
18 | # should be considered fixed. | |
19 |
|
19 | |||
20 | ESC_SHELL = '!' # Send line to underlying system shell |
|
20 | ESC_SHELL = '!' # Send line to underlying system shell | |
21 | ESC_SH_CAP = '!!' # Send line to system shell and capture output |
|
21 | ESC_SH_CAP = '!!' # Send line to system shell and capture output | |
22 | ESC_HELP = '?' # Find information about object |
|
22 | ESC_HELP = '?' # Find information about object | |
23 | ESC_HELP2 = '??' # Find extra-detailed information about object |
|
23 | ESC_HELP2 = '??' # Find extra-detailed information about object | |
24 | ESC_MAGIC = '%' # Call magic function |
|
24 | ESC_MAGIC = '%' # Call magic function | |
25 | ESC_MAGIC2 = '%%' # Call cell-magic function |
|
25 | ESC_MAGIC2 = '%%' # Call cell-magic function | |
26 | ESC_QUOTE = ',' # Split args on whitespace, quote each as string and call |
|
26 | ESC_QUOTE = ',' # Split args on whitespace, quote each as string and call | |
27 | ESC_QUOTE2 = ';' # Quote all args as a single string, call |
|
27 | ESC_QUOTE2 = ';' # Quote all args as a single string, call | |
28 | ESC_PAREN = '/' # Call first argument with rest of line as arguments |
|
28 | ESC_PAREN = '/' # Call first argument with rest of line as arguments | |
29 |
|
29 | |||
30 | ESC_SEQUENCES = [ESC_SHELL, ESC_SH_CAP, ESC_HELP ,\ |
|
30 | ESC_SEQUENCES = [ESC_SHELL, ESC_SH_CAP, ESC_HELP ,\ | |
31 | ESC_HELP2, ESC_MAGIC, ESC_MAGIC2,\ |
|
31 | ESC_HELP2, ESC_MAGIC, ESC_MAGIC2,\ | |
32 | ESC_QUOTE, ESC_QUOTE2, ESC_PAREN ] |
|
32 | ESC_QUOTE, ESC_QUOTE2, ESC_PAREN ] | |
33 |
|
33 | |||
34 |
|
34 | |||
35 | class InputTransformer(object): |
|
35 | class InputTransformer(object): | |
36 | """Abstract base class for line-based input transformers.""" |
|
36 | """Abstract base class for line-based input transformers.""" | |
37 | __metaclass__ = abc.ABCMeta |
|
37 | __metaclass__ = abc.ABCMeta | |
38 |
|
38 | |||
39 | @abc.abstractmethod |
|
39 | @abc.abstractmethod | |
40 | def push(self, line): |
|
40 | def push(self, line): | |
41 | """Send a line of input to the transformer, returning the transformed |
|
41 | """Send a line of input to the transformer, returning the transformed | |
42 | input or None if the transformer is waiting for more input. |
|
42 | input or None if the transformer is waiting for more input. | |
43 |
|
43 | |||
44 | Must be overridden by subclasses. |
|
44 | Must be overridden by subclasses. | |
45 | """ |
|
45 | """ | |
46 | pass |
|
46 | pass | |
47 |
|
47 | |||
48 | @abc.abstractmethod |
|
48 | @abc.abstractmethod | |
49 | def reset(self): |
|
49 | def reset(self): | |
50 | """Return, transformed any lines that the transformer has accumulated, |
|
50 | """Return, transformed any lines that the transformer has accumulated, | |
51 | and reset its internal state. |
|
51 | and reset its internal state. | |
52 |
|
52 | |||
53 | Must be overridden by subclasses. |
|
53 | Must be overridden by subclasses. | |
54 | """ |
|
54 | """ | |
55 | pass |
|
55 | pass | |
56 |
|
56 | |||
57 | @classmethod |
|
57 | @classmethod | |
58 | def wrap(cls, func): |
|
58 | def wrap(cls, func): | |
59 | """Can be used by subclasses as a decorator, to return a factory that |
|
59 | """Can be used by subclasses as a decorator, to return a factory that | |
60 | will allow instantiation with the decorated object. |
|
60 | will allow instantiation with the decorated object. | |
61 | """ |
|
61 | """ | |
62 | @functools.wraps(func) |
|
62 | @functools.wraps(func) | |
63 | def transformer_factory(**kwargs): |
|
63 | def transformer_factory(**kwargs): | |
64 | return cls(func, **kwargs) |
|
64 | return cls(func, **kwargs) | |
65 |
|
65 | |||
66 | return transformer_factory |
|
66 | return transformer_factory | |
67 |
|
67 | |||
68 | class StatelessInputTransformer(InputTransformer): |
|
68 | class StatelessInputTransformer(InputTransformer): | |
69 | """Wrapper for a stateless input transformer implemented as a function.""" |
|
69 | """Wrapper for a stateless input transformer implemented as a function.""" | |
70 | def __init__(self, func): |
|
70 | def __init__(self, func): | |
71 | self.func = func |
|
71 | self.func = func | |
72 |
|
72 | |||
73 | def __repr__(self): |
|
73 | def __repr__(self): | |
74 | return "StatelessInputTransformer(func={!r})".format(self.func) |
|
74 | return "StatelessInputTransformer(func={0!r})".format(self.func) | |
75 |
|
75 | |||
76 | def push(self, line): |
|
76 | def push(self, line): | |
77 | """Send a line of input to the transformer, returning the |
|
77 | """Send a line of input to the transformer, returning the | |
78 | transformed input.""" |
|
78 | transformed input.""" | |
79 | return self.func(line) |
|
79 | return self.func(line) | |
80 |
|
80 | |||
81 | def reset(self): |
|
81 | def reset(self): | |
82 | """No-op - exists for compatibility.""" |
|
82 | """No-op - exists for compatibility.""" | |
83 | pass |
|
83 | pass | |
84 |
|
84 | |||
85 | class CoroutineInputTransformer(InputTransformer): |
|
85 | class CoroutineInputTransformer(InputTransformer): | |
86 | """Wrapper for an input transformer implemented as a coroutine.""" |
|
86 | """Wrapper for an input transformer implemented as a coroutine.""" | |
87 | def __init__(self, coro, **kwargs): |
|
87 | def __init__(self, coro, **kwargs): | |
88 | # Prime it |
|
88 | # Prime it | |
89 | self.coro = coro(**kwargs) |
|
89 | self.coro = coro(**kwargs) | |
90 | next(self.coro) |
|
90 | next(self.coro) | |
91 |
|
91 | |||
92 | def __repr__(self): |
|
92 | def __repr__(self): | |
93 | return "CoroutineInputTransformer(coro={!r})".format(self.coro) |
|
93 | return "CoroutineInputTransformer(coro={0!r})".format(self.coro) | |
94 |
|
94 | |||
95 | def push(self, line): |
|
95 | def push(self, line): | |
96 | """Send a line of input to the transformer, returning the |
|
96 | """Send a line of input to the transformer, returning the | |
97 | transformed input or None if the transformer is waiting for more |
|
97 | transformed input or None if the transformer is waiting for more | |
98 | input. |
|
98 | input. | |
99 | """ |
|
99 | """ | |
100 | return self.coro.send(line) |
|
100 | return self.coro.send(line) | |
101 |
|
101 | |||
102 | def reset(self): |
|
102 | def reset(self): | |
103 | """Return, transformed any lines that the transformer has |
|
103 | """Return, transformed any lines that the transformer has | |
104 | accumulated, and reset its internal state. |
|
104 | accumulated, and reset its internal state. | |
105 | """ |
|
105 | """ | |
106 | return self.coro.send(None) |
|
106 | return self.coro.send(None) | |
107 |
|
107 | |||
108 | class TokenInputTransformer(InputTransformer): |
|
108 | class TokenInputTransformer(InputTransformer): | |
109 | """Wrapper for a token-based input transformer. |
|
109 | """Wrapper for a token-based input transformer. | |
110 |
|
110 | |||
111 | func should accept a list of tokens (5-tuples, see tokenize docs), and |
|
111 | func should accept a list of tokens (5-tuples, see tokenize docs), and | |
112 | return an iterable which can be passed to tokenize.untokenize(). |
|
112 | return an iterable which can be passed to tokenize.untokenize(). | |
113 | """ |
|
113 | """ | |
114 | def __init__(self, func): |
|
114 | def __init__(self, func): | |
115 | self.func = func |
|
115 | self.func = func | |
116 | self.current_line = "" |
|
116 | self.current_line = "" | |
117 | self.line_used = False |
|
117 | self.line_used = False | |
118 | self.reset_tokenizer() |
|
118 | self.reset_tokenizer() | |
119 |
|
119 | |||
120 | def reset_tokenizer(self): |
|
120 | def reset_tokenizer(self): | |
121 | self.tokenizer = generate_tokens(self.get_line) |
|
121 | self.tokenizer = generate_tokens(self.get_line) | |
122 |
|
122 | |||
123 | def get_line(self): |
|
123 | def get_line(self): | |
124 | if self.line_used: |
|
124 | if self.line_used: | |
125 | raise TokenError |
|
125 | raise TokenError | |
126 | self.line_used = True |
|
126 | self.line_used = True | |
127 | return self.current_line |
|
127 | return self.current_line | |
128 |
|
128 | |||
129 | def push(self, line): |
|
129 | def push(self, line): | |
130 | self.current_line += line + "\n" |
|
130 | self.current_line += line + "\n" | |
131 | if self.current_line.isspace(): |
|
131 | if self.current_line.isspace(): | |
132 | return self.reset() |
|
132 | return self.reset() | |
133 |
|
133 | |||
134 | self.line_used = False |
|
134 | self.line_used = False | |
135 | tokens = [] |
|
135 | tokens = [] | |
136 | stop_at_NL = False |
|
136 | stop_at_NL = False | |
137 | try: |
|
137 | try: | |
138 | for intok in self.tokenizer: |
|
138 | for intok in self.tokenizer: | |
139 | tokens.append(intok) |
|
139 | tokens.append(intok) | |
140 | t = intok[0] |
|
140 | t = intok[0] | |
141 | if t == tokenize2.NEWLINE or (stop_at_NL and t == tokenize2.NL): |
|
141 | if t == tokenize2.NEWLINE or (stop_at_NL and t == tokenize2.NL): | |
142 | # Stop before we try to pull a line we don't have yet |
|
142 | # Stop before we try to pull a line we don't have yet | |
143 | break |
|
143 | break | |
144 | elif t == tokenize2.ERRORTOKEN: |
|
144 | elif t == tokenize2.ERRORTOKEN: | |
145 | stop_at_NL = True |
|
145 | stop_at_NL = True | |
146 | except TokenError: |
|
146 | except TokenError: | |
147 | # Multi-line statement - stop and try again with the next line |
|
147 | # Multi-line statement - stop and try again with the next line | |
148 | self.reset_tokenizer() |
|
148 | self.reset_tokenizer() | |
149 | return None |
|
149 | return None | |
150 |
|
150 | |||
151 | return self.output(tokens) |
|
151 | return self.output(tokens) | |
152 |
|
152 | |||
153 | def output(self, tokens): |
|
153 | def output(self, tokens): | |
154 | self.current_line = "" |
|
154 | self.current_line = "" | |
155 | self.reset_tokenizer() |
|
155 | self.reset_tokenizer() | |
156 | return untokenize(self.func(tokens)).rstrip('\n') |
|
156 | return untokenize(self.func(tokens)).rstrip('\n') | |
157 |
|
157 | |||
158 | def reset(self): |
|
158 | def reset(self): | |
159 | l = self.current_line |
|
159 | l = self.current_line | |
160 | self.current_line = "" |
|
160 | self.current_line = "" | |
161 | self.reset_tokenizer() |
|
161 | self.reset_tokenizer() | |
162 | if l: |
|
162 | if l: | |
163 | return l.rstrip('\n') |
|
163 | return l.rstrip('\n') | |
164 |
|
164 | |||
165 | class assemble_python_lines(TokenInputTransformer): |
|
165 | class assemble_python_lines(TokenInputTransformer): | |
166 | def __init__(self): |
|
166 | def __init__(self): | |
167 | super(assemble_python_lines, self).__init__(None) |
|
167 | super(assemble_python_lines, self).__init__(None) | |
168 |
|
168 | |||
169 | def output(self, tokens): |
|
169 | def output(self, tokens): | |
170 | return self.reset() |
|
170 | return self.reset() | |
171 |
|
171 | |||
172 | @CoroutineInputTransformer.wrap |
|
172 | @CoroutineInputTransformer.wrap | |
173 | def assemble_logical_lines(): |
|
173 | def assemble_logical_lines(): | |
174 | """Join lines following explicit line continuations (\)""" |
|
174 | """Join lines following explicit line continuations (\)""" | |
175 | line = '' |
|
175 | line = '' | |
176 | while True: |
|
176 | while True: | |
177 | line = (yield line) |
|
177 | line = (yield line) | |
178 | if not line or line.isspace(): |
|
178 | if not line or line.isspace(): | |
179 | continue |
|
179 | continue | |
180 |
|
180 | |||
181 | parts = [] |
|
181 | parts = [] | |
182 | while line is not None: |
|
182 | while line is not None: | |
183 | if line.endswith('\\') and (not has_comment(line)): |
|
183 | if line.endswith('\\') and (not has_comment(line)): | |
184 | parts.append(line[:-1]) |
|
184 | parts.append(line[:-1]) | |
185 | line = (yield None) # Get another line |
|
185 | line = (yield None) # Get another line | |
186 | else: |
|
186 | else: | |
187 | parts.append(line) |
|
187 | parts.append(line) | |
188 | break |
|
188 | break | |
189 |
|
189 | |||
190 | # Output |
|
190 | # Output | |
191 | line = ''.join(parts) |
|
191 | line = ''.join(parts) | |
192 |
|
192 | |||
193 | # Utilities |
|
193 | # Utilities | |
194 | def _make_help_call(target, esc, lspace, next_input=None): |
|
194 | def _make_help_call(target, esc, lspace, next_input=None): | |
195 | """Prepares a pinfo(2)/psearch call from a target name and the escape |
|
195 | """Prepares a pinfo(2)/psearch call from a target name and the escape | |
196 | (i.e. ? or ??)""" |
|
196 | (i.e. ? or ??)""" | |
197 | method = 'pinfo2' if esc == '??' \ |
|
197 | method = 'pinfo2' if esc == '??' \ | |
198 | else 'psearch' if '*' in target \ |
|
198 | else 'psearch' if '*' in target \ | |
199 | else 'pinfo' |
|
199 | else 'pinfo' | |
200 | arg = " ".join([method, target]) |
|
200 | arg = " ".join([method, target]) | |
201 | if next_input is None: |
|
201 | if next_input is None: | |
202 | return '%sget_ipython().magic(%r)' % (lspace, arg) |
|
202 | return '%sget_ipython().magic(%r)' % (lspace, arg) | |
203 | else: |
|
203 | else: | |
204 | return '%sget_ipython().set_next_input(%r);get_ipython().magic(%r)' % \ |
|
204 | return '%sget_ipython().set_next_input(%r);get_ipython().magic(%r)' % \ | |
205 | (lspace, next_input, arg) |
|
205 | (lspace, next_input, arg) | |
206 |
|
206 | |||
207 | # These define the transformations for the different escape characters. |
|
207 | # These define the transformations for the different escape characters. | |
208 | def _tr_system(line_info): |
|
208 | def _tr_system(line_info): | |
209 | "Translate lines escaped with: !" |
|
209 | "Translate lines escaped with: !" | |
210 | cmd = line_info.line.lstrip().lstrip(ESC_SHELL) |
|
210 | cmd = line_info.line.lstrip().lstrip(ESC_SHELL) | |
211 | return '%sget_ipython().system(%r)' % (line_info.pre, cmd) |
|
211 | return '%sget_ipython().system(%r)' % (line_info.pre, cmd) | |
212 |
|
212 | |||
213 | def _tr_system2(line_info): |
|
213 | def _tr_system2(line_info): | |
214 | "Translate lines escaped with: !!" |
|
214 | "Translate lines escaped with: !!" | |
215 | cmd = line_info.line.lstrip()[2:] |
|
215 | cmd = line_info.line.lstrip()[2:] | |
216 | return '%sget_ipython().getoutput(%r)' % (line_info.pre, cmd) |
|
216 | return '%sget_ipython().getoutput(%r)' % (line_info.pre, cmd) | |
217 |
|
217 | |||
218 | def _tr_help(line_info): |
|
218 | def _tr_help(line_info): | |
219 | "Translate lines escaped with: ?/??" |
|
219 | "Translate lines escaped with: ?/??" | |
220 | # A naked help line should just fire the intro help screen |
|
220 | # A naked help line should just fire the intro help screen | |
221 | if not line_info.line[1:]: |
|
221 | if not line_info.line[1:]: | |
222 | return 'get_ipython().show_usage()' |
|
222 | return 'get_ipython().show_usage()' | |
223 |
|
223 | |||
224 | return _make_help_call(line_info.ifun, line_info.esc, line_info.pre) |
|
224 | return _make_help_call(line_info.ifun, line_info.esc, line_info.pre) | |
225 |
|
225 | |||
226 | def _tr_magic(line_info): |
|
226 | def _tr_magic(line_info): | |
227 | "Translate lines escaped with: %" |
|
227 | "Translate lines escaped with: %" | |
228 | tpl = '%sget_ipython().magic(%r)' |
|
228 | tpl = '%sget_ipython().magic(%r)' | |
229 | cmd = ' '.join([line_info.ifun, line_info.the_rest]).strip() |
|
229 | cmd = ' '.join([line_info.ifun, line_info.the_rest]).strip() | |
230 | return tpl % (line_info.pre, cmd) |
|
230 | return tpl % (line_info.pre, cmd) | |
231 |
|
231 | |||
232 | def _tr_quote(line_info): |
|
232 | def _tr_quote(line_info): | |
233 | "Translate lines escaped with: ," |
|
233 | "Translate lines escaped with: ," | |
234 | return '%s%s("%s")' % (line_info.pre, line_info.ifun, |
|
234 | return '%s%s("%s")' % (line_info.pre, line_info.ifun, | |
235 | '", "'.join(line_info.the_rest.split()) ) |
|
235 | '", "'.join(line_info.the_rest.split()) ) | |
236 |
|
236 | |||
237 | def _tr_quote2(line_info): |
|
237 | def _tr_quote2(line_info): | |
238 | "Translate lines escaped with: ;" |
|
238 | "Translate lines escaped with: ;" | |
239 | return '%s%s("%s")' % (line_info.pre, line_info.ifun, |
|
239 | return '%s%s("%s")' % (line_info.pre, line_info.ifun, | |
240 | line_info.the_rest) |
|
240 | line_info.the_rest) | |
241 |
|
241 | |||
242 | def _tr_paren(line_info): |
|
242 | def _tr_paren(line_info): | |
243 | "Translate lines escaped with: /" |
|
243 | "Translate lines escaped with: /" | |
244 | return '%s%s(%s)' % (line_info.pre, line_info.ifun, |
|
244 | return '%s%s(%s)' % (line_info.pre, line_info.ifun, | |
245 | ", ".join(line_info.the_rest.split())) |
|
245 | ", ".join(line_info.the_rest.split())) | |
246 |
|
246 | |||
247 | tr = { ESC_SHELL : _tr_system, |
|
247 | tr = { ESC_SHELL : _tr_system, | |
248 | ESC_SH_CAP : _tr_system2, |
|
248 | ESC_SH_CAP : _tr_system2, | |
249 | ESC_HELP : _tr_help, |
|
249 | ESC_HELP : _tr_help, | |
250 | ESC_HELP2 : _tr_help, |
|
250 | ESC_HELP2 : _tr_help, | |
251 | ESC_MAGIC : _tr_magic, |
|
251 | ESC_MAGIC : _tr_magic, | |
252 | ESC_QUOTE : _tr_quote, |
|
252 | ESC_QUOTE : _tr_quote, | |
253 | ESC_QUOTE2 : _tr_quote2, |
|
253 | ESC_QUOTE2 : _tr_quote2, | |
254 | ESC_PAREN : _tr_paren } |
|
254 | ESC_PAREN : _tr_paren } | |
255 |
|
255 | |||
256 | @StatelessInputTransformer.wrap |
|
256 | @StatelessInputTransformer.wrap | |
257 | def escaped_commands(line): |
|
257 | def escaped_commands(line): | |
258 | """Transform escaped commands - %magic, !system, ?help + various autocalls. |
|
258 | """Transform escaped commands - %magic, !system, ?help + various autocalls. | |
259 | """ |
|
259 | """ | |
260 | if not line or line.isspace(): |
|
260 | if not line or line.isspace(): | |
261 | return line |
|
261 | return line | |
262 | lineinf = LineInfo(line) |
|
262 | lineinf = LineInfo(line) | |
263 | if lineinf.esc not in tr: |
|
263 | if lineinf.esc not in tr: | |
264 | return line |
|
264 | return line | |
265 |
|
265 | |||
266 | return tr[lineinf.esc](lineinf) |
|
266 | return tr[lineinf.esc](lineinf) | |
267 |
|
267 | |||
268 | _initial_space_re = re.compile(r'\s*') |
|
268 | _initial_space_re = re.compile(r'\s*') | |
269 |
|
269 | |||
270 | _help_end_re = re.compile(r"""(%{0,2} |
|
270 | _help_end_re = re.compile(r"""(%{0,2} | |
271 | [a-zA-Z_*][\w*]* # Variable name |
|
271 | [a-zA-Z_*][\w*]* # Variable name | |
272 | (\.[a-zA-Z_*][\w*]*)* # .etc.etc |
|
272 | (\.[a-zA-Z_*][\w*]*)* # .etc.etc | |
273 | ) |
|
273 | ) | |
274 | (\?\??)$ # ? or ??""", |
|
274 | (\?\??)$ # ? or ??""", | |
275 | re.VERBOSE) |
|
275 | re.VERBOSE) | |
276 |
|
276 | |||
277 | def has_comment(src): |
|
277 | def has_comment(src): | |
278 | """Indicate whether an input line has (i.e. ends in, or is) a comment. |
|
278 | """Indicate whether an input line has (i.e. ends in, or is) a comment. | |
279 |
|
279 | |||
280 | This uses tokenize, so it can distinguish comments from # inside strings. |
|
280 | This uses tokenize, so it can distinguish comments from # inside strings. | |
281 |
|
281 | |||
282 | Parameters |
|
282 | Parameters | |
283 | ---------- |
|
283 | ---------- | |
284 | src : string |
|
284 | src : string | |
285 | A single line input string. |
|
285 | A single line input string. | |
286 |
|
286 | |||
287 | Returns |
|
287 | Returns | |
288 | ------- |
|
288 | ------- | |
289 | comment : bool |
|
289 | comment : bool | |
290 | True if source has a comment. |
|
290 | True if source has a comment. | |
291 | """ |
|
291 | """ | |
292 | readline = StringIO(src).readline |
|
292 | readline = StringIO(src).readline | |
293 | toktypes = set() |
|
293 | toktypes = set() | |
294 | try: |
|
294 | try: | |
295 | for t in generate_tokens(readline): |
|
295 | for t in generate_tokens(readline): | |
296 | toktypes.add(t[0]) |
|
296 | toktypes.add(t[0]) | |
297 | except TokenError: |
|
297 | except TokenError: | |
298 | pass |
|
298 | pass | |
299 | return(tokenize2.COMMENT in toktypes) |
|
299 | return(tokenize2.COMMENT in toktypes) | |
300 |
|
300 | |||
301 |
|
301 | |||
302 | @StatelessInputTransformer.wrap |
|
302 | @StatelessInputTransformer.wrap | |
303 | def help_end(line): |
|
303 | def help_end(line): | |
304 | """Translate lines with ?/?? at the end""" |
|
304 | """Translate lines with ?/?? at the end""" | |
305 | m = _help_end_re.search(line) |
|
305 | m = _help_end_re.search(line) | |
306 | if m is None or has_comment(line): |
|
306 | if m is None or has_comment(line): | |
307 | return line |
|
307 | return line | |
308 | target = m.group(1) |
|
308 | target = m.group(1) | |
309 | esc = m.group(3) |
|
309 | esc = m.group(3) | |
310 | lspace = _initial_space_re.match(line).group(0) |
|
310 | lspace = _initial_space_re.match(line).group(0) | |
311 |
|
311 | |||
312 | # If we're mid-command, put it back on the next prompt for the user. |
|
312 | # If we're mid-command, put it back on the next prompt for the user. | |
313 | next_input = line.rstrip('?') if line.strip() != m.group(0) else None |
|
313 | next_input = line.rstrip('?') if line.strip() != m.group(0) else None | |
314 |
|
314 | |||
315 | return _make_help_call(target, esc, lspace, next_input) |
|
315 | return _make_help_call(target, esc, lspace, next_input) | |
316 |
|
316 | |||
317 |
|
317 | |||
318 | @CoroutineInputTransformer.wrap |
|
318 | @CoroutineInputTransformer.wrap | |
319 | def cellmagic(end_on_blank_line=False): |
|
319 | def cellmagic(end_on_blank_line=False): | |
320 | """Captures & transforms cell magics. |
|
320 | """Captures & transforms cell magics. | |
321 |
|
321 | |||
322 | After a cell magic is started, this stores up any lines it gets until it is |
|
322 | After a cell magic is started, this stores up any lines it gets until it is | |
323 | reset (sent None). |
|
323 | reset (sent None). | |
324 | """ |
|
324 | """ | |
325 | tpl = 'get_ipython().run_cell_magic(%r, %r, %r)' |
|
325 | tpl = 'get_ipython().run_cell_magic(%r, %r, %r)' | |
326 | cellmagic_help_re = re.compile('%%\w+\?') |
|
326 | cellmagic_help_re = re.compile('%%\w+\?') | |
327 | line = '' |
|
327 | line = '' | |
328 | while True: |
|
328 | while True: | |
329 | line = (yield line) |
|
329 | line = (yield line) | |
330 | if (not line) or (not line.startswith(ESC_MAGIC2)): |
|
330 | if (not line) or (not line.startswith(ESC_MAGIC2)): | |
331 | continue |
|
331 | continue | |
332 |
|
332 | |||
333 | if cellmagic_help_re.match(line): |
|
333 | if cellmagic_help_re.match(line): | |
334 | # This case will be handled by help_end |
|
334 | # This case will be handled by help_end | |
335 | continue |
|
335 | continue | |
336 |
|
336 | |||
337 | first = line |
|
337 | first = line | |
338 | body = [] |
|
338 | body = [] | |
339 | line = (yield None) |
|
339 | line = (yield None) | |
340 | while (line is not None) and \ |
|
340 | while (line is not None) and \ | |
341 | ((line.strip() != '') or not end_on_blank_line): |
|
341 | ((line.strip() != '') or not end_on_blank_line): | |
342 | body.append(line) |
|
342 | body.append(line) | |
343 | line = (yield None) |
|
343 | line = (yield None) | |
344 |
|
344 | |||
345 | # Output |
|
345 | # Output | |
346 | magic_name, _, first = first.partition(' ') |
|
346 | magic_name, _, first = first.partition(' ') | |
347 | magic_name = magic_name.lstrip(ESC_MAGIC2) |
|
347 | magic_name = magic_name.lstrip(ESC_MAGIC2) | |
348 | line = tpl % (magic_name, first, u'\n'.join(body)) |
|
348 | line = tpl % (magic_name, first, u'\n'.join(body)) | |
349 |
|
349 | |||
350 |
|
350 | |||
351 | def _strip_prompts(prompt_re): |
|
351 | def _strip_prompts(prompt_re): | |
352 | """Remove matching input prompts from a block of input.""" |
|
352 | """Remove matching input prompts from a block of input.""" | |
353 | line = '' |
|
353 | line = '' | |
354 | while True: |
|
354 | while True: | |
355 | line = (yield line) |
|
355 | line = (yield line) | |
356 |
|
356 | |||
357 | # First line of cell |
|
357 | # First line of cell | |
358 | if line is None: |
|
358 | if line is None: | |
359 | continue |
|
359 | continue | |
360 | out, n1 = prompt_re.subn('', line, count=1) |
|
360 | out, n1 = prompt_re.subn('', line, count=1) | |
361 | line = (yield out) |
|
361 | line = (yield out) | |
362 |
|
362 | |||
363 | # Second line of cell, because people often copy from just after the |
|
363 | # Second line of cell, because people often copy from just after the | |
364 | # first prompt, so we might not see it in the first line. |
|
364 | # first prompt, so we might not see it in the first line. | |
365 | if line is None: |
|
365 | if line is None: | |
366 | continue |
|
366 | continue | |
367 | out, n2 = prompt_re.subn('', line, count=1) |
|
367 | out, n2 = prompt_re.subn('', line, count=1) | |
368 | line = (yield out) |
|
368 | line = (yield out) | |
369 |
|
369 | |||
370 | if n1 or n2: |
|
370 | if n1 or n2: | |
371 | # Found the input prompt in the first two lines - check for it in |
|
371 | # Found the input prompt in the first two lines - check for it in | |
372 | # the rest of the cell as well. |
|
372 | # the rest of the cell as well. | |
373 | while line is not None: |
|
373 | while line is not None: | |
374 | line = (yield prompt_re.sub('', line, count=1)) |
|
374 | line = (yield prompt_re.sub('', line, count=1)) | |
375 |
|
375 | |||
376 | else: |
|
376 | else: | |
377 | # Prompts not in input - wait for reset |
|
377 | # Prompts not in input - wait for reset | |
378 | while line is not None: |
|
378 | while line is not None: | |
379 | line = (yield line) |
|
379 | line = (yield line) | |
380 |
|
380 | |||
381 | @CoroutineInputTransformer.wrap |
|
381 | @CoroutineInputTransformer.wrap | |
382 | def classic_prompt(): |
|
382 | def classic_prompt(): | |
383 | """Strip the >>>/... prompts of the Python interactive shell.""" |
|
383 | """Strip the >>>/... prompts of the Python interactive shell.""" | |
384 | prompt_re = re.compile(r'^(>>> ?|^\.\.\. ?)') |
|
384 | prompt_re = re.compile(r'^(>>> ?|^\.\.\. ?)') | |
385 | return _strip_prompts(prompt_re) |
|
385 | return _strip_prompts(prompt_re) | |
386 |
|
386 | |||
387 | @CoroutineInputTransformer.wrap |
|
387 | @CoroutineInputTransformer.wrap | |
388 | def ipy_prompt(): |
|
388 | def ipy_prompt(): | |
389 | """Strip IPython's In [1]:/...: prompts.""" |
|
389 | """Strip IPython's In [1]:/...: prompts.""" | |
390 | prompt_re = re.compile(r'^(In \[\d+\]: |^\ \ \ \.\.\.+: )') |
|
390 | prompt_re = re.compile(r'^(In \[\d+\]: |^\ \ \ \.\.\.+: )') | |
391 | return _strip_prompts(prompt_re) |
|
391 | return _strip_prompts(prompt_re) | |
392 |
|
392 | |||
393 |
|
393 | |||
394 | @CoroutineInputTransformer.wrap |
|
394 | @CoroutineInputTransformer.wrap | |
395 | def leading_indent(): |
|
395 | def leading_indent(): | |
396 | """Remove leading indentation. |
|
396 | """Remove leading indentation. | |
397 |
|
397 | |||
398 | If the first line starts with a spaces or tabs, the same whitespace will be |
|
398 | If the first line starts with a spaces or tabs, the same whitespace will be | |
399 | removed from each following line until it is reset. |
|
399 | removed from each following line until it is reset. | |
400 | """ |
|
400 | """ | |
401 | space_re = re.compile(r'^[ \t]+') |
|
401 | space_re = re.compile(r'^[ \t]+') | |
402 | line = '' |
|
402 | line = '' | |
403 | while True: |
|
403 | while True: | |
404 | line = (yield line) |
|
404 | line = (yield line) | |
405 |
|
405 | |||
406 | if line is None: |
|
406 | if line is None: | |
407 | continue |
|
407 | continue | |
408 |
|
408 | |||
409 | m = space_re.match(line) |
|
409 | m = space_re.match(line) | |
410 | if m: |
|
410 | if m: | |
411 | space = m.group(0) |
|
411 | space = m.group(0) | |
412 | while line is not None: |
|
412 | while line is not None: | |
413 | if line.startswith(space): |
|
413 | if line.startswith(space): | |
414 | line = line[len(space):] |
|
414 | line = line[len(space):] | |
415 | line = (yield line) |
|
415 | line = (yield line) | |
416 | else: |
|
416 | else: | |
417 | # No leading spaces - wait for reset |
|
417 | # No leading spaces - wait for reset | |
418 | while line is not None: |
|
418 | while line is not None: | |
419 | line = (yield line) |
|
419 | line = (yield line) | |
420 |
|
420 | |||
421 |
|
421 | |||
422 | assign_system_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))' |
|
422 | assign_system_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))' | |
423 | r'\s*=\s*!\s*(?P<cmd>.*)') |
|
423 | r'\s*=\s*!\s*(?P<cmd>.*)') | |
424 | assign_system_template = '%s = get_ipython().getoutput(%r)' |
|
424 | assign_system_template = '%s = get_ipython().getoutput(%r)' | |
425 | @StatelessInputTransformer.wrap |
|
425 | @StatelessInputTransformer.wrap | |
426 | def assign_from_system(line): |
|
426 | def assign_from_system(line): | |
427 | """Transform assignment from system commands (e.g. files = !ls)""" |
|
427 | """Transform assignment from system commands (e.g. files = !ls)""" | |
428 | m = assign_system_re.match(line) |
|
428 | m = assign_system_re.match(line) | |
429 | if m is None: |
|
429 | if m is None: | |
430 | return line |
|
430 | return line | |
431 |
|
431 | |||
432 | return assign_system_template % m.group('lhs', 'cmd') |
|
432 | return assign_system_template % m.group('lhs', 'cmd') | |
433 |
|
433 | |||
434 | assign_magic_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))' |
|
434 | assign_magic_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))' | |
435 | r'\s*=\s*%\s*(?P<cmd>.*)') |
|
435 | r'\s*=\s*%\s*(?P<cmd>.*)') | |
436 | assign_magic_template = '%s = get_ipython().magic(%r)' |
|
436 | assign_magic_template = '%s = get_ipython().magic(%r)' | |
437 | @StatelessInputTransformer.wrap |
|
437 | @StatelessInputTransformer.wrap | |
438 | def assign_from_magic(line): |
|
438 | def assign_from_magic(line): | |
439 | """Transform assignment from magic commands (e.g. a = %who_ls)""" |
|
439 | """Transform assignment from magic commands (e.g. a = %who_ls)""" | |
440 | m = assign_magic_re.match(line) |
|
440 | m = assign_magic_re.match(line) | |
441 | if m is None: |
|
441 | if m is None: | |
442 | return line |
|
442 | return line | |
443 |
|
443 | |||
444 | return assign_magic_template % m.group('lhs', 'cmd') |
|
444 | return assign_magic_template % m.group('lhs', 'cmd') |
General Comments 0
You need to be logged in to leave comments.
Login now