Show More
@@ -20,11 +20,11 b" _indent_re = re.compile(r'^[ \\t]+')" | |||
|
20 | 20 | |
|
21 | 21 | def leading_indent(lines): |
|
22 | 22 | """Remove leading indentation. |
|
23 | ||
|
23 | ||
|
24 | 24 | If the first line starts with a spaces or tabs, the same whitespace will be |
|
25 | 25 | removed from each following line in the cell. |
|
26 | 26 | """ |
|
27 |
if not lines: |
|
|
27 | if not lines: | |
|
28 | 28 | return lines |
|
29 | 29 | m = _indent_re.match(lines[0]) |
|
30 | 30 | if not m: |
@@ -36,7 +36,7 b' def leading_indent(lines):' | |||
|
36 | 36 | |
|
37 | 37 | class PromptStripper: |
|
38 | 38 | """Remove matching input prompts from a block of input. |
|
39 | ||
|
39 | ||
|
40 | 40 | Parameters |
|
41 | 41 | ---------- |
|
42 | 42 | prompt_re : regular expression |
@@ -47,7 +47,7 b' class PromptStripper:' | |||
|
47 | 47 | If no initial expression is given, prompt_re will be used everywhere. |
|
48 | 48 | Used mainly for plain Python prompts (``>>>``), where the continuation prompt |
|
49 | 49 | ``...`` is a valid Python expression in Python 3, so shouldn't be stripped. |
|
50 | ||
|
50 | ||
|
51 | 51 | If initial_re and prompt_re differ, |
|
52 | 52 | only initial_re will be tested against the first line. |
|
53 | 53 | If any prompt is found on the first two lines, |
@@ -61,6 +61,8 b' class PromptStripper:' | |||
|
61 | 61 | return [self.prompt_re.sub('', l, count=1) for l in lines] |
|
62 | 62 | |
|
63 | 63 | def __call__(self, lines): |
|
64 | if not lines: | |
|
65 | return lines | |
|
64 | 66 | if self.initial_re.match(lines[0]) or \ |
|
65 | 67 | (len(lines) > 1 and self.prompt_re.match(lines[1])): |
|
66 | 68 | return self._strip(lines) |
@@ -74,7 +76,7 b' classic_prompt = PromptStripper(' | |||
|
74 | 76 | ipython_prompt = PromptStripper(re.compile(r'^(In \[\d+\]: |\s*\.{3,}: ?)')) |
|
75 | 77 | |
|
76 | 78 | def cell_magic(lines): |
|
77 | if not lines[0].startswith('%%'): | |
|
79 | if not lines or not lines[0].startswith('%%'): | |
|
78 | 80 | return lines |
|
79 | 81 | if re.match('%%\w+\?', lines[0]): |
|
80 | 82 | # This case will be handled by help_end |
@@ -94,7 +96,7 b' def _find_assign_op(token_line):' | |||
|
94 | 96 | for i, ti in enumerate(token_line): |
|
95 | 97 | s = ti.string |
|
96 | 98 | if s == '=' and paren_level == 0: |
|
97 |
return i |
|
|
99 | return i | |
|
98 | 100 | if s in '([{': |
|
99 | 101 | paren_level += 1 |
|
100 | 102 | elif s in ')]}': |
@@ -114,7 +116,7 b' def find_end_of_continued_line(lines, start_line: int):' | |||
|
114 | 116 | return end_line |
|
115 | 117 | |
|
116 | 118 | def assemble_continued_line(lines, start: Tuple[int, int], end_line: int): |
|
117 |
"""Assemble a single line from multiple continued line pieces |
|
|
119 | """Assemble a single line from multiple continued line pieces | |
|
118 | 120 | |
|
119 | 121 | Continued lines are lines ending in ``\``, and the line following the last |
|
120 | 122 | ``\`` in the block. |
@@ -204,7 +206,7 b' class MagicAssign(TokenTransformBase):' | |||
|
204 | 206 | and (line[assign_ix+1].string == '%') \ |
|
205 | 207 | and (line[assign_ix+2].type == tokenize.NAME): |
|
206 | 208 | return cls(line[assign_ix+1].start) |
|
207 | ||
|
209 | ||
|
208 | 210 | def transform(self, lines: List[str]): |
|
209 | 211 | """Transform a magic assignment found by the ``find()`` classmethod. |
|
210 | 212 | """ |
@@ -214,12 +216,12 b' class MagicAssign(TokenTransformBase):' | |||
|
214 | 216 | rhs = assemble_continued_line(lines, (start_line, start_col), end_line) |
|
215 | 217 | assert rhs.startswith('%'), rhs |
|
216 | 218 | magic_name, _, args = rhs[1:].partition(' ') |
|
217 | ||
|
219 | ||
|
218 | 220 | lines_before = lines[:start_line] |
|
219 | 221 | call = "get_ipython().run_line_magic({!r}, {!r})".format(magic_name, args) |
|
220 | 222 | new_line = lhs + call + '\n' |
|
221 | 223 | lines_after = lines[end_line+1:] |
|
222 | ||
|
224 | ||
|
223 | 225 | return lines_before + [new_line] + lines_after |
|
224 | 226 | |
|
225 | 227 | |
@@ -466,7 +468,7 b' def make_tokens_by_line(lines):' | |||
|
466 | 468 | pass |
|
467 | 469 | if not tokens_by_line[-1]: |
|
468 | 470 | tokens_by_line.pop() |
|
469 | ||
|
471 | ||
|
470 | 472 | return tokens_by_line |
|
471 | 473 | |
|
472 | 474 | def show_linewise_tokens(s: str): |
@@ -503,12 +505,12 b' class TransformerManager:' | |||
|
503 | 505 | EscapedCommand, |
|
504 | 506 | HelpEnd, |
|
505 | 507 | ] |
|
506 | ||
|
508 | ||
|
507 | 509 | def do_one_token_transform(self, lines): |
|
508 | 510 | """Find and run the transform earliest in the code. |
|
509 | ||
|
511 | ||
|
510 | 512 | Returns (changed, lines). |
|
511 | ||
|
513 | ||
|
512 | 514 | This method is called repeatedly until changed is False, indicating |
|
513 | 515 | that all available transformations are complete. |
|
514 | 516 |
General Comments 0
You need to be logged in to leave comments.
Login now