##// END OF EJS Templates
deprecate format arg in reads/writes...
MinRK -
Show More
@@ -6,14 +6,13 b''
6 from __future__ import print_function
6 from __future__ import print_function
7
7
8 import re
8 import re
9
9 import warnings
10 from IPython.utils.py3compat import unicode_type
11
10
12 from IPython.nbformat.v3 import (
11 from IPython.nbformat.v3 import (
13 NotebookNode,
12 NotebookNode,
14 new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet,
13 new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet,
15 parse_filename, new_metadata, new_author, new_heading_cell, nbformat,
14 parse_filename, new_metadata, new_author, new_heading_cell, nbformat,
16 nbformat_minor, nbformat_schema, to_notebook_json
15 nbformat_minor, nbformat_schema, to_notebook_json,
17 )
16 )
18 from IPython.nbformat import v3 as _v_latest
17 from IPython.nbformat import v3 as _v_latest
19
18
@@ -40,6 +39,11 b' class NBFormatError(ValueError):'
40 pass
39 pass
41
40
42
41
42 def _warn_format():
43 warnings.warn("""Non-JSON file support in nbformat is deprecated.
44 Use nbconvert to create files of other formats.""")
45
46
43 def parse_py(s, **kwargs):
47 def parse_py(s, **kwargs):
44 """Parse a string into a (nbformat, string) tuple."""
48 """Parse a string into a (nbformat, string) tuple."""
45 nbf = current_nbformat
49 nbf = current_nbformat
@@ -57,34 +61,18 b' def parse_py(s, **kwargs):'
57
61
58
62
59 def reads_json(nbjson, **kwargs):
63 def reads_json(nbjson, **kwargs):
60 """Read a JSON notebook from a string and return the NotebookNode
64 """DEPRECATED, use reads"""
61 object. Report if any JSON format errors are detected.
65 warnings.warn("reads_json is deprecated, use reads")
62
66 return reads(nbjson)
63 """
64 nb = reader_reads(nbjson, **kwargs)
65 nb_current = convert(nb, current_nbformat)
66 try:
67 validate(nb_current)
68 except ValidationError as e:
69 get_logger().error("Notebook JSON is invalid: %s", e)
70 return nb_current
71
72
67
73 def writes_json(nb, **kwargs):
68 def writes_json(nb, **kwargs):
74 """Take a NotebookNode object and write out a JSON string. Report if
69 """DEPRECATED, use writes"""
75 any JSON format errors are detected.
70 warnings.warn("writes_json is deprecated, use writes")
76
71 return writes(nb, **kwargs)
77 """
78 try:
79 validate(nb)
80 except ValidationError as e:
81 get_logger().error("Notebook JSON is invalid: %s", e)
82 nbjson = versions[current_nbformat].writes_json(nb, **kwargs)
83 return nbjson
84
85
72
86 def reads_py(s, **kwargs):
73 def reads_py(s, **kwargs):
87 """Read a .py notebook from a string and return the NotebookNode object."""
74 """DEPRECATED: use nbconvert"""
75 _warn_format()
88 nbf, nbm, s = parse_py(s, **kwargs)
76 nbf, nbm, s = parse_py(s, **kwargs)
89 if nbf in (2, 3):
77 if nbf in (2, 3):
90 nb = versions[nbf].to_notebook_py(s, **kwargs)
78 nb = versions[nbf].to_notebook_py(s, **kwargs)
@@ -92,9 +80,9 b' def reads_py(s, **kwargs):'
92 raise NBFormatError('Unsupported PY nbformat version: %i' % nbf)
80 raise NBFormatError('Unsupported PY nbformat version: %i' % nbf)
93 return nb
81 return nb
94
82
95
96 def writes_py(nb, **kwargs):
83 def writes_py(nb, **kwargs):
97 # nbformat 3 is the latest format that supports py
84 """DEPRECATED: use nbconvert"""
85 _warn_format()
98 return versions[3].writes_py(nb, **kwargs)
86 return versions[3].writes_py(nb, **kwargs)
99
87
100
88
@@ -117,7 +105,9 b" def reads(s, format='DEPRECATED', version=current_nbformat, **kwargs):"
117 nb : NotebookNode
105 nb : NotebookNode
118 The notebook that was read.
106 The notebook that was read.
119 """
107 """
120 nb = versions[version].reads_json(s, **kwargs)
108 if format not in {'DEPRECATED', 'json'}:
109 _warn_format()
110 nb = reader_reads(s, **kwargs)
121 nb = convert(nb, version)
111 nb = convert(nb, version)
122 try:
112 try:
123 validate(nb)
113 validate(nb)
@@ -144,11 +134,13 b" def writes(nb, format='DEPRECATED', version=current_nbformat, **kwargs):"
144 s : unicode
134 s : unicode
145 The notebook string.
135 The notebook string.
146 """
136 """
137 if format not in {'DEPRECATED', 'json'}:
138 _warn_format()
139 nb = convert(nb, version)
147 try:
140 try:
148 validate(nb)
141 validate(nb)
149 except ValidationError as e:
142 except ValidationError as e:
150 get_logger().error("Notebook JSON is invalid: %s", e)
143 get_logger().error("Notebook JSON is invalid: %s", e)
151 nb = convert(nb, version)
152 return versions[version].writes_json(nb, **kwargs)
144 return versions[version].writes_json(nb, **kwargs)
153
145
154
146
@@ -192,18 +184,3 b" def write(nb, fp, format='DEPRECATED', **kwargs):"
192 """
184 """
193 return fp.write(writes(nb, **kwargs))
185 return fp.write(writes(nb, **kwargs))
194
186
195 def _convert_to_metadata():
196 """Convert to a notebook having notebook metadata."""
197 import glob
198 for fname in glob.glob('*.ipynb'):
199 print('Converting file:',fname)
200 with open(fname,'r') as f:
201 nb = read(f,u'json')
202 md = new_metadata()
203 if u'name' in nb:
204 md.name = nb.name
205 del nb[u'name']
206 nb.metadata = md
207 with open(fname,'w') as f:
208 write(nb, f, u'json')
209
General Comments 0
You need to be logged in to leave comments. Login now