Skip to content
This repository

add %save -f #1937

Merged
merged 1 commit into from almost 2 years ago

2 participants

Min RK Fernando Perez
Min RK
Owner
minrk commented June 12, 2012

adds force flag, and displays message about -f when ask_yes_no fails due to no stdin (Notebook).

closes #1542

Min RK add %save -f
adds force flag, and displays message about -f when ask_yes_no fails
due to no stdin (Notebook).

closes #1542
8d53f9a
Min RK
Owner
minrk commented June 12, 2012

Test results for commit 8d53f9a merged into master
Platform: darwin

  • python2.6: OK (libraries not available: cython matplotlib pygments pymongo qt rpy2 tornado wx wx.aui)
  • python2.7: OK (libraries not available: wx wx.aui)
  • python3.2: OK (libraries not available: cython matplotlib pymongo qt rpy2 wx wx.aui)

Not available for testing: python3.1

Fernando Perez
Owner
fperez commented June 13, 2012

Looks great, thanks! Merging now.

Fernando Perez fperez merged commit 468a8fd into from June 13, 2012
Fernando Perez fperez closed this June 13, 2012
Min RK minrk deleted the branch March 31, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jun 12, 2012
Min RK add %save -f
adds force flag, and displays message about -f when ask_yes_no fails
due to no stdin (Notebook).

closes #1542
8d53f9a
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 12 additions and 4 deletions. Show diff stats Hide diff stats

  1. 16  IPython/core/magics/code.py
16  IPython/core/magics/code.py
@@ -21,7 +21,7 @@
21 21
 from urllib2 import urlopen
22 22
 
23 23
 # Our own packages
24  
-from IPython.core.error import TryNext
  24
+from IPython.core.error import TryNext, StdinNotImplementedError
25 25
 from IPython.core.macro import Macro
26 26
 from IPython.core.magic import Magics, magics_class, line_magic
27 27
 from IPython.core.oinspect import find_file, find_source_lines
@@ -57,6 +57,9 @@ def save(self, parameter_s=''):
57 57
           so that magics are loaded in their transformed version to valid
58 58
           Python.  If this option is given, the raw input as typed as the
59 59
           command line is used instead.
  60
+          
  61
+          -f: force overwrite.  If file exists, %save will prompt for overwrite
  62
+          unless -f is given.
60 63
 
61 64
         This function uses the same syntax as %history for input ranges,
62 65
         then saves the lines to the filename you specify.
@@ -67,14 +70,19 @@ def save(self, parameter_s=''):
67 70
         If `-r` option is used, the default extension is `.ipy`.
68 71
         """
69 72
 
70  
-        opts,args = self.parse_options(parameter_s,'r',mode='list')
  73
+        opts,args = self.parse_options(parameter_s,'fr',mode='list')
71 74
         raw = 'r' in opts
  75
+        force = 'f' in opts
72 76
         ext = u'.ipy' if raw else u'.py'
73 77
         fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
74 78
         if not fname.endswith((u'.py',u'.ipy')):
75 79
             fname += ext
76  
-        if os.path.isfile(fname):
77  
-            overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
  80
+        if os.path.isfile(fname) and not force:
  81
+            try:
  82
+                overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
  83
+            except StdinNotImplementedError:
  84
+                print "File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s)
  85
+                return
78 86
             if not overwrite :
79 87
                 print 'Operation cancelled.'
80 88
                 return
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.