-
Notifications
You must be signed in to change notification settings - Fork 153
/
current.py
192 lines (146 loc) · 5.23 KB
/
current.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""Deprecated API for working with notebooks
- use nbformat for read/write/validate public API
- use nbformat.vX directly for Python API for composing notebooks
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import print_function
import re
import warnings
warnings.warn("""nbformat.current is deprecated.
- use nbformat for read/write/validate public API
- use nbformat.vX directly to composing notebooks of a particular version
""")
from nbformat.v3 import (
NotebookNode,
new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet,
parse_filename, new_metadata, new_author, new_heading_cell, nbformat,
nbformat_minor, nbformat_schema, to_notebook_json,
)
from nbformat import v3 as _v_latest
from .reader import reads as reader_reads
from . import versions
from .converter import convert
from .validator import validate, ValidationError
from traitlets.log import get_logger
__all__ = ['NotebookNode', 'new_code_cell', 'new_text_cell', 'new_notebook',
'new_output', 'new_worksheet', 'parse_filename', 'new_metadata', 'new_author',
'new_heading_cell', 'nbformat', 'nbformat_minor', 'nbformat_schema',
'to_notebook_json', 'convert', 'validate', 'NBFormatError', 'parse_py',
'reads_json', 'writes_json', 'reads_py', 'writes_py', 'reads', 'writes', 'read',
'write']
current_nbformat = nbformat
current_nbformat_minor = nbformat_minor
current_nbformat_module = _v_latest.__name__
class NBFormatError(ValueError):
pass
def _warn_format():
warnings.warn("""Non-JSON file support in nbformat is deprecated.
Use nbconvert to create files of other formats.""")
def parse_py(s, **kwargs):
"""Parse a string into a (nbformat, string) tuple."""
nbf = current_nbformat
nbm = current_nbformat_minor
pattern = r'# <nbformat>(?P<nbformat>\d+[\.\d+]*)</nbformat>'
m = re.search(pattern,s)
if m is not None:
digits = m.group('nbformat').split('.')
nbf = int(digits[0])
if len(digits) > 1:
nbm = int(digits[1])
return nbf, nbm, s
def reads_json(nbjson, **kwargs):
"""DEPRECATED, use reads"""
warnings.warn("reads_json is deprecated, use reads")
return reads(nbjson)
def writes_json(nb, **kwargs):
"""DEPRECATED, use writes"""
warnings.warn("writes_json is deprecated, use writes")
return writes(nb, **kwargs)
def reads_py(s, **kwargs):
"""DEPRECATED: use nbconvert"""
_warn_format()
nbf, nbm, s = parse_py(s, **kwargs)
if nbf in (2, 3):
nb = versions[nbf].to_notebook_py(s, **kwargs)
else:
raise NBFormatError('Unsupported PY nbformat version: %i' % nbf)
return nb
def writes_py(nb, **kwargs):
"""DEPRECATED: use nbconvert"""
_warn_format()
return versions[3].writes_py(nb, **kwargs)
# High level API
def reads(s, format='DEPRECATED', version=current_nbformat, **kwargs):
"""Read a notebook from a string and return the NotebookNode object.
This function properly handles notebooks of any version. The notebook
returned will always be in the current version's format.
Parameters
----------
s : unicode
The raw unicode string to read the notebook from.
Returns
-------
nb : NotebookNode
The notebook that was read.
"""
if format not in {'DEPRECATED', 'json'}:
_warn_format()
nb = reader_reads(s, **kwargs)
nb = convert(nb, version)
try:
validate(nb)
except ValidationError as e:
get_logger().error("Notebook JSON is invalid: %s", e)
return nb
def writes(nb, format='DEPRECATED', version=current_nbformat, **kwargs):
"""Write a notebook to a string in a given format in the current nbformat version.
This function always writes the notebook in the current nbformat version.
Parameters
----------
nb : NotebookNode
The notebook to write.
version : int
The nbformat version to write.
Used for downgrading notebooks.
Returns
-------
s : unicode
The notebook string.
"""
if format not in {'DEPRECATED', 'json'}:
_warn_format()
nb = convert(nb, version)
try:
validate(nb)
except ValidationError as e:
get_logger().error("Notebook JSON is invalid: %s", e)
return versions[version].writes_json(nb, **kwargs)
def read(fp, format='DEPRECATED', **kwargs):
"""Read a notebook from a file and return the NotebookNode object.
This function properly handles notebooks of any version. The notebook
returned will always be in the current version's format.
Parameters
----------
fp : file
Any file-like object with a read method.
Returns
-------
nb : NotebookNode
The notebook that was read.
"""
return reads(fp.read(), **kwargs)
def write(nb, fp, format='DEPRECATED', **kwargs):
"""Write a notebook to a file in a given format in the current nbformat version.
This function always writes the notebook in the current nbformat version.
Parameters
----------
nb : NotebookNode
The notebook to write.
fp : file
Any file-like object with a write method.
"""
s = writes(nb, **kwargs)
if isinstance(s, bytes):
s = s.decode('utf8')
return fp.write(s)