Show More
@@ -82,45 +82,6 def strip_email_quotes(raw_lines): | |||
|
82 | 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 | 86 | # Terminal-specific magics |
|
126 | 87 | #------------------------------------------------------------------------ |
@@ -128,6 +89,37 def rerun_pasted(shell, name='pasted_block'): | |||
|
128 | 89 | @magics_class |
|
129 | 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 | 123 | @line_magic |
|
132 | 124 | def autoindent(self, parameter_s = ''): |
|
133 | 125 | """Toggle autoindent on/off (if available).""" |
@@ -181,12 +173,12 class TerminalMagics(Magics): | |||
|
181 | 173 | |
|
182 | 174 | opts, name = self.parse_options(parameter_s, 'rs:', mode='string') |
|
183 | 175 | if 'r' in opts: |
|
184 |
rerun_pasted( |
|
|
176 | self.rerun_pasted() | |
|
185 | 177 | return |
|
186 | 178 | |
|
187 | 179 | sentinel = opts.get('s', '--') |
|
188 | 180 | block = strip_email_quotes(get_pasted_lines(sentinel)) |
|
189 |
store_or_execute( |
|
|
181 | self.store_or_execute(block, name) | |
|
190 | 182 | |
|
191 | 183 | @line_magic |
|
192 | 184 | def paste(self, parameter_s=''): |
@@ -222,7 +214,7 class TerminalMagics(Magics): | |||
|
222 | 214 | """ |
|
223 | 215 | opts, name = self.parse_options(parameter_s, 'rq', mode='string') |
|
224 | 216 | if 'r' in opts: |
|
225 |
rerun_pasted( |
|
|
217 | self.rerun_pasted() | |
|
226 | 218 | return |
|
227 | 219 | try: |
|
228 | 220 | text = self.shell.hooks.clipboard_get() |
@@ -243,7 +235,7 class TerminalMagics(Magics): | |||
|
243 | 235 | write('\n') |
|
244 | 236 | write("## -- End pasted text --\n") |
|
245 | 237 | |
|
246 |
store_or_execute( |
|
|
238 | self.store_or_execute(block, name) | |
|
247 | 239 | |
|
248 | 240 | # Class-level: add a '%cls' magic only on Windows |
|
249 | 241 | if sys.platform == 'win32': |
General Comments 0
You need to be logged in to leave comments.
Login now