current.py
182 lines
| 5.0 KiB
| text/x-python
|
PythonLexer
MinRK
|
r17057 | """The official API for working with notebooks in the current format version.""" | ||
Brian E. Granger
|
r4609 | |||
MinRK
|
r18243 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
Brian E. Granger
|
r4637 | from __future__ import print_function | ||
Jonathan Frederic
|
r12493 | |||
Brian E. Granger
|
r4406 | import re | ||
MinRK
|
r18253 | import warnings | ||
Thomas Kluyver
|
r13353 | |||
Brian Granger
|
r6025 | from IPython.nbformat.v3 import ( | ||
Brian E. Granger
|
r4484 | NotebookNode, | ||
Brian E. Granger
|
r4520 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, | ||
MinRK
|
r7545 | parse_filename, new_metadata, new_author, new_heading_cell, nbformat, | ||
MinRK
|
r18253 | nbformat_minor, nbformat_schema, to_notebook_json, | ||
Brian E. Granger
|
r4484 | ) | ||
Thomas Kluyver
|
r13627 | from IPython.nbformat import v3 as _v_latest | ||
Brian E. Granger
|
r4484 | |||
Jonathan Frederic
|
r12753 | from .reader import reads as reader_reads | ||
from .reader import versions | ||||
Jonathan Frederic
|
r12493 | from .convert import convert | ||
MinRK
|
r18243 | from .validator import validate, ValidationError | ||
Jonathan Frederic
|
r12493 | |||
MinRK
|
r17057 | from IPython.utils.log import get_logger | ||
Jessica B. Hamrick
|
r16342 | |||
Thomas Kluyver
|
r17120 | __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'] | ||||
Brian E. Granger
|
r4406 | |||
Brian Granger
|
r6061 | current_nbformat = nbformat | ||
MinRK
|
r7545 | current_nbformat_minor = nbformat_minor | ||
Thomas Kluyver
|
r13627 | current_nbformat_module = _v_latest.__name__ | ||
Jessica B. Hamrick
|
r16330 | |||
MinRK
|
r11643 | class NBFormatError(ValueError): | ||
pass | ||||
Brian E. Granger
|
r4406 | |||
MinRK
|
r18253 | def _warn_format(): | ||
warnings.warn("""Non-JSON file support in nbformat is deprecated. | ||||
Use nbconvert to create files of other formats.""") | ||||
Brian E. Granger
|
r4406 | def parse_py(s, **kwargs): | ||
"""Parse a string into a (nbformat, string) tuple.""" | ||||
MinRK
|
r7566 | nbf = current_nbformat | ||
nbm = current_nbformat_minor | ||||
pattern = r'# <nbformat>(?P<nbformat>\d+[\.\d+]*)</nbformat>' | ||||
Brian E. Granger
|
r4406 | m = re.search(pattern,s) | ||
if m is not None: | ||||
MinRK
|
r7566 | digits = m.group('nbformat').split('.') | ||
nbf = int(digits[0]) | ||||
if len(digits) > 1: | ||||
nbm = int(digits[1]) | ||||
return nbf, nbm, s | ||||
Brian E. Granger
|
r4406 | |||
Jessica B. Hamrick
|
r16403 | def reads_json(nbjson, **kwargs): | ||
MinRK
|
r18253 | """DEPRECATED, use reads""" | ||
warnings.warn("reads_json is deprecated, use reads") | ||||
return reads(nbjson) | ||||
Brian E. Granger
|
r4406 | |||
def writes_json(nb, **kwargs): | ||||
MinRK
|
r18253 | """DEPRECATED, use writes""" | ||
warnings.warn("writes_json is deprecated, use writes") | ||||
return writes(nb, **kwargs) | ||||
Brian E. Granger
|
r4406 | |||
def reads_py(s, **kwargs): | ||||
MinRK
|
r18253 | """DEPRECATED: use nbconvert""" | ||
_warn_format() | ||||
MinRK
|
r7566 | nbf, nbm, s = parse_py(s, **kwargs) | ||
Jonathan Frederic
|
r12755 | if nbf in (2, 3): | ||
Jonathan Frederic
|
r12753 | nb = versions[nbf].to_notebook_py(s, **kwargs) | ||
Brian E. Granger
|
r4406 | else: | ||
Brian Granger
|
r6061 | raise NBFormatError('Unsupported PY nbformat version: %i' % nbf) | ||
Brian E. Granger
|
r4406 | return nb | ||
def writes_py(nb, **kwargs): | ||||
MinRK
|
r18253 | """DEPRECATED: use nbconvert""" | ||
_warn_format() | ||||
Jonathan Frederic
|
r12753 | return versions[3].writes_py(nb, **kwargs) | ||
Brian E. Granger
|
r4406 | |||
# High level API | ||||
MinRK
|
r18246 | def reads(s, format='DEPRECATED', version=current_nbformat, **kwargs): | ||
Brian E. Granger
|
r4406 | """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 | ||||
---------- | ||||
Brian E. Granger
|
r4637 | s : unicode | ||
The raw unicode string to read the notebook from. | ||||
Brian E. Granger
|
r4406 | |||
Returns | ||||
------- | ||||
nb : NotebookNode | ||||
The notebook that was read. | ||||
""" | ||||
MinRK
|
r18253 | if format not in {'DEPRECATED', 'json'}: | ||
_warn_format() | ||||
nb = reader_reads(s, **kwargs) | ||||
MinRK
|
r18246 | nb = convert(nb, version) | ||
try: | ||||
validate(nb) | ||||
except ValidationError as e: | ||||
get_logger().error("Notebook JSON is invalid: %s", e) | ||||
return nb | ||||
Brian E. Granger
|
r4406 | |||
MinRK
|
r18246 | def writes(nb, format='DEPRECATED', version=current_nbformat, **kwargs): | ||
Brian E. Granger
|
r4406 | """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. | ||||
MinRK
|
r18246 | version : int | ||
The nbformat version to write. | ||||
Used for downgrading notebooks. | ||||
Brian E. Granger
|
r4406 | |||
Returns | ||||
------- | ||||
Brian E. Granger
|
r4637 | s : unicode | ||
Brian E. Granger
|
r4406 | The notebook string. | ||
""" | ||||
MinRK
|
r18253 | if format not in {'DEPRECATED', 'json'}: | ||
_warn_format() | ||||
nb = convert(nb, version) | ||||
MinRK
|
r18246 | try: | ||
validate(nb) | ||||
except ValidationError as e: | ||||
get_logger().error("Notebook JSON is invalid: %s", e) | ||||
return versions[version].writes_json(nb, **kwargs) | ||||
Brian E. Granger
|
r4406 | |||
MinRK
|
r18246 | def read(fp, format='DEPRECATED', **kwargs): | ||
Brian E. Granger
|
r4406 | """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. | ||||
""" | ||||
MinRK
|
r18246 | return reads(fp.read(), **kwargs) | ||
Brian E. Granger
|
r4406 | |||
MinRK
|
r18246 | def write(nb, fp, format='DEPRECATED', **kwargs): | ||
Brian E. Granger
|
r4406 | """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. | ||||
""" | ||||
MinRK
|
r18261 | s = writes(nb, **kwargs) | ||
if isinstance(s, bytes): | ||||
s = s.decode('utf8') | ||||
return fp.write(s) | ||||
Brian E. Granger
|
r4406 | |||