##// END OF EJS Templates
fix some validation bugs in v3...
MinRK -
Show More
@@ -1,73 +1,60
1 """
1 """Tests for the coalescestreams preprocessor"""
2 Module with tests for the coalescestreams preprocessor
3 """
4
2
5 #-----------------------------------------------------------------------------
3 # Copyright (c) IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
8 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
12
5
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16 from IPython.nbformat import current as nbformat
6 from IPython.nbformat import current as nbformat
17
7
18 from .base import PreprocessorTestsBase
8 from .base import PreprocessorTestsBase
19 from ..coalescestreams import coalesce_streams
9 from ..coalescestreams import coalesce_streams
20
10
21
11
22 #-----------------------------------------------------------------------------
23 # Class
24 #-----------------------------------------------------------------------------
25 class TestCoalesceStreams(PreprocessorTestsBase):
12 class TestCoalesceStreams(PreprocessorTestsBase):
26 """Contains test functions for coalescestreams.py"""
13 """Contains test functions for coalescestreams.py"""
27
14
28 def test_coalesce_streams(self):
15 def test_coalesce_streams(self):
29 """coalesce_streams preprocessor output test"""
16 """coalesce_streams preprocessor output test"""
30 nb = self.build_notebook()
17 nb = self.build_notebook()
31 res = self.build_resources()
18 res = self.build_resources()
32 nb, res = coalesce_streams(nb, res)
19 nb, res = coalesce_streams(nb, res)
33 outputs = nb.worksheets[0].cells[0].outputs
20 outputs = nb.worksheets[0].cells[0].outputs
34 self.assertEqual(outputs[0].text, "a")
21 self.assertEqual(outputs[0].text, "a")
35 self.assertEqual(outputs[1].output_type, "text")
22 self.assertEqual(outputs[1].output_type, "text")
36 self.assertEqual(outputs[2].text, "cd")
23 self.assertEqual(outputs[2].text, "cd")
37 self.assertEqual(outputs[3].text, "ef")
24 self.assertEqual(outputs[3].text, "ef")
38
25
39 def test_coalesce_sequenced_streams(self):
26 def test_coalesce_sequenced_streams(self):
40 """Can the coalesce streams preprocessor merge a sequence of streams?"""
27 """Can the coalesce streams preprocessor merge a sequence of streams?"""
41 outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="0"),
28 outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="0"),
42 nbformat.new_output(output_type="stream", stream="stdout", output_text="1"),
29 nbformat.new_output(output_type="stream", stream="stdout", output_text="1"),
43 nbformat.new_output(output_type="stream", stream="stdout", output_text="2"),
30 nbformat.new_output(output_type="stream", stream="stdout", output_text="2"),
44 nbformat.new_output(output_type="stream", stream="stdout", output_text="3"),
31 nbformat.new_output(output_type="stream", stream="stdout", output_text="3"),
45 nbformat.new_output(output_type="stream", stream="stdout", output_text="4"),
32 nbformat.new_output(output_type="stream", stream="stdout", output_text="4"),
46 nbformat.new_output(output_type="stream", stream="stdout", output_text="5"),
33 nbformat.new_output(output_type="stream", stream="stdout", output_text="5"),
47 nbformat.new_output(output_type="stream", stream="stdout", output_text="6"),
34 nbformat.new_output(output_type="stream", stream="stdout", output_text="6"),
48 nbformat.new_output(output_type="stream", stream="stdout", output_text="7")]
35 nbformat.new_output(output_type="stream", stream="stdout", output_text="7")]
49 cells=[nbformat.new_code_cell(input="# None", prompt_number=1,outputs=outputs)]
36 cells=[nbformat.new_code_cell(input="# None", prompt_number=1,outputs=outputs)]
50 worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)]
37 worksheets = [nbformat.new_worksheet(cells=cells)]
51
38
52 nb = nbformat.new_notebook(name="notebook1", worksheets=worksheets)
39 nb = nbformat.new_notebook(name="notebook1", worksheets=worksheets)
53 res = self.build_resources()
40 res = self.build_resources()
54 nb, res = coalesce_streams(nb, res)
41 nb, res = coalesce_streams(nb, res)
55 outputs = nb.worksheets[0].cells[0].outputs
42 outputs = nb.worksheets[0].cells[0].outputs
56 self.assertEqual(outputs[0].text, u'01234567')
43 self.assertEqual(outputs[0].text, u'01234567')
57
44
58 def test_coalesce_replace_streams(self):
45 def test_coalesce_replace_streams(self):
59 """Are \\r characters handled?"""
46 """Are \\r characters handled?"""
60 outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="z"),
47 outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="z"),
61 nbformat.new_output(output_type="stream", stream="stdout", output_text="\ra"),
48 nbformat.new_output(output_type="stream", stream="stdout", output_text="\ra"),
62 nbformat.new_output(output_type="stream", stream="stdout", output_text="\nz\rb"),
49 nbformat.new_output(output_type="stream", stream="stdout", output_text="\nz\rb"),
63 nbformat.new_output(output_type="stream", stream="stdout", output_text="\nz"),
50 nbformat.new_output(output_type="stream", stream="stdout", output_text="\nz"),
64 nbformat.new_output(output_type="stream", stream="stdout", output_text="\rc\n"),
51 nbformat.new_output(output_type="stream", stream="stdout", output_text="\rc\n"),
65 nbformat.new_output(output_type="stream", stream="stdout", output_text="z\rz\rd")]
52 nbformat.new_output(output_type="stream", stream="stdout", output_text="z\rz\rd")]
66 cells=[nbformat.new_code_cell(input="# None", prompt_number=1,outputs=outputs)]
53 cells=[nbformat.new_code_cell(input="# None", prompt_number=1,outputs=outputs)]
67 worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)]
54 worksheets = [nbformat.new_worksheet(cells=cells)]
68
55
69 nb = nbformat.new_notebook(name="notebook1", worksheets=worksheets)
56 nb = nbformat.new_notebook(name="notebook1", worksheets=worksheets)
70 res = self.build_resources()
57 res = self.build_resources()
71 nb, res = coalesce_streams(nb, res)
58 nb, res = coalesce_streams(nb, res)
72 outputs = nb.worksheets[0].cells[0].outputs
59 outputs = nb.worksheets[0].cells[0].outputs
73 self.assertEqual(outputs[0].text, u'a\nb\nc\nd')
60 self.assertEqual(outputs[0].text, u'a\nb\nc\nd')
@@ -1,94 +1,79
1 """
1 """Tests for the revealhelp preprocessor"""
2 Module with tests for the revealhelp preprocessor
3 """
4
2
5 #-----------------------------------------------------------------------------
3 # Copyright (c) IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
8 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
5
17 from IPython.nbformat import current as nbformat
6 from IPython.nbformat import current as nbformat
18
7
19 from .base import PreprocessorTestsBase
8 from .base import PreprocessorTestsBase
20 from ..revealhelp import RevealHelpPreprocessor
9 from ..revealhelp import RevealHelpPreprocessor
21
10
22
11
23 #-----------------------------------------------------------------------------
24 # Class
25 #-----------------------------------------------------------------------------
26
27 class Testrevealhelp(PreprocessorTestsBase):
12 class Testrevealhelp(PreprocessorTestsBase):
28 """Contains test functions for revealhelp.py"""
13 """Contains test functions for revealhelp.py"""
29
14
30 def build_notebook(self):
15 def build_notebook(self):
31 """Build a reveal slides notebook in memory for use with tests.
16 """Build a reveal slides notebook in memory for use with tests.
32 Overrides base in PreprocessorTestsBase"""
17 Overrides base in PreprocessorTestsBase"""
33
18
34 outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="a")]
19 outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="a")]
35
20
36 slide_metadata = {'slideshow' : {'slide_type': 'slide'}}
21 slide_metadata = {'slideshow' : {'slide_type': 'slide'}}
37 subslide_metadata = {'slideshow' : {'slide_type': 'subslide'}}
22 subslide_metadata = {'slideshow' : {'slide_type': 'subslide'}}
38
23
39 cells=[nbformat.new_code_cell(input="", prompt_number=1, outputs=outputs),
24 cells=[nbformat.new_code_cell(input="", prompt_number=1, outputs=outputs),
40 nbformat.new_text_cell('markdown', source="", metadata=slide_metadata),
25 nbformat.new_text_cell('markdown', source="", metadata=slide_metadata),
41 nbformat.new_code_cell(input="", prompt_number=2, outputs=outputs),
26 nbformat.new_code_cell(input="", prompt_number=2, outputs=outputs),
42 nbformat.new_text_cell('markdown', source="", metadata=slide_metadata),
27 nbformat.new_text_cell('markdown', source="", metadata=slide_metadata),
43 nbformat.new_text_cell('markdown', source="", metadata=subslide_metadata)]
28 nbformat.new_text_cell('markdown', source="", metadata=subslide_metadata)]
44 worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)]
29 worksheets = [nbformat.new_worksheet(cells=cells)]
45
30
46 return nbformat.new_notebook(name="notebook1", worksheets=worksheets)
31 return nbformat.new_notebook(name="notebook1", worksheets=worksheets)
47
32
48
33
49 def build_preprocessor(self):
34 def build_preprocessor(self):
50 """Make an instance of a preprocessor"""
35 """Make an instance of a preprocessor"""
51 preprocessor = RevealHelpPreprocessor()
36 preprocessor = RevealHelpPreprocessor()
52 preprocessor.enabled = True
37 preprocessor.enabled = True
53 return preprocessor
38 return preprocessor
54
39
55
40
56 def test_constructor(self):
41 def test_constructor(self):
57 """Can a RevealHelpPreprocessor be constructed?"""
42 """Can a RevealHelpPreprocessor be constructed?"""
58 self.build_preprocessor()
43 self.build_preprocessor()
59
44
60
45
61 def test_reveal_attribute(self):
46 def test_reveal_attribute(self):
62 """Make sure the reveal url_prefix resources is set"""
47 """Make sure the reveal url_prefix resources is set"""
63 nb = self.build_notebook()
48 nb = self.build_notebook()
64 res = self.build_resources()
49 res = self.build_resources()
65 preprocessor = self.build_preprocessor()
50 preprocessor = self.build_preprocessor()
66 nb, res = preprocessor(nb, res)
51 nb, res = preprocessor(nb, res)
67 assert 'reveal' in res
52 assert 'reveal' in res
68 assert 'url_prefix' in res['reveal']
53 assert 'url_prefix' in res['reveal']
69
54
70
55
71 def test_reveal_output(self):
56 def test_reveal_output(self):
72 """Make sure that the reveal preprocessor """
57 """Make sure that the reveal preprocessor """
73 nb = self.build_notebook()
58 nb = self.build_notebook()
74 res = self.build_resources()
59 res = self.build_resources()
75 preprocessor = self.build_preprocessor()
60 preprocessor = self.build_preprocessor()
76 nb, res = preprocessor(nb, res)
61 nb, res = preprocessor(nb, res)
77 cells = nb.worksheets[0].cells
62 cells = nb.worksheets[0].cells
78
63
79 # Make sure correct metadata tags are available on every cell.
64 # Make sure correct metadata tags are available on every cell.
80 for cell in cells:
65 for cell in cells:
81 assert 'slide_type' in cell.metadata
66 assert 'slide_type' in cell.metadata
82
67
83 # Make sure slide end is only applied to the cells preceeding slide
68 # Make sure slide end is only applied to the cells preceeding slide
84 # cells.
69 # cells.
85 assert 'slide_helper' in cells[1].metadata
70 assert 'slide_helper' in cells[1].metadata
86 self.assertEqual(cells[1].metadata['slide_helper'], '-')
71 self.assertEqual(cells[1].metadata['slide_helper'], '-')
87
72
88 # Verify 'slide-end'
73 # Verify 'slide-end'
89 assert 'slide_helper' in cells[0].metadata
74 assert 'slide_helper' in cells[0].metadata
90 self.assertEqual(cells[0].metadata['slide_helper'], 'slide_end')
75 self.assertEqual(cells[0].metadata['slide_helper'], 'slide_end')
91 assert 'slide_helper' in cells[2].metadata
76 assert 'slide_helper' in cells[2].metadata
92 self.assertEqual(cells[2].metadata['slide_helper'], 'slide_end')
77 self.assertEqual(cells[2].metadata['slide_helper'], 'slide_end')
93 assert 'slide_helper' in cells[3].metadata
78 assert 'slide_helper' in cells[3].metadata
94 self.assertEqual(cells[3].metadata['slide_helper'], 'subslide_end')
79 self.assertEqual(cells[3].metadata['slide_helper'], 'subslide_end')
@@ -1,205 +1,200
1 """The basic dict based notebook format.
1 """The basic dict based notebook format.
2
2
3 The Python representation of a notebook is a nested structure of
3 The Python representation of a notebook is a nested structure of
4 dictionary subclasses that support attribute access
4 dictionary subclasses that support attribute access
5 (IPython.utils.ipstruct.Struct). The functions in this module are merely
5 (IPython.utils.ipstruct.Struct). The functions in this module are merely
6 helpers to build the structs in the right form.
6 helpers to build the structs in the right form.
7 """
7 """
8
8
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 import pprint
12 import pprint
13 import uuid
13 import uuid
14
14
15 from IPython.utils.ipstruct import Struct
15 from IPython.utils.ipstruct import Struct
16 from IPython.utils.py3compat import cast_unicode, unicode_type
16 from IPython.utils.py3compat import cast_unicode, unicode_type
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Code
19 # Code
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 # Change this when incrementing the nbformat version
22 # Change this when incrementing the nbformat version
23 nbformat = 3
23 nbformat = 3
24 nbformat_minor = 0
24 nbformat_minor = 0
25 nbformat_schema = 'nbformat.v3.schema.json'
25 nbformat_schema = 'nbformat.v3.schema.json'
26
26
27 class NotebookNode(Struct):
27 class NotebookNode(Struct):
28 pass
28 pass
29
29
30
30
31 def from_dict(d):
31 def from_dict(d):
32 if isinstance(d, dict):
32 if isinstance(d, dict):
33 newd = NotebookNode()
33 newd = NotebookNode()
34 for k,v in d.items():
34 for k,v in d.items():
35 newd[k] = from_dict(v)
35 newd[k] = from_dict(v)
36 return newd
36 return newd
37 elif isinstance(d, (tuple, list)):
37 elif isinstance(d, (tuple, list)):
38 return [from_dict(i) for i in d]
38 return [from_dict(i) for i in d]
39 else:
39 else:
40 return d
40 return d
41
41
42
42
43 def new_output(output_type, output_text=None, output_png=None,
43 def new_output(output_type, output_text=None, output_png=None,
44 output_html=None, output_svg=None, output_latex=None, output_json=None,
44 output_html=None, output_svg=None, output_latex=None, output_json=None,
45 output_javascript=None, output_jpeg=None, prompt_number=None,
45 output_javascript=None, output_jpeg=None, prompt_number=None,
46 ename=None, evalue=None, traceback=None, stream=None, metadata=None):
46 ename=None, evalue=None, traceback=None, stream=None, metadata=None):
47 """Create a new output, to go in the ``cell.outputs`` list of a code cell.
47 """Create a new output, to go in the ``cell.outputs`` list of a code cell.
48 """
48 """
49 output = NotebookNode()
49 output = NotebookNode()
50 output.output_type = unicode_type(output_type)
50 output.output_type = unicode_type(output_type)
51
51
52 if metadata is None:
52 if metadata is None:
53 metadata = {}
53 metadata = {}
54 if not isinstance(metadata, dict):
54 if not isinstance(metadata, dict):
55 raise TypeError("metadata must be dict")
55 raise TypeError("metadata must be dict")
56 output.metadata = metadata
57
56
58 if output_type != 'pyerr':
57 if output_type != 'pyerr':
59 if output_text is not None:
58 if output_text is not None:
60 output.text = cast_unicode(output_text)
59 output.text = cast_unicode(output_text)
61 if output_png is not None:
60 if output_png is not None:
62 output.png = cast_unicode(output_png)
61 output.png = cast_unicode(output_png)
63 if output_jpeg is not None:
62 if output_jpeg is not None:
64 output.jpeg = cast_unicode(output_jpeg)
63 output.jpeg = cast_unicode(output_jpeg)
65 if output_html is not None:
64 if output_html is not None:
66 output.html = cast_unicode(output_html)
65 output.html = cast_unicode(output_html)
67 if output_svg is not None:
66 if output_svg is not None:
68 output.svg = cast_unicode(output_svg)
67 output.svg = cast_unicode(output_svg)
69 if output_latex is not None:
68 if output_latex is not None:
70 output.latex = cast_unicode(output_latex)
69 output.latex = cast_unicode(output_latex)
71 if output_json is not None:
70 if output_json is not None:
72 output.json = cast_unicode(output_json)
71 output.json = cast_unicode(output_json)
73 if output_javascript is not None:
72 if output_javascript is not None:
74 output.javascript = cast_unicode(output_javascript)
73 output.javascript = cast_unicode(output_javascript)
75
74
76 if output_type == u'pyout':
75 if output_type == u'pyout':
77 if prompt_number is not None:
76 if prompt_number is not None:
78 output.prompt_number = int(prompt_number)
77 output.prompt_number = int(prompt_number)
79
78
80 if output_type == u'pyerr':
79 if output_type == u'pyerr':
81 if ename is not None:
80 if ename is not None:
82 output.ename = cast_unicode(ename)
81 output.ename = cast_unicode(ename)
83 if evalue is not None:
82 if evalue is not None:
84 output.evalue = cast_unicode(evalue)
83 output.evalue = cast_unicode(evalue)
85 if traceback is not None:
84 if traceback is not None:
86 output.traceback = [cast_unicode(frame) for frame in list(traceback)]
85 output.traceback = [cast_unicode(frame) for frame in list(traceback)]
87
86
88 if output_type == u'stream':
87 if output_type == u'stream':
89 output.stream = 'stdout' if stream is None else cast_unicode(stream)
88 output.stream = 'stdout' if stream is None else cast_unicode(stream)
89 else:
90 output.metadata = metadata
90
91
91 return output
92 return output
92
93
93
94
94 def new_code_cell(input=None, prompt_number=None, outputs=None,
95 def new_code_cell(input=None, prompt_number=None, outputs=None,
95 language=u'python', collapsed=False, metadata=None):
96 language=u'python', collapsed=False, metadata=None):
96 """Create a new code cell with input and output"""
97 """Create a new code cell with input and output"""
97 cell = NotebookNode()
98 cell = NotebookNode()
98 cell.cell_type = u'code'
99 cell.cell_type = u'code'
99 if language is not None:
100 if language is not None:
100 cell.language = cast_unicode(language)
101 cell.language = cast_unicode(language)
101 if input is not None:
102 if input is not None:
102 cell.input = cast_unicode(input)
103 cell.input = cast_unicode(input)
103 if prompt_number is not None:
104 if prompt_number is not None:
104 cell.prompt_number = int(prompt_number)
105 cell.prompt_number = int(prompt_number)
105 if outputs is None:
106 if outputs is None:
106 cell.outputs = []
107 cell.outputs = []
107 else:
108 else:
108 cell.outputs = outputs
109 cell.outputs = outputs
109 if collapsed is not None:
110 if collapsed is not None:
110 cell.collapsed = bool(collapsed)
111 cell.collapsed = bool(collapsed)
111 cell.metadata = NotebookNode(metadata or {})
112 cell.metadata = NotebookNode(metadata or {})
112
113
113 return cell
114 return cell
114
115
115 def new_text_cell(cell_type, source=None, rendered=None, metadata=None):
116 def new_text_cell(cell_type, source=None, rendered=None, metadata=None):
116 """Create a new text cell."""
117 """Create a new text cell."""
117 cell = NotebookNode()
118 cell = NotebookNode()
118 # VERSIONHACK: plaintext -> raw
119 # VERSIONHACK: plaintext -> raw
119 # handle never-released plaintext name for raw cells
120 # handle never-released plaintext name for raw cells
120 if cell_type == 'plaintext':
121 if cell_type == 'plaintext':
121 cell_type = 'raw'
122 cell_type = 'raw'
122 if source is not None:
123 if source is not None:
123 cell.source = cast_unicode(source)
124 cell.source = cast_unicode(source)
124 if rendered is not None:
125 cell.rendered = cast_unicode(rendered)
126 cell.metadata = NotebookNode(metadata or {})
125 cell.metadata = NotebookNode(metadata or {})
127 cell.cell_type = cell_type
126 cell.cell_type = cell_type
128 return cell
127 return cell
129
128
130
129
131 def new_heading_cell(source=None, rendered=None, level=1, metadata=None):
130 def new_heading_cell(source=None, level=1, rendered=None, metadata=None):
132 """Create a new section cell with a given integer level."""
131 """Create a new section cell with a given integer level."""
133 cell = NotebookNode()
132 cell = NotebookNode()
134 cell.cell_type = u'heading'
133 cell.cell_type = u'heading'
135 if source is not None:
134 if source is not None:
136 cell.source = cast_unicode(source)
135 cell.source = cast_unicode(source)
137 if rendered is not None:
138 cell.rendered = cast_unicode(rendered)
139 cell.level = int(level)
136 cell.level = int(level)
140 cell.metadata = NotebookNode(metadata or {})
137 cell.metadata = NotebookNode(metadata or {})
141 return cell
138 return cell
142
139
143
140
144 def new_worksheet(name=None, cells=None, metadata=None):
141 def new_worksheet(name=None, cells=None, metadata=None):
145 """Create a worksheet by name with with a list of cells."""
142 """Create a worksheet by name with with a list of cells."""
146 ws = NotebookNode()
143 ws = NotebookNode()
147 if name is not None:
148 ws.name = cast_unicode(name)
149 if cells is None:
144 if cells is None:
150 ws.cells = []
145 ws.cells = []
151 else:
146 else:
152 ws.cells = list(cells)
147 ws.cells = list(cells)
153 ws.metadata = NotebookNode(metadata or {})
148 ws.metadata = NotebookNode(metadata or {})
154 return ws
149 return ws
155
150
156
151
157 def new_notebook(name=None, metadata=None, worksheets=None):
152 def new_notebook(name=None, metadata=None, worksheets=None):
158 """Create a notebook by name, id and a list of worksheets."""
153 """Create a notebook by name, id and a list of worksheets."""
159 nb = NotebookNode()
154 nb = NotebookNode()
160 nb.nbformat = nbformat
155 nb.nbformat = nbformat
161 nb.nbformat_minor = nbformat_minor
156 nb.nbformat_minor = nbformat_minor
162 if worksheets is None:
157 if worksheets is None:
163 nb.worksheets = []
158 nb.worksheets = []
164 else:
159 else:
165 nb.worksheets = list(worksheets)
160 nb.worksheets = list(worksheets)
166 if metadata is None:
161 if metadata is None:
167 nb.metadata = new_metadata()
162 nb.metadata = new_metadata()
168 else:
163 else:
169 nb.metadata = NotebookNode(metadata)
164 nb.metadata = NotebookNode(metadata)
170 if name is not None:
165 if name is not None:
171 nb.metadata.name = cast_unicode(name)
166 nb.metadata.name = cast_unicode(name)
172 return nb
167 return nb
173
168
174
169
175 def new_metadata(name=None, authors=None, license=None, created=None,
170 def new_metadata(name=None, authors=None, license=None, created=None,
176 modified=None, gistid=None):
171 modified=None, gistid=None):
177 """Create a new metadata node."""
172 """Create a new metadata node."""
178 metadata = NotebookNode()
173 metadata = NotebookNode()
179 if name is not None:
174 if name is not None:
180 metadata.name = cast_unicode(name)
175 metadata.name = cast_unicode(name)
181 if authors is not None:
176 if authors is not None:
182 metadata.authors = list(authors)
177 metadata.authors = list(authors)
183 if created is not None:
178 if created is not None:
184 metadata.created = cast_unicode(created)
179 metadata.created = cast_unicode(created)
185 if modified is not None:
180 if modified is not None:
186 metadata.modified = cast_unicode(modified)
181 metadata.modified = cast_unicode(modified)
187 if license is not None:
182 if license is not None:
188 metadata.license = cast_unicode(license)
183 metadata.license = cast_unicode(license)
189 if gistid is not None:
184 if gistid is not None:
190 metadata.gistid = cast_unicode(gistid)
185 metadata.gistid = cast_unicode(gistid)
191 return metadata
186 return metadata
192
187
193 def new_author(name=None, email=None, affiliation=None, url=None):
188 def new_author(name=None, email=None, affiliation=None, url=None):
194 """Create a new author."""
189 """Create a new author."""
195 author = NotebookNode()
190 author = NotebookNode()
196 if name is not None:
191 if name is not None:
197 author.name = cast_unicode(name)
192 author.name = cast_unicode(name)
198 if email is not None:
193 if email is not None:
199 author.email = cast_unicode(email)
194 author.email = cast_unicode(email)
200 if affiliation is not None:
195 if affiliation is not None:
201 author.affiliation = cast_unicode(affiliation)
196 author.affiliation = cast_unicode(affiliation)
202 if url is not None:
197 if url is not None:
203 author.url = cast_unicode(url)
198 author.url = cast_unicode(url)
204 return author
199 return author
205
200
@@ -1,363 +1,363
1 {
1 {
2 "$schema": "http://json-schema.org/draft-04/schema#",
2 "$schema": "http://json-schema.org/draft-04/schema#",
3 "description": "IPython Notebook v3.0 JSON schema.",
3 "description": "IPython Notebook v3.0 JSON schema.",
4 "type": "object",
4 "type": "object",
5 "additionalProperties": false,
5 "additionalProperties": false,
6 "required": ["metadata", "nbformat_minor", "nbformat", "worksheets"],
6 "required": ["metadata", "nbformat_minor", "nbformat", "worksheets"],
7 "properties": {
7 "properties": {
8 "metadata": {
8 "metadata": {
9 "description": "Notebook root-level metadata.",
9 "description": "Notebook root-level metadata.",
10 "type": "object",
10 "type": "object",
11 "additionalProperties": true,
11 "additionalProperties": true,
12 "properties": {
12 "properties": {
13 "kernel_info": {
13 "kernel_info": {
14 "description": "Kernel information.",
14 "description": "Kernel information.",
15 "type": "object",
15 "type": "object",
16 "required": ["name", "language"],
16 "required": ["name", "language"],
17 "properties": {
17 "properties": {
18 "name": {
18 "name": {
19 "description": "Name of the kernel specification.",
19 "description": "Name of the kernel specification.",
20 "type": "string"
20 "type": "string"
21 },
21 },
22 "language": {
22 "language": {
23 "description": "The programming language which this kernel runs.",
23 "description": "The programming language which this kernel runs.",
24 "type": "string"
24 "type": "string"
25 },
25 },
26 "codemirror_mode": {
26 "codemirror_mode": {
27 "description": "The codemirror mode to use for code in this language.",
27 "description": "The codemirror mode to use for code in this language.",
28 "type": "string"
28 "type": "string"
29 }
29 }
30 }
30 }
31 },
31 },
32 "signature": {
32 "signature": {
33 "description": "Hash of the notebook.",
33 "description": "Hash of the notebook.",
34 "type": "string"
34 "type": "string"
35 }
35 }
36 }
36 }
37 },
37 },
38 "nbformat_minor": {
38 "nbformat_minor": {
39 "description": "Notebook format (minor number). Incremented for backward compatible changes to the notebook format.",
39 "description": "Notebook format (minor number). Incremented for backward compatible changes to the notebook format.",
40 "type": "integer",
40 "type": "integer",
41 "minimum": 0
41 "minimum": 0
42 },
42 },
43 "nbformat": {
43 "nbformat": {
44 "description": "Notebook format (major number). Incremented between backwards incompatible changes to the notebook format.",
44 "description": "Notebook format (major number). Incremented between backwards incompatible changes to the notebook format.",
45 "type": "integer",
45 "type": "integer",
46 "minimum": 3,
46 "minimum": 3,
47 "maximum": 3
47 "maximum": 3
48 },
48 },
49 "orig_nbformat": {
49 "orig_nbformat": {
50 "description": "Original notebook format (major number) before converting the notebook between versions.",
50 "description": "Original notebook format (major number) before converting the notebook between versions.",
51 "type": "integer",
51 "type": "integer",
52 "minimum": 1
52 "minimum": 1
53 },
53 },
54 "worksheets" : {
54 "worksheets" : {
55 "description": "Array of worksheets",
55 "description": "Array of worksheets",
56 "type": "array",
56 "type": "array",
57 "items": {"$ref": "#/definitions/worksheet"}
57 "items": {"$ref": "#/definitions/worksheet"}
58 }
58 }
59 },
59 },
60
60
61 "definitions": {
61 "definitions": {
62 "worksheet": {
62 "worksheet": {
63 "additionalProperties": false,
63 "additionalProperties": false,
64 "required" : ["cells"],
64 "required" : ["cells"],
65 "properties":{
65 "properties":{
66 "cells": {
66 "cells": {
67 "description": "Array of cells of the current notebook.",
67 "description": "Array of cells of the current notebook.",
68 "type": "array",
68 "type": "array",
69 "items": {
69 "items": {
70 "type": "object",
70 "type": "object",
71 "oneOf": [
71 "oneOf": [
72 {"$ref": "#/definitions/raw_cell"},
72 {"$ref": "#/definitions/raw_cell"},
73 {"$ref": "#/definitions/markdown_cell"},
73 {"$ref": "#/definitions/markdown_cell"},
74 {"$ref": "#/definitions/heading_cell"},
74 {"$ref": "#/definitions/heading_cell"},
75 {"$ref": "#/definitions/code_cell"}
75 {"$ref": "#/definitions/code_cell"}
76 ]
76 ]
77 }
77 }
78 },
78 },
79 "metadata": {
79 "metadata": {
80 "type": "object",
80 "type": "object",
81 "description": "metadata of the current worksheet"
81 "description": "metadata of the current worksheet"
82 }
82 }
83 }
83 }
84 },
84 },
85 "raw_cell": {
85 "raw_cell": {
86 "description": "Notebook raw nbconvert cell.",
86 "description": "Notebook raw nbconvert cell.",
87 "type": "object",
87 "type": "object",
88 "additionalProperties": false,
88 "additionalProperties": false,
89 "required": ["cell_type", "source"],
89 "required": ["cell_type", "source"],
90 "properties": {
90 "properties": {
91 "cell_type": {
91 "cell_type": {
92 "description": "String identifying the type of cell.",
92 "description": "String identifying the type of cell.",
93 "enum": ["raw"]
93 "enum": ["raw"]
94 },
94 },
95 "metadata": {
95 "metadata": {
96 "description": "Cell-level metadata.",
96 "description": "Cell-level metadata.",
97 "type": "object",
97 "type": "object",
98 "additionalProperties": true,
98 "additionalProperties": true,
99 "properties": {
99 "properties": {
100 "format": {
100 "format": {
101 "description": "Raw cell metadata format for nbconvert.",
101 "description": "Raw cell metadata format for nbconvert.",
102 "type": "string"
102 "type": "string"
103 },
103 },
104 "name": {"$ref": "#/definitions/misc/metadata_name"},
104 "name": {"$ref": "#/definitions/misc/metadata_name"},
105 "tags": {"$ref": "#/definitions/misc/metadata_tags"}
105 "tags": {"$ref": "#/definitions/misc/metadata_tags"}
106 }
106 }
107 },
107 },
108 "source": {"$ref": "#/definitions/misc/source"}
108 "source": {"$ref": "#/definitions/misc/source"}
109 }
109 }
110 },
110 },
111
111
112 "markdown_cell": {
112 "markdown_cell": {
113 "description": "Notebook markdown cell.",
113 "description": "Notebook markdown cell.",
114 "type": "object",
114 "type": "object",
115 "additionalProperties": false,
115 "additionalProperties": false,
116 "required": ["cell_type", "source"],
116 "required": ["cell_type", "source"],
117 "properties": {
117 "properties": {
118 "cell_type": {
118 "cell_type": {
119 "description": "String identifying the type of cell.",
119 "description": "String identifying the type of cell.",
120 "enum": ["markdown"]
120 "enum": ["markdown", "html"]
121 },
121 },
122 "metadata": {
122 "metadata": {
123 "description": "Cell-level metadata.",
123 "description": "Cell-level metadata.",
124 "type": "object",
124 "type": "object",
125 "properties": {
125 "properties": {
126 "name": {"$ref": "#/definitions/misc/metadata_name"},
126 "name": {"$ref": "#/definitions/misc/metadata_name"},
127 "tags": {"$ref": "#/definitions/misc/metadata_tags"}
127 "tags": {"$ref": "#/definitions/misc/metadata_tags"}
128 },
128 },
129 "additionalProperties": true
129 "additionalProperties": true
130 },
130 },
131 "source": {"$ref": "#/definitions/misc/source"}
131 "source": {"$ref": "#/definitions/misc/source"}
132 }
132 }
133 },
133 },
134
134
135 "heading_cell": {
135 "heading_cell": {
136 "description": "Notebook heading cell.",
136 "description": "Notebook heading cell.",
137 "type": "object",
137 "type": "object",
138 "additionalProperties": false,
138 "additionalProperties": false,
139 "required": ["cell_type", "source", "level"],
139 "required": ["cell_type", "source", "level"],
140 "properties": {
140 "properties": {
141 "cell_type": {
141 "cell_type": {
142 "description": "String identifying the type of cell.",
142 "description": "String identifying the type of cell.",
143 "enum": ["heading"]
143 "enum": ["heading"]
144 },
144 },
145 "metadata": {
145 "metadata": {
146 "description": "Cell-level metadata.",
146 "description": "Cell-level metadata.",
147 "type": "object",
147 "type": "object",
148 "additionalProperties": true
148 "additionalProperties": true
149 },
149 },
150 "source": {"$ref": "#/definitions/misc/source"},
150 "source": {"$ref": "#/definitions/misc/source"},
151 "level": {
151 "level": {
152 "description": "Level of heading cells.",
152 "description": "Level of heading cells.",
153 "type": "integer",
153 "type": "integer",
154 "minimum": 1
154 "minimum": 1
155 }
155 }
156 }
156 }
157 },
157 },
158
158
159 "code_cell": {
159 "code_cell": {
160 "description": "Notebook code cell.",
160 "description": "Notebook code cell.",
161 "type": "object",
161 "type": "object",
162 "additionalProperties": false,
162 "additionalProperties": false,
163 "required": ["cell_type", "input", "outputs", "collapsed", "language"],
163 "required": ["cell_type", "input", "outputs", "collapsed", "language"],
164 "properties": {
164 "properties": {
165 "cell_type": {
165 "cell_type": {
166 "description": "String identifying the type of cell.",
166 "description": "String identifying the type of cell.",
167 "enum": ["code"]
167 "enum": ["code"]
168 },
168 },
169 "language": {
169 "language": {
170 "description": "The cell's language (always Python)",
170 "description": "The cell's language (always Python)",
171 "type": "string"
171 "type": "string"
172 },
172 },
173 "collapsed": {
173 "collapsed": {
174 "description": "Whether the cell is collapsed/expanded.",
174 "description": "Whether the cell is collapsed/expanded.",
175 "type": "boolean"
175 "type": "boolean"
176 },
176 },
177 "metadata": {
177 "metadata": {
178 "description": "Cell-level metadata.",
178 "description": "Cell-level metadata.",
179 "type": "object",
179 "type": "object",
180 "additionalProperties": true
180 "additionalProperties": true
181 },
181 },
182 "input": {"$ref": "#/definitions/misc/source"},
182 "input": {"$ref": "#/definitions/misc/source"},
183 "outputs": {
183 "outputs": {
184 "description": "Execution, display, or stream outputs.",
184 "description": "Execution, display, or stream outputs.",
185 "type": "array",
185 "type": "array",
186 "items": {"$ref": "#/definitions/output"}
186 "items": {"$ref": "#/definitions/output"}
187 },
187 },
188 "prompt_number": {
188 "prompt_number": {
189 "description": "The code cell's prompt number. Will be null if the cell has not been run.",
189 "description": "The code cell's prompt number. Will be null if the cell has not been run.",
190 "type": ["integer", "null"],
190 "type": ["integer", "null"],
191 "minimum": 0
191 "minimum": 0
192 }
192 }
193 }
193 }
194 },
194 },
195 "output": {
195 "output": {
196 "type": "object",
196 "type": "object",
197 "oneOf": [
197 "oneOf": [
198 {"$ref": "#/definitions/pyout"},
198 {"$ref": "#/definitions/pyout"},
199 {"$ref": "#/definitions/display_data"},
199 {"$ref": "#/definitions/display_data"},
200 {"$ref": "#/definitions/stream"},
200 {"$ref": "#/definitions/stream"},
201 {"$ref": "#/definitions/pyerr"}
201 {"$ref": "#/definitions/pyerr"}
202 ]
202 ]
203 },
203 },
204 "pyout": {
204 "pyout": {
205 "description": "Result of executing a code cell.",
205 "description": "Result of executing a code cell.",
206 "type": "object",
206 "type": "object",
207 "additionalProperties": false,
207 "additionalProperties": false,
208 "required": ["output_type", "prompt_number"],
208 "required": ["output_type", "prompt_number"],
209 "properties": {
209 "properties": {
210 "output_type": {
210 "output_type": {
211 "description": "Type of cell output.",
211 "description": "Type of cell output.",
212 "enum": ["pyout"]
212 "enum": ["pyout"]
213 },
213 },
214 "prompt_number": {
214 "prompt_number": {
215 "description": "A result's prompt number.",
215 "description": "A result's prompt number.",
216 "type": ["integer"],
216 "type": ["integer"],
217 "minimum": 0
217 "minimum": 0
218 },
218 },
219 "text": {"$ref": "#/definitions/misc/multiline_string"},
219 "text": {"$ref": "#/definitions/misc/multiline_string"},
220 "latex": {"$ref": "#/definitions/misc/multiline_string"},
220 "latex": {"$ref": "#/definitions/misc/multiline_string"},
221 "png": {"$ref": "#/definitions/misc/multiline_string"},
221 "png": {"$ref": "#/definitions/misc/multiline_string"},
222 "jpeg": {"$ref": "#/definitions/misc/multiline_string"},
222 "jpeg": {"$ref": "#/definitions/misc/multiline_string"},
223 "svg": {"$ref": "#/definitions/misc/multiline_string"},
223 "svg": {"$ref": "#/definitions/misc/multiline_string"},
224 "html": {"$ref": "#/definitions/misc/multiline_string"},
224 "html": {"$ref": "#/definitions/misc/multiline_string"},
225 "javascript": {"$ref": "#/definitions/misc/multiline_string"},
225 "javascript": {"$ref": "#/definitions/misc/multiline_string"},
226 "json": {"$ref": "#/definitions/misc/multiline_string"},
226 "json": {"$ref": "#/definitions/misc/multiline_string"},
227 "pdf": {"$ref": "#/definitions/misc/multiline_string"},
227 "pdf": {"$ref": "#/definitions/misc/multiline_string"},
228 "metadata": {"$ref": "#/definitions/misc/output_metadata"}
228 "metadata": {"$ref": "#/definitions/misc/output_metadata"}
229 },
229 },
230 "patternProperties": {
230 "patternProperties": {
231 "^[a-zA-Z0-9]+/[a-zA-Z0-9\\-\\+\\.]+$": {
231 "^[a-zA-Z0-9]+/[a-zA-Z0-9\\-\\+\\.]+$": {
232 "description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
232 "description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
233 "$ref": "#/definitions/misc/multiline_string"
233 "$ref": "#/definitions/misc/multiline_string"
234 }
234 }
235 }
235 }
236 },
236 },
237
237
238 "display_data": {
238 "display_data": {
239 "description": "Data displayed as a result of code cell execution.",
239 "description": "Data displayed as a result of code cell execution.",
240 "type": "object",
240 "type": "object",
241 "additionalProperties": false,
241 "additionalProperties": false,
242 "required": ["output_type"],
242 "required": ["output_type"],
243 "properties": {
243 "properties": {
244 "output_type": {
244 "output_type": {
245 "description": "Type of cell output.",
245 "description": "Type of cell output.",
246 "enum": ["display_data"]
246 "enum": ["display_data"]
247 },
247 },
248 "text": {"$ref": "#/definitions/misc/multiline_string"},
248 "text": {"$ref": "#/definitions/misc/multiline_string"},
249 "latex": {"$ref": "#/definitions/misc/multiline_string"},
249 "latex": {"$ref": "#/definitions/misc/multiline_string"},
250 "png": {"$ref": "#/definitions/misc/multiline_string"},
250 "png": {"$ref": "#/definitions/misc/multiline_string"},
251 "jpeg": {"$ref": "#/definitions/misc/multiline_string"},
251 "jpeg": {"$ref": "#/definitions/misc/multiline_string"},
252 "svg": {"$ref": "#/definitions/misc/multiline_string"},
252 "svg": {"$ref": "#/definitions/misc/multiline_string"},
253 "html": {"$ref": "#/definitions/misc/multiline_string"},
253 "html": {"$ref": "#/definitions/misc/multiline_string"},
254 "javascript": {"$ref": "#/definitions/misc/multiline_string"},
254 "javascript": {"$ref": "#/definitions/misc/multiline_string"},
255 "json": {"$ref": "#/definitions/misc/multiline_string"},
255 "json": {"$ref": "#/definitions/misc/multiline_string"},
256 "pdf": {"$ref": "#/definitions/misc/multiline_string"},
256 "pdf": {"$ref": "#/definitions/misc/multiline_string"},
257 "metadata": {"$ref": "#/definitions/misc/output_metadata"}
257 "metadata": {"$ref": "#/definitions/misc/output_metadata"}
258 },
258 },
259 "patternProperties": {
259 "patternProperties": {
260 "[a-zA-Z0-9]+/[a-zA-Z0-9\\-\\+\\.]+$": {
260 "[a-zA-Z0-9]+/[a-zA-Z0-9\\-\\+\\.]+$": {
261 "description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
261 "description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
262 "$ref": "#/definitions/misc/multiline_string"
262 "$ref": "#/definitions/misc/multiline_string"
263 }
263 }
264 }
264 }
265 },
265 },
266
266
267 "stream": {
267 "stream": {
268 "description": "Stream output from a code cell.",
268 "description": "Stream output from a code cell.",
269 "type": "object",
269 "type": "object",
270 "additionalProperties": false,
270 "additionalProperties": false,
271 "required": ["output_type", "stream", "text"],
271 "required": ["output_type", "stream", "text"],
272 "properties": {
272 "properties": {
273 "output_type": {
273 "output_type": {
274 "description": "Type of cell output.",
274 "description": "Type of cell output.",
275 "enum": ["stream"]
275 "enum": ["stream"]
276 },
276 },
277 "stream": {
277 "stream": {
278 "description": "The stream type/destination.",
278 "description": "The stream type/destination.",
279 "type": "string"
279 "type": "string"
280 },
280 },
281 "text": {
281 "text": {
282 "description": "The stream's text output, represented as an array of strings.",
282 "description": "The stream's text output, represented as an array of strings.",
283 "$ref": "#/definitions/misc/multiline_string"
283 "$ref": "#/definitions/misc/multiline_string"
284 }
284 }
285 }
285 }
286 },
286 },
287
287
288 "pyerr": {
288 "pyerr": {
289 "description": "Output of an error that occurred during code cell execution.",
289 "description": "Output of an error that occurred during code cell execution.",
290 "type": "object",
290 "type": "object",
291 "additionalProperties": false,
291 "additionalProperties": false,
292 "required": ["output_type", "ename", "evalue", "traceback"],
292 "required": ["output_type", "ename", "evalue", "traceback"],
293 "properties": {
293 "properties": {
294 "output_type": {
294 "output_type": {
295 "description": "Type of cell output.",
295 "description": "Type of cell output.",
296 "enum": ["pyerr"]
296 "enum": ["pyerr"]
297 },
297 },
298 "metadata": {"$ref": "#/definitions/misc/output_metadata"},
298 "metadata": {"$ref": "#/definitions/misc/output_metadata"},
299 "ename": {
299 "ename": {
300 "description": "The name of the error.",
300 "description": "The name of the error.",
301 "type": "string"
301 "type": "string"
302 },
302 },
303 "evalue": {
303 "evalue": {
304 "description": "The value, or message, of the error.",
304 "description": "The value, or message, of the error.",
305 "type": "string"
305 "type": "string"
306 },
306 },
307 "traceback": {
307 "traceback": {
308 "description": "The error's traceback, represented as an array of strings.",
308 "description": "The error's traceback, represented as an array of strings.",
309 "type": "array",
309 "type": "array",
310 "items": {"type": "string"}
310 "items": {"type": "string"}
311 }
311 }
312 }
312 }
313 },
313 },
314
314
315 "misc": {
315 "misc": {
316 "metadata_name": {
316 "metadata_name": {
317 "description": "The cell's name. If present, must be a non-empty string.",
317 "description": "The cell's name. If present, must be a non-empty string.",
318 "type": "string",
318 "type": "string",
319 "pattern": "^.+$"
319 "pattern": "^.+$"
320 },
320 },
321 "metadata_tags": {
321 "metadata_tags": {
322 "description": "The cell's tags. Tags must be unique, and must not contain commas.",
322 "description": "The cell's tags. Tags must be unique, and must not contain commas.",
323 "type": "array",
323 "type": "array",
324 "uniqueItems": true,
324 "uniqueItems": true,
325 "items": {
325 "items": {
326 "type": "string",
326 "type": "string",
327 "pattern": "^[^,]+$"
327 "pattern": "^[^,]+$"
328 }
328 }
329 },
329 },
330 "source": {
330 "source": {
331 "description": "Contents of the cell, represented as an array of lines.",
331 "description": "Contents of the cell, represented as an array of lines.",
332 "$ref": "#/definitions/misc/multiline_string"
332 "$ref": "#/definitions/misc/multiline_string"
333 },
333 },
334 "prompt_number": {
334 "prompt_number": {
335 "description": "The code cell's prompt number. Will be null if the cell has not been run.",
335 "description": "The code cell's prompt number. Will be null if the cell has not been run.",
336 "type": ["integer", "null"],
336 "type": ["integer", "null"],
337 "minimum": 0
337 "minimum": 0
338 },
338 },
339 "mimetype": {
339 "mimetype": {
340 "patternProperties": {
340 "patternProperties": {
341 "^[a-zA-Z0-9\\-\\+]+/[a-zA-Z0-9\\-\\+]+": {
341 "^[a-zA-Z0-9\\-\\+]+/[a-zA-Z0-9\\-\\+]+": {
342 "description": "The cell's mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
342 "description": "The cell's mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
343 "$ref": "#/definitions/misc/multiline_string"
343 "$ref": "#/definitions/misc/multiline_string"
344 }
344 }
345 }
345 }
346 },
346 },
347 "output_metadata": {
347 "output_metadata": {
348 "description": "Cell output metadata.",
348 "description": "Cell output metadata.",
349 "type": "object",
349 "type": "object",
350 "additionalProperties": true
350 "additionalProperties": true
351 },
351 },
352 "multiline_string": {
352 "multiline_string": {
353 "oneOf" : [
353 "oneOf" : [
354 {"type": "string"},
354 {"type": "string"},
355 {
355 {
356 "type": "array",
356 "type": "array",
357 "items": {"type": "string"}
357 "items": {"type": "string"}
358 }
358 }
359 ]
359 ]
360 }
360 }
361 }
361 }
362 }
362 }
363 }
363 }
@@ -1,154 +1,152
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 import os
3 import os
4 from base64 import encodestring
4 from base64 import encodestring
5
5
6 from ..nbbase import (
6 from ..nbbase import (
7 NotebookNode,
7 NotebookNode,
8 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
8 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
9 new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor
9 new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor
10 )
10 )
11
11
12 # some random base64-encoded *text*
12 # some random base64-encoded *text*
13 png = encodestring(os.urandom(5)).decode('ascii')
13 png = encodestring(os.urandom(5)).decode('ascii')
14 jpeg = encodestring(os.urandom(6)).decode('ascii')
14 jpeg = encodestring(os.urandom(6)).decode('ascii')
15
15
16 ws = new_worksheet(name='worksheet1')
16 ws = new_worksheet()
17
17
18 ws.cells.append(new_text_cell(
18 ws.cells.append(new_text_cell(
19 u'html',
19 u'html',
20 source='Some NumPy Examples',
20 source='Some NumPy Examples',
21 rendered='Some NumPy Examples'
22 ))
21 ))
23
22
24
23
25 ws.cells.append(new_code_cell(
24 ws.cells.append(new_code_cell(
26 input='import numpy',
25 input='import numpy',
27 prompt_number=1,
26 prompt_number=1,
28 collapsed=False
27 collapsed=False
29 ))
28 ))
30
29
31 ws.cells.append(new_text_cell(
30 ws.cells.append(new_text_cell(
32 u'markdown',
31 u'markdown',
33 source='A random array',
32 source='A random array',
34 rendered='A random array'
35 ))
33 ))
36
34
37 ws.cells.append(new_text_cell(
35 ws.cells.append(new_text_cell(
38 u'raw',
36 u'raw',
39 source='A random array',
37 source='A random array',
40 ))
38 ))
41
39
42 ws.cells.append(new_heading_cell(
40 ws.cells.append(new_heading_cell(
43 u'My Heading',
41 u'My Heading',
44 level=2
42 level=2
45 ))
43 ))
46
44
47 ws.cells.append(new_code_cell(
45 ws.cells.append(new_code_cell(
48 input='a = numpy.random.rand(100)',
46 input='a = numpy.random.rand(100)',
49 prompt_number=2,
47 prompt_number=2,
50 collapsed=True
48 collapsed=True
51 ))
49 ))
52 ws.cells.append(new_code_cell(
50 ws.cells.append(new_code_cell(
53 input='a = 10\nb = 5\n',
51 input='a = 10\nb = 5\n',
54 prompt_number=3,
52 prompt_number=3,
55 ))
53 ))
56 ws.cells.append(new_code_cell(
54 ws.cells.append(new_code_cell(
57 input='a = 10\nb = 5',
55 input='a = 10\nb = 5',
58 prompt_number=4,
56 prompt_number=4,
59 ))
57 ))
60
58
61 ws.cells.append(new_code_cell(
59 ws.cells.append(new_code_cell(
62 input=u'print "ünîcødé"',
60 input=u'print "ünîcødé"',
63 prompt_number=3,
61 prompt_number=3,
64 collapsed=False,
62 collapsed=False,
65 outputs=[new_output(
63 outputs=[new_output(
66 output_type=u'pyout',
64 output_type=u'pyout',
67 output_text=u'<array a>',
65 output_text=u'<array a>',
68 output_html=u'The HTML rep',
66 output_html=u'The HTML rep',
69 output_latex=u'$a$',
67 output_latex=u'$a$',
70 output_png=png,
68 output_png=png,
71 output_jpeg=jpeg,
69 output_jpeg=jpeg,
72 output_svg=u'<svg>',
70 output_svg=u'<svg>',
73 output_json=u'json data',
71 output_json=u'{"json": "data"}',
74 output_javascript=u'var i=0;',
72 output_javascript=u'var i=0;',
75 prompt_number=3
73 prompt_number=3
76 ),new_output(
74 ),new_output(
77 output_type=u'display_data',
75 output_type=u'display_data',
78 output_text=u'<array a>',
76 output_text=u'<array a>',
79 output_html=u'The HTML rep',
77 output_html=u'The HTML rep',
80 output_latex=u'$a$',
78 output_latex=u'$a$',
81 output_png=png,
79 output_png=png,
82 output_jpeg=jpeg,
80 output_jpeg=jpeg,
83 output_svg=u'<svg>',
81 output_svg=u'<svg>',
84 output_json=u'json data',
82 output_json=u'{"json": "data"}',
85 output_javascript=u'var i=0;'
83 output_javascript=u'var i=0;'
86 ),new_output(
84 ),new_output(
87 output_type=u'pyerr',
85 output_type=u'pyerr',
88 ename=u'NameError',
86 ename=u'NameError',
89 evalue=u'NameError was here',
87 evalue=u'NameError was here',
90 traceback=[u'frame 0', u'frame 1', u'frame 2']
88 traceback=[u'frame 0', u'frame 1', u'frame 2']
91 ),new_output(
89 ),new_output(
92 output_type=u'stream',
90 output_type=u'stream',
93 output_text='foo\rbar\r\n'
91 output_text='foo\rbar\r\n'
94 ),new_output(
92 ),new_output(
95 output_type=u'stream',
93 output_type=u'stream',
96 stream='stderr',
94 stream='stderr',
97 output_text='\rfoo\rbar\n'
95 output_text='\rfoo\rbar\n'
98 )]
96 )]
99 ))
97 ))
100
98
101 authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com',
99 authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com',
102 affiliation=u'Fox',url=u'http://www.fox.com')]
100 affiliation=u'Fox',url=u'http://www.fox.com')]
103 md = new_metadata(name=u'My Notebook',license=u'BSD',created=u'8601_goes_here',
101 md = new_metadata(name=u'My Notebook',license=u'BSD',created=u'8601_goes_here',
104 modified=u'8601_goes_here',gistid=u'21341231',authors=authors)
102 modified=u'8601_goes_here',gistid=u'21341231',authors=authors)
105
103
106 nb0 = new_notebook(
104 nb0 = new_notebook(
107 worksheets=[ws, new_worksheet(name='worksheet2')],
105 worksheets=[ws, new_worksheet()],
108 metadata=md
106 metadata=md
109 )
107 )
110
108
111 nb0_py = u"""# -*- coding: utf-8 -*-
109 nb0_py = u"""# -*- coding: utf-8 -*-
112 # <nbformat>%i.%i</nbformat>
110 # <nbformat>%i.%i</nbformat>
113
111
114 # <htmlcell>
112 # <htmlcell>
115
113
116 # Some NumPy Examples
114 # Some NumPy Examples
117
115
118 # <codecell>
116 # <codecell>
119
117
120 import numpy
118 import numpy
121
119
122 # <markdowncell>
120 # <markdowncell>
123
121
124 # A random array
122 # A random array
125
123
126 # <rawcell>
124 # <rawcell>
127
125
128 # A random array
126 # A random array
129
127
130 # <headingcell level=2>
128 # <headingcell level=2>
131
129
132 # My Heading
130 # My Heading
133
131
134 # <codecell>
132 # <codecell>
135
133
136 a = numpy.random.rand(100)
134 a = numpy.random.rand(100)
137
135
138 # <codecell>
136 # <codecell>
139
137
140 a = 10
138 a = 10
141 b = 5
139 b = 5
142
140
143 # <codecell>
141 # <codecell>
144
142
145 a = 10
143 a = 10
146 b = 5
144 b = 5
147
145
148 # <codecell>
146 # <codecell>
149
147
150 print "ünîcødé"
148 print "ünîcødé"
151
149
152 """ % (nbformat, nbformat_minor)
150 """ % (nbformat, nbformat_minor)
153
151
154
152
@@ -1,157 +1,148
1 from unittest import TestCase
1 from unittest import TestCase
2
2
3 from ..nbbase import (
3 from ..nbbase import (
4 NotebookNode,
4 NotebookNode,
5 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
5 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
6 new_author, new_metadata, new_heading_cell, nbformat
6 new_author, new_metadata, new_heading_cell, nbformat
7 )
7 )
8
8
9 class TestCell(TestCase):
9 class TestCell(TestCase):
10
10
11 def test_empty_code_cell(self):
11 def test_empty_code_cell(self):
12 cc = new_code_cell()
12 cc = new_code_cell()
13 self.assertEqual(cc.cell_type,u'code')
13 self.assertEqual(cc.cell_type,u'code')
14 self.assertEqual(u'input' not in cc, True)
14 self.assertEqual(u'input' not in cc, True)
15 self.assertEqual(u'prompt_number' not in cc, True)
15 self.assertEqual(u'prompt_number' not in cc, True)
16 self.assertEqual(cc.outputs, [])
16 self.assertEqual(cc.outputs, [])
17 self.assertEqual(cc.collapsed, False)
17 self.assertEqual(cc.collapsed, False)
18
18
19 def test_code_cell(self):
19 def test_code_cell(self):
20 cc = new_code_cell(input='a=10', prompt_number=0, collapsed=True)
20 cc = new_code_cell(input='a=10', prompt_number=0, collapsed=True)
21 cc.outputs = [new_output(output_type=u'pyout',
21 cc.outputs = [new_output(output_type=u'pyout',
22 output_svg=u'foo',output_text=u'10',prompt_number=0)]
22 output_svg=u'foo',output_text=u'10',prompt_number=0)]
23 self.assertEqual(cc.input, u'a=10')
23 self.assertEqual(cc.input, u'a=10')
24 self.assertEqual(cc.prompt_number, 0)
24 self.assertEqual(cc.prompt_number, 0)
25 self.assertEqual(cc.language, u'python')
25 self.assertEqual(cc.language, u'python')
26 self.assertEqual(cc.outputs[0].svg, u'foo')
26 self.assertEqual(cc.outputs[0].svg, u'foo')
27 self.assertEqual(cc.outputs[0].text, u'10')
27 self.assertEqual(cc.outputs[0].text, u'10')
28 self.assertEqual(cc.outputs[0].prompt_number, 0)
28 self.assertEqual(cc.outputs[0].prompt_number, 0)
29 self.assertEqual(cc.collapsed, True)
29 self.assertEqual(cc.collapsed, True)
30
30
31 def test_pyerr(self):
31 def test_pyerr(self):
32 o = new_output(output_type=u'pyerr', ename=u'NameError',
32 o = new_output(output_type=u'pyerr', ename=u'NameError',
33 evalue=u'Name not found', traceback=[u'frame 0', u'frame 1', u'frame 2']
33 evalue=u'Name not found', traceback=[u'frame 0', u'frame 1', u'frame 2']
34 )
34 )
35 self.assertEqual(o.output_type, u'pyerr')
35 self.assertEqual(o.output_type, u'pyerr')
36 self.assertEqual(o.ename, u'NameError')
36 self.assertEqual(o.ename, u'NameError')
37 self.assertEqual(o.evalue, u'Name not found')
37 self.assertEqual(o.evalue, u'Name not found')
38 self.assertEqual(o.traceback, [u'frame 0', u'frame 1', u'frame 2'])
38 self.assertEqual(o.traceback, [u'frame 0', u'frame 1', u'frame 2'])
39
39
40 def test_empty_html_cell(self):
40 def test_empty_html_cell(self):
41 tc = new_text_cell(u'html')
41 tc = new_text_cell(u'html')
42 self.assertEqual(tc.cell_type, u'html')
42 self.assertEqual(tc.cell_type, u'html')
43 self.assertEqual(u'source' not in tc, True)
43 self.assertEqual(u'source' not in tc, True)
44 self.assertEqual(u'rendered' not in tc, True)
45
44
46 def test_html_cell(self):
45 def test_html_cell(self):
47 tc = new_text_cell(u'html', 'hi', 'hi')
46 tc = new_text_cell(u'html', 'hi')
48 self.assertEqual(tc.source, u'hi')
47 self.assertEqual(tc.source, u'hi')
49 self.assertEqual(tc.rendered, u'hi')
50
48
51 def test_empty_markdown_cell(self):
49 def test_empty_markdown_cell(self):
52 tc = new_text_cell(u'markdown')
50 tc = new_text_cell(u'markdown')
53 self.assertEqual(tc.cell_type, u'markdown')
51 self.assertEqual(tc.cell_type, u'markdown')
54 self.assertEqual(u'source' not in tc, True)
52 self.assertEqual(u'source' not in tc, True)
55 self.assertEqual(u'rendered' not in tc, True)
56
53
57 def test_markdown_cell(self):
54 def test_markdown_cell(self):
58 tc = new_text_cell(u'markdown', 'hi', 'hi')
55 tc = new_text_cell(u'markdown', 'hi')
59 self.assertEqual(tc.source, u'hi')
56 self.assertEqual(tc.source, u'hi')
60 self.assertEqual(tc.rendered, u'hi')
61
57
62 def test_empty_raw_cell(self):
58 def test_empty_raw_cell(self):
63 tc = new_text_cell(u'raw')
59 tc = new_text_cell(u'raw')
64 self.assertEqual(tc.cell_type, u'raw')
60 self.assertEqual(tc.cell_type, u'raw')
65 self.assertEqual(u'source' not in tc, True)
61 self.assertEqual(u'source' not in tc, True)
66 self.assertEqual(u'rendered' not in tc, True)
67
62
68 def test_raw_cell(self):
63 def test_raw_cell(self):
69 tc = new_text_cell(u'raw', 'hi', 'hi')
64 tc = new_text_cell(u'raw', 'hi')
70 self.assertEqual(tc.source, u'hi')
65 self.assertEqual(tc.source, u'hi')
71 self.assertEqual(tc.rendered, u'hi')
72
66
73 def test_empty_heading_cell(self):
67 def test_empty_heading_cell(self):
74 tc = new_heading_cell()
68 tc = new_heading_cell()
75 self.assertEqual(tc.cell_type, u'heading')
69 self.assertEqual(tc.cell_type, u'heading')
76 self.assertEqual(u'source' not in tc, True)
70 self.assertEqual(u'source' not in tc, True)
77 self.assertEqual(u'rendered' not in tc, True)
78
71
79 def test_heading_cell(self):
72 def test_heading_cell(self):
80 tc = new_heading_cell(u'hi', u'hi', level=2)
73 tc = new_heading_cell(u'hi', level=2)
81 self.assertEqual(tc.source, u'hi')
74 self.assertEqual(tc.source, u'hi')
82 self.assertEqual(tc.rendered, u'hi')
83 self.assertEqual(tc.level, 2)
75 self.assertEqual(tc.level, 2)
84
76
85
77
86 class TestWorksheet(TestCase):
78 class TestWorksheet(TestCase):
87
79
88 def test_empty_worksheet(self):
80 def test_empty_worksheet(self):
89 ws = new_worksheet()
81 ws = new_worksheet()
90 self.assertEqual(ws.cells,[])
82 self.assertEqual(ws.cells,[])
91 self.assertEqual(u'name' not in ws, True)
83 self.assertEqual(u'name' not in ws, True)
92
84
93 def test_worksheet(self):
85 def test_worksheet(self):
94 cells = [new_code_cell(), new_text_cell(u'html')]
86 cells = [new_code_cell(), new_text_cell(u'html')]
95 ws = new_worksheet(cells=cells,name=u'foo')
87 ws = new_worksheet(cells=cells)
96 self.assertEqual(ws.cells,cells)
88 self.assertEqual(ws.cells,cells)
97 self.assertEqual(ws.name,u'foo')
98
89
99 class TestNotebook(TestCase):
90 class TestNotebook(TestCase):
100
91
101 def test_empty_notebook(self):
92 def test_empty_notebook(self):
102 nb = new_notebook()
93 nb = new_notebook()
103 self.assertEqual(nb.worksheets, [])
94 self.assertEqual(nb.worksheets, [])
104 self.assertEqual(nb.metadata, NotebookNode())
95 self.assertEqual(nb.metadata, NotebookNode())
105 self.assertEqual(nb.nbformat,nbformat)
96 self.assertEqual(nb.nbformat,nbformat)
106
97
107 def test_notebook(self):
98 def test_notebook(self):
108 worksheets = [new_worksheet(),new_worksheet()]
99 worksheets = [new_worksheet(),new_worksheet()]
109 metadata = new_metadata(name=u'foo')
100 metadata = new_metadata(name=u'foo')
110 nb = new_notebook(metadata=metadata,worksheets=worksheets)
101 nb = new_notebook(metadata=metadata,worksheets=worksheets)
111 self.assertEqual(nb.metadata.name,u'foo')
102 self.assertEqual(nb.metadata.name,u'foo')
112 self.assertEqual(nb.worksheets,worksheets)
103 self.assertEqual(nb.worksheets,worksheets)
113 self.assertEqual(nb.nbformat,nbformat)
104 self.assertEqual(nb.nbformat,nbformat)
114
105
115 def test_notebook_name(self):
106 def test_notebook_name(self):
116 worksheets = [new_worksheet(),new_worksheet()]
107 worksheets = [new_worksheet(),new_worksheet()]
117 nb = new_notebook(name='foo',worksheets=worksheets)
108 nb = new_notebook(name='foo',worksheets=worksheets)
118 self.assertEqual(nb.metadata.name,u'foo')
109 self.assertEqual(nb.metadata.name,u'foo')
119 self.assertEqual(nb.worksheets,worksheets)
110 self.assertEqual(nb.worksheets,worksheets)
120 self.assertEqual(nb.nbformat,nbformat)
111 self.assertEqual(nb.nbformat,nbformat)
121
112
122 class TestMetadata(TestCase):
113 class TestMetadata(TestCase):
123
114
124 def test_empty_metadata(self):
115 def test_empty_metadata(self):
125 md = new_metadata()
116 md = new_metadata()
126 self.assertEqual(u'name' not in md, True)
117 self.assertEqual(u'name' not in md, True)
127 self.assertEqual(u'authors' not in md, True)
118 self.assertEqual(u'authors' not in md, True)
128 self.assertEqual(u'license' not in md, True)
119 self.assertEqual(u'license' not in md, True)
129 self.assertEqual(u'saved' not in md, True)
120 self.assertEqual(u'saved' not in md, True)
130 self.assertEqual(u'modified' not in md, True)
121 self.assertEqual(u'modified' not in md, True)
131 self.assertEqual(u'gistid' not in md, True)
122 self.assertEqual(u'gistid' not in md, True)
132
123
133 def test_metadata(self):
124 def test_metadata(self):
134 authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com')]
125 authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com')]
135 md = new_metadata(name=u'foo',license=u'BSD',created=u'today',
126 md = new_metadata(name=u'foo',license=u'BSD',created=u'today',
136 modified=u'now',gistid=u'21341231',authors=authors)
127 modified=u'now',gistid=u'21341231',authors=authors)
137 self.assertEqual(md.name, u'foo')
128 self.assertEqual(md.name, u'foo')
138 self.assertEqual(md.license, u'BSD')
129 self.assertEqual(md.license, u'BSD')
139 self.assertEqual(md.created, u'today')
130 self.assertEqual(md.created, u'today')
140 self.assertEqual(md.modified, u'now')
131 self.assertEqual(md.modified, u'now')
141 self.assertEqual(md.gistid, u'21341231')
132 self.assertEqual(md.gistid, u'21341231')
142 self.assertEqual(md.authors, authors)
133 self.assertEqual(md.authors, authors)
143
134
144 class TestOutputs(TestCase):
135 class TestOutputs(TestCase):
145 def test_binary_png(self):
136 def test_binary_png(self):
146 out = new_output(output_png=b'\x89PNG\r\n\x1a\n', output_type='display_data')
137 out = new_output(output_png=b'\x89PNG\r\n\x1a\n', output_type='display_data')
147
138
148 def test_b64b6tes_png(self):
139 def test_b64b6tes_png(self):
149 out = new_output(output_png=b'iVBORw0KG', output_type='display_data')
140 out = new_output(output_png=b'iVBORw0KG', output_type='display_data')
150
141
151 def test_binary_jpeg(self):
142 def test_binary_jpeg(self):
152 out = new_output(output_jpeg=b'\xff\xd8', output_type='display_data')
143 out = new_output(output_jpeg=b'\xff\xd8', output_type='display_data')
153
144
154 def test_b64b6tes_jpeg(self):
145 def test_b64b6tes_jpeg(self):
155 out = new_output(output_jpeg=b'/9', output_type='display_data')
146 out = new_output(output_jpeg=b'/9', output_type='display_data')
156
147
157
148
General Comments 0
You need to be logged in to leave comments. Login now