Show More
@@ -1,182 +1,192 b'' | |||
|
1 | """The official API for working with notebooks in the current format version.""" | |
|
1 | """Deprecated API for working with notebooks | |
|
2 | ||
|
3 | - use IPython.nbformat for read/write/validate public API | |
|
4 | - use IPython.nbformat.vX directly for Python API for composing notebooks | |
|
5 | """ | |
|
2 | 6 | |
|
3 | 7 | # Copyright (c) IPython Development Team. |
|
4 | 8 | # Distributed under the terms of the Modified BSD License. |
|
5 | 9 | |
|
6 | 10 | from __future__ import print_function |
|
7 | 11 | |
|
8 | 12 | import re |
|
9 | 13 | import warnings |
|
10 | 14 | |
|
15 | warnings.warn("""IPython.nbformat.current is deprecated. | |
|
16 | ||
|
17 | - use IPython.nbformat for read/write/validate public API | |
|
18 | - use IPython.nbformat.vX directly to composing notebooks of a particular version | |
|
19 | """) | |
|
20 | ||
|
11 | 21 | from IPython.nbformat.v3 import ( |
|
12 | 22 | NotebookNode, |
|
13 | 23 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, |
|
14 | 24 | parse_filename, new_metadata, new_author, new_heading_cell, nbformat, |
|
15 | 25 | nbformat_minor, nbformat_schema, to_notebook_json, |
|
16 | 26 | ) |
|
17 | 27 | from IPython.nbformat import v3 as _v_latest |
|
18 | 28 | |
|
19 | 29 | from .reader import reads as reader_reads |
|
20 | 30 | from . import versions |
|
21 | 31 | from .converter import convert |
|
22 | 32 | from .validator import validate, ValidationError |
|
23 | 33 | |
|
24 | 34 | from IPython.utils.log import get_logger |
|
25 | 35 | |
|
26 | 36 | __all__ = ['NotebookNode', 'new_code_cell', 'new_text_cell', 'new_notebook', |
|
27 | 37 | 'new_output', 'new_worksheet', 'parse_filename', 'new_metadata', 'new_author', |
|
28 | 38 | 'new_heading_cell', 'nbformat', 'nbformat_minor', 'nbformat_schema', |
|
29 | 39 | 'to_notebook_json', 'convert', 'validate', 'NBFormatError', 'parse_py', |
|
30 | 40 | 'reads_json', 'writes_json', 'reads_py', 'writes_py', 'reads', 'writes', 'read', |
|
31 | 41 | 'write'] |
|
32 | 42 | |
|
33 | 43 | current_nbformat = nbformat |
|
34 | 44 | current_nbformat_minor = nbformat_minor |
|
35 | 45 | current_nbformat_module = _v_latest.__name__ |
|
36 | 46 | |
|
37 | 47 | |
|
38 | 48 | class NBFormatError(ValueError): |
|
39 | 49 | pass |
|
40 | 50 | |
|
41 | 51 | |
|
42 | 52 | def _warn_format(): |
|
43 | 53 | warnings.warn("""Non-JSON file support in nbformat is deprecated. |
|
44 | 54 | Use nbconvert to create files of other formats.""") |
|
45 | 55 | |
|
46 | 56 | |
|
47 | 57 | def parse_py(s, **kwargs): |
|
48 | 58 | """Parse a string into a (nbformat, string) tuple.""" |
|
49 | 59 | nbf = current_nbformat |
|
50 | 60 | nbm = current_nbformat_minor |
|
51 | 61 | |
|
52 | 62 | pattern = r'# <nbformat>(?P<nbformat>\d+[\.\d+]*)</nbformat>' |
|
53 | 63 | m = re.search(pattern,s) |
|
54 | 64 | if m is not None: |
|
55 | 65 | digits = m.group('nbformat').split('.') |
|
56 | 66 | nbf = int(digits[0]) |
|
57 | 67 | if len(digits) > 1: |
|
58 | 68 | nbm = int(digits[1]) |
|
59 | 69 | |
|
60 | 70 | return nbf, nbm, s |
|
61 | 71 | |
|
62 | 72 | |
|
63 | 73 | def reads_json(nbjson, **kwargs): |
|
64 | 74 | """DEPRECATED, use reads""" |
|
65 | 75 | warnings.warn("reads_json is deprecated, use reads") |
|
66 | 76 | return reads(nbjson) |
|
67 | 77 | |
|
68 | 78 | def writes_json(nb, **kwargs): |
|
69 | 79 | """DEPRECATED, use writes""" |
|
70 | 80 | warnings.warn("writes_json is deprecated, use writes") |
|
71 | 81 | return writes(nb, **kwargs) |
|
72 | 82 | |
|
73 | 83 | def reads_py(s, **kwargs): |
|
74 | 84 | """DEPRECATED: use nbconvert""" |
|
75 | 85 | _warn_format() |
|
76 | 86 | nbf, nbm, s = parse_py(s, **kwargs) |
|
77 | 87 | if nbf in (2, 3): |
|
78 | 88 | nb = versions[nbf].to_notebook_py(s, **kwargs) |
|
79 | 89 | else: |
|
80 | 90 | raise NBFormatError('Unsupported PY nbformat version: %i' % nbf) |
|
81 | 91 | return nb |
|
82 | 92 | |
|
83 | 93 | def writes_py(nb, **kwargs): |
|
84 | 94 | """DEPRECATED: use nbconvert""" |
|
85 | 95 | _warn_format() |
|
86 | 96 | return versions[3].writes_py(nb, **kwargs) |
|
87 | 97 | |
|
88 | 98 | |
|
89 | 99 | # High level API |
|
90 | 100 | |
|
91 | 101 | |
|
92 | 102 | def reads(s, format='DEPRECATED', version=current_nbformat, **kwargs): |
|
93 | 103 | """Read a notebook from a string and return the NotebookNode object. |
|
94 | 104 | |
|
95 | 105 | This function properly handles notebooks of any version. The notebook |
|
96 | 106 | returned will always be in the current version's format. |
|
97 | 107 | |
|
98 | 108 | Parameters |
|
99 | 109 | ---------- |
|
100 | 110 | s : unicode |
|
101 | 111 | The raw unicode string to read the notebook from. |
|
102 | 112 | |
|
103 | 113 | Returns |
|
104 | 114 | ------- |
|
105 | 115 | nb : NotebookNode |
|
106 | 116 | The notebook that was read. |
|
107 | 117 | """ |
|
108 | 118 | if format not in {'DEPRECATED', 'json'}: |
|
109 | 119 | _warn_format() |
|
110 | 120 | nb = reader_reads(s, **kwargs) |
|
111 | 121 | nb = convert(nb, version) |
|
112 | 122 | try: |
|
113 | 123 | validate(nb) |
|
114 | 124 | except ValidationError as e: |
|
115 | 125 | get_logger().error("Notebook JSON is invalid: %s", e) |
|
116 | 126 | return nb |
|
117 | 127 | |
|
118 | 128 | |
|
119 | 129 | def writes(nb, format='DEPRECATED', version=current_nbformat, **kwargs): |
|
120 | 130 | """Write a notebook to a string in a given format in the current nbformat version. |
|
121 | 131 | |
|
122 | 132 | This function always writes the notebook in the current nbformat version. |
|
123 | 133 | |
|
124 | 134 | Parameters |
|
125 | 135 | ---------- |
|
126 | 136 | nb : NotebookNode |
|
127 | 137 | The notebook to write. |
|
128 | 138 | version : int |
|
129 | 139 | The nbformat version to write. |
|
130 | 140 | Used for downgrading notebooks. |
|
131 | 141 | |
|
132 | 142 | Returns |
|
133 | 143 | ------- |
|
134 | 144 | s : unicode |
|
135 | 145 | The notebook string. |
|
136 | 146 | """ |
|
137 | 147 | if format not in {'DEPRECATED', 'json'}: |
|
138 | 148 | _warn_format() |
|
139 | 149 | nb = convert(nb, version) |
|
140 | 150 | try: |
|
141 | 151 | validate(nb) |
|
142 | 152 | except ValidationError as e: |
|
143 | 153 | get_logger().error("Notebook JSON is invalid: %s", e) |
|
144 | 154 | return versions[version].writes_json(nb, **kwargs) |
|
145 | 155 | |
|
146 | 156 | |
|
147 | 157 | def read(fp, format='DEPRECATED', **kwargs): |
|
148 | 158 | """Read a notebook from a file and return the NotebookNode object. |
|
149 | 159 | |
|
150 | 160 | This function properly handles notebooks of any version. The notebook |
|
151 | 161 | returned will always be in the current version's format. |
|
152 | 162 | |
|
153 | 163 | Parameters |
|
154 | 164 | ---------- |
|
155 | 165 | fp : file |
|
156 | 166 | Any file-like object with a read method. |
|
157 | 167 | |
|
158 | 168 | Returns |
|
159 | 169 | ------- |
|
160 | 170 | nb : NotebookNode |
|
161 | 171 | The notebook that was read. |
|
162 | 172 | """ |
|
163 | 173 | return reads(fp.read(), **kwargs) |
|
164 | 174 | |
|
165 | 175 | |
|
166 | 176 | def write(nb, fp, format='DEPRECATED', **kwargs): |
|
167 | 177 | """Write a notebook to a file in a given format in the current nbformat version. |
|
168 | 178 | |
|
169 | 179 | This function always writes the notebook in the current nbformat version. |
|
170 | 180 | |
|
171 | 181 | Parameters |
|
172 | 182 | ---------- |
|
173 | 183 | nb : NotebookNode |
|
174 | 184 | The notebook to write. |
|
175 | 185 | fp : file |
|
176 | 186 | Any file-like object with a write method. |
|
177 | 187 | """ |
|
178 | 188 | s = writes(nb, **kwargs) |
|
179 | 189 | if isinstance(s, bytes): |
|
180 | 190 | s = s.decode('utf8') |
|
181 | 191 | return fp.write(s) |
|
182 | 192 |
General Comments 0
You need to be logged in to leave comments.
Login now