##// END OF EJS Templates
Move functions into magic methods....
Fernando Perez -
Show More
@@ -82,33 +82,33 b' 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):
85 #------------------------------------------------------------------------
86 # Terminal-specific magics
87 #------------------------------------------------------------------------
88
89 @magics_class
90 class TerminalMagics(Magics):
91
92 def store_or_execute(self, block, name):
93 93 """ Execute a block, or store it in a variable, per the user's request.
94 94 """
95 95 # Dedent and prefilter so what we store matches what is executed by
96 96 # run_cell.
97 b = shell.prefilter(textwrap.dedent(block))
97 b = self.shell.prefilter(textwrap.dedent(block))
98 98
99 99 if name:
100 100 # If storing it for further editing, run the prefilter on it
101 shell.user_ns[name] = SList(b.splitlines())
101 self.shell.user_ns[name] = SList(b.splitlines())
102 102 print "Block assigned to '%s'" % name
103 103 else:
104 shell.user_ns['pasted_block'] = b
105 shell.run_cell(b)
104 self.shell.user_ns['pasted_block'] = b
105 self.shell.run_cell(b)
106 106
107 107
108 def rerun_pasted(shell, name='pasted_block'):
108 def rerun_pasted(self, name='pasted_block'):
109 109 """ Rerun a previously pasted command.
110 110 """
111 b = shell.user_ns.get(name)
111 b = self.shell.user_ns.get(name)
112 112
113 113 # Sanity checks
114 114 if b is None:
@@ -118,15 +118,7 b" def rerun_pasted(shell, name='pasted_block'):"
118 118 "Variable 'pasted_block' is not a string, can't execute")
119 119
120 120 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
121 shell.run_cell(b)
122
123
124 #------------------------------------------------------------------------
125 # Terminal-specific magics
126 #------------------------------------------------------------------------
127
128 @magics_class
129 class TerminalMagics(Magics):
121 self.shell.run_cell(b)
130 122
131 123 @line_magic
132 124 def autoindent(self, parameter_s = ''):
@@ -181,12 +173,12 b' 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(self.shell)
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(self.shell, block, name)
181 self.store_or_execute(block, name)
190 182
191 183 @line_magic
192 184 def paste(self, parameter_s=''):
@@ -222,7 +214,7 b' 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(self.shell)
217 self.rerun_pasted()
226 218 return
227 219 try:
228 220 text = self.shell.hooks.clipboard_get()
@@ -243,7 +235,7 b' class TerminalMagics(Magics):'
243 235 write('\n')
244 236 write("## -- End pasted text --\n")
245 237
246 store_or_execute(self.shell, block, name)
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