Show More
@@ -13,6 +13,13 b' from .nbbase import (' | |||||
13 | from IPython.nbformat import v3 |
|
13 | from IPython.nbformat import v3 | |
14 | from IPython.utils.log import get_logger |
|
14 | from IPython.utils.log import get_logger | |
15 |
|
15 | |||
|
16 | def _warn_if_invalid(nb, version): | |||
|
17 | """Log validation errors, if there are any.""" | |||
|
18 | from IPython.nbformat.current import validate, ValidationError | |||
|
19 | try: | |||
|
20 | validate(nb, version=version) | |||
|
21 | except ValidationError as e: | |||
|
22 | get_logger().error("Notebook JSON is not valid v%i: %s", version, e) | |||
16 |
|
23 | |||
17 | def upgrade(nb, from_version=3, from_minor=0): |
|
24 | def upgrade(nb, from_version=3, from_minor=0): | |
18 | """Convert a notebook to v4. |
|
25 | """Convert a notebook to v4. | |
@@ -26,13 +33,9 b' def upgrade(nb, from_version=3, from_minor=0):' | |||||
26 | from_minor : int |
|
33 | from_minor : int | |
27 | The original minor version of the notebook to convert (only relevant for v >= 3). |
|
34 | The original minor version of the notebook to convert (only relevant for v >= 3). | |
28 | """ |
|
35 | """ | |
29 | from IPython.nbformat.current import validate, ValidationError |
|
|||
30 | if from_version == 3: |
|
36 | if from_version == 3: | |
31 | # Validate the notebook before conversion |
|
37 | # Validate the notebook before conversion | |
32 | try: |
|
38 | _warn_if_invalid(nb, from_version) | |
33 | validate(nb, version=from_version) |
|
|||
34 | except ValidationError as e: |
|
|||
35 | get_logger().error("Notebook JSON is not valid v%i: %s", from_version, e) |
|
|||
36 |
|
39 | |||
37 | # Mark the original nbformat so consumers know it has been converted |
|
40 | # Mark the original nbformat so consumers know it has been converted | |
38 | orig_nbformat = nb.pop('orig_nbformat', None) |
|
41 | orig_nbformat = nb.pop('orig_nbformat', None) | |
@@ -53,10 +56,7 b' def upgrade(nb, from_version=3, from_minor=0):' | |||||
53 | # upgrade metadata |
|
56 | # upgrade metadata | |
54 | nb.metadata.pop('name', '') |
|
57 | nb.metadata.pop('name', '') | |
55 | # Validate the converted notebook before returning it |
|
58 | # Validate the converted notebook before returning it | |
56 | try: |
|
59 | _warn_if_invalid(nb, nbformat) | |
57 | validate(nb, version=nbformat) |
|
|||
58 | except ValidationError as e: |
|
|||
59 | get_logger().error("Notebook JSON is not valid v%i: %s", nbformat, e) |
|
|||
60 | return nb |
|
60 | return nb | |
61 | elif from_version == 4: |
|
61 | elif from_version == 4: | |
62 | # nothing to do |
|
62 | # nothing to do | |
@@ -211,12 +211,12 b' def downgrade(nb):' | |||||
211 | nb : NotebookNode |
|
211 | nb : NotebookNode | |
212 | The Python representation of the notebook to convert. |
|
212 | The Python representation of the notebook to convert. | |
213 | """ |
|
213 | """ | |
214 | from IPython.nbformat.current import validate |
|
214 | if nb.nbformat != nbformat: | |
|
215 | return nb | |||
|
216 | ||||
215 | # Validate the notebook before conversion |
|
217 | # Validate the notebook before conversion | |
216 |
valid |
|
218 | _warn_if_invalid(nb, nbformat) | |
217 |
|
219 | |||
218 | if nb.nbformat != 4: |
|
|||
219 | return nb |
|
|||
220 | nb.nbformat = v3.nbformat |
|
220 | nb.nbformat = v3.nbformat | |
221 | nb.nbformat_minor = v3.nbformat_minor |
|
221 | nb.nbformat_minor = v3.nbformat_minor | |
222 | cells = [ downgrade_cell(cell) for cell in nb.pop('cells') ] |
|
222 | cells = [ downgrade_cell(cell) for cell in nb.pop('cells') ] | |
@@ -226,5 +226,5 b' def downgrade(nb):' | |||||
226 | nb.metadata.pop('orig_nbformat_minor', None) |
|
226 | nb.metadata.pop('orig_nbformat_minor', None) | |
227 |
|
227 | |||
228 | # Validate the converted notebook before returning it |
|
228 | # Validate the converted notebook before returning it | |
229 |
valid |
|
229 | _warn_if_invalid(nb, v3.nbformat) | |
230 | return nb |
|
230 | return nb |
General Comments 0
You need to be logged in to leave comments.
Login now