##// END OF EJS Templates
Add tests for email quote stripping....
Fernando Perez -
Show More
@@ -71,30 +71,55 def get_pasted_lines(sentinel, input=raw_input):
71 71 return
72 72
73 73
74 def strip_email_quotes(raw_lines):
75 """ Strip email quotation marks at the beginning of each line.
76
77 We don't do any more input transofrmations here because the main shell's
78 prefiltering handles other cases.
79 """
80 lines = [re.sub(r'^\s*(\s?>)+', '', l) for l in raw_lines]
81 return '\n'.join(lines) + '\n'
82
83
84 # These two functions are needed by the %paste/%cpaste magics. In practice
85 # they are basically methods (they take the shell as their first argument), but
86 # we leave them as standalone functions because eventually the magics
87 # themselves will become separate objects altogether. At that point, the
88 # magics will have access to the shell object, and these functions can be made
89 # methods of the magic object, but not of the shell.
90
74 91 def store_or_execute(shell, block, name):
75 92 """ Execute a block, or store it in a variable, per the user's request.
76 93 """
94 # Dedent and prefilter so what we store matches what is executed by
95 # run_cell.
96 b = shell.prefilter(textwrap.dedent(block))
97
77 98 if name:
78 99 # If storing it for further editing, run the prefilter on it
79 shell.user_ns[name] = SList(shell.prefilter(block).splitlines())
100 shell.user_ns[name] = SList(b.splitlines())
80 101 print "Block assigned to '%s'" % name
81 102 else:
82 # For execution we just dedent it, as all other filtering is
83 # automatically applied by run_cell
84 b = textwrap.dedent(block)
85 103 shell.user_ns['pasted_block'] = b
86 104 shell.run_cell(b)
87 105
88 106
89 def rerun_pasted(shell):
107 def rerun_pasted(shell, name='pasted_block'):
90 108 """ Rerun a previously pasted command.
91 109 """
92 b = shell.user_ns.get('pasted_block')
110 b = shell.user_ns.get(name)
111
112 # Sanity checks
93 113 if b is None:
94 114 raise UsageError('No previous pasted block available')
115 if not isinstance(b, basestring):
116 raise UsageError(
117 "Variable 'pasted_block' is not a string, can't execute")
118
95 119 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
96 120 shell.run_cell(b)
97 121
122
98 123 #-----------------------------------------------------------------------------
99 124 # Main class
100 125 #-----------------------------------------------------------------------------
@@ -605,14 +630,13 class TerminalInteractiveShell(InteractiveShell):
605 630 Hello world!
606 631 """
607 632
608 opts, args = self.parse_options(parameter_s, 'rs:', mode='string')
609 name = args.strip()
633 opts, name = self.parse_options(parameter_s, 'rs:', mode='string')
610 634 if 'r' in opts:
611 635 rerun_pasted(self.shell)
612 636 return
613 637
614 638 sentinel = opts.get('s', '--')
615 block = '\n'.join(get_pasted_lines(sentinel))
639 block = strip_email_quotes(get_pasted_lines(sentinel))
616 640 store_or_execute(self.shell, block, name)
617 641
618 642 def magic_paste(self, parameter_s=''):
@@ -646,14 +670,13 class TerminalInteractiveShell(InteractiveShell):
646 670 --------
647 671 cpaste: manually paste code into terminal until you mark its end.
648 672 """
649 opts, args = self.parse_options(parameter_s, 'rq', mode='string')
650 name = args.strip()
673 opts, name = self.parse_options(parameter_s, 'rq', mode='string')
651 674 if 'r' in opts:
652 675 rerun_pasted(self.shell)
653 676 return
654 677 try:
655 678 text = self.shell.hooks.clipboard_get()
656 block = self._strip_pasted_lines_for_code(text.splitlines())
679 block = strip_email_quotes(text.splitlines())
657 680 except TryNext as clipboard_exc:
658 681 message = getattr(clipboard_exc, 'args')
659 682 if message:
@@ -672,6 +695,7 class TerminalInteractiveShell(InteractiveShell):
672 695
673 696 store_or_execute(self.shell, block, name)
674 697
698 # Class-level: add a '%cls' magic only on Windows
675 699 if sys.platform == 'win32':
676 700 def magic_cls(self, s):
677 701 """Clear screen.
@@ -680,7 +704,8 class TerminalInteractiveShell(InteractiveShell):
680 704
681 705 def showindentationerror(self):
682 706 super(TerminalInteractiveShell, self).showindentationerror()
683 print("If you want to paste code into IPython, try the %paste magic function.")
707 print("If you want to paste code into IPython, try the "
708 "%paste and %cpaste magic functions.")
684 709
685 710
686 711 InteractiveShellABC.register(TerminalInteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now