##// END OF EJS Templates
don't pick up tokens right of cursor...
don't pick up tokens right of cursor inspection of `foo(|a` was picking up 'a' instead of 'foo' due to an off by one error

File last commit:

r18261:8b08f0cd
r18453:c04db182
Show More
current.py
182 lines | 5.0 KiB | text/x-python | PythonLexer
MinRK
use utils.log.get_logger where appropriate
r17057 """The official API for working with notebooks in the current format version."""
Brian E. Granger
More review changes....
r4609
MinRK
Use Draft4 JSON Schema for both v3 and v4...
r18243 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Brian E. Granger
Implemented metadata for notebook format.
r4637 from __future__ import print_function
Jonathan Frederic
Notebook version conversions done right?
r12493
Brian E. Granger
Full versioning added to nbformat.
r4406 import re
MinRK
deprecate format arg in reads/writes...
r18253 import warnings
Thomas Kluyver
Replace references to unicode and basestring
r13353
Brian Granger
Changing rst->plaintext in nbformat.
r6025 from IPython.nbformat.v3 import (
Brian E. Granger
Massive work on the notebook document format....
r4484 NotebookNode,
Brian E. Granger
Created new notebook magic that can export/convert notebooks....
r4520 new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet,
MinRK
add nbformat_minor for minor revisions to nbformat
r7545 parse_filename, new_metadata, new_author, new_heading_cell, nbformat,
MinRK
deprecate format arg in reads/writes...
r18253 nbformat_minor, nbformat_schema, to_notebook_json,
Brian E. Granger
Massive work on the notebook document format....
r4484 )
Thomas Kluyver
Decorator for docstrings referring to objects imported from nbformat.current
r13627 from IPython.nbformat import v3 as _v_latest
Brian E. Granger
Massive work on the notebook document format....
r4484
Jonathan Frederic
Relative import
r12753 from .reader import reads as reader_reads
from .reader import versions
Jonathan Frederic
Notebook version conversions done right?
r12493 from .convert import convert
MinRK
Use Draft4 JSON Schema for both v3 and v4...
r18243 from .validator import validate, ValidationError
Jonathan Frederic
Notebook version conversions done right?
r12493
MinRK
use utils.log.get_logger where appropriate
r17057 from IPython.utils.log import get_logger
Jessica B. Hamrick
Use logging rather than print statements
r16342
Thomas Kluyver
Add option to generate API docs based on __all__
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
Full versioning added to nbformat.
r4406
Brian Granger
Proper error handling for nbformat versions in client code....
r6061 current_nbformat = nbformat
MinRK
add nbformat_minor for minor revisions to nbformat
r7545 current_nbformat_minor = nbformat_minor
Thomas Kluyver
Decorator for docstrings referring to objects imported from nbformat.current
r13627 current_nbformat_module = _v_latest.__name__
Jessica B. Hamrick
Allow validator to be called from reads_json and writes_json
r16330
MinRK
better message when notebook format is not supported...
r11643 class NBFormatError(ValueError):
pass
Brian E. Granger
Full versioning added to nbformat.
r4406
MinRK
deprecate format arg in reads/writes...
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
Full versioning added to nbformat.
r4406 def parse_py(s, **kwargs):
"""Parse a string into a (nbformat, string) tuple."""
MinRK
add minor-version support to .py export
r7566 nbf = current_nbformat
nbm = current_nbformat_minor
pattern = r'# <nbformat>(?P<nbformat>\d+[\.\d+]*)</nbformat>'
Brian E. Granger
Full versioning added to nbformat.
r4406 m = re.search(pattern,s)
if m is not None:
MinRK
add minor-version support to .py export
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
Full versioning added to nbformat.
r4406
Jessica B. Hamrick
Make sure to pass correct datatype to validate
r16403 def reads_json(nbjson, **kwargs):
MinRK
deprecate format arg in reads/writes...
r18253 """DEPRECATED, use reads"""
warnings.warn("reads_json is deprecated, use reads")
return reads(nbjson)
Brian E. Granger
Full versioning added to nbformat.
r4406
def writes_json(nb, **kwargs):
MinRK
deprecate format arg in reads/writes...
r18253 """DEPRECATED, use writes"""
warnings.warn("writes_json is deprecated, use writes")
return writes(nb, **kwargs)
Brian E. Granger
Full versioning added to nbformat.
r4406
def reads_py(s, **kwargs):
MinRK
deprecate format arg in reads/writes...
r18253 """DEPRECATED: use nbconvert"""
_warn_format()
MinRK
add minor-version support to .py export
r7566 nbf, nbm, s = parse_py(s, **kwargs)
Jonathan Frederic
Some small fixes and changes of nb version conversion
r12755 if nbf in (2, 3):
Jonathan Frederic
Relative import
r12753 nb = versions[nbf].to_notebook_py(s, **kwargs)
Brian E. Granger
Full versioning added to nbformat.
r4406 else:
Brian Granger
Proper error handling for nbformat versions in client code....
r6061 raise NBFormatError('Unsupported PY nbformat version: %i' % nbf)
Brian E. Granger
Full versioning added to nbformat.
r4406 return nb
def writes_py(nb, **kwargs):
MinRK
deprecate format arg in reads/writes...
r18253 """DEPRECATED: use nbconvert"""
_warn_format()
Jonathan Frederic
Relative import
r12753 return versions[3].writes_py(nb, **kwargs)
Brian E. Granger
Full versioning added to nbformat.
r4406
# High level API
MinRK
add `nbformat.writes(version=X)` for downgrade...
r18246 def reads(s, format='DEPRECATED', version=current_nbformat, **kwargs):
Brian E. Granger
Full versioning added to nbformat.
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
Implemented metadata for notebook format.
r4637 s : unicode
The raw unicode string to read the notebook from.
Brian E. Granger
Full versioning added to nbformat.
r4406
Returns
-------
nb : NotebookNode
The notebook that was read.
"""
MinRK
deprecate format arg in reads/writes...
r18253 if format not in {'DEPRECATED', 'json'}:
_warn_format()
nb = reader_reads(s, **kwargs)
MinRK
add `nbformat.writes(version=X)` for downgrade...
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
Full versioning added to nbformat.
r4406
MinRK
add `nbformat.writes(version=X)` for downgrade...
r18246 def writes(nb, format='DEPRECATED', version=current_nbformat, **kwargs):
Brian E. Granger
Full versioning added to nbformat.
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
add `nbformat.writes(version=X)` for downgrade...
r18246 version : int
The nbformat version to write.
Used for downgrading notebooks.
Brian E. Granger
Full versioning added to nbformat.
r4406
Returns
-------
Brian E. Granger
Implemented metadata for notebook format.
r4637 s : unicode
Brian E. Granger
Full versioning added to nbformat.
r4406 The notebook string.
"""
MinRK
deprecate format arg in reads/writes...
r18253 if format not in {'DEPRECATED', 'json'}:
_warn_format()
nb = convert(nb, version)
MinRK
add `nbformat.writes(version=X)` for downgrade...
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
Full versioning added to nbformat.
r4406
MinRK
add `nbformat.writes(version=X)` for downgrade...
r18246 def read(fp, format='DEPRECATED', **kwargs):
Brian E. Granger
Full versioning added to nbformat.
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
add `nbformat.writes(version=X)` for downgrade...
r18246 return reads(fp.read(), **kwargs)
Brian E. Granger
Full versioning added to nbformat.
r4406
MinRK
add `nbformat.writes(version=X)` for downgrade...
r18246 def write(nb, fp, format='DEPRECATED', **kwargs):
Brian E. Granger
Full versioning added to nbformat.
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
cast writes to unicode before write
r18261 s = writes(nb, **kwargs)
if isinstance(s, bytes):
s = s.decode('utf8')
return fp.write(s)
Brian E. Granger
Full versioning added to nbformat.
r4406