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