diff --git a/IPython/nbformat/current.py b/IPython/nbformat/current.py index 2396d14..d2e4562 100644 --- a/IPython/nbformat/current.py +++ b/IPython/nbformat/current.py @@ -28,7 +28,8 @@ from IPython.nbformat import v1 from IPython.nbformat.v3 import ( NotebookNode, new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, - parse_filename, new_metadata, new_author, new_heading_cell, nbformat + parse_filename, new_metadata, new_author, new_heading_cell, nbformat, + nbformat_minor, ) #----------------------------------------------------------------------------- @@ -36,6 +37,8 @@ from IPython.nbformat.v3 import ( #----------------------------------------------------------------------------- current_nbformat = nbformat +current_nbformat_minor = nbformat_minor + class NBFormatError(Exception): @@ -45,8 +48,9 @@ class NBFormatError(Exception): def parse_json(s, **kwargs): """Parse a string into a (nbformat, dict) tuple.""" d = json.loads(s, **kwargs) - nbf = d.get('nbformat',1) - return nbf, d + nbf = d.get('nbformat', 1) + nbm = d.get('nbformat_minor', 0) + return nbf, nbm, d def parse_py(s, **kwargs): @@ -62,9 +66,7 @@ def parse_py(s, **kwargs): def reads_json(s, **kwargs): """Read a JSON notebook from a string and return the NotebookNode object.""" - nbf, d = parse_json(s, **kwargs) - # cast int for reading - 3.x notebooks should be readable on 3.0, etc. - nbf = int(nbf) + nbf, minor, d = parse_json(s, **kwargs) if nbf == 1: nb = v1.to_notebook_json(d, **kwargs) nb = v3.convert_to_this_nbformat(nb, orig_version=1) @@ -73,6 +75,7 @@ def reads_json(s, **kwargs): nb = v3.convert_to_this_nbformat(nb, orig_version=2) elif nbf == 3: nb = v3.to_notebook_json(d, **kwargs) + nb = v3.convert_to_this_nbformat(nb, orig_version=3, orig_minor=minor) else: raise NBFormatError('Unsupported JSON nbformat version: %i' % nbf) return nb @@ -85,8 +88,7 @@ def writes_json(nb, **kwargs): def reads_py(s, **kwargs): """Read a .py notebook from a string and return the NotebookNode object.""" nbf, s = parse_py(s, **kwargs) - # cast int for reading - 3.x notebooks should be readable on 3.0, etc. - nbf = int(nbf) + nbf = nbf if nbf == 2: nb = v2.to_notebook_py(s, **kwargs) elif nbf == 3: diff --git a/IPython/nbformat/v3/__init__.py b/IPython/nbformat/v3/__init__.py index c8fa25d..d3838e3 100644 --- a/IPython/nbformat/v3/__init__.py +++ b/IPython/nbformat/v3/__init__.py @@ -19,7 +19,7 @@ Authors: from .nbbase import ( NotebookNode, new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, - new_metadata, new_author, new_heading_cell, nbformat + new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor ) from .nbjson import reads as reads_json, writes as writes_json diff --git a/IPython/nbformat/v3/convert.py b/IPython/nbformat/v3/convert.py index 8914fef..6421b80 100644 --- a/IPython/nbformat/v3/convert.py +++ b/IPython/nbformat/v3/convert.py @@ -17,7 +17,8 @@ Authors: #----------------------------------------------------------------------------- from .nbbase import ( - new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output + new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output, + nbformat, nbformat_minor ) from IPython.nbformat import v2 @@ -26,7 +27,7 @@ from IPython.nbformat import v2 # Code #----------------------------------------------------------------------------- -def convert_to_this_nbformat(nb, orig_version=2): +def convert_to_this_nbformat(nb, orig_version=2, orig_minor=0): """Convert a notebook to the v3 format. Parameters @@ -35,16 +36,23 @@ def convert_to_this_nbformat(nb, orig_version=2): The Python representation of the notebook to convert. orig_version : int The original version of the notebook to convert. + orig_minor : int + The original minor version of the notebook to convert (only relevant for v >= 3). """ if orig_version == 1: nb = v2.convert_to_this_nbformat(nb) orig_version = 2 if orig_version == 2: # Mark the original nbformat so consumers know it has been converted. - nb.nbformat = 3 + nb.nbformat = nbformat + nb.nbformat_minor = nbformat_minor + nb.orig_nbformat = 2 return nb elif orig_version == 3: + if orig_minor != nbformat_minor: + nb.orig_nbformat_minor = orig_minor + nb.nbformat_minor = nbformat_minor return nb else: raise ValueError('Cannot convert a notebook from v%s to v3' % orig_version) diff --git a/IPython/nbformat/v3/nbbase.py b/IPython/nbformat/v3/nbbase.py index 6e990f8..f4e312b 100644 --- a/IPython/nbformat/v3/nbbase.py +++ b/IPython/nbformat/v3/nbbase.py @@ -31,7 +31,8 @@ from IPython.utils.ipstruct import Struct #----------------------------------------------------------------------------- # Change this when incrementing the nbformat version -nbformat = 3.0 +nbformat = 3 +nbformat_minor = 0 class NotebookNode(Struct): pass @@ -158,6 +159,7 @@ def new_notebook(name=None, metadata=None, worksheets=None): """Create a notebook by name, id and a list of worksheets.""" nb = NotebookNode() nb.nbformat = nbformat + nb.nbformat_minor = nbformat_minor if worksheets is None: nb.worksheets = [] else: