Show More
@@ -1,150 +1,150 b'' | |||
|
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 | 27 | _encoding_declaration_re = re.compile(r"^#\s*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 = [] | |
|
117 |
lines.extend([ |
|
|
116 | lines = [u'# coding: utf-8'] | |
|
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 |
General Comments 0
You need to be logged in to leave comments.
Login now