Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

add %save -f #1937

Merged
merged 1 commit into from

2 participants

@minrk
Owner

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

closes #1542

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

closes #1542
8d53f9a
@minrk
Owner

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

@fperez
Owner

Looks great, thanks! Merging now.

@fperez fperez merged commit 468a8fd into ipython:master
@minrk minrk deleted the minrk:save branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 13, 2012
  1. @minrk

    add %save -f

    minrk authored
    adds force flag, and displays message about -f when ask_yes_no fails
    due to no stdin (Notebook).
    
    closes #1542
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 4 deletions.
  1. +12 −4 IPython/core/magics/code.py
View
16 IPython/core/magics/code.py
@@ -21,7 +21,7 @@
from urllib2 import urlopen
# Our own packages
-from IPython.core.error import TryNext
+from IPython.core.error import TryNext, StdinNotImplementedError
from IPython.core.macro import Macro
from IPython.core.magic import Magics, magics_class, line_magic
from IPython.core.oinspect import find_file, find_source_lines
@@ -57,6 +57,9 @@ def save(self, parameter_s=''):
so that magics are loaded in their transformed version to valid
Python. If this option is given, the raw input as typed as the
command line is used instead.
+
+ -f: force overwrite. If file exists, %save will prompt for overwrite
+ unless -f is given.
This function uses the same syntax as %history for input ranges,
then saves the lines to the filename you specify.
@@ -67,14 +70,19 @@ def save(self, parameter_s=''):
If `-r` option is used, the default extension is `.ipy`.
"""
- opts,args = self.parse_options(parameter_s,'r',mode='list')
+ opts,args = self.parse_options(parameter_s,'fr',mode='list')
raw = 'r' in opts
+ force = 'f' in opts
ext = u'.ipy' if raw else u'.py'
fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
if not fname.endswith((u'.py',u'.ipy')):
fname += ext
- if os.path.isfile(fname):
- overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
+ if os.path.isfile(fname) and not force:
+ try:
+ overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
+ except StdinNotImplementedError:
+ print "File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s)
+ return
if not overwrite :
print 'Operation cancelled.'
return
Something went wrong with that request. Please try again.