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