Skip to content

Commit

Permalink
Merge pull request #2131 from doda/master
Browse files Browse the repository at this point in the history
Add option append (-a) to %save
  • Loading branch information
takluyver committed Jul 16, 2012
2 parents 7d40222 + d5e7d79 commit 7efca4e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
20 changes: 15 additions & 5 deletions IPython/core/magics/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def save(self, parameter_s=''):
-f: force overwrite. If file exists, %save will prompt for overwrite
unless -f is given.
-a: append to the file instead of overwriting it.
This function uses the same syntax as %history for input ranges,
then saves the lines to the filename you specify.
Expand All @@ -70,14 +72,17 @@ def save(self, parameter_s=''):
If `-r` option is used, the default extension is `.ipy`.
"""

opts,args = self.parse_options(parameter_s,'fr',mode='list')
opts,args = self.parse_options(parameter_s,'fra',mode='list')
raw = 'r' in opts
force = 'f' in opts
append = 'a' in opts
mode = 'a' if append else 'w'
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) and not force:
file_exists = os.path.isfile(fname)
if file_exists and not force and not append:
try:
overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
except StdinNotImplementedError:
Expand All @@ -91,9 +96,14 @@ def save(self, parameter_s=''):
except (TypeError, ValueError) as e:
print e.args[0]
return
with io.open(fname,'w', encoding="utf-8") as f:
f.write(u"# coding: utf-8\n")
f.write(py3compat.cast_unicode(cmds))
out = py3compat.cast_unicode(cmds)
with io.open(fname, mode, encoding="utf-8") as f:
if not file_exists or not append:
f.write(u"# coding: utf-8\n")
f.write(out)
# make sure we end on a newline
if not out.endswith(u'\n'):
f.write(u'\n')
print 'The following commands were written to file `%s`:' % fname
print cmds

Expand Down
2 changes: 1 addition & 1 deletion IPython/core/tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_history():
ip.magic("save " + testfilename + " ~1/1-3")
with py3compat.open(testfilename, encoding='utf-8') as testfile:
nt.assert_equal(testfile.read(),
u"# coding: utf-8\n" + u"\n".join(hist))
u"# coding: utf-8\n" + u"\n".join(hist)+u"\n")

# Duplicate line numbers - check that it doesn't crash, and
# gets a new session
Expand Down
20 changes: 20 additions & 0 deletions IPython/core/tests/test_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,3 +742,23 @@ def test_alias_magic():
ip.run_line_magic('alias_magic', '--line env_alias env')
nt.assert_equal(ip.run_line_magic('env', ''),
ip.run_line_magic('env_alias', ''))

def test_save():
"""Test %save."""
ip = get_ipython()
ip.history_manager.reset() # Clear any existing history.
cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"]
for i, cmd in enumerate(cmds, start=1):
ip.history_manager.store_inputs(i, cmd)
with TemporaryDirectory() as tmpdir:
file = os.path.join(tmpdir, "testsave.py")
ip.run_line_magic("save", "%s 1-10" % file)
with open(file) as f:
content = f.read()
nt.assert_equal(content.count(cmds[0]), 1)
nt.assert_true('coding: utf-8' in content)
ip.run_line_magic("save", "-a %s 1-10" % file)
with open(file) as f:
content = f.read()
nt.assert_equal(content.count(cmds[0]), 2)
nt.assert_true('coding: utf-8' in content)

0 comments on commit 7efca4e

Please sign in to comment.