##// END OF EJS Templates
add %save -f...
MinRK -
Show More
@@ -21,7 +21,7 b' import sys'
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
@@ -58,6 +58,9 b' class CodeMagics(Magics):'
58 58 Python. If this option is given, the raw input as typed as the
59 59 command line is used instead.
60 60
61 -f: force overwrite. If file exists, %save will prompt for overwrite
62 unless -f is given.
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.
63 66
@@ -67,14 +70,19 b' class CodeMagics(Magics):'
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):
80 if os.path.isfile(fname) and not force:
81 try:
77 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
General Comments 0
You need to be logged in to leave comments. Login now