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