##// END OF EJS Templates
Use emacs-recognised encoding declaration.
Thomas Kluyver -
Show More
@@ -1,150 +1,150
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 new_code_cell, new_text_cell, new_worksheet, new_notebook
21 from .nbbase import new_code_cell, new_text_cell, new_worksheet, new_notebook
22
22
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24 # Code
24 # Code
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
26
27 _encoding_declaration_re = re.compile(r"^#\s*coding[:=]\s*([-\w.]+)")
27 _encoding_declaration_re = re.compile(r"^#.*coding[:=]\s*([-\w.]+)")
28
28
29 class PyReaderError(Exception):
29 class PyReaderError(Exception):
30 pass
30 pass
31
31
32
32
33 class PyReader(NotebookReader):
33 class PyReader(NotebookReader):
34
34
35 def reads(self, s, **kwargs):
35 def reads(self, s, **kwargs):
36 return self.to_notebook(s,**kwargs)
36 return self.to_notebook(s,**kwargs)
37
37
38 def to_notebook(self, s, **kwargs):
38 def to_notebook(self, s, **kwargs):
39 lines = s.splitlines()
39 lines = s.splitlines()
40 cells = []
40 cells = []
41 cell_lines = []
41 cell_lines = []
42 state = u'codecell'
42 state = u'codecell'
43 for line in lines:
43 for line in lines:
44 if line.startswith(u'# <nbformat>') or _encoding_declaration_re.match(line):
44 if line.startswith(u'# <nbformat>') or _encoding_declaration_re.match(line):
45 pass
45 pass
46 elif line.startswith(u'# <codecell>'):
46 elif line.startswith(u'# <codecell>'):
47 cell = self.new_cell(state, cell_lines)
47 cell = self.new_cell(state, cell_lines)
48 if cell is not None:
48 if cell is not None:
49 cells.append(cell)
49 cells.append(cell)
50 state = u'codecell'
50 state = u'codecell'
51 cell_lines = []
51 cell_lines = []
52 elif line.startswith(u'# <htmlcell>'):
52 elif line.startswith(u'# <htmlcell>'):
53 cell = self.new_cell(state, cell_lines)
53 cell = self.new_cell(state, cell_lines)
54 if cell is not None:
54 if cell is not None:
55 cells.append(cell)
55 cells.append(cell)
56 state = u'htmlcell'
56 state = u'htmlcell'
57 cell_lines = []
57 cell_lines = []
58 elif line.startswith(u'# <markdowncell>'):
58 elif line.startswith(u'# <markdowncell>'):
59 cell = self.new_cell(state, cell_lines)
59 cell = self.new_cell(state, cell_lines)
60 if cell is not None:
60 if cell is not None:
61 cells.append(cell)
61 cells.append(cell)
62 state = u'markdowncell'
62 state = u'markdowncell'
63 cell_lines = []
63 cell_lines = []
64 else:
64 else:
65 cell_lines.append(line)
65 cell_lines.append(line)
66 if cell_lines and state == u'codecell':
66 if cell_lines and state == u'codecell':
67 cell = self.new_cell(state, cell_lines)
67 cell = self.new_cell(state, cell_lines)
68 if cell is not None:
68 if cell is not None:
69 cells.append(cell)
69 cells.append(cell)
70 ws = new_worksheet(cells=cells)
70 ws = new_worksheet(cells=cells)
71 nb = new_notebook(worksheets=[ws])
71 nb = new_notebook(worksheets=[ws])
72 return nb
72 return nb
73
73
74 def new_cell(self, state, lines):
74 def new_cell(self, state, lines):
75 if state == u'codecell':
75 if state == u'codecell':
76 input = u'\n'.join(lines)
76 input = u'\n'.join(lines)
77 input = input.strip(u'\n')
77 input = input.strip(u'\n')
78 if input:
78 if input:
79 return new_code_cell(input=input)
79 return new_code_cell(input=input)
80 elif state == u'htmlcell':
80 elif state == u'htmlcell':
81 text = self._remove_comments(lines)
81 text = self._remove_comments(lines)
82 if text:
82 if text:
83 return new_text_cell(u'html',source=text)
83 return new_text_cell(u'html',source=text)
84 elif state == u'markdowncell':
84 elif state == u'markdowncell':
85 text = self._remove_comments(lines)
85 text = self._remove_comments(lines)
86 if text:
86 if text:
87 return new_text_cell(u'markdown',source=text)
87 return new_text_cell(u'markdown',source=text)
88
88
89 def _remove_comments(self, lines):
89 def _remove_comments(self, lines):
90 new_lines = []
90 new_lines = []
91 for line in lines:
91 for line in lines:
92 if line.startswith(u'#'):
92 if line.startswith(u'#'):
93 new_lines.append(line[2:])
93 new_lines.append(line[2:])
94 else:
94 else:
95 new_lines.append(line)
95 new_lines.append(line)
96 text = u'\n'.join(new_lines)
96 text = u'\n'.join(new_lines)
97 text = text.strip(u'\n')
97 text = text.strip(u'\n')
98 return text
98 return text
99
99
100 def split_lines_into_blocks(self, lines):
100 def split_lines_into_blocks(self, lines):
101 if len(lines) == 1:
101 if len(lines) == 1:
102 yield lines[0]
102 yield lines[0]
103 raise StopIteration()
103 raise StopIteration()
104 import ast
104 import ast
105 source = '\n'.join(lines)
105 source = '\n'.join(lines)
106 code = ast.parse(source)
106 code = ast.parse(source)
107 starts = [x.lineno-1 for x in code.body]
107 starts = [x.lineno-1 for x in code.body]
108 for i in range(len(starts)-1):
108 for i in range(len(starts)-1):
109 yield '\n'.join(lines[starts[i]:starts[i+1]]).strip('\n')
109 yield '\n'.join(lines[starts[i]:starts[i+1]]).strip('\n')
110 yield '\n'.join(lines[starts[-1]:]).strip('\n')
110 yield '\n'.join(lines[starts[-1]:]).strip('\n')
111
111
112
112
113 class PyWriter(NotebookWriter):
113 class PyWriter(NotebookWriter):
114
114
115 def writes(self, nb, **kwargs):
115 def writes(self, nb, **kwargs):
116 lines = [u'# coding: utf-8']
116 lines = [u'# -*- coding: utf-8 -*-']
117 lines.extend([u'# <nbformat>2</nbformat>',''])
117 lines.extend([u'# <nbformat>2</nbformat>',''])
118 for ws in nb.worksheets:
118 for ws in nb.worksheets:
119 for cell in ws.cells:
119 for cell in ws.cells:
120 if cell.cell_type == u'code':
120 if cell.cell_type == u'code':
121 input = cell.get(u'input')
121 input = cell.get(u'input')
122 if input is not None:
122 if input is not None:
123 lines.extend([u'# <codecell>',u''])
123 lines.extend([u'# <codecell>',u''])
124 lines.extend(input.splitlines())
124 lines.extend(input.splitlines())
125 lines.append(u'')
125 lines.append(u'')
126 elif cell.cell_type == u'html':
126 elif cell.cell_type == u'html':
127 input = cell.get(u'source')
127 input = cell.get(u'source')
128 if input is not None:
128 if input is not None:
129 lines.extend([u'# <htmlcell>',u''])
129 lines.extend([u'# <htmlcell>',u''])
130 lines.extend([u'# ' + line for line in input.splitlines()])
130 lines.extend([u'# ' + line for line in input.splitlines()])
131 lines.append(u'')
131 lines.append(u'')
132 elif cell.cell_type == u'markdown':
132 elif cell.cell_type == u'markdown':
133 input = cell.get(u'source')
133 input = cell.get(u'source')
134 if input is not None:
134 if input is not None:
135 lines.extend([u'# <markdowncell>',u''])
135 lines.extend([u'# <markdowncell>',u''])
136 lines.extend([u'# ' + line for line in input.splitlines()])
136 lines.extend([u'# ' + line for line in input.splitlines()])
137 lines.append(u'')
137 lines.append(u'')
138 lines.append('')
138 lines.append('')
139 return unicode('\n'.join(lines))
139 return unicode('\n'.join(lines))
140
140
141
141
142 _reader = PyReader()
142 _reader = PyReader()
143 _writer = PyWriter()
143 _writer = PyWriter()
144
144
145 reads = _reader.reads
145 reads = _reader.reads
146 read = _reader.read
146 read = _reader.read
147 to_notebook = _reader.to_notebook
147 to_notebook = _reader.to_notebook
148 write = _writer.write
148 write = _writer.write
149 writes = _writer.writes
149 writes = _writer.writes
150
150
@@ -1,109 +1,109
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
7 new_metadata, new_author
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_code_cell(
35 ws.cells.append(new_code_cell(
36 input='a = numpy.random.rand(100)',
36 input='a = numpy.random.rand(100)',
37 prompt_number=2,
37 prompt_number=2,
38 collapsed=True
38 collapsed=True
39 ))
39 ))
40
40
41 ws.cells.append(new_code_cell(
41 ws.cells.append(new_code_cell(
42 input='print a',
42 input='print a',
43 prompt_number=3,
43 prompt_number=3,
44 collapsed=False,
44 collapsed=False,
45 outputs=[new_output(
45 outputs=[new_output(
46 output_type=u'pyout',
46 output_type=u'pyout',
47 output_text=u'<array a>',
47 output_text=u'<array a>',
48 output_html=u'The HTML rep',
48 output_html=u'The HTML rep',
49 output_latex=u'$a$',
49 output_latex=u'$a$',
50 output_png=png,
50 output_png=png,
51 output_jpeg=jpeg,
51 output_jpeg=jpeg,
52 output_svg=u'<svg>',
52 output_svg=u'<svg>',
53 output_json=u'json data',
53 output_json=u'json data',
54 output_javascript=u'var i=0;',
54 output_javascript=u'var i=0;',
55 prompt_number=3
55 prompt_number=3
56 ),new_output(
56 ),new_output(
57 output_type=u'display_data',
57 output_type=u'display_data',
58 output_text=u'<array a>',
58 output_text=u'<array a>',
59 output_html=u'The HTML rep',
59 output_html=u'The HTML rep',
60 output_latex=u'$a$',
60 output_latex=u'$a$',
61 output_png=png,
61 output_png=png,
62 output_jpeg=jpeg,
62 output_jpeg=jpeg,
63 output_svg=u'<svg>',
63 output_svg=u'<svg>',
64 output_json=u'json data',
64 output_json=u'json data',
65 output_javascript=u'var i=0;'
65 output_javascript=u'var i=0;'
66 ),new_output(
66 ),new_output(
67 output_type=u'pyerr',
67 output_type=u'pyerr',
68 etype=u'NameError',
68 etype=u'NameError',
69 evalue=u'NameError was here',
69 evalue=u'NameError was here',
70 traceback=[u'frame 0', u'frame 1', u'frame 2']
70 traceback=[u'frame 0', u'frame 1', u'frame 2']
71 )]
71 )]
72 ))
72 ))
73
73
74 authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com',
74 authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com',
75 affiliation=u'Fox',url=u'http://www.fox.com')]
75 affiliation=u'Fox',url=u'http://www.fox.com')]
76 md = new_metadata(name=u'My Notebook',license=u'BSD',created=u'8601_goes_here',
76 md = new_metadata(name=u'My Notebook',license=u'BSD',created=u'8601_goes_here',
77 modified=u'8601_goes_here',gistid=u'21341231',authors=authors)
77 modified=u'8601_goes_here',gistid=u'21341231',authors=authors)
78
78
79 nb0 = new_notebook(
79 nb0 = new_notebook(
80 worksheets=[ws, new_worksheet(name='worksheet2')],
80 worksheets=[ws, new_worksheet(name='worksheet2')],
81 metadata=md
81 metadata=md
82 )
82 )
83
83
84 nb0_py = """# coding: utf-8
84 nb0_py = """# -*- coding: utf-8 -*-
85 # <nbformat>2</nbformat>
85 # <nbformat>2</nbformat>
86
86
87 # <htmlcell>
87 # <htmlcell>
88
88
89 # Some NumPy Examples
89 # Some NumPy Examples
90
90
91 # <codecell>
91 # <codecell>
92
92
93 import numpy
93 import numpy
94
94
95 # <markdowncell>
95 # <markdowncell>
96
96
97 # A random array
97 # A random array
98
98
99 # <codecell>
99 # <codecell>
100
100
101 a = numpy.random.rand(100)
101 a = numpy.random.rand(100)
102
102
103 # <codecell>
103 # <codecell>
104
104
105 print a
105 print a
106
106
107 """
107 """
108
108
109
109
General Comments 0
You need to be logged in to leave comments. Login now