Show More
@@ -5,6 +5,8 b' Use this module to read or write notebook files as particular nbformat versions.' | |||
|
5 | 5 | |
|
6 | 6 | # Copyright (c) IPython Development Team. |
|
7 | 7 | # Distributed under the terms of the Modified BSD License. |
|
8 | import io | |
|
9 | from IPython.utils import py3compat | |
|
8 | 10 | |
|
9 | 11 | from IPython.utils.log import get_logger |
|
10 | 12 | |
@@ -112,8 +114,8 b' def read(fp, as_version, **kwargs):' | |||
|
112 | 114 | |
|
113 | 115 | Parameters |
|
114 | 116 | ---------- |
|
115 | fp : file | |
|
116 | Any file-like object with a read method. | |
|
117 | fp : file or str | |
|
118 | Any file-like object with a read method, or a path to a file. | |
|
117 | 119 | as_version: int |
|
118 | 120 | The version of the notebook format to return. |
|
119 | 121 | The notebook will be converted, if necessary. |
@@ -124,6 +126,10 b' def read(fp, as_version, **kwargs):' | |||
|
124 | 126 | nb : NotebookNode |
|
125 | 127 | The notebook that was read. |
|
126 | 128 | """ |
|
129 | if isinstance(fp, py3compat.string_types): | |
|
130 | with io.open(fp, encoding='utf-8') as f: | |
|
131 | return read(f, as_version, **kwargs) | |
|
132 | ||
|
127 | 133 | return reads(fp.read(), as_version, **kwargs) |
|
128 | 134 | |
|
129 | 135 | |
@@ -136,14 +142,19 b' def write(nb, fp, version=NO_CONVERT, **kwargs):' | |||
|
136 | 142 | ---------- |
|
137 | 143 | nb : NotebookNode |
|
138 | 144 | The notebook to write. |
|
139 | fp : file | |
|
140 |
Any file-like object with a write method that accepts unicode |
|
|
145 | fp : file or str | |
|
146 | Any file-like object with a write method that accepts unicode, or | |
|
147 | a path to write a file. | |
|
141 | 148 | version : int, optional |
|
142 | 149 | The nbformat version to write. |
|
143 | 150 | If nb is not this version, it will be converted. |
|
144 | 151 | If unspecified, or specified as nbformat.NO_CONVERT, |
|
145 | 152 | the notebook's own version will be used and no conversion performed. |
|
146 | 153 | """ |
|
154 | if isinstance(fp, py3compat.string_types): | |
|
155 | with open(fp, 'w', encoding='utf-8') as f: | |
|
156 | return write(nb, f, version=version, **kwargs) | |
|
157 | ||
|
147 | 158 | s = writes(nb, version, **kwargs) |
|
148 | 159 | if isinstance(s, bytes): |
|
149 | 160 | s = s.decode('utf8') |
@@ -4,11 +4,13 b'' | |||
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 | 6 | import json |
|
7 | import os | |
|
7 | 8 | |
|
8 | 9 | from .base import TestsBase |
|
9 | 10 | |
|
11 | from IPython.utils.tempdir import TemporaryDirectory | |
|
10 | 12 | from ..reader import get_version |
|
11 | from IPython.nbformat import read, current_nbformat, writes | |
|
13 | from IPython.nbformat import read, current_nbformat, writes, write | |
|
12 | 14 | |
|
13 | 15 | |
|
14 | 16 | class TestAPI(TestsBase): |
@@ -35,3 +37,13 b' class TestAPI(TestsBase):' | |||
|
35 | 37 | nb2 = json.loads(jsons) |
|
36 | 38 | (major, minor) = get_version(nb2) |
|
37 | 39 | self.assertEqual(major, 2) |
|
40 | ||
|
41 | def test_read_write_path(self): | |
|
42 | """read() and write() take filesystem paths""" | |
|
43 | path = os.path.join(self._get_files_path(), u'test4.ipynb') | |
|
44 | nb = read(path, as_version=4) | |
|
45 | ||
|
46 | with TemporaryDirectory() as td: | |
|
47 | dest = os.path.join(td, 'echidna.ipynb') | |
|
48 | write(nb, dest) | |
|
49 | assert os.path.isfile(dest) |
General Comments 0
You need to be logged in to leave comments.
Login now