Show More
@@ -1,205 +1,206 b'' | |||||
1 | """The official API for working with notebooks in the current format version. |
|
1 | """The official API for working with notebooks in the current format version. | |
2 |
|
2 | |||
3 | Authors: |
|
3 | Authors: | |
4 |
|
4 | |||
5 | * Brian Granger |
|
5 | * Brian Granger | |
6 | """ |
|
6 | """ | |
7 |
|
7 | |||
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 | # Copyright (C) 2008-2011 The IPython Development Team |
|
9 | # Copyright (C) 2008-2011 The IPython Development Team | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | from __future__ import print_function |
|
19 | from __future__ import print_function | |
20 | import json |
|
20 | import json | |
21 | from xml.etree import ElementTree as ET |
|
21 | from xml.etree import ElementTree as ET | |
22 | import re |
|
22 | import re | |
23 |
|
23 | |||
|
24 | from IPython.nbformat import v3 | |||
24 | from IPython.nbformat import v2 |
|
25 | from IPython.nbformat import v2 | |
25 | from IPython.nbformat import v1 |
|
26 | from IPython.nbformat import v1 | |
26 |
|
27 | |||
27 |
from IPython.nbformat.v |
|
28 | from IPython.nbformat.v3 import ( | |
28 | NotebookNode, |
|
29 | NotebookNode, | |
29 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, |
|
30 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, | |
30 | parse_filename, new_metadata, new_author |
|
31 | parse_filename, new_metadata, new_author | |
31 | ) |
|
32 | ) | |
32 |
|
33 | |||
33 | #----------------------------------------------------------------------------- |
|
34 | #----------------------------------------------------------------------------- | |
34 | # Code |
|
35 | # Code | |
35 | #----------------------------------------------------------------------------- |
|
36 | #----------------------------------------------------------------------------- | |
36 |
|
37 | |||
37 | current_nbformat = 2 |
|
38 | current_nbformat = 2 | |
38 |
|
39 | |||
39 |
|
40 | |||
40 | class NBFormatError(Exception): |
|
41 | class NBFormatError(Exception): | |
41 | pass |
|
42 | pass | |
42 |
|
43 | |||
43 |
|
44 | |||
44 | def parse_json(s, **kwargs): |
|
45 | def parse_json(s, **kwargs): | |
45 | """Parse a string into a (nbformat, dict) tuple.""" |
|
46 | """Parse a string into a (nbformat, dict) tuple.""" | |
46 | d = json.loads(s, **kwargs) |
|
47 | d = json.loads(s, **kwargs) | |
47 | nbformat = d.get('nbformat',1) |
|
48 | nbformat = d.get('nbformat',1) | |
48 | return nbformat, d |
|
49 | return nbformat, d | |
49 |
|
50 | |||
50 |
|
51 | |||
51 | def parse_py(s, **kwargs): |
|
52 | def parse_py(s, **kwargs): | |
52 | """Parse a string into a (nbformat, string) tuple.""" |
|
53 | """Parse a string into a (nbformat, string) tuple.""" | |
53 | pattern = r'# <nbformat>(?P<nbformat>\d+)</nbformat>' |
|
54 | pattern = r'# <nbformat>(?P<nbformat>\d+)</nbformat>' | |
54 | m = re.search(pattern,s) |
|
55 | m = re.search(pattern,s) | |
55 | if m is not None: |
|
56 | if m is not None: | |
56 | nbformat = int(m.group('nbformat')) |
|
57 | nbformat = int(m.group('nbformat')) | |
57 | else: |
|
58 | else: | |
58 | nbformat = 2 |
|
59 | nbformat = 2 | |
59 | return nbformat, s |
|
60 | return nbformat, s | |
60 |
|
61 | |||
61 |
|
62 | |||
62 | def reads_json(s, **kwargs): |
|
63 | def reads_json(s, **kwargs): | |
63 | """Read a JSON notebook from a string and return the NotebookNode object.""" |
|
64 | """Read a JSON notebook from a string and return the NotebookNode object.""" | |
64 | nbformat, d = parse_json(s, **kwargs) |
|
65 | nbformat, d = parse_json(s, **kwargs) | |
65 | if nbformat == 1: |
|
66 | if nbformat == 1: | |
66 | nb = v1.to_notebook_json(d, **kwargs) |
|
67 | nb = v1.to_notebook_json(d, **kwargs) | |
67 | nb = v2.convert_to_this_nbformat(nb, orig_version=1) |
|
68 | nb = v2.convert_to_this_nbformat(nb, orig_version=1) | |
68 | elif nbformat == 2: |
|
69 | elif nbformat == 2: | |
69 | nb = v2.to_notebook_json(d, **kwargs) |
|
70 | nb = v2.to_notebook_json(d, **kwargs) | |
70 | else: |
|
71 | else: | |
71 | raise NBFormatError('Unsupported JSON nbformat version: %i' % nbformat) |
|
72 | raise NBFormatError('Unsupported JSON nbformat version: %i' % nbformat) | |
72 | return nb |
|
73 | return nb | |
73 |
|
74 | |||
74 |
|
75 | |||
75 | def writes_json(nb, **kwargs): |
|
76 | def writes_json(nb, **kwargs): | |
76 | return v2.writes_json(nb, **kwargs) |
|
77 | return v2.writes_json(nb, **kwargs) | |
77 |
|
78 | |||
78 |
|
79 | |||
79 | def reads_py(s, **kwargs): |
|
80 | def reads_py(s, **kwargs): | |
80 | """Read a .py notebook from a string and return the NotebookNode object.""" |
|
81 | """Read a .py notebook from a string and return the NotebookNode object.""" | |
81 | nbformat, s = parse_py(s, **kwargs) |
|
82 | nbformat, s = parse_py(s, **kwargs) | |
82 | if nbformat == 2: |
|
83 | if nbformat == 2: | |
83 | nb = v2.to_notebook_py(s, **kwargs) |
|
84 | nb = v2.to_notebook_py(s, **kwargs) | |
84 | else: |
|
85 | else: | |
85 | raise NBFormatError('Unsupported PY nbformat version: %i' % nbformat) |
|
86 | raise NBFormatError('Unsupported PY nbformat version: %i' % nbformat) | |
86 | return nb |
|
87 | return nb | |
87 |
|
88 | |||
88 |
|
89 | |||
89 | def writes_py(nb, **kwargs): |
|
90 | def writes_py(nb, **kwargs): | |
90 | return v2.writes_py(nb, **kwargs) |
|
91 | return v2.writes_py(nb, **kwargs) | |
91 |
|
92 | |||
92 |
|
93 | |||
93 | # High level API |
|
94 | # High level API | |
94 |
|
95 | |||
95 |
|
96 | |||
96 | def reads(s, format, **kwargs): |
|
97 | def reads(s, format, **kwargs): | |
97 | """Read a notebook from a string and return the NotebookNode object. |
|
98 | """Read a notebook from a string and return the NotebookNode object. | |
98 |
|
99 | |||
99 | This function properly handles notebooks of any version. The notebook |
|
100 | This function properly handles notebooks of any version. The notebook | |
100 | returned will always be in the current version's format. |
|
101 | returned will always be in the current version's format. | |
101 |
|
102 | |||
102 | Parameters |
|
103 | Parameters | |
103 | ---------- |
|
104 | ---------- | |
104 | s : unicode |
|
105 | s : unicode | |
105 | The raw unicode string to read the notebook from. |
|
106 | The raw unicode string to read the notebook from. | |
106 | format : (u'json', u'ipynb', u'py') |
|
107 | format : (u'json', u'ipynb', u'py') | |
107 | The format that the string is in. |
|
108 | The format that the string is in. | |
108 |
|
109 | |||
109 | Returns |
|
110 | Returns | |
110 | ------- |
|
111 | ------- | |
111 | nb : NotebookNode |
|
112 | nb : NotebookNode | |
112 | The notebook that was read. |
|
113 | The notebook that was read. | |
113 | """ |
|
114 | """ | |
114 | format = unicode(format) |
|
115 | format = unicode(format) | |
115 | if format == u'json' or format == u'ipynb': |
|
116 | if format == u'json' or format == u'ipynb': | |
116 | return reads_json(s, **kwargs) |
|
117 | return reads_json(s, **kwargs) | |
117 | elif format == u'py': |
|
118 | elif format == u'py': | |
118 | return reads_py(s, **kwargs) |
|
119 | return reads_py(s, **kwargs) | |
119 | else: |
|
120 | else: | |
120 | raise NBFormatError('Unsupported format: %s' % format) |
|
121 | raise NBFormatError('Unsupported format: %s' % format) | |
121 |
|
122 | |||
122 |
|
123 | |||
123 | def writes(nb, format, **kwargs): |
|
124 | def writes(nb, format, **kwargs): | |
124 | """Write a notebook to a string in a given format in the current nbformat version. |
|
125 | """Write a notebook to a string in a given format in the current nbformat version. | |
125 |
|
126 | |||
126 | This function always writes the notebook in the current nbformat version. |
|
127 | This function always writes the notebook in the current nbformat version. | |
127 |
|
128 | |||
128 | Parameters |
|
129 | Parameters | |
129 | ---------- |
|
130 | ---------- | |
130 | nb : NotebookNode |
|
131 | nb : NotebookNode | |
131 | The notebook to write. |
|
132 | The notebook to write. | |
132 | format : (u'json', u'ipynb', u'py') |
|
133 | format : (u'json', u'ipynb', u'py') | |
133 | The format to write the notebook in. |
|
134 | The format to write the notebook in. | |
134 |
|
135 | |||
135 | Returns |
|
136 | Returns | |
136 | ------- |
|
137 | ------- | |
137 | s : unicode |
|
138 | s : unicode | |
138 | The notebook string. |
|
139 | The notebook string. | |
139 | """ |
|
140 | """ | |
140 | format = unicode(format) |
|
141 | format = unicode(format) | |
141 | if format == u'json' or format == u'ipynb': |
|
142 | if format == u'json' or format == u'ipynb': | |
142 | return writes_json(nb, **kwargs) |
|
143 | return writes_json(nb, **kwargs) | |
143 | elif format == u'py': |
|
144 | elif format == u'py': | |
144 | return writes_py(nb, **kwargs) |
|
145 | return writes_py(nb, **kwargs) | |
145 | else: |
|
146 | else: | |
146 | raise NBFormatError('Unsupported format: %s' % format) |
|
147 | raise NBFormatError('Unsupported format: %s' % format) | |
147 |
|
148 | |||
148 |
|
149 | |||
149 | def read(fp, format, **kwargs): |
|
150 | def read(fp, format, **kwargs): | |
150 | """Read a notebook from a file and return the NotebookNode object. |
|
151 | """Read a notebook from a file and return the NotebookNode object. | |
151 |
|
152 | |||
152 | This function properly handles notebooks of any version. The notebook |
|
153 | This function properly handles notebooks of any version. The notebook | |
153 | returned will always be in the current version's format. |
|
154 | returned will always be in the current version's format. | |
154 |
|
155 | |||
155 | Parameters |
|
156 | Parameters | |
156 | ---------- |
|
157 | ---------- | |
157 | fp : file |
|
158 | fp : file | |
158 | Any file-like object with a read method. |
|
159 | Any file-like object with a read method. | |
159 | format : (u'json', u'ipynb', u'py') |
|
160 | format : (u'json', u'ipynb', u'py') | |
160 | The format that the string is in. |
|
161 | The format that the string is in. | |
161 |
|
162 | |||
162 | Returns |
|
163 | Returns | |
163 | ------- |
|
164 | ------- | |
164 | nb : NotebookNode |
|
165 | nb : NotebookNode | |
165 | The notebook that was read. |
|
166 | The notebook that was read. | |
166 | """ |
|
167 | """ | |
167 | return reads(fp.read(), format, **kwargs) |
|
168 | return reads(fp.read(), format, **kwargs) | |
168 |
|
169 | |||
169 |
|
170 | |||
170 | def write(nb, fp, format, **kwargs): |
|
171 | def write(nb, fp, format, **kwargs): | |
171 | """Write a notebook to a file in a given format in the current nbformat version. |
|
172 | """Write a notebook to a file in a given format in the current nbformat version. | |
172 |
|
173 | |||
173 | This function always writes the notebook in the current nbformat version. |
|
174 | This function always writes the notebook in the current nbformat version. | |
174 |
|
175 | |||
175 | Parameters |
|
176 | Parameters | |
176 | ---------- |
|
177 | ---------- | |
177 | nb : NotebookNode |
|
178 | nb : NotebookNode | |
178 | The notebook to write. |
|
179 | The notebook to write. | |
179 | fp : file |
|
180 | fp : file | |
180 | Any file-like object with a write method. |
|
181 | Any file-like object with a write method. | |
181 | format : (u'json', u'ipynb', u'py') |
|
182 | format : (u'json', u'ipynb', u'py') | |
182 | The format to write the notebook in. |
|
183 | The format to write the notebook in. | |
183 |
|
184 | |||
184 | Returns |
|
185 | Returns | |
185 | ------- |
|
186 | ------- | |
186 | s : unicode |
|
187 | s : unicode | |
187 | The notebook string. |
|
188 | The notebook string. | |
188 | """ |
|
189 | """ | |
189 | return fp.write(writes(nb, format, **kwargs)) |
|
190 | return fp.write(writes(nb, format, **kwargs)) | |
190 |
|
191 | |||
191 | def _convert_to_metadata(): |
|
192 | def _convert_to_metadata(): | |
192 | """Convert to a notebook having notebook metadata.""" |
|
193 | """Convert to a notebook having notebook metadata.""" | |
193 | import glob |
|
194 | import glob | |
194 | for fname in glob.glob('*.ipynb'): |
|
195 | for fname in glob.glob('*.ipynb'): | |
195 | print('Converting file:',fname) |
|
196 | print('Converting file:',fname) | |
196 | with open(fname,'r') as f: |
|
197 | with open(fname,'r') as f: | |
197 | nb = read(f,u'json') |
|
198 | nb = read(f,u'json') | |
198 | md = new_metadata() |
|
199 | md = new_metadata() | |
199 | if u'name' in nb: |
|
200 | if u'name' in nb: | |
200 | md.name = nb.name |
|
201 | md.name = nb.name | |
201 | del nb[u'name'] |
|
202 | del nb[u'name'] | |
202 | nb.metadata = md |
|
203 | nb.metadata = md | |
203 | with open(fname,'w') as f: |
|
204 | with open(fname,'w') as f: | |
204 | write(nb, f, u'json') |
|
205 | write(nb, f, u'json') | |
205 |
|
206 |
@@ -1,200 +1,200 b'' | |||||
1 | """Read and write notebooks as regular .py files. |
|
1 | """Read and write notebooks as regular .py files. | |
2 |
|
2 | |||
3 | Authors: |
|
3 | Authors: | |
4 |
|
4 | |||
5 | * Brian Granger |
|
5 | * Brian Granger | |
6 | """ |
|
6 | """ | |
7 |
|
7 | |||
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 | # Copyright (C) 2008-2011 The IPython Development Team |
|
9 | # Copyright (C) 2008-2011 The IPython Development Team | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | import re |
|
19 | import re | |
20 | from .rwbase import NotebookReader, NotebookWriter |
|
20 | from .rwbase import NotebookReader, NotebookWriter | |
21 | from .nbbase import ( |
|
21 | from .nbbase import ( | |
22 | new_code_cell, new_text_cell, new_worksheet, |
|
22 | new_code_cell, new_text_cell, new_worksheet, | |
23 | new_notebook, new_heading_cell |
|
23 | new_notebook, new_heading_cell | |
24 | ) |
|
24 | ) | |
25 |
|
25 | |||
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | # Code |
|
27 | # Code | |
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 |
|
29 | |||
30 | _encoding_declaration_re = re.compile(r"^#.*coding[:=]\s*([-\w.]+)") |
|
30 | _encoding_declaration_re = re.compile(r"^#.*coding[:=]\s*([-\w.]+)") | |
31 |
|
31 | |||
32 | class PyReaderError(Exception): |
|
32 | class PyReaderError(Exception): | |
33 | pass |
|
33 | pass | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | class PyReader(NotebookReader): |
|
36 | class PyReader(NotebookReader): | |
37 |
|
37 | |||
38 | def reads(self, s, **kwargs): |
|
38 | def reads(self, s, **kwargs): | |
39 | return self.to_notebook(s,**kwargs) |
|
39 | return self.to_notebook(s,**kwargs) | |
40 |
|
40 | |||
41 | def to_notebook(self, s, **kwargs): |
|
41 | def to_notebook(self, s, **kwargs): | |
42 | lines = s.splitlines() |
|
42 | lines = s.splitlines() | |
43 | cells = [] |
|
43 | cells = [] | |
44 | cell_lines = [] |
|
44 | cell_lines = [] | |
45 | kwargs = {} |
|
45 | kwargs = {} | |
46 | state = u'codecell' |
|
46 | state = u'codecell' | |
47 | for line in lines: |
|
47 | for line in lines: | |
48 | if line.startswith(u'# <nbformat>') or _encoding_declaration_re.match(line): |
|
48 | if line.startswith(u'# <nbformat>') or _encoding_declaration_re.match(line): | |
49 | pass |
|
49 | pass | |
50 | elif line.startswith(u'# <codecell>'): |
|
50 | elif line.startswith(u'# <codecell>'): | |
51 | cell = self.new_cell(state, cell_lines, **kwargs) |
|
51 | cell = self.new_cell(state, cell_lines, **kwargs) | |
52 | if cell is not None: |
|
52 | if cell is not None: | |
53 | cells.append(cell) |
|
53 | cells.append(cell) | |
54 | state = u'codecell' |
|
54 | state = u'codecell' | |
55 | cell_lines = [] |
|
55 | cell_lines = [] | |
56 | kwargs = {} |
|
56 | kwargs = {} | |
57 | elif line.startswith(u'# <htmlcell>'): |
|
57 | elif line.startswith(u'# <htmlcell>'): | |
58 | cell = self.new_cell(state, cell_lines, **kwargs) |
|
58 | cell = self.new_cell(state, cell_lines, **kwargs) | |
59 | if cell is not None: |
|
59 | if cell is not None: | |
60 | cells.append(cell) |
|
60 | cells.append(cell) | |
61 | state = u'htmlcell' |
|
61 | state = u'htmlcell' | |
62 | cell_lines = [] |
|
62 | cell_lines = [] | |
63 | kwargs = {} |
|
63 | kwargs = {} | |
64 | elif line.startswith(u'# <markdowncell>'): |
|
64 | elif line.startswith(u'# <markdowncell>'): | |
65 | cell = self.new_cell(state, cell_lines, **kwargs) |
|
65 | cell = self.new_cell(state, cell_lines, **kwargs) | |
66 | if cell is not None: |
|
66 | if cell is not None: | |
67 | cells.append(cell) |
|
67 | cells.append(cell) | |
68 | state = u'markdowncell' |
|
68 | state = u'markdowncell' | |
69 | cell_lines = [] |
|
69 | cell_lines = [] | |
70 | kwargs = {} |
|
70 | kwargs = {} | |
71 |
elif line.startswith(u'# < |
|
71 | elif line.startswith(u'# <plaintextcell>'): | |
72 | cell = self.new_cell(state, cell_lines, **kwargs) |
|
72 | cell = self.new_cell(state, cell_lines, **kwargs) | |
73 | if cell is not None: |
|
73 | if cell is not None: | |
74 | cells.append(cell) |
|
74 | cells.append(cell) | |
75 |
state = u' |
|
75 | state = u'plaintextcell' | |
76 | cell_lines = [] |
|
76 | cell_lines = [] | |
77 | kwargs = {} |
|
77 | kwargs = {} | |
78 | elif line.startswith(u'# <headingcell'): |
|
78 | elif line.startswith(u'# <headingcell'): | |
79 | cell = self.new_cell(state, cell_lines, **kwargs) |
|
79 | cell = self.new_cell(state, cell_lines, **kwargs) | |
80 | if cell is not None: |
|
80 | if cell is not None: | |
81 | cells.append(cell) |
|
81 | cells.append(cell) | |
82 | cell_lines = [] |
|
82 | cell_lines = [] | |
83 | m = re.match(r'# <headingcell level=(?P<level>\d)>',line) |
|
83 | m = re.match(r'# <headingcell level=(?P<level>\d)>',line) | |
84 | if m is not None: |
|
84 | if m is not None: | |
85 | state = u'headingcell' |
|
85 | state = u'headingcell' | |
86 | kwargs = {} |
|
86 | kwargs = {} | |
87 | kwargs['level'] = int(m.group('level')) |
|
87 | kwargs['level'] = int(m.group('level')) | |
88 | else: |
|
88 | else: | |
89 | state = u'codecell' |
|
89 | state = u'codecell' | |
90 | kwargs = {} |
|
90 | kwargs = {} | |
91 | cell_lines = [] |
|
91 | cell_lines = [] | |
92 | else: |
|
92 | else: | |
93 | cell_lines.append(line) |
|
93 | cell_lines.append(line) | |
94 | if cell_lines and state == u'codecell': |
|
94 | if cell_lines and state == u'codecell': | |
95 | cell = self.new_cell(state, cell_lines) |
|
95 | cell = self.new_cell(state, cell_lines) | |
96 | if cell is not None: |
|
96 | if cell is not None: | |
97 | cells.append(cell) |
|
97 | cells.append(cell) | |
98 | ws = new_worksheet(cells=cells) |
|
98 | ws = new_worksheet(cells=cells) | |
99 | nb = new_notebook(worksheets=[ws]) |
|
99 | nb = new_notebook(worksheets=[ws]) | |
100 | return nb |
|
100 | return nb | |
101 |
|
101 | |||
102 | def new_cell(self, state, lines, **kwargs): |
|
102 | def new_cell(self, state, lines, **kwargs): | |
103 | if state == u'codecell': |
|
103 | if state == u'codecell': | |
104 | input = u'\n'.join(lines) |
|
104 | input = u'\n'.join(lines) | |
105 | input = input.strip(u'\n') |
|
105 | input = input.strip(u'\n') | |
106 | if input: |
|
106 | if input: | |
107 | return new_code_cell(input=input) |
|
107 | return new_code_cell(input=input) | |
108 | elif state == u'htmlcell': |
|
108 | elif state == u'htmlcell': | |
109 | text = self._remove_comments(lines) |
|
109 | text = self._remove_comments(lines) | |
110 | if text: |
|
110 | if text: | |
111 | return new_text_cell(u'html',source=text) |
|
111 | return new_text_cell(u'html',source=text) | |
112 | elif state == u'markdowncell': |
|
112 | elif state == u'markdowncell': | |
113 | text = self._remove_comments(lines) |
|
113 | text = self._remove_comments(lines) | |
114 | if text: |
|
114 | if text: | |
115 | return new_text_cell(u'markdown',source=text) |
|
115 | return new_text_cell(u'markdown',source=text) | |
116 |
elif state == u' |
|
116 | elif state == u'plaintextcell': | |
117 | text = self._remove_comments(lines) |
|
117 | text = self._remove_comments(lines) | |
118 | if text: |
|
118 | if text: | |
119 |
return new_text_cell(u' |
|
119 | return new_text_cell(u'plaintext',source=text) | |
120 | elif state == u'headingcell': |
|
120 | elif state == u'headingcell': | |
121 | text = self._remove_comments(lines) |
|
121 | text = self._remove_comments(lines) | |
122 | level = kwargs.get('level',1) |
|
122 | level = kwargs.get('level',1) | |
123 | if text: |
|
123 | if text: | |
124 | return new_heading_cell(source=text,level=level) |
|
124 | return new_heading_cell(source=text,level=level) | |
125 |
|
125 | |||
126 | def _remove_comments(self, lines): |
|
126 | def _remove_comments(self, lines): | |
127 | new_lines = [] |
|
127 | new_lines = [] | |
128 | for line in lines: |
|
128 | for line in lines: | |
129 | if line.startswith(u'#'): |
|
129 | if line.startswith(u'#'): | |
130 | new_lines.append(line[2:]) |
|
130 | new_lines.append(line[2:]) | |
131 | else: |
|
131 | else: | |
132 | new_lines.append(line) |
|
132 | new_lines.append(line) | |
133 | text = u'\n'.join(new_lines) |
|
133 | text = u'\n'.join(new_lines) | |
134 | text = text.strip(u'\n') |
|
134 | text = text.strip(u'\n') | |
135 | return text |
|
135 | return text | |
136 |
|
136 | |||
137 | def split_lines_into_blocks(self, lines): |
|
137 | def split_lines_into_blocks(self, lines): | |
138 | if len(lines) == 1: |
|
138 | if len(lines) == 1: | |
139 | yield lines[0] |
|
139 | yield lines[0] | |
140 | raise StopIteration() |
|
140 | raise StopIteration() | |
141 | import ast |
|
141 | import ast | |
142 | source = '\n'.join(lines) |
|
142 | source = '\n'.join(lines) | |
143 | code = ast.parse(source) |
|
143 | code = ast.parse(source) | |
144 | starts = [x.lineno-1 for x in code.body] |
|
144 | starts = [x.lineno-1 for x in code.body] | |
145 | for i in range(len(starts)-1): |
|
145 | for i in range(len(starts)-1): | |
146 | yield '\n'.join(lines[starts[i]:starts[i+1]]).strip('\n') |
|
146 | yield '\n'.join(lines[starts[i]:starts[i+1]]).strip('\n') | |
147 | yield '\n'.join(lines[starts[-1]:]).strip('\n') |
|
147 | yield '\n'.join(lines[starts[-1]:]).strip('\n') | |
148 |
|
148 | |||
149 |
|
149 | |||
150 | class PyWriter(NotebookWriter): |
|
150 | class PyWriter(NotebookWriter): | |
151 |
|
151 | |||
152 | def writes(self, nb, **kwargs): |
|
152 | def writes(self, nb, **kwargs): | |
153 | lines = [u'# -*- coding: utf-8 -*-'] |
|
153 | lines = [u'# -*- coding: utf-8 -*-'] | |
154 | lines.extend([u'# <nbformat>2</nbformat>','']) |
|
154 | lines.extend([u'# <nbformat>2</nbformat>','']) | |
155 | for ws in nb.worksheets: |
|
155 | for ws in nb.worksheets: | |
156 | for cell in ws.cells: |
|
156 | for cell in ws.cells: | |
157 | if cell.cell_type == u'code': |
|
157 | if cell.cell_type == u'code': | |
158 | input = cell.get(u'input') |
|
158 | input = cell.get(u'input') | |
159 | if input is not None: |
|
159 | if input is not None: | |
160 | lines.extend([u'# <codecell>',u'']) |
|
160 | lines.extend([u'# <codecell>',u'']) | |
161 | lines.extend(input.splitlines()) |
|
161 | lines.extend(input.splitlines()) | |
162 | lines.append(u'') |
|
162 | lines.append(u'') | |
163 | elif cell.cell_type == u'html': |
|
163 | elif cell.cell_type == u'html': | |
164 | input = cell.get(u'source') |
|
164 | input = cell.get(u'source') | |
165 | if input is not None: |
|
165 | if input is not None: | |
166 | lines.extend([u'# <htmlcell>',u'']) |
|
166 | lines.extend([u'# <htmlcell>',u'']) | |
167 | lines.extend([u'# ' + line for line in input.splitlines()]) |
|
167 | lines.extend([u'# ' + line for line in input.splitlines()]) | |
168 | lines.append(u'') |
|
168 | lines.append(u'') | |
169 | elif cell.cell_type == u'markdown': |
|
169 | elif cell.cell_type == u'markdown': | |
170 | input = cell.get(u'source') |
|
170 | input = cell.get(u'source') | |
171 | if input is not None: |
|
171 | if input is not None: | |
172 | lines.extend([u'# <markdowncell>',u'']) |
|
172 | lines.extend([u'# <markdowncell>',u'']) | |
173 | lines.extend([u'# ' + line for line in input.splitlines()]) |
|
173 | lines.extend([u'# ' + line for line in input.splitlines()]) | |
174 | lines.append(u'') |
|
174 | lines.append(u'') | |
175 |
elif cell.cell_type == u' |
|
175 | elif cell.cell_type == u'plaintext': | |
176 | input = cell.get(u'source') |
|
176 | input = cell.get(u'source') | |
177 | if input is not None: |
|
177 | if input is not None: | |
178 |
lines.extend([u'# < |
|
178 | lines.extend([u'# <plaintextcell>',u'']) | |
179 | lines.extend([u'# ' + line for line in input.splitlines()]) |
|
179 | lines.extend([u'# ' + line for line in input.splitlines()]) | |
180 | lines.append(u'') |
|
180 | lines.append(u'') | |
181 | elif cell.cell_type == u'heading': |
|
181 | elif cell.cell_type == u'heading': | |
182 | input = cell.get(u'source') |
|
182 | input = cell.get(u'source') | |
183 | level = cell.get(u'level',1) |
|
183 | level = cell.get(u'level',1) | |
184 | if input is not None: |
|
184 | if input is not None: | |
185 | lines.extend([u'# <headingcell level=%s>' % level,u'']) |
|
185 | lines.extend([u'# <headingcell level=%s>' % level,u'']) | |
186 | lines.extend([u'# ' + line for line in input.splitlines()]) |
|
186 | lines.extend([u'# ' + line for line in input.splitlines()]) | |
187 | lines.append(u'') |
|
187 | lines.append(u'') | |
188 | lines.append('') |
|
188 | lines.append('') | |
189 | return unicode('\n'.join(lines)) |
|
189 | return unicode('\n'.join(lines)) | |
190 |
|
190 | |||
191 |
|
191 | |||
192 | _reader = PyReader() |
|
192 | _reader = PyReader() | |
193 | _writer = PyWriter() |
|
193 | _writer = PyWriter() | |
194 |
|
194 | |||
195 | reads = _reader.reads |
|
195 | reads = _reader.reads | |
196 | read = _reader.read |
|
196 | read = _reader.read | |
197 | to_notebook = _reader.to_notebook |
|
197 | to_notebook = _reader.to_notebook | |
198 | write = _writer.write |
|
198 | write = _writer.write | |
199 | writes = _writer.writes |
|
199 | writes = _writer.writes | |
200 |
|
200 |
@@ -1,127 +1,127 b'' | |||||
1 | import os |
|
1 | import os | |
2 | from base64 import encodestring |
|
2 | from base64 import encodestring | |
3 |
|
3 | |||
4 | from ..nbbase import ( |
|
4 | from ..nbbase import ( | |
5 | NotebookNode, |
|
5 | NotebookNode, | |
6 | new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output, |
|
6 | new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output, | |
7 | new_metadata, new_author, new_heading_cell |
|
7 | new_metadata, new_author, new_heading_cell | |
8 | ) |
|
8 | ) | |
9 |
|
9 | |||
10 | # some random base64-encoded *bytes* |
|
10 | # some random base64-encoded *bytes* | |
11 | png = encodestring(os.urandom(5)) |
|
11 | png = encodestring(os.urandom(5)) | |
12 | jpeg = encodestring(os.urandom(6)) |
|
12 | jpeg = encodestring(os.urandom(6)) | |
13 |
|
13 | |||
14 | ws = new_worksheet(name='worksheet1') |
|
14 | ws = new_worksheet(name='worksheet1') | |
15 |
|
15 | |||
16 | ws.cells.append(new_text_cell( |
|
16 | ws.cells.append(new_text_cell( | |
17 | u'html', |
|
17 | u'html', | |
18 | source='Some NumPy Examples', |
|
18 | source='Some NumPy Examples', | |
19 | rendered='Some NumPy Examples' |
|
19 | rendered='Some NumPy Examples' | |
20 | )) |
|
20 | )) | |
21 |
|
21 | |||
22 |
|
22 | |||
23 | ws.cells.append(new_code_cell( |
|
23 | ws.cells.append(new_code_cell( | |
24 | input='import numpy', |
|
24 | input='import numpy', | |
25 | prompt_number=1, |
|
25 | prompt_number=1, | |
26 | collapsed=False |
|
26 | collapsed=False | |
27 | )) |
|
27 | )) | |
28 |
|
28 | |||
29 | ws.cells.append(new_text_cell( |
|
29 | ws.cells.append(new_text_cell( | |
30 | u'markdown', |
|
30 | u'markdown', | |
31 | source='A random array', |
|
31 | source='A random array', | |
32 | rendered='A random array' |
|
32 | rendered='A random array' | |
33 | )) |
|
33 | )) | |
34 |
|
34 | |||
35 | ws.cells.append(new_text_cell( |
|
35 | ws.cells.append(new_text_cell( | |
36 |
u' |
|
36 | u'plaintext', | |
37 | source='A random array', |
|
37 | source='A random array', | |
38 | )) |
|
38 | )) | |
39 |
|
39 | |||
40 | ws.cells.append(new_heading_cell( |
|
40 | ws.cells.append(new_heading_cell( | |
41 | u'My Heading', |
|
41 | u'My Heading', | |
42 | level=2 |
|
42 | level=2 | |
43 | )) |
|
43 | )) | |
44 |
|
44 | |||
45 | ws.cells.append(new_code_cell( |
|
45 | ws.cells.append(new_code_cell( | |
46 | input='a = numpy.random.rand(100)', |
|
46 | input='a = numpy.random.rand(100)', | |
47 | prompt_number=2, |
|
47 | prompt_number=2, | |
48 | collapsed=True |
|
48 | collapsed=True | |
49 | )) |
|
49 | )) | |
50 |
|
50 | |||
51 | ws.cells.append(new_code_cell( |
|
51 | ws.cells.append(new_code_cell( | |
52 | input='print a', |
|
52 | input='print a', | |
53 | prompt_number=3, |
|
53 | prompt_number=3, | |
54 | collapsed=False, |
|
54 | collapsed=False, | |
55 | outputs=[new_output( |
|
55 | outputs=[new_output( | |
56 | output_type=u'pyout', |
|
56 | output_type=u'pyout', | |
57 | output_text=u'<array a>', |
|
57 | output_text=u'<array a>', | |
58 | output_html=u'The HTML rep', |
|
58 | output_html=u'The HTML rep', | |
59 | output_latex=u'$a$', |
|
59 | output_latex=u'$a$', | |
60 | output_png=png, |
|
60 | output_png=png, | |
61 | output_jpeg=jpeg, |
|
61 | output_jpeg=jpeg, | |
62 | output_svg=u'<svg>', |
|
62 | output_svg=u'<svg>', | |
63 | output_json=u'json data', |
|
63 | output_json=u'json data', | |
64 | output_javascript=u'var i=0;', |
|
64 | output_javascript=u'var i=0;', | |
65 | prompt_number=3 |
|
65 | prompt_number=3 | |
66 | ),new_output( |
|
66 | ),new_output( | |
67 | output_type=u'display_data', |
|
67 | output_type=u'display_data', | |
68 | output_text=u'<array a>', |
|
68 | output_text=u'<array a>', | |
69 | output_html=u'The HTML rep', |
|
69 | output_html=u'The HTML rep', | |
70 | output_latex=u'$a$', |
|
70 | output_latex=u'$a$', | |
71 | output_png=png, |
|
71 | output_png=png, | |
72 | output_jpeg=jpeg, |
|
72 | output_jpeg=jpeg, | |
73 | output_svg=u'<svg>', |
|
73 | output_svg=u'<svg>', | |
74 | output_json=u'json data', |
|
74 | output_json=u'json data', | |
75 | output_javascript=u'var i=0;' |
|
75 | output_javascript=u'var i=0;' | |
76 | ),new_output( |
|
76 | ),new_output( | |
77 | output_type=u'pyerr', |
|
77 | output_type=u'pyerr', | |
78 | etype=u'NameError', |
|
78 | etype=u'NameError', | |
79 | evalue=u'NameError was here', |
|
79 | evalue=u'NameError was here', | |
80 | traceback=[u'frame 0', u'frame 1', u'frame 2'] |
|
80 | traceback=[u'frame 0', u'frame 1', u'frame 2'] | |
81 | )] |
|
81 | )] | |
82 | )) |
|
82 | )) | |
83 |
|
83 | |||
84 | authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com', |
|
84 | authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com', | |
85 | affiliation=u'Fox',url=u'http://www.fox.com')] |
|
85 | affiliation=u'Fox',url=u'http://www.fox.com')] | |
86 | md = new_metadata(name=u'My Notebook',license=u'BSD',created=u'8601_goes_here', |
|
86 | md = new_metadata(name=u'My Notebook',license=u'BSD',created=u'8601_goes_here', | |
87 | modified=u'8601_goes_here',gistid=u'21341231',authors=authors) |
|
87 | modified=u'8601_goes_here',gistid=u'21341231',authors=authors) | |
88 |
|
88 | |||
89 | nb0 = new_notebook( |
|
89 | nb0 = new_notebook( | |
90 | worksheets=[ws, new_worksheet(name='worksheet2')], |
|
90 | worksheets=[ws, new_worksheet(name='worksheet2')], | |
91 | metadata=md |
|
91 | metadata=md | |
92 | ) |
|
92 | ) | |
93 |
|
93 | |||
94 | nb0_py = """# -*- coding: utf-8 -*- |
|
94 | nb0_py = """# -*- coding: utf-8 -*- | |
95 | # <nbformat>2</nbformat> |
|
95 | # <nbformat>2</nbformat> | |
96 |
|
96 | |||
97 | # <htmlcell> |
|
97 | # <htmlcell> | |
98 |
|
98 | |||
99 | # Some NumPy Examples |
|
99 | # Some NumPy Examples | |
100 |
|
100 | |||
101 | # <codecell> |
|
101 | # <codecell> | |
102 |
|
102 | |||
103 | import numpy |
|
103 | import numpy | |
104 |
|
104 | |||
105 | # <markdowncell> |
|
105 | # <markdowncell> | |
106 |
|
106 | |||
107 | # A random array |
|
107 | # A random array | |
108 |
|
108 | |||
109 |
# < |
|
109 | # <plaintextcell> | |
110 |
|
110 | |||
111 | # A random array |
|
111 | # A random array | |
112 |
|
112 | |||
113 | # <headingcell level=2> |
|
113 | # <headingcell level=2> | |
114 |
|
114 | |||
115 | # My Heading |
|
115 | # My Heading | |
116 |
|
116 | |||
117 | # <codecell> |
|
117 | # <codecell> | |
118 |
|
118 | |||
119 | a = numpy.random.rand(100) |
|
119 | a = numpy.random.rand(100) | |
120 |
|
120 | |||
121 | # <codecell> |
|
121 | # <codecell> | |
122 |
|
122 | |||
123 | print a |
|
123 | print a | |
124 |
|
124 | |||
125 | """ |
|
125 | """ | |
126 |
|
126 | |||
127 |
|
127 |
@@ -1,136 +1,136 b'' | |||||
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 |
|
6 | new_author, new_metadata, new_heading_cell | |
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.assertEquals(cc.cell_type,u'code') |
|
13 | self.assertEquals(cc.cell_type,u'code') | |
14 | self.assertEquals(u'input' not in cc, True) |
|
14 | self.assertEquals(u'input' not in cc, True) | |
15 | self.assertEquals(u'prompt_number' not in cc, True) |
|
15 | self.assertEquals(u'prompt_number' not in cc, True) | |
16 | self.assertEquals(cc.outputs, []) |
|
16 | self.assertEquals(cc.outputs, []) | |
17 | self.assertEquals(cc.collapsed, False) |
|
17 | self.assertEquals(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.assertEquals(cc.input, u'a=10') |
|
23 | self.assertEquals(cc.input, u'a=10') | |
24 | self.assertEquals(cc.prompt_number, 0) |
|
24 | self.assertEquals(cc.prompt_number, 0) | |
25 | self.assertEquals(cc.language, u'python') |
|
25 | self.assertEquals(cc.language, u'python') | |
26 | self.assertEquals(cc.outputs[0].svg, u'foo') |
|
26 | self.assertEquals(cc.outputs[0].svg, u'foo') | |
27 | self.assertEquals(cc.outputs[0].text, u'10') |
|
27 | self.assertEquals(cc.outputs[0].text, u'10') | |
28 | self.assertEquals(cc.outputs[0].prompt_number, 0) |
|
28 | self.assertEquals(cc.outputs[0].prompt_number, 0) | |
29 | self.assertEquals(cc.collapsed, True) |
|
29 | self.assertEquals(cc.collapsed, True) | |
30 |
|
30 | |||
31 | def test_pyerr(self): |
|
31 | def test_pyerr(self): | |
32 | o = new_output(output_type=u'pyerr', etype=u'NameError', |
|
32 | o = new_output(output_type=u'pyerr', etype=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.assertEquals(o.output_type, u'pyerr') |
|
35 | self.assertEquals(o.output_type, u'pyerr') | |
36 | self.assertEquals(o.etype, u'NameError') |
|
36 | self.assertEquals(o.etype, u'NameError') | |
37 | self.assertEquals(o.evalue, u'Name not found') |
|
37 | self.assertEquals(o.evalue, u'Name not found') | |
38 | self.assertEquals(o.traceback, [u'frame 0', u'frame 1', u'frame 2']) |
|
38 | self.assertEquals(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.assertEquals(tc.cell_type, u'html') |
|
42 | self.assertEquals(tc.cell_type, u'html') | |
43 | self.assertEquals(u'source' not in tc, True) |
|
43 | self.assertEquals(u'source' not in tc, True) | |
44 | self.assertEquals(u'rendered' not in tc, True) |
|
44 | self.assertEquals(u'rendered' not in tc, True) | |
45 |
|
45 | |||
46 | def test_html_cell(self): |
|
46 | def test_html_cell(self): | |
47 | tc = new_text_cell(u'html', 'hi', 'hi') |
|
47 | tc = new_text_cell(u'html', 'hi', 'hi') | |
48 | self.assertEquals(tc.source, u'hi') |
|
48 | self.assertEquals(tc.source, u'hi') | |
49 | self.assertEquals(tc.rendered, u'hi') |
|
49 | self.assertEquals(tc.rendered, u'hi') | |
50 |
|
50 | |||
51 | def test_empty_markdown_cell(self): |
|
51 | def test_empty_markdown_cell(self): | |
52 | tc = new_text_cell(u'markdown') |
|
52 | tc = new_text_cell(u'markdown') | |
53 | self.assertEquals(tc.cell_type, u'markdown') |
|
53 | self.assertEquals(tc.cell_type, u'markdown') | |
54 | self.assertEquals(u'source' not in tc, True) |
|
54 | self.assertEquals(u'source' not in tc, True) | |
55 | self.assertEquals(u'rendered' not in tc, True) |
|
55 | self.assertEquals(u'rendered' not in tc, True) | |
56 |
|
56 | |||
57 | def test_markdown_cell(self): |
|
57 | def test_markdown_cell(self): | |
58 | tc = new_text_cell(u'markdown', 'hi', 'hi') |
|
58 | tc = new_text_cell(u'markdown', 'hi', 'hi') | |
59 | self.assertEquals(tc.source, u'hi') |
|
59 | self.assertEquals(tc.source, u'hi') | |
60 | self.assertEquals(tc.rendered, u'hi') |
|
60 | self.assertEquals(tc.rendered, u'hi') | |
61 |
|
61 | |||
62 |
def test_empty_ |
|
62 | def test_empty_plaintext_cell(self): | |
63 |
tc = new_text_cell(u' |
|
63 | tc = new_text_cell(u'plaintext') | |
64 |
self.assertEquals(tc.cell_type, u' |
|
64 | self.assertEquals(tc.cell_type, u'plaintext') | |
65 | self.assertEquals(u'source' not in tc, True) |
|
65 | self.assertEquals(u'source' not in tc, True) | |
66 | self.assertEquals(u'rendered' not in tc, True) |
|
66 | self.assertEquals(u'rendered' not in tc, True) | |
67 |
|
67 | |||
68 |
def test_ |
|
68 | def test_plaintext_cell(self): | |
69 |
tc = new_text_cell(u' |
|
69 | tc = new_text_cell(u'plaintext', 'hi', 'hi') | |
70 | self.assertEquals(tc.source, u'hi') |
|
70 | self.assertEquals(tc.source, u'hi') | |
71 | self.assertEquals(tc.rendered, u'hi') |
|
71 | self.assertEquals(tc.rendered, u'hi') | |
72 |
|
72 | |||
73 | def test_empty_heading_cell(self): |
|
73 | def test_empty_heading_cell(self): | |
74 | tc = new_heading_cell() |
|
74 | tc = new_heading_cell() | |
75 | self.assertEquals(tc.cell_type, u'heading') |
|
75 | self.assertEquals(tc.cell_type, u'heading') | |
76 | self.assertEquals(u'source' not in tc, True) |
|
76 | self.assertEquals(u'source' not in tc, True) | |
77 | self.assertEquals(u'rendered' not in tc, True) |
|
77 | self.assertEquals(u'rendered' not in tc, True) | |
78 |
|
78 | |||
79 | def test_heading_cell(self): |
|
79 | def test_heading_cell(self): | |
80 | tc = new_heading_cell(u'hi', u'hi', level=2) |
|
80 | tc = new_heading_cell(u'hi', u'hi', level=2) | |
81 | self.assertEquals(tc.source, u'hi') |
|
81 | self.assertEquals(tc.source, u'hi') | |
82 | self.assertEquals(tc.rendered, u'hi') |
|
82 | self.assertEquals(tc.rendered, u'hi') | |
83 | self.assertEquals(tc.level, 2) |
|
83 | self.assertEquals(tc.level, 2) | |
84 |
|
84 | |||
85 |
|
85 | |||
86 | class TestWorksheet(TestCase): |
|
86 | class TestWorksheet(TestCase): | |
87 |
|
87 | |||
88 | def test_empty_worksheet(self): |
|
88 | def test_empty_worksheet(self): | |
89 | ws = new_worksheet() |
|
89 | ws = new_worksheet() | |
90 | self.assertEquals(ws.cells,[]) |
|
90 | self.assertEquals(ws.cells,[]) | |
91 | self.assertEquals(u'name' not in ws, True) |
|
91 | self.assertEquals(u'name' not in ws, True) | |
92 |
|
92 | |||
93 | def test_worksheet(self): |
|
93 | def test_worksheet(self): | |
94 | cells = [new_code_cell(), new_text_cell(u'html')] |
|
94 | cells = [new_code_cell(), new_text_cell(u'html')] | |
95 | ws = new_worksheet(cells=cells,name=u'foo') |
|
95 | ws = new_worksheet(cells=cells,name=u'foo') | |
96 | self.assertEquals(ws.cells,cells) |
|
96 | self.assertEquals(ws.cells,cells) | |
97 | self.assertEquals(ws.name,u'foo') |
|
97 | self.assertEquals(ws.name,u'foo') | |
98 |
|
98 | |||
99 | class TestNotebook(TestCase): |
|
99 | class TestNotebook(TestCase): | |
100 |
|
100 | |||
101 | def test_empty_notebook(self): |
|
101 | def test_empty_notebook(self): | |
102 | nb = new_notebook() |
|
102 | nb = new_notebook() | |
103 | self.assertEquals(nb.worksheets, []) |
|
103 | self.assertEquals(nb.worksheets, []) | |
104 | self.assertEquals(nb.metadata, NotebookNode()) |
|
104 | self.assertEquals(nb.metadata, NotebookNode()) | |
105 | self.assertEquals(nb.nbformat,2) |
|
105 | self.assertEquals(nb.nbformat,2) | |
106 |
|
106 | |||
107 | def test_notebook(self): |
|
107 | def test_notebook(self): | |
108 | worksheets = [new_worksheet(),new_worksheet()] |
|
108 | worksheets = [new_worksheet(),new_worksheet()] | |
109 | metadata = new_metadata(name=u'foo') |
|
109 | metadata = new_metadata(name=u'foo') | |
110 | nb = new_notebook(metadata=metadata,worksheets=worksheets) |
|
110 | nb = new_notebook(metadata=metadata,worksheets=worksheets) | |
111 | self.assertEquals(nb.metadata.name,u'foo') |
|
111 | self.assertEquals(nb.metadata.name,u'foo') | |
112 | self.assertEquals(nb.worksheets,worksheets) |
|
112 | self.assertEquals(nb.worksheets,worksheets) | |
113 | self.assertEquals(nb.nbformat,2) |
|
113 | self.assertEquals(nb.nbformat,2) | |
114 |
|
114 | |||
115 | class TestMetadata(TestCase): |
|
115 | class TestMetadata(TestCase): | |
116 |
|
116 | |||
117 | def test_empty_metadata(self): |
|
117 | def test_empty_metadata(self): | |
118 | md = new_metadata() |
|
118 | md = new_metadata() | |
119 | self.assertEquals(u'name' not in md, True) |
|
119 | self.assertEquals(u'name' not in md, True) | |
120 | self.assertEquals(u'authors' not in md, True) |
|
120 | self.assertEquals(u'authors' not in md, True) | |
121 | self.assertEquals(u'license' not in md, True) |
|
121 | self.assertEquals(u'license' not in md, True) | |
122 | self.assertEquals(u'saved' not in md, True) |
|
122 | self.assertEquals(u'saved' not in md, True) | |
123 | self.assertEquals(u'modified' not in md, True) |
|
123 | self.assertEquals(u'modified' not in md, True) | |
124 | self.assertEquals(u'gistid' not in md, True) |
|
124 | self.assertEquals(u'gistid' not in md, True) | |
125 |
|
125 | |||
126 | def test_metadata(self): |
|
126 | def test_metadata(self): | |
127 | authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com')] |
|
127 | authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com')] | |
128 | md = new_metadata(name=u'foo',license=u'BSD',created=u'today', |
|
128 | md = new_metadata(name=u'foo',license=u'BSD',created=u'today', | |
129 | modified=u'now',gistid=u'21341231',authors=authors) |
|
129 | modified=u'now',gistid=u'21341231',authors=authors) | |
130 | self.assertEquals(md.name, u'foo') |
|
130 | self.assertEquals(md.name, u'foo') | |
131 | self.assertEquals(md.license, u'BSD') |
|
131 | self.assertEquals(md.license, u'BSD') | |
132 | self.assertEquals(md.created, u'today') |
|
132 | self.assertEquals(md.created, u'today') | |
133 | self.assertEquals(md.modified, u'now') |
|
133 | self.assertEquals(md.modified, u'now') | |
134 | self.assertEquals(md.gistid, u'21341231') |
|
134 | self.assertEquals(md.gistid, u'21341231') | |
135 | self.assertEquals(md.authors, authors) |
|
135 | self.assertEquals(md.authors, authors) | |
136 |
|
136 |
General Comments 0
You need to be logged in to leave comments.
Login now