##// END OF EJS Templates
Move terminal-only magics to the terminal class....
Fernando Perez -
Show More
@@ -425,12 +425,6 b' Currently the magic system has the following functions:\\n"""'
425 425 Magic.auto_status[self.shell.automagic] ) )
426 426 page.page(outmsg)
427 427
428 def magic_autoindent(self, parameter_s = ''):
429 """Toggle autoindent on/off (if available)."""
430
431 self.shell.set_autoindent()
432 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
433
434 428 def magic_automagic(self, parameter_s = ''):
435 429 """Make magic functions callable without having to type the initial %.
436 430
@@ -2428,22 +2422,6 b' Defaulting color scheme to \'NoColor\'"""'
2428 2422 else:
2429 2423 shell.inspector.set_active_scheme('NoColor')
2430 2424
2431 def magic_color_info(self,parameter_s = ''):
2432 """Toggle color_info.
2433
2434 The color_info configuration parameter controls whether colors are
2435 used for displaying object details (by things like %psource, %pfile or
2436 the '?' system). This function toggles this value with each call.
2437
2438 Note that unless you have a fairly recent pager (less works better
2439 than more) in your system, using colored object information displays
2440 will not work properly. Test it and see."""
2441
2442 self.shell.color_info = not self.shell.color_info
2443 self.magic_colors(self.shell.colors)
2444 print 'Object introspection functions have now coloring:',
2445 print ['OFF','ON'][int(self.shell.color_info)]
2446
2447 2425 def magic_Pprint(self, parameter_s=''):
2448 2426 """Toggle pretty printing on/off."""
2449 2427
@@ -2544,7 +2522,6 b' Defaulting color scheme to \'NoColor\'"""'
2544 2522 print "Removing %stored alias",aname
2545 2523 del stored[aname]
2546 2524 self.db['stored_aliases'] = stored
2547
2548 2525
2549 2526 def magic_rehashx(self, parameter_s = ''):
2550 2527 """Update the alias table with all executable files in $PATH.
@@ -3150,100 +3127,6 b' Defaulting color scheme to \'NoColor\'"""'
3150 3127 self.user_ns[par] = SList(block.splitlines())
3151 3128 print "Block assigned to '%s'" % par
3152 3129
3153 def magic_cpaste(self, parameter_s=''):
3154 """Allows you to paste & execute a pre-formatted code block from clipboard.
3155
3156 You must terminate the block with '--' (two minus-signs) alone on the
3157 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
3158 is the new sentinel for this operation)
3159
3160 The block is dedented prior to execution to enable execution of method
3161 definitions. '>' and '+' characters at the beginning of a line are
3162 ignored, to allow pasting directly from e-mails, diff files and
3163 doctests (the '...' continuation prompt is also stripped). The
3164 executed block is also assigned to variable named 'pasted_block' for
3165 later editing with '%edit pasted_block'.
3166
3167 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
3168 This assigns the pasted block to variable 'foo' as string, without
3169 dedenting or executing it (preceding >>> and + is still stripped)
3170
3171 '%cpaste -r' re-executes the block previously entered by cpaste.
3172
3173 Do not be alarmed by garbled output on Windows (it's a readline bug).
3174 Just press enter and type -- (and press enter again) and the block
3175 will be what was just pasted.
3176
3177 IPython statements (magics, shell escapes) are not supported (yet).
3178
3179 See also
3180 --------
3181 paste: automatically pull code from clipboard.
3182 """
3183
3184 opts,args = self.parse_options(parameter_s,'rs:',mode='string')
3185 par = args.strip()
3186 if opts.has_key('r'):
3187 self._rerun_pasted()
3188 return
3189
3190 sentinel = opts.get('s','--')
3191
3192 block = self._strip_pasted_lines_for_code(
3193 self._get_pasted_lines(sentinel))
3194
3195 self._execute_block(block, par)
3196
3197 def magic_paste(self, parameter_s=''):
3198 """Allows you to paste & execute a pre-formatted code block from clipboard.
3199
3200 The text is pulled directly from the clipboard without user
3201 intervention and printed back on the screen before execution (unless
3202 the -q flag is given to force quiet mode).
3203
3204 The block is dedented prior to execution to enable execution of method
3205 definitions. '>' and '+' characters at the beginning of a line are
3206 ignored, to allow pasting directly from e-mails, diff files and
3207 doctests (the '...' continuation prompt is also stripped). The
3208 executed block is also assigned to variable named 'pasted_block' for
3209 later editing with '%edit pasted_block'.
3210
3211 You can also pass a variable name as an argument, e.g. '%paste foo'.
3212 This assigns the pasted block to variable 'foo' as string, without
3213 dedenting or executing it (preceding >>> and + is still stripped)
3214
3215 Options
3216 -------
3217
3218 -r: re-executes the block previously entered by cpaste.
3219
3220 -q: quiet mode: do not echo the pasted text back to the terminal.
3221
3222 IPython statements (magics, shell escapes) are not supported (yet).
3223
3224 See also
3225 --------
3226 cpaste: manually paste code into terminal until you mark its end.
3227 """
3228 opts,args = self.parse_options(parameter_s,'rq',mode='string')
3229 par = args.strip()
3230 if opts.has_key('r'):
3231 self._rerun_pasted()
3232 return
3233
3234 text = self.shell.hooks.clipboard_get()
3235 block = self._strip_pasted_lines_for_code(text.splitlines())
3236
3237 # By default, echo back to terminal unless quiet mode is requested
3238 if not opts.has_key('q'):
3239 write = self.shell.write
3240 write(self.shell.pycolorize(block))
3241 if not block.endswith('\n'):
3242 write('\n')
3243 write("## -- End pasted text --\n")
3244
3245 self._execute_block(block, par)
3246
3247 3130 def magic_quickref(self,arg):
3248 3131 """ Show a quick reference sheet """
3249 3132 import IPython.core.usage
@@ -517,6 +517,114 b' class TerminalInteractiveShell(InteractiveShell):'
517 517 self.ask_exit()
518 518 else:
519 519 self.ask_exit()
520
521 #------------------------------------------------------------------------
522 # Magic overrides
523 #------------------------------------------------------------------------
524 # Once the base class stops inheriting from magic, this code needs to be
525 # moved into a separate machinery as well. For now, at least isolate here
526 # the magics which this class needs to implement differently from the base
527 # class, or that are unique to it.
528
529 def magic_autoindent(self, parameter_s = ''):
530 """Toggle autoindent on/off (if available)."""
531
532 self.shell.set_autoindent()
533 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
534
535 def magic_cpaste(self, parameter_s=''):
536 """Paste & execute a pre-formatted code block from clipboard.
537
538 You must terminate the block with '--' (two minus-signs) alone on the
539 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
540 is the new sentinel for this operation)
541
542 The block is dedented prior to execution to enable execution of method
543 definitions. '>' and '+' characters at the beginning of a line are
544 ignored, to allow pasting directly from e-mails, diff files and
545 doctests (the '...' continuation prompt is also stripped). The
546 executed block is also assigned to variable named 'pasted_block' for
547 later editing with '%edit pasted_block'.
548
549 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
550 This assigns the pasted block to variable 'foo' as string, without
551 dedenting or executing it (preceding >>> and + is still stripped)
552
553 '%cpaste -r' re-executes the block previously entered by cpaste.
554
555 Do not be alarmed by garbled output on Windows (it's a readline bug).
556 Just press enter and type -- (and press enter again) and the block
557 will be what was just pasted.
558
559 IPython statements (magics, shell escapes) are not supported (yet).
560
561 See also
562 --------
563 paste: automatically pull code from clipboard.
564 """
565
566 opts,args = self.parse_options(parameter_s,'rs:',mode='string')
567 par = args.strip()
568 if opts.has_key('r'):
569 self._rerun_pasted()
570 return
571
572 sentinel = opts.get('s','--')
573
574 block = self._strip_pasted_lines_for_code(
575 self._get_pasted_lines(sentinel))
576
577 self._execute_block(block, par)
578
579 def magic_paste(self, parameter_s=''):
580 """Paste & execute a pre-formatted code block from clipboard.
581
582 The text is pulled directly from the clipboard without user
583 intervention and printed back on the screen before execution (unless
584 the -q flag is given to force quiet mode).
585
586 The block is dedented prior to execution to enable execution of method
587 definitions. '>' and '+' characters at the beginning of a line are
588 ignored, to allow pasting directly from e-mails, diff files and
589 doctests (the '...' continuation prompt is also stripped). The
590 executed block is also assigned to variable named 'pasted_block' for
591 later editing with '%edit pasted_block'.
592
593 You can also pass a variable name as an argument, e.g. '%paste foo'.
594 This assigns the pasted block to variable 'foo' as string, without
595 dedenting or executing it (preceding >>> and + is still stripped)
596
597 Options
598 -------
599
600 -r: re-executes the block previously entered by cpaste.
601
602 -q: quiet mode: do not echo the pasted text back to the terminal.
603
604 IPython statements (magics, shell escapes) are not supported (yet).
605
606 See also
607 --------
608 cpaste: manually paste code into terminal until you mark its end.
609 """
610 opts,args = self.parse_options(parameter_s,'rq',mode='string')
611 par = args.strip()
612 if opts.has_key('r'):
613 self._rerun_pasted()
614 return
615
616 text = self.shell.hooks.clipboard_get()
617 block = self._strip_pasted_lines_for_code(text.splitlines())
618
619 # By default, echo back to terminal unless quiet mode is requested
620 if not opts.has_key('q'):
621 write = self.shell.write
622 write(self.shell.pycolorize(block))
623 if not block.endswith('\n'):
624 write('\n')
625 write("## -- End pasted text --\n")
626
627 self._execute_block(block, par)
520 628
521 629
522 630 InteractiveShellABC.register(TerminalInteractiveShell)
@@ -78,6 +78,61 b' class ZMQInteractiveShell(InteractiveShell):'
78 78
79 79 displayhook_class = Type(ZMQDisplayHook)
80 80
81
82 def auto_rewrite_input(self, cmd):
83 """Called to show the auto-rewritten input for autocall and friends.
84
85 FIXME: this payload is currently not correctly processed by the
86 frontend.
87 """
88 new = self.displayhook.prompt1.auto_rewrite() + cmd
89 payload = dict(
90 source='IPython.zmq.zmqshell.ZMQInteractiveShell.auto_rewrite_input',
91 transformed_input=new,
92 )
93 self.payload_manager.write_payload(payload)
94
95 def ask_exit(self):
96 """Engage the exit actions."""
97 payload = dict(
98 source='IPython.zmq.zmqshell.ZMQInteractiveShell.ask_exit',
99 exit=True,
100 )
101 self.payload_manager.write_payload(payload)
102
103 def _showtraceback(self, etype, evalue, stb):
104
105 exc_content = {
106 u'traceback' : stb,
107 u'ename' : unicode(etype.__name__),
108 u'evalue' : unicode(evalue)
109 }
110
111 dh = self.displayhook
112 exc_msg = dh.session.msg(u'pyerr', exc_content, dh.parent_header)
113 # Send exception info over pub socket for other clients than the caller
114 # to pick up
115 dh.pub_socket.send_json(exc_msg)
116
117 # FIXME - Hack: store exception info in shell object. Right now, the
118 # caller is reading this info after the fact, we need to fix this logic
119 # to remove this hack. Even uglier, we need to store the error status
120 # here, because in the main loop, the logic that sets it is being
121 # skipped because runlines swallows the exceptions.
122 exc_content[u'status'] = u'error'
123 self._reply_content = exc_content
124 # /FIXME
125
126 return exc_content
127
128 #------------------------------------------------------------------------
129 # Magic overrides
130 #------------------------------------------------------------------------
131 # Once the base class stops inheriting from magic, this code needs to be
132 # moved into a separate machinery as well. For now, at least isolate here
133 # the magics which this class needs to implement differently from the base
134 # class, or that are unique to it.
135
81 136 def magic_doctest_mode(self,parameter_s=''):
82 137 """Toggle doctest mode on and off.
83 138
@@ -423,55 +478,12 b' class ZMQInteractiveShell(InteractiveShell):'
423 478 self.payload_manager.write_payload(payload)
424 479
425 480 def magic_gui(self, *args, **kwargs):
426 raise NotImplementedError('GUI support must be enabled in command line options.')
481 raise NotImplementedError(
482 'GUI support must be enabled in command line options.')
427 483
428 484 def magic_pylab(self, *args, **kwargs):
429 raise NotImplementedError('pylab support must be enabled in commandl in options.')
430
431 def auto_rewrite_input(self, cmd):
432 """Called to show the auto-rewritten input for autocall and friends.
433
434 FIXME: this payload is currently not correctly processed by the
435 frontend.
436 """
437 new = self.displayhook.prompt1.auto_rewrite() + cmd
438 payload = dict(
439 source='IPython.zmq.zmqshell.ZMQInteractiveShell.auto_rewrite_input',
440 transformed_input=new,
441 )
442 self.payload_manager.write_payload(payload)
443
444 def ask_exit(self):
445 """Engage the exit actions."""
446 payload = dict(
447 source='IPython.zmq.zmqshell.ZMQInteractiveShell.ask_exit',
448 exit=True,
449 )
450 self.payload_manager.write_payload(payload)
451
452 def _showtraceback(self, etype, evalue, stb):
453
454 exc_content = {
455 u'traceback' : stb,
456 u'ename' : unicode(etype.__name__),
457 u'evalue' : unicode(evalue)
458 }
485 raise NotImplementedError(
486 'pylab support must be enabled in command line options.')
459 487
460 dh = self.displayhook
461 exc_msg = dh.session.msg(u'pyerr', exc_content, dh.parent_header)
462 # Send exception info over pub socket for other clients than the caller
463 # to pick up
464 dh.pub_socket.send_json(exc_msg)
465
466 # FIXME - Hack: store exception info in shell object. Right now, the
467 # caller is reading this info after the fact, we need to fix this logic
468 # to remove this hack. Even uglier, we need to store the error status
469 # here, because in the main loop, the logic that sets it is being
470 # skipped because runlines swallows the exceptions.
471 exc_content[u'status'] = u'error'
472 self._reply_content = exc_content
473 # /FIXME
474
475 return exc_content
476 488
477 489 InteractiveShellABC.register(ZMQInteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now