##// END OF EJS Templates
Remove unused os import
Jessica B. Hamrick -
Show More
@@ -1,206 +1,205 b''
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 import os
15
14
16 from IPython.utils.ipstruct import Struct
15 from IPython.utils.ipstruct import Struct
17 from IPython.utils.py3compat import cast_unicode, unicode_type
16 from IPython.utils.py3compat import cast_unicode, unicode_type
18
17
19 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
20 # Code
19 # Code
21 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
22
21
23 # Change this when incrementing the nbformat version
22 # Change this when incrementing the nbformat version
24 nbformat = 3
23 nbformat = 3
25 nbformat_minor = 0
24 nbformat_minor = 0
26 nbformat_schema = 'v3.withref.json'
25 nbformat_schema = 'v3.withref.json'
27
26
28 class NotebookNode(Struct):
27 class NotebookNode(Struct):
29 pass
28 pass
30
29
31
30
32 def from_dict(d):
31 def from_dict(d):
33 if isinstance(d, dict):
32 if isinstance(d, dict):
34 newd = NotebookNode()
33 newd = NotebookNode()
35 for k,v in d.items():
34 for k,v in d.items():
36 newd[k] = from_dict(v)
35 newd[k] = from_dict(v)
37 return newd
36 return newd
38 elif isinstance(d, (tuple, list)):
37 elif isinstance(d, (tuple, list)):
39 return [from_dict(i) for i in d]
38 return [from_dict(i) for i in d]
40 else:
39 else:
41 return d
40 return d
42
41
43
42
44 def new_output(output_type, output_text=None, output_png=None,
43 def new_output(output_type, output_text=None, output_png=None,
45 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,
46 output_javascript=None, output_jpeg=None, prompt_number=None,
45 output_javascript=None, output_jpeg=None, prompt_number=None,
47 ename=None, evalue=None, traceback=None, stream=None, metadata=None):
46 ename=None, evalue=None, traceback=None, stream=None, metadata=None):
48 """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.
49 """
48 """
50 output = NotebookNode()
49 output = NotebookNode()
51 output.output_type = unicode_type(output_type)
50 output.output_type = unicode_type(output_type)
52
51
53 if metadata is None:
52 if metadata is None:
54 metadata = {}
53 metadata = {}
55 if not isinstance(metadata, dict):
54 if not isinstance(metadata, dict):
56 raise TypeError("metadata must be dict")
55 raise TypeError("metadata must be dict")
57 output.metadata = metadata
56 output.metadata = metadata
58
57
59 if output_type != 'pyerr':
58 if output_type != 'pyerr':
60 if output_text is not None:
59 if output_text is not None:
61 output.text = cast_unicode(output_text)
60 output.text = cast_unicode(output_text)
62 if output_png is not None:
61 if output_png is not None:
63 output.png = cast_unicode(output_png)
62 output.png = cast_unicode(output_png)
64 if output_jpeg is not None:
63 if output_jpeg is not None:
65 output.jpeg = cast_unicode(output_jpeg)
64 output.jpeg = cast_unicode(output_jpeg)
66 if output_html is not None:
65 if output_html is not None:
67 output.html = cast_unicode(output_html)
66 output.html = cast_unicode(output_html)
68 if output_svg is not None:
67 if output_svg is not None:
69 output.svg = cast_unicode(output_svg)
68 output.svg = cast_unicode(output_svg)
70 if output_latex is not None:
69 if output_latex is not None:
71 output.latex = cast_unicode(output_latex)
70 output.latex = cast_unicode(output_latex)
72 if output_json is not None:
71 if output_json is not None:
73 output.json = cast_unicode(output_json)
72 output.json = cast_unicode(output_json)
74 if output_javascript is not None:
73 if output_javascript is not None:
75 output.javascript = cast_unicode(output_javascript)
74 output.javascript = cast_unicode(output_javascript)
76
75
77 if output_type == u'pyout':
76 if output_type == u'pyout':
78 if prompt_number is not None:
77 if prompt_number is not None:
79 output.prompt_number = int(prompt_number)
78 output.prompt_number = int(prompt_number)
80
79
81 if output_type == u'pyerr':
80 if output_type == u'pyerr':
82 if ename is not None:
81 if ename is not None:
83 output.ename = cast_unicode(ename)
82 output.ename = cast_unicode(ename)
84 if evalue is not None:
83 if evalue is not None:
85 output.evalue = cast_unicode(evalue)
84 output.evalue = cast_unicode(evalue)
86 if traceback is not None:
85 if traceback is not None:
87 output.traceback = [cast_unicode(frame) for frame in list(traceback)]
86 output.traceback = [cast_unicode(frame) for frame in list(traceback)]
88
87
89 if output_type == u'stream':
88 if output_type == u'stream':
90 output.stream = 'stdout' if stream is None else cast_unicode(stream)
89 output.stream = 'stdout' if stream is None else cast_unicode(stream)
91
90
92 return output
91 return output
93
92
94
93
95 def new_code_cell(input=None, prompt_number=None, outputs=None,
94 def new_code_cell(input=None, prompt_number=None, outputs=None,
96 language=u'python', collapsed=False, metadata=None):
95 language=u'python', collapsed=False, metadata=None):
97 """Create a new code cell with input and output"""
96 """Create a new code cell with input and output"""
98 cell = NotebookNode()
97 cell = NotebookNode()
99 cell.cell_type = u'code'
98 cell.cell_type = u'code'
100 if language is not None:
99 if language is not None:
101 cell.language = cast_unicode(language)
100 cell.language = cast_unicode(language)
102 if input is not None:
101 if input is not None:
103 cell.input = cast_unicode(input)
102 cell.input = cast_unicode(input)
104 if prompt_number is not None:
103 if prompt_number is not None:
105 cell.prompt_number = int(prompt_number)
104 cell.prompt_number = int(prompt_number)
106 if outputs is None:
105 if outputs is None:
107 cell.outputs = []
106 cell.outputs = []
108 else:
107 else:
109 cell.outputs = outputs
108 cell.outputs = outputs
110 if collapsed is not None:
109 if collapsed is not None:
111 cell.collapsed = bool(collapsed)
110 cell.collapsed = bool(collapsed)
112 cell.metadata = NotebookNode(metadata or {})
111 cell.metadata = NotebookNode(metadata or {})
113
112
114 return cell
113 return cell
115
114
116 def new_text_cell(cell_type, source=None, rendered=None, metadata=None):
115 def new_text_cell(cell_type, source=None, rendered=None, metadata=None):
117 """Create a new text cell."""
116 """Create a new text cell."""
118 cell = NotebookNode()
117 cell = NotebookNode()
119 # VERSIONHACK: plaintext -> raw
118 # VERSIONHACK: plaintext -> raw
120 # handle never-released plaintext name for raw cells
119 # handle never-released plaintext name for raw cells
121 if cell_type == 'plaintext':
120 if cell_type == 'plaintext':
122 cell_type = 'raw'
121 cell_type = 'raw'
123 if source is not None:
122 if source is not None:
124 cell.source = cast_unicode(source)
123 cell.source = cast_unicode(source)
125 if rendered is not None:
124 if rendered is not None:
126 cell.rendered = cast_unicode(rendered)
125 cell.rendered = cast_unicode(rendered)
127 cell.metadata = NotebookNode(metadata or {})
126 cell.metadata = NotebookNode(metadata or {})
128 cell.cell_type = cell_type
127 cell.cell_type = cell_type
129 return cell
128 return cell
130
129
131
130
132 def new_heading_cell(source=None, rendered=None, level=1, metadata=None):
131 def new_heading_cell(source=None, rendered=None, level=1, metadata=None):
133 """Create a new section cell with a given integer level."""
132 """Create a new section cell with a given integer level."""
134 cell = NotebookNode()
133 cell = NotebookNode()
135 cell.cell_type = u'heading'
134 cell.cell_type = u'heading'
136 if source is not None:
135 if source is not None:
137 cell.source = cast_unicode(source)
136 cell.source = cast_unicode(source)
138 if rendered is not None:
137 if rendered is not None:
139 cell.rendered = cast_unicode(rendered)
138 cell.rendered = cast_unicode(rendered)
140 cell.level = int(level)
139 cell.level = int(level)
141 cell.metadata = NotebookNode(metadata or {})
140 cell.metadata = NotebookNode(metadata or {})
142 return cell
141 return cell
143
142
144
143
145 def new_worksheet(name=None, cells=None, metadata=None):
144 def new_worksheet(name=None, cells=None, metadata=None):
146 """Create a worksheet by name with with a list of cells."""
145 """Create a worksheet by name with with a list of cells."""
147 ws = NotebookNode()
146 ws = NotebookNode()
148 if name is not None:
147 if name is not None:
149 ws.name = cast_unicode(name)
148 ws.name = cast_unicode(name)
150 if cells is None:
149 if cells is None:
151 ws.cells = []
150 ws.cells = []
152 else:
151 else:
153 ws.cells = list(cells)
152 ws.cells = list(cells)
154 ws.metadata = NotebookNode(metadata or {})
153 ws.metadata = NotebookNode(metadata or {})
155 return ws
154 return ws
156
155
157
156
158 def new_notebook(name=None, metadata=None, worksheets=None):
157 def new_notebook(name=None, metadata=None, worksheets=None):
159 """Create a notebook by name, id and a list of worksheets."""
158 """Create a notebook by name, id and a list of worksheets."""
160 nb = NotebookNode()
159 nb = NotebookNode()
161 nb.nbformat = nbformat
160 nb.nbformat = nbformat
162 nb.nbformat_minor = nbformat_minor
161 nb.nbformat_minor = nbformat_minor
163 if worksheets is None:
162 if worksheets is None:
164 nb.worksheets = []
163 nb.worksheets = []
165 else:
164 else:
166 nb.worksheets = list(worksheets)
165 nb.worksheets = list(worksheets)
167 if metadata is None:
166 if metadata is None:
168 nb.metadata = new_metadata()
167 nb.metadata = new_metadata()
169 else:
168 else:
170 nb.metadata = NotebookNode(metadata)
169 nb.metadata = NotebookNode(metadata)
171 if name is not None:
170 if name is not None:
172 nb.metadata.name = cast_unicode(name)
171 nb.metadata.name = cast_unicode(name)
173 return nb
172 return nb
174
173
175
174
176 def new_metadata(name=None, authors=None, license=None, created=None,
175 def new_metadata(name=None, authors=None, license=None, created=None,
177 modified=None, gistid=None):
176 modified=None, gistid=None):
178 """Create a new metadata node."""
177 """Create a new metadata node."""
179 metadata = NotebookNode()
178 metadata = NotebookNode()
180 if name is not None:
179 if name is not None:
181 metadata.name = cast_unicode(name)
180 metadata.name = cast_unicode(name)
182 if authors is not None:
181 if authors is not None:
183 metadata.authors = list(authors)
182 metadata.authors = list(authors)
184 if created is not None:
183 if created is not None:
185 metadata.created = cast_unicode(created)
184 metadata.created = cast_unicode(created)
186 if modified is not None:
185 if modified is not None:
187 metadata.modified = cast_unicode(modified)
186 metadata.modified = cast_unicode(modified)
188 if license is not None:
187 if license is not None:
189 metadata.license = cast_unicode(license)
188 metadata.license = cast_unicode(license)
190 if gistid is not None:
189 if gistid is not None:
191 metadata.gistid = cast_unicode(gistid)
190 metadata.gistid = cast_unicode(gistid)
192 return metadata
191 return metadata
193
192
194 def new_author(name=None, email=None, affiliation=None, url=None):
193 def new_author(name=None, email=None, affiliation=None, url=None):
195 """Create a new author."""
194 """Create a new author."""
196 author = NotebookNode()
195 author = NotebookNode()
197 if name is not None:
196 if name is not None:
198 author.name = cast_unicode(name)
197 author.name = cast_unicode(name)
199 if email is not None:
198 if email is not None:
200 author.email = cast_unicode(email)
199 author.email = cast_unicode(email)
201 if affiliation is not None:
200 if affiliation is not None:
202 author.affiliation = cast_unicode(affiliation)
201 author.affiliation = cast_unicode(affiliation)
203 if url is not None:
202 if url is not None:
204 author.url = cast_unicode(url)
203 author.url = cast_unicode(url)
205 return author
204 return author
206
205
General Comments 0
You need to be logged in to leave comments. Login now