Show More
@@ -61,6 +61,8 b' class CodeMagics(Magics):' | |||||
61 | -f: force overwrite. If file exists, %save will prompt for overwrite |
|
61 | -f: force overwrite. If file exists, %save will prompt for overwrite | |
62 | unless -f is given. |
|
62 | unless -f is given. | |
63 |
|
63 | |||
|
64 | -a: append to the file instead of overwriting it. | |||
|
65 | ||||
64 | This function uses the same syntax as %history for input ranges, |
|
66 | This function uses the same syntax as %history for input ranges, | |
65 | then saves the lines to the filename you specify. |
|
67 | then saves the lines to the filename you specify. | |
66 |
|
68 | |||
@@ -70,14 +72,17 b' class CodeMagics(Magics):' | |||||
70 | If `-r` option is used, the default extension is `.ipy`. |
|
72 | If `-r` option is used, the default extension is `.ipy`. | |
71 | """ |
|
73 | """ | |
72 |
|
74 | |||
73 | opts,args = self.parse_options(parameter_s,'fr',mode='list') |
|
75 | opts,args = self.parse_options(parameter_s,'fra',mode='list') | |
74 | raw = 'r' in opts |
|
76 | raw = 'r' in opts | |
75 | force = 'f' in opts |
|
77 | force = 'f' in opts | |
|
78 | append = 'a' in opts | |||
|
79 | mode = 'a' if append else 'w' | |||
76 | ext = u'.ipy' if raw else u'.py' |
|
80 | ext = u'.ipy' if raw else u'.py' | |
77 | fname, codefrom = unquote_filename(args[0]), " ".join(args[1:]) |
|
81 | fname, codefrom = unquote_filename(args[0]), " ".join(args[1:]) | |
78 | if not fname.endswith((u'.py',u'.ipy')): |
|
82 | if not fname.endswith((u'.py',u'.ipy')): | |
79 | fname += ext |
|
83 | fname += ext | |
80 |
|
|
84 | file_exists = os.path.isfile(fname) | |
|
85 | if file_exists and not force and not append: | |||
81 | try: |
|
86 | try: | |
82 | overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n') |
|
87 | overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n') | |
83 | except StdinNotImplementedError: |
|
88 | except StdinNotImplementedError: | |
@@ -91,9 +96,14 b' class CodeMagics(Magics):' | |||||
91 | except (TypeError, ValueError) as e: |
|
96 | except (TypeError, ValueError) as e: | |
92 | print e.args[0] |
|
97 | print e.args[0] | |
93 | return |
|
98 | return | |
94 | with io.open(fname,'w', encoding="utf-8") as f: |
|
99 | out = py3compat.cast_unicode(cmds) | |
|
100 | with io.open(fname, mode, encoding="utf-8") as f: | |||
|
101 | if not file_exists or not append: | |||
95 | f.write(u"# coding: utf-8\n") |
|
102 | f.write(u"# coding: utf-8\n") | |
96 |
f.write( |
|
103 | f.write(out) | |
|
104 | # make sure we end on a newline | |||
|
105 | if not out.endswith(u'\n'): | |||
|
106 | f.write(u'\n') | |||
97 | print 'The following commands were written to file `%s`:' % fname |
|
107 | print 'The following commands were written to file `%s`:' % fname | |
98 | print cmds |
|
108 | print cmds | |
99 |
|
109 |
@@ -95,7 +95,7 b' def test_history():' | |||||
95 | ip.magic("save " + testfilename + " ~1/1-3") |
|
95 | ip.magic("save " + testfilename + " ~1/1-3") | |
96 | with py3compat.open(testfilename, encoding='utf-8') as testfile: |
|
96 | with py3compat.open(testfilename, encoding='utf-8') as testfile: | |
97 | nt.assert_equal(testfile.read(), |
|
97 | nt.assert_equal(testfile.read(), | |
98 | u"# coding: utf-8\n" + u"\n".join(hist)) |
|
98 | u"# coding: utf-8\n" + u"\n".join(hist)+u"\n") | |
99 |
|
99 | |||
100 | # Duplicate line numbers - check that it doesn't crash, and |
|
100 | # Duplicate line numbers - check that it doesn't crash, and | |
101 | # gets a new session |
|
101 | # gets a new session |
@@ -742,3 +742,23 b' def test_alias_magic():' | |||||
742 | ip.run_line_magic('alias_magic', '--line env_alias env') |
|
742 | ip.run_line_magic('alias_magic', '--line env_alias env') | |
743 | nt.assert_equal(ip.run_line_magic('env', ''), |
|
743 | nt.assert_equal(ip.run_line_magic('env', ''), | |
744 | ip.run_line_magic('env_alias', '')) |
|
744 | ip.run_line_magic('env_alias', '')) | |
|
745 | ||||
|
746 | def test_save(): | |||
|
747 | """Test %save.""" | |||
|
748 | ip = get_ipython() | |||
|
749 | ip.history_manager.reset() # Clear any existing history. | |||
|
750 | cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"] | |||
|
751 | for i, cmd in enumerate(cmds, start=1): | |||
|
752 | ip.history_manager.store_inputs(i, cmd) | |||
|
753 | with TemporaryDirectory() as tmpdir: | |||
|
754 | file = os.path.join(tmpdir, "testsave.py") | |||
|
755 | ip.run_line_magic("save", "%s 1-10" % file) | |||
|
756 | with open(file) as f: | |||
|
757 | content = f.read() | |||
|
758 | nt.assert_equal(content.count(cmds[0]), 1) | |||
|
759 | nt.assert_true('coding: utf-8' in content) | |||
|
760 | ip.run_line_magic("save", "-a %s 1-10" % file) | |||
|
761 | with open(file) as f: | |||
|
762 | content = f.read() | |||
|
763 | nt.assert_equal(content.count(cmds[0]), 2) | |||
|
764 | nt.assert_true('coding: utf-8' in content) |
General Comments 0
You need to be logged in to leave comments.
Login now