Skip to content

Commit

Permalink
Apply formatting per pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-bates committed Mar 13, 2022
1 parent 21f3c99 commit 34edc9d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
40 changes: 32 additions & 8 deletions jupyter_server/services/contents/fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ def _read_notebook(self, os_path, as_version=4, capture_validation_error=None):
"""Read a notebook from an os path."""
with self.open(os_path, "r", encoding="utf-8") as f:
try:
return nbformat.read(f, as_version=as_version, capture_validation_error=capture_validation_error)
return nbformat.read(
f, as_version=as_version, capture_validation_error=capture_validation_error
)
except Exception as e:
e_orig = e

Expand All @@ -284,12 +286,19 @@ def _read_notebook(self, os_path, as_version=4, capture_validation_error=None):
invalid_file = path_to_invalid(os_path)
replace_file(os_path, invalid_file)
replace_file(tmp_path, os_path)
return self._read_notebook(os_path, as_version, capture_validation_error=capture_validation_error)
return self._read_notebook(
os_path, as_version, capture_validation_error=capture_validation_error
)

def _save_notebook(self, os_path, nb, capture_validation_error=None):
"""Save a notebook to an os_path."""
with self.atomic_writing(os_path, encoding="utf-8") as f:
nbformat.write(nb, f, version=nbformat.NO_CONVERT, capture_validation_error=capture_validation_error)
nbformat.write(
nb,
f,
version=nbformat.NO_CONVERT,
capture_validation_error=capture_validation_error,
)

def _read_file(self, os_path, format):
"""Read a non-notebook file.
Expand Down Expand Up @@ -356,8 +365,14 @@ async def _read_notebook(self, os_path, as_version=4, capture_validation_error=N
"""Read a notebook from an os path."""
with self.open(os_path, "r", encoding="utf-8") as f:
try:
return await run_sync(partial(nbformat.read, as_version=as_version,
capture_validation_error=capture_validation_error), f)
return await run_sync(
partial(
nbformat.read,
as_version=as_version,
capture_validation_error=capture_validation_error,
),
f,
)
except Exception as e:
e_orig = e

Expand All @@ -376,13 +391,22 @@ async def _read_notebook(self, os_path, as_version=4, capture_validation_error=N
invalid_file = path_to_invalid(os_path)
await async_replace_file(os_path, invalid_file)
await async_replace_file(tmp_path, os_path)
return await self._read_notebook(os_path, as_version, capture_validation_error=capture_validation_error)
return await self._read_notebook(
os_path, as_version, capture_validation_error=capture_validation_error
)

async def _save_notebook(self, os_path, nb, capture_validation_error=None):
"""Save a notebook to an os_path."""
with self.atomic_writing(os_path, encoding="utf-8") as f:
await run_sync(partial(nbformat.write, version=nbformat.NO_CONVERT,
capture_validation_error=capture_validation_error), nb, f)
await run_sync(
partial(
nbformat.write,
version=nbformat.NO_CONVERT,
capture_validation_error=capture_validation_error,
),
nb,
f,
)

async def _read_file(self, os_path, format):
"""Read a non-notebook file.
Expand Down
8 changes: 6 additions & 2 deletions jupyter_server/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,9 @@ def _notebook_model(self, path, content=True):

if content:
validation_error = {}
nb = self._read_notebook(os_path, as_version=4, capture_validation_error=validation_error)
nb = self._read_notebook(
os_path, as_version=4, capture_validation_error=validation_error
)
self.mark_trusted_cells(nb, path)
model["content"] = nb
model["format"] = "json"
Expand Down Expand Up @@ -710,7 +712,9 @@ async def _notebook_model(self, path, content=True):

if content:
validation_error = {}
nb = await self._read_notebook(os_path, as_version=4, capture_validation_error=validation_error)
nb = await self._read_notebook(
os_path, as_version=4, capture_validation_error=validation_error
)
self.mark_trusted_cells(nb, path)
model["content"] = nb
model["format"] = "json"
Expand Down
6 changes: 3 additions & 3 deletions jupyter_server/services/contents/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,9 @@ def validate_notebook_model(self, model, validation_error=None):
validate_nb(model["content"])
except ValidationError as e:
model["message"] = "Notebook validation failed: {}:\n{}".format(
e.message,
json.dumps(e.instance, indent=1, default=lambda obj: "<UNKNOWN>"),
)
e.message,
json.dumps(e.instance, indent=1, default=lambda obj: "<UNKNOWN>"),
)
return model

def new_untitled(self, path="", type="", ext=""):
Expand Down
15 changes: 8 additions & 7 deletions tests/services/contents/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import os
import sys
import time
from itertools import combinations
from typing import Dict
from typing import Optional
from typing import Tuple
from unittest.mock import Mock, patch

from itertools import combinations
from unittest.mock import patch

import jsonschema
import pytest
from nbformat import v4 as nbformat
from nbformat import ValidationError
Expand Down Expand Up @@ -77,7 +75,9 @@ def add_invalid_cell(notebook):
notebook.cells.append(cell)


async def prepare_notebook(jp_contents_manager, make_invalid: Optional[bool] = False) -> Tuple[Dict, str]:
async def prepare_notebook(
jp_contents_manager, make_invalid: Optional[bool] = False
) -> Tuple[Dict, str]:
cm = jp_contents_manager
model = await ensure_async(cm.new_untitled(type="notebook"))
name = model["name"]
Expand Down Expand Up @@ -704,8 +704,9 @@ async def test_nb_validation(jp_contents_manager):
# successful methods and ensure that calls to the aliased "validate_nb" are
# zero. Note that since patching side-effects the validation error case, we'll
# skip call-count assertions for that portion of the test.
with patch("nbformat.validate") as mock_validate, \
patch("jupyter_server.services.contents.manager.validate_nb") as mock_validate_nb:
with patch("nbformat.validate") as mock_validate, patch(
"jupyter_server.services.contents.manager.validate_nb"
) as mock_validate_nb:
# Valid notebook, save, then get
model = await ensure_async(cm.save(model, path))
assert "message" not in model
Expand Down

0 comments on commit 34edc9d

Please sign in to comment.