##// END OF EJS Templates
Use logger string formatting, add some docstrings
Jessica B. Hamrick -
Show More
@@ -1,228 +1,237 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 * Jonathan Frederic
6 * Jonathan Frederic
7 """
7 """
8
8
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10 # Copyright (C) 2008-2011 The IPython Development Team
10 # Copyright (C) 2008-2011 The IPython Development Team
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 # Imports
17 # Imports
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19
19
20 from __future__ import print_function
20 from __future__ import print_function
21
21
22 from xml.etree import ElementTree as ET
22 from xml.etree import ElementTree as ET
23 import re
23 import re
24
24
25 from IPython.utils.py3compat import unicode_type
25 from IPython.utils.py3compat import unicode_type
26
26
27 from IPython.nbformat.v3 import (
27 from IPython.nbformat.v3 import (
28 NotebookNode,
28 NotebookNode,
29 new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet,
29 new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet,
30 parse_filename, new_metadata, new_author, new_heading_cell, nbformat,
30 parse_filename, new_metadata, new_author, new_heading_cell, nbformat,
31 nbformat_minor, nbformat_schema, to_notebook_json
31 nbformat_minor, nbformat_schema, to_notebook_json
32 )
32 )
33 from IPython.nbformat import v3 as _v_latest
33 from IPython.nbformat import v3 as _v_latest
34
34
35 from .reader import reads as reader_reads
35 from .reader import reads as reader_reads
36 from .reader import versions
36 from .reader import versions
37 from .convert import convert
37 from .convert import convert
38 from .validator import validate
38 from .validator import validate
39
39
40 import logging
40 import logging
41 logger = logging.getLogger('NotebookApp')
41 logger = logging.getLogger('NotebookApp')
42
42
43 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
44 # Code
44 # Code
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46
46
47 current_nbformat = nbformat
47 current_nbformat = nbformat
48 current_nbformat_minor = nbformat_minor
48 current_nbformat_minor = nbformat_minor
49 current_nbformat_module = _v_latest.__name__
49 current_nbformat_module = _v_latest.__name__
50
50
51
51
52 def docstring_nbformat_mod(func):
52 def docstring_nbformat_mod(func):
53 """Decorator for docstrings referring to classes/functions accessed through
53 """Decorator for docstrings referring to classes/functions accessed through
54 nbformat.current.
54 nbformat.current.
55
55
56 Put {nbformat_mod} in the docstring in place of 'IPython.nbformat.v3'.
56 Put {nbformat_mod} in the docstring in place of 'IPython.nbformat.v3'.
57 """
57 """
58 func.__doc__ = func.__doc__.format(nbformat_mod=current_nbformat_module)
58 func.__doc__ = func.__doc__.format(nbformat_mod=current_nbformat_module)
59 return func
59 return func
60
60
61
61
62 class NBFormatError(ValueError):
62 class NBFormatError(ValueError):
63 pass
63 pass
64
64
65
65
66 def parse_py(s, **kwargs):
66 def parse_py(s, **kwargs):
67 """Parse a string into a (nbformat, string) tuple."""
67 """Parse a string into a (nbformat, string) tuple."""
68 nbf = current_nbformat
68 nbf = current_nbformat
69 nbm = current_nbformat_minor
69 nbm = current_nbformat_minor
70
70
71 pattern = r'# <nbformat>(?P<nbformat>\d+[\.\d+]*)</nbformat>'
71 pattern = r'# <nbformat>(?P<nbformat>\d+[\.\d+]*)</nbformat>'
72 m = re.search(pattern,s)
72 m = re.search(pattern,s)
73 if m is not None:
73 if m is not None:
74 digits = m.group('nbformat').split('.')
74 digits = m.group('nbformat').split('.')
75 nbf = int(digits[0])
75 nbf = int(digits[0])
76 if len(digits) > 1:
76 if len(digits) > 1:
77 nbm = int(digits[1])
77 nbm = int(digits[1])
78
78
79 return nbf, nbm, s
79 return nbf, nbm, s
80
80
81
81
82 def reads_json(s, **kwargs):
82 def reads_json(s, **kwargs):
83 """Read a JSON notebook from a string and return the NotebookNode object."""
83 """Read a JSON notebook from a string and return the NotebookNode
84 object. Report if any JSON format errors are detected.
85
86 """
84 nbjson = reader_reads(s)
87 nbjson = reader_reads(s)
85 num_errors = validate(nbjson)
88 num_errors = validate(nbjson)
86 if num_errors > 0:
89 if num_errors > 0:
87 logger.error(
90 logger.error(
88 "Notebook JSON is invalid (%d errors detected)" % num_errors)
91 "Notebook JSON is invalid (%d errors detected)",
92 num_errors)
89 return convert(nbjson, current_nbformat)
93 return convert(nbjson, current_nbformat)
90
94
91
95
92 def writes_json(nb, **kwargs):
96 def writes_json(nb, **kwargs):
97 """Take a NotebookNode object and write out a JSON string. Report if
98 any JSON format errors are detected.
99
100 """
93 nbjson = versions[current_nbformat].writes_json(nb, **kwargs)
101 nbjson = versions[current_nbformat].writes_json(nb, **kwargs)
94 num_errors = validate(nbjson)
102 num_errors = validate(nbjson)
95 if num_errors > 0:
103 if num_errors > 0:
96 logger.error(
104 logger.error(
97 "Notebook JSON is invalid (%d errors detected)" % num_errors)
105 "Notebook JSON is invalid (%d errors detected)",
106 num_errors)
98 return nbjson
107 return nbjson
99
108
100
109
101 def reads_py(s, **kwargs):
110 def reads_py(s, **kwargs):
102 """Read a .py notebook from a string and return the NotebookNode object."""
111 """Read a .py notebook from a string and return the NotebookNode object."""
103 nbf, nbm, s = parse_py(s, **kwargs)
112 nbf, nbm, s = parse_py(s, **kwargs)
104 if nbf in (2, 3):
113 if nbf in (2, 3):
105 nb = versions[nbf].to_notebook_py(s, **kwargs)
114 nb = versions[nbf].to_notebook_py(s, **kwargs)
106 else:
115 else:
107 raise NBFormatError('Unsupported PY nbformat version: %i' % nbf)
116 raise NBFormatError('Unsupported PY nbformat version: %i' % nbf)
108 return nb
117 return nb
109
118
110
119
111 def writes_py(nb, **kwargs):
120 def writes_py(nb, **kwargs):
112 # nbformat 3 is the latest format that supports py
121 # nbformat 3 is the latest format that supports py
113 return versions[3].writes_py(nb, **kwargs)
122 return versions[3].writes_py(nb, **kwargs)
114
123
115
124
116 # High level API
125 # High level API
117
126
118
127
119 def reads(s, format, **kwargs):
128 def reads(s, format, **kwargs):
120 """Read a notebook from a string and return the NotebookNode object.
129 """Read a notebook from a string and return the NotebookNode object.
121
130
122 This function properly handles notebooks of any version. The notebook
131 This function properly handles notebooks of any version. The notebook
123 returned will always be in the current version's format.
132 returned will always be in the current version's format.
124
133
125 Parameters
134 Parameters
126 ----------
135 ----------
127 s : unicode
136 s : unicode
128 The raw unicode string to read the notebook from.
137 The raw unicode string to read the notebook from.
129 format : (u'json', u'ipynb', u'py')
138 format : (u'json', u'ipynb', u'py')
130 The format that the string is in.
139 The format that the string is in.
131
140
132 Returns
141 Returns
133 -------
142 -------
134 nb : NotebookNode
143 nb : NotebookNode
135 The notebook that was read.
144 The notebook that was read.
136 """
145 """
137 format = unicode_type(format)
146 format = unicode_type(format)
138 if format == u'json' or format == u'ipynb':
147 if format == u'json' or format == u'ipynb':
139 return reads_json(s, **kwargs)
148 return reads_json(s, **kwargs)
140 elif format == u'py':
149 elif format == u'py':
141 return reads_py(s, **kwargs)
150 return reads_py(s, **kwargs)
142 else:
151 else:
143 raise NBFormatError('Unsupported format: %s' % format)
152 raise NBFormatError('Unsupported format: %s' % format)
144
153
145
154
146 def writes(nb, format, **kwargs):
155 def writes(nb, format, **kwargs):
147 """Write a notebook to a string in a given format in the current nbformat version.
156 """Write a notebook to a string in a given format in the current nbformat version.
148
157
149 This function always writes the notebook in the current nbformat version.
158 This function always writes the notebook in the current nbformat version.
150
159
151 Parameters
160 Parameters
152 ----------
161 ----------
153 nb : NotebookNode
162 nb : NotebookNode
154 The notebook to write.
163 The notebook to write.
155 format : (u'json', u'ipynb', u'py')
164 format : (u'json', u'ipynb', u'py')
156 The format to write the notebook in.
165 The format to write the notebook in.
157
166
158 Returns
167 Returns
159 -------
168 -------
160 s : unicode
169 s : unicode
161 The notebook string.
170 The notebook string.
162 """
171 """
163 format = unicode_type(format)
172 format = unicode_type(format)
164 if format == u'json' or format == u'ipynb':
173 if format == u'json' or format == u'ipynb':
165 return writes_json(nb, **kwargs)
174 return writes_json(nb, **kwargs)
166 elif format == u'py':
175 elif format == u'py':
167 return writes_py(nb, **kwargs)
176 return writes_py(nb, **kwargs)
168 else:
177 else:
169 raise NBFormatError('Unsupported format: %s' % format)
178 raise NBFormatError('Unsupported format: %s' % format)
170
179
171
180
172 def read(fp, format, **kwargs):
181 def read(fp, format, **kwargs):
173 """Read a notebook from a file and return the NotebookNode object.
182 """Read a notebook from a file and return the NotebookNode object.
174
183
175 This function properly handles notebooks of any version. The notebook
184 This function properly handles notebooks of any version. The notebook
176 returned will always be in the current version's format.
185 returned will always be in the current version's format.
177
186
178 Parameters
187 Parameters
179 ----------
188 ----------
180 fp : file
189 fp : file
181 Any file-like object with a read method.
190 Any file-like object with a read method.
182 format : (u'json', u'ipynb', u'py')
191 format : (u'json', u'ipynb', u'py')
183 The format that the string is in.
192 The format that the string is in.
184
193
185 Returns
194 Returns
186 -------
195 -------
187 nb : NotebookNode
196 nb : NotebookNode
188 The notebook that was read.
197 The notebook that was read.
189 """
198 """
190 return reads(fp.read(), format, **kwargs)
199 return reads(fp.read(), format, **kwargs)
191
200
192
201
193 def write(nb, fp, format, **kwargs):
202 def write(nb, fp, format, **kwargs):
194 """Write a notebook to a file in a given format in the current nbformat version.
203 """Write a notebook to a file in a given format in the current nbformat version.
195
204
196 This function always writes the notebook in the current nbformat version.
205 This function always writes the notebook in the current nbformat version.
197
206
198 Parameters
207 Parameters
199 ----------
208 ----------
200 nb : NotebookNode
209 nb : NotebookNode
201 The notebook to write.
210 The notebook to write.
202 fp : file
211 fp : file
203 Any file-like object with a write method.
212 Any file-like object with a write method.
204 format : (u'json', u'ipynb', u'py')
213 format : (u'json', u'ipynb', u'py')
205 The format to write the notebook in.
214 The format to write the notebook in.
206
215
207 Returns
216 Returns
208 -------
217 -------
209 s : unicode
218 s : unicode
210 The notebook string.
219 The notebook string.
211 """
220 """
212 return fp.write(writes(nb, format, **kwargs))
221 return fp.write(writes(nb, format, **kwargs))
213
222
214 def _convert_to_metadata():
223 def _convert_to_metadata():
215 """Convert to a notebook having notebook metadata."""
224 """Convert to a notebook having notebook metadata."""
216 import glob
225 import glob
217 for fname in glob.glob('*.ipynb'):
226 for fname in glob.glob('*.ipynb'):
218 print('Converting file:',fname)
227 print('Converting file:',fname)
219 with open(fname,'r') as f:
228 with open(fname,'r') as f:
220 nb = read(f,u'json')
229 nb = read(f,u'json')
221 md = new_metadata()
230 md = new_metadata()
222 if u'name' in nb:
231 if u'name' in nb:
223 md.name = nb.name
232 md.name = nb.name
224 del nb[u'name']
233 del nb[u'name']
225 nb.metadata = md
234 nb.metadata = md
226 with open(fname,'w') as f:
235 with open(fname,'w') as f:
227 write(nb, f, u'json')
236 write(nb, f, u'json')
228
237
General Comments 0
You need to be logged in to leave comments. Login now