Changeset 2992

Show
Ignore:
Timestamp:
01/29/08 01:42:03 (10 months ago)
Author:
fperez
Message:

Add -f flag to %history to direct output to file

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ipython/trunk/IPython/history.py

    r2740 r2992  
    33""" History related magics and functionality """ 
    44 
     5# Stdlib imports 
    56import fnmatch 
     7import os 
     8 
     9# IPython imports 
     10from IPython.genutils import Term, ask_yes_no 
    611 
    712def magic_history(self, parameter_s = ''): 
     
    4752        print 'This feature is only available if numbered prompts are in use.' 
    4853        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                 
    5073 
    5174    if opts.has_key('t'): 
     
    93116    if found: 
    94117        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)" 
    96119        print "=== start of normal history ===" 
    97120         
     
    103126        multiline = int(inline.count('\n') > 1) 
    104127        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() 
    107134 
    108135 
  • ipython/trunk/doc/ChangeLog

    r2965 r2992  
     12008-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 
    162008-01-21  Walter Doerwald  <walter@livinglogic.de> 
    27