Skip to content

Commit

Permalink
maybe formating?
Browse files Browse the repository at this point in the history
  • Loading branch information
gwincr11 authored Jan 7, 2022
1 parent eb0fa0a commit 0a75534
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
16 changes: 10 additions & 6 deletions nbformat/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
import json
from .validator import ValidationError


class NotJSONError(ValueError):
pass


def parse_json(s, **kwargs):
"""Parse a JSON string into a dict."""
try:
nb_dict = json.loads(s, **kwargs)
except ValueError as e:
# Limit the error message to 80 characters. Display whatever JSON will fit.
raise NotJSONError(("Notebook does not appear to be JSON: %r" % s)[:77] + "...") from e
raise NotJSONError(("Notebook does not appear to be JSON: %r" % s)[
:77] + "...") from e
return nb_dict

# High level API


def get_version(nb):
"""Get the version of a notebook.
Expand Down Expand Up @@ -53,24 +57,25 @@ def reads(s, **kwargs):
-------
nb : NotebookNode
The notebook that was read.
Raises
------
ValidationError
Notebook JSON for a given version is missing an expected key and cannot be read.
NBFormatError
Specified major version is invalid or unsupported.
"""
from . import versions, NBFormatError

nb_dict = parse_json(s, **kwargs)
(major, minor) = get_version(nb_dict)
if major in versions:
try:
return versions[major].to_notebook_json(nb_dict, minor=minor)
except AttributeError as e:
raise ValidationError(f"The notebook is invalid and is missing an expected key: {e}")
raise ValidationError(
f"The notebook is invalid and is missing an expected key: {e}")
else:
raise NBFormatError('Unsupported nbformat version %s' % major)

Expand All @@ -92,4 +97,3 @@ def read(fp, **kwargs):
The notebook that was read.
"""
return reads(fp.read(), **kwargs)

17 changes: 9 additions & 8 deletions nbformat/v3/rwbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ def restore_bytes(nb):
if "png" in output:
output.png = output.png.encode("ascii", "replace")
if "jpeg" in output:
output.jpeg = output.jpeg.encode("ascii", "replace")
output.jpeg = output.jpeg.encode(
"ascii", "replace")
except KeyError as e:
raise validator.ValidationError(f"The notebook was invalid missing the key: {e.message}")
raise validator.ValidationError(
f"The notebook was invalid missing the key: {e.message}")
return nb


# output keys that are likely to have multiline values
_multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json']

Expand Down Expand Up @@ -67,7 +70,7 @@ def rejoin_lines(nb):
item = output.get(key, None)
if isinstance(item, list):
output[key] = _join_lines(item)
else: # text, heading cell
else: # text, heading cell
for key in ['source', 'rendered']:
item = cell.get(key, None)
if isinstance(item, list):
Expand All @@ -93,7 +96,7 @@ def split_lines(nb):
item = output.get(key, None)
if isinstance(item, str):
output[key] = item.splitlines(True)
else: # text, heading cell
else: # text, heading cell
for key in ['source', 'rendered']:
item = cell.get(key, None)
if isinstance(item, str):
Expand All @@ -103,6 +106,7 @@ def split_lines(nb):
# b64 encode/decode are never actually used, because all bytes objects in
# the notebook are already b64-encoded, and we don't need/want to double-encode


def base64_decode(nb):
"""Restore all bytes objects in the notebook from base64-encoded strings.
Expand Down Expand Up @@ -179,8 +183,5 @@ def writes(self, nb, **kwargs):

def write(self, nb, fp, **kwargs):
"""Write a notebook to a file like object"""
nbs = self.writes(nb,**kwargs)
nbs = self.writes(nb, **kwargs)
return fp.write(nbs)



0 comments on commit 0a75534

Please sign in to comment.