##// END OF EJS Templates
Add -f flag to %history to direct output to file
fperez -
Show More
@@ -2,7 +2,12 b''
2 2
3 3 """ History related magics and functionality """
4 4
5 # Stdlib imports
5 6 import fnmatch
7 import os
8
9 # IPython imports
10 from IPython.genutils import Term, ask_yes_no
6 11
7 12 def magic_history(self, parameter_s = ''):
8 13 """Print input history (_i<n> variables), with most recent last.
@@ -46,7 +51,25 b" def magic_history(self, parameter_s = ''):"
46 51 if not shell.outputcache.do_full_cache:
47 52 print 'This feature is only available if numbered prompts are in use.'
48 53 return
49 opts,args = self.parse_options(parameter_s,'gntsr',mode='list')
54 opts,args = self.parse_options(parameter_s,'gntsrf:',mode='list')
55
56 # Check if output to specific file was requested.
57 try:
58 outfname = opts['f']
59 except KeyError:
60 outfile = Term.cout
61 # We don't want to close stdout at the end!
62 close_at_end = False
63 else:
64 if os.path.exists(outfname):
65 ans = ask_yes_no("File %r exists. Overwrite?" % outfname)
66 if not ans:
67 print 'Aborting.'
68 return
69 else:
70 outfile = open(outfname,'w')
71 close_at_end = True
72
50 73
51 74 if opts.has_key('t'):
52 75 input_hist = shell.input_hist
@@ -92,7 +115,7 b" def magic_history(self, parameter_s = ''):"
92 115
93 116 if found:
94 117 print "==="
95 print "^shadow history ends, fetch by %rep <number> (must start with 0)"
118 print "shadow history ends, fetch by %rep <number> (must start with 0)"
96 119 print "=== start of normal history ==="
97 120
98 121 for in_num in range(init,final):
@@ -102,8 +125,12 b" def magic_history(self, parameter_s = ''):"
102 125
103 126 multiline = int(inline.count('\n') > 1)
104 127 if print_nums:
105 print '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),
106 print inline,
128 print >> outfile, \
129 '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),
130 print >> outfile, inline,
131
132 if close_at_end:
133 outfile.close()
107 134
108 135
109 136
@@ -1,3 +1,8 b''
1 2008-01-29 Fernando Perez <Fernando.Perez@colorado.edu>
2
3 * IPython/history.py (magic_history): Add support for declaring an
4 output file directly from the history command.
5
1 6 2008-01-21 Walter Doerwald <walter@livinglogic.de>
2 7
3 8 * ipipe.py: Register ipipe's displayhooks via the generic function
General Comments 0
You need to be logged in to leave comments. Login now