Show More
@@ -82,45 +82,6 def strip_email_quotes(raw_lines): | |||||
82 | return '\n'.join(lines) + '\n' |
|
82 | return '\n'.join(lines) + '\n' | |
83 |
|
83 | |||
84 |
|
84 | |||
85 | # These two functions are needed by the %paste/%cpaste magics. In practice |
|
|||
86 | # they are basically methods (they take the shell as their first argument), but |
|
|||
87 | # we leave them as standalone functions because eventually the magics |
|
|||
88 | # themselves will become separate objects altogether. At that point, the |
|
|||
89 | # magics will have access to the shell object, and these functions can be made |
|
|||
90 | # methods of the magic object, but not of the shell. |
|
|||
91 |
|
||||
92 | def store_or_execute(shell, block, name): |
|
|||
93 | """ Execute a block, or store it in a variable, per the user's request. |
|
|||
94 | """ |
|
|||
95 | # Dedent and prefilter so what we store matches what is executed by |
|
|||
96 | # run_cell. |
|
|||
97 | b = shell.prefilter(textwrap.dedent(block)) |
|
|||
98 |
|
||||
99 | if name: |
|
|||
100 | # If storing it for further editing, run the prefilter on it |
|
|||
101 | shell.user_ns[name] = SList(b.splitlines()) |
|
|||
102 | print "Block assigned to '%s'" % name |
|
|||
103 | else: |
|
|||
104 | shell.user_ns['pasted_block'] = b |
|
|||
105 | shell.run_cell(b) |
|
|||
106 |
|
||||
107 |
|
||||
108 | def rerun_pasted(shell, name='pasted_block'): |
|
|||
109 | """ Rerun a previously pasted command. |
|
|||
110 | """ |
|
|||
111 | b = shell.user_ns.get(name) |
|
|||
112 |
|
||||
113 | # Sanity checks |
|
|||
114 | if b is None: |
|
|||
115 | raise UsageError('No previous pasted block available') |
|
|||
116 | if not isinstance(b, basestring): |
|
|||
117 | raise UsageError( |
|
|||
118 | "Variable 'pasted_block' is not a string, can't execute") |
|
|||
119 |
|
||||
120 | print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b)) |
|
|||
121 | shell.run_cell(b) |
|
|||
122 |
|
||||
123 |
|
||||
124 | #------------------------------------------------------------------------ |
|
85 | #------------------------------------------------------------------------ | |
125 | # Terminal-specific magics |
|
86 | # Terminal-specific magics | |
126 | #------------------------------------------------------------------------ |
|
87 | #------------------------------------------------------------------------ | |
@@ -128,6 +89,37 def rerun_pasted(shell, name='pasted_block'): | |||||
128 | @magics_class |
|
89 | @magics_class | |
129 | class TerminalMagics(Magics): |
|
90 | class TerminalMagics(Magics): | |
130 |
|
91 | |||
|
92 | def store_or_execute(self, block, name): | |||
|
93 | """ Execute a block, or store it in a variable, per the user's request. | |||
|
94 | """ | |||
|
95 | # Dedent and prefilter so what we store matches what is executed by | |||
|
96 | # run_cell. | |||
|
97 | b = self.shell.prefilter(textwrap.dedent(block)) | |||
|
98 | ||||
|
99 | if name: | |||
|
100 | # If storing it for further editing, run the prefilter on it | |||
|
101 | self.shell.user_ns[name] = SList(b.splitlines()) | |||
|
102 | print "Block assigned to '%s'" % name | |||
|
103 | else: | |||
|
104 | self.shell.user_ns['pasted_block'] = b | |||
|
105 | self.shell.run_cell(b) | |||
|
106 | ||||
|
107 | ||||
|
108 | def rerun_pasted(self, name='pasted_block'): | |||
|
109 | """ Rerun a previously pasted command. | |||
|
110 | """ | |||
|
111 | b = self.shell.user_ns.get(name) | |||
|
112 | ||||
|
113 | # Sanity checks | |||
|
114 | if b is None: | |||
|
115 | raise UsageError('No previous pasted block available') | |||
|
116 | if not isinstance(b, basestring): | |||
|
117 | raise UsageError( | |||
|
118 | "Variable 'pasted_block' is not a string, can't execute") | |||
|
119 | ||||
|
120 | print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b)) | |||
|
121 | self.shell.run_cell(b) | |||
|
122 | ||||
131 | @line_magic |
|
123 | @line_magic | |
132 | def autoindent(self, parameter_s = ''): |
|
124 | def autoindent(self, parameter_s = ''): | |
133 | """Toggle autoindent on/off (if available).""" |
|
125 | """Toggle autoindent on/off (if available).""" | |
@@ -181,12 +173,12 class TerminalMagics(Magics): | |||||
181 |
|
173 | |||
182 | opts, name = self.parse_options(parameter_s, 'rs:', mode='string') |
|
174 | opts, name = self.parse_options(parameter_s, 'rs:', mode='string') | |
183 | if 'r' in opts: |
|
175 | if 'r' in opts: | |
184 |
rerun_pasted( |
|
176 | self.rerun_pasted() | |
185 | return |
|
177 | return | |
186 |
|
178 | |||
187 | sentinel = opts.get('s', '--') |
|
179 | sentinel = opts.get('s', '--') | |
188 | block = strip_email_quotes(get_pasted_lines(sentinel)) |
|
180 | block = strip_email_quotes(get_pasted_lines(sentinel)) | |
189 |
store_or_execute( |
|
181 | self.store_or_execute(block, name) | |
190 |
|
182 | |||
191 | @line_magic |
|
183 | @line_magic | |
192 | def paste(self, parameter_s=''): |
|
184 | def paste(self, parameter_s=''): | |
@@ -222,7 +214,7 class TerminalMagics(Magics): | |||||
222 | """ |
|
214 | """ | |
223 | opts, name = self.parse_options(parameter_s, 'rq', mode='string') |
|
215 | opts, name = self.parse_options(parameter_s, 'rq', mode='string') | |
224 | if 'r' in opts: |
|
216 | if 'r' in opts: | |
225 |
rerun_pasted( |
|
217 | self.rerun_pasted() | |
226 | return |
|
218 | return | |
227 | try: |
|
219 | try: | |
228 | text = self.shell.hooks.clipboard_get() |
|
220 | text = self.shell.hooks.clipboard_get() | |
@@ -243,7 +235,7 class TerminalMagics(Magics): | |||||
243 | write('\n') |
|
235 | write('\n') | |
244 | write("## -- End pasted text --\n") |
|
236 | write("## -- End pasted text --\n") | |
245 |
|
237 | |||
246 |
store_or_execute( |
|
238 | self.store_or_execute(block, name) | |
247 |
|
239 | |||
248 | # Class-level: add a '%cls' magic only on Windows |
|
240 | # Class-level: add a '%cls' magic only on Windows | |
249 | if sys.platform == 'win32': |
|
241 | if sys.platform == 'win32': |
General Comments 0
You need to be logged in to leave comments.
Login now