diff --git a/nbformat/reader.py b/nbformat/reader.py index 465dac4a..71c32801 100644 --- a/nbformat/reader.py +++ b/nbformat/reader.py @@ -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. @@ -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) @@ -92,4 +97,3 @@ def read(fp, **kwargs): The notebook that was read. """ return reads(fp.read(), **kwargs) - diff --git a/nbformat/v3/rwbase.py b/nbformat/v3/rwbase.py index a433df4c..3b19af57 100644 --- a/nbformat/v3/rwbase.py +++ b/nbformat/v3/rwbase.py @@ -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'] @@ -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): @@ -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): @@ -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. @@ -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) - - -