def reads(s, as_version, **kwargs): """Read a notebook from a string and return the NotebookNode object as the given version. The string can contain a notebook of any version. The notebook will be returned `as_version`, converting, if necessary. Notebook format errors will be logged. Parameters ---------- s : unicode The raw unicode string to read the notebook from. as_version : int The version of the notebook format to return. The notebook will be converted, if necessary. Pass nbformat.NO_CONVERT to prevent conversion. Returns ------- nb : NotebookNode The notebook that was read. """ nb = reader.reads(s, **kwargs) if as_version is not NO_CONVERT: nb = convert(nb, as_version) try: validate(nb) except ValidationError as e: get_logger().error("Notebook JSON is invalid: %s", e) return nb
def writes(nb, version=NO_CONVERT, **kwargs): """Write a notebook to a string in a given format in the given nbformat version. Any notebook format errors will be logged. Parameters ---------- nb : NotebookNode The notebook to write. version : int, optional The nbformat version to write. If unspecified, or specified as nbformat.NO_CONVERT, the notebook's own version will be used and no conversion performed. Returns ------- s : unicode The notebook as a JSON string. """ if version is not NO_CONVERT: nb = convert(nb, version) else: version, _ = reader.get_version(nb) try: validate(nb) except ValidationError as e: get_logger().error("Notebook JSON is invalid: %s", e) return versions[version].writes_json(nb, **kwargs)
def wrappedfunc(nb, resources): get_logger().debug( "Applying preprocessor: %s", function.__name__ ) for index, cell in enumerate(nb.cells): nb.cells[index], resources = function(cell, resources, index) return nb, resources
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 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 _warn_if_invalid(nb, version): """Log validation errors, if there are any.""" from IPython.nbformat import validate, ValidationError try: validate(nb, version=version) except ValidationError as e: get_logger().error("Notebook JSON is not valid v%i: %s", version, e)
def wrappedfunc(nb, resources): get_logger().debug( "Applying preprocessor: %s", function.__name__ ) for worksheet in nb.worksheets: for index, cell in enumerate(worksheet.cells): worksheet.cells[index], resources = function(cell, resources, index)
def writes_json(nb, **kwargs): """Take a NotebookNode object and write out a JSON string. Report if any JSON format errors are detected. """ errors = validate(nb) if errors: get_logger().error( "Notebook JSON is invalid (%d errors detected during write)", len(errors)) nbjson = versions[current_nbformat].writes_json(nb, **kwargs) return nbjson
def reads_json(nbjson, **kwargs): """Read a JSON notebook from a string and return the NotebookNode object. Report if any JSON format errors are detected. """ nb = reader_reads(nbjson, **kwargs) nb_current = convert(nb, current_nbformat) errors = validate(nb_current) if errors: get_logger().error( "Notebook JSON is invalid (%d errors detected during read)", len(errors)) return nb_current
def terminate_children(sig, frame): log = get_logger() log.critical("Got signal %i, terminating children..." % sig) for child in children: child.terminate() sys.exit(sig != SIGINT)
def terminate_children(sig, frame): log = get_logger() log.critical("Got signal %i, terminating children..."%sig) for child in children: child.terminate() sys.exit(sig != SIGINT)
def _import_mapping(mapping, original=None): """import any string-keys in a type mapping """ log = get_logger() log.debug("Importing canning map") for key,value in list(mapping.items()): if isinstance(key, string_types): try: cls = import_item(key) except Exception: if original and key not in original: # only message on user-added classes log.error("canning class not importable: %r", key, exc_info=True) mapping.pop(key) else: mapping[cls] = mapping.pop(key)
def _log_default(self): from IPython.utils import log return log.get_logger()
def _log_default(self): from IPython.utils.log import get_logger return get_logger()
from .converter import convert from . import reader from .notebooknode import from_dict, NotebookNode from .v4 import ( nbformat as current_nbformat, nbformat_minor as current_nbformat_minor, ) class NBFormatError(ValueError): pass # no-conversion singleton NO_CONVERT = object() get_logger().debug(__file__) def reads(s, as_version, **kwargs): """Read a notebook from a string and return the NotebookNode object as the given version. The string can contain a notebook of any version. The notebook will be returned `as_version`, converting, if necessary. Notebook format errors will be logged. Parameters ---------- s : unicode The raw unicode string to read the notebook from. as_version : int The version of the notebook format to return.
def wrappedfunc(nb, resources): get_logger().debug("Applying preprocessor: %s", function.__name__) for index, cell in enumerate(nb.cells): nb.cells[index], resources = function(cell, resources, index) return nb, resources