##// END OF EJS Templates
move NotebookNode to top-level...
MinRK -
Show More
@@ -0,0 +1,21 b''
1 """NotebookNode - adding attribute access to dicts"""
2
3 from IPython.utils.ipstruct import Struct
4
5 class NotebookNode(Struct):
6 """A dict-like node with attribute-access"""
7 pass
8
9 def from_dict(d):
10 """Convert dict to dict-like NotebookNode
11
12 Recursively converts any dict in the container to a NotebookNode
13 """
14 if isinstance(d, dict):
15 return NotebookNode({k:from_dict(v) for k,v in d.items()})
16 elif isinstance(d, (tuple, list)):
17 return [from_dict(i) for i in d]
18 else:
19 return d
20
21
@@ -5,7 +5,7 b''
5
5
6 from functools import wraps
6 from functools import wraps
7
7
8 from IPython.nbformat.v4 import NotebookNode
8 from IPython.nbformat import NotebookNode
9 from IPython.utils.decorators import undoc
9 from IPython.utils.decorators import undoc
10 from IPython.utils.py3compat import string_types
10 from IPython.utils.py3compat import string_types
11
11
@@ -29,7 +29,7 b' def DocDecorator(f):'
29
29
30 #Set docstring of function
30 #Set docstring of function
31 f.__doc__ = f.__doc__ + """
31 f.__doc__ = f.__doc__ + """
32 nb : :class:`~{nbnode_mod}.NotebookNode`
32 nb : :class:`~IPython.nbformat.NotebookNode`
33 The notebook to export.
33 The notebook to export.
34 config : config (optional, keyword arg)
34 config : config (optional, keyword arg)
35 User configuration instance.
35 User configuration instance.
@@ -52,7 +52,7 b' def DocDecorator(f):'
52 Notes
52 Notes
53 -----
53 -----
54 WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT
54 WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT
55 """.format(nbnode_mod=NotebookNode.__module__)
55 """
56
56
57 @wraps(f)
57 @wraps(f)
58 def decorator(*args, **kwargs):
58 def decorator(*args, **kwargs):
@@ -89,7 +89,7 b' class Exporter(LoggingConfigurable):'
89
89
90 Parameters
90 Parameters
91 ----------
91 ----------
92 nb : :class:`~IPython.nbformat.current.NotebookNode`
92 nb : :class:`~IPython.nbformat.NotebookNode`
93 Notebook node (dict-like with attr-access)
93 Notebook node (dict-like with attr-access)
94 resources : dict
94 resources : dict
95 Additional resources that can be accessed read/write by
95 Additional resources that can be accessed read/write by
@@ -199,7 +199,7 b' class TemplateExporter(Exporter):'
199
199
200 Parameters
200 Parameters
201 ----------
201 ----------
202 nb : :class:`~IPython.nbformat.current.NotebookNode`
202 nb : :class:`~IPython.nbformat.NotebookNode`
203 Notebook node
203 Notebook node
204 resources : dict
204 resources : dict
205 Additional resources that can be accessed read/write by
205 Additional resources that can be accessed read/write by
@@ -23,6 +23,7 b' versions = {'
23 from .validator import validate, ValidationError
23 from .validator import validate, ValidationError
24 from .converter import convert
24 from .converter import convert
25 from . import reader
25 from . import reader
26 from .notebooknode import from_dict, NotebookNode
26
27
27 from .v4 import (
28 from .v4 import (
28 nbformat as current_nbformat,
29 nbformat as current_nbformat,
@@ -4,15 +4,15 b''
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 from .nbbase import (
6 from .nbbase import (
7 NotebookNode, from_dict,
8 nbformat, nbformat_minor, nbformat_schema,
7 nbformat, nbformat_minor, nbformat_schema,
9 new_code_cell, new_markdown_cell, new_notebook,
8 new_code_cell, new_markdown_cell, new_notebook,
10 new_output, output_from_msg,
9 new_output, output_from_msg,
11 )
10 )
12
11
13 from .nbjson import reads as reads_json, writes as writes_json
12 from .nbjson import reads, writes, to_notebook
14 from .nbjson import reads as read_json, writes as write_json
13 reads_json = reads
15 from .nbjson import to_notebook as to_notebook_json
14 writes_json = writes
15 to_notebook_json = to_notebook
16
16
17 from .convert import downgrade, upgrade
17 from .convert import downgrade, upgrade
18
18
@@ -9,7 +9,7 b' helpers to build the structs in the right form.'
9 # Copyright (c) IPython Development Team.
9 # Copyright (c) IPython Development Team.
10 # Distributed under the terms of the Modified BSD License.
10 # Distributed under the terms of the Modified BSD License.
11
11
12 from IPython.utils.ipstruct import Struct
12 from ..notebooknode import from_dict, NotebookNode
13
13
14 # Change this when incrementing the nbformat version
14 # Change this when incrementing the nbformat version
15 nbformat = 4
15 nbformat = 4
@@ -23,18 +23,6 b' def validate(node, ref=None):'
23 return validate(node, ref=ref, version=nbformat)
23 return validate(node, ref=ref, version=nbformat)
24
24
25
25
26 class NotebookNode(Struct):
27 pass
28
29 def from_dict(d):
30 if isinstance(d, dict):
31 return NotebookNode({k:from_dict(v) for k,v in d.items()})
32 elif isinstance(d, (tuple, list)):
33 return [from_dict(i) for i in d]
34 else:
35 return d
36
37
38 def new_output(output_type, data=None, **kwargs):
26 def new_output(output_type, data=None, **kwargs):
39 """Create a new output, to go in the ``cell.outputs`` list of a code cell."""
27 """Create a new output, to go in the ``cell.outputs`` list of a code cell."""
40 output = NotebookNode(output_type=output_type)
28 output = NotebookNode(output_type=output_type)
@@ -25,12 +25,18 b' class BytesEncoder(json.JSONEncoder):'
25 class JSONReader(NotebookReader):
25 class JSONReader(NotebookReader):
26
26
27 def reads(self, s, **kwargs):
27 def reads(self, s, **kwargs):
28 """Read a JSON string into a Notebook object"""
28 nb = json.loads(s, **kwargs)
29 nb = json.loads(s, **kwargs)
29 nb = self.to_notebook(nb, **kwargs)
30 nb = self.to_notebook(nb, **kwargs)
30 return nb
31 return nb
31
32
32 def to_notebook(self, d, **kwargs):
33 def to_notebook(self, d, **kwargs):
33 nb = rejoin_lines(from_dict(d))
34 """Convert a disk-format notebook dict to in-memory NotebookNode
35
36 handles multi-line values as strings, scrubbing of transient values, etc.
37 """
38 nb = from_dict(d)
39 nb = rejoin_lines(nb)
34 nb = strip_transient(nb)
40 nb = strip_transient(nb)
35 return nb
41 return nb
36
42
@@ -38,6 +44,7 b' class JSONReader(NotebookReader):'
38 class JSONWriter(NotebookWriter):
44 class JSONWriter(NotebookWriter):
39
45
40 def writes(self, nb, **kwargs):
46 def writes(self, nb, **kwargs):
47 """Serialize a NotebookNode object as a JSON string"""
41 kwargs['cls'] = BytesEncoder
48 kwargs['cls'] = BytesEncoder
42 kwargs['indent'] = 1
49 kwargs['indent'] = 1
43 kwargs['sort_keys'] = True
50 kwargs['sort_keys'] = True
General Comments 0
You need to be logged in to leave comments. Login now