Skip to content

Commit

Permalink
fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Sep 12, 2022
1 parent efa2fb5 commit 7d013fb
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 131 deletions.
5 changes: 1 addition & 4 deletions nbqa/__main__.py
Expand Up @@ -618,10 +618,7 @@ def _main(cli_args: CLIArgs, configs: Configs) -> int:
try: # pylint disable=R0912

if not nb_to_tmp_mapping:
sys.stderr.write(
"No notebooks found in given directories: "
f"{' '.join(i for i in cli_args.root_dirs if os.path.isdir(i))}\n"
)
sys.stderr.write("No notebooks found in given path(s)\n")
return 0
saved_sources = SAVE_SOURCES[configs["md"]](
nb_to_tmp_mapping,
Expand Down
77 changes: 44 additions & 33 deletions nbqa/path_utils.py
Expand Up @@ -2,7 +2,7 @@
import json
import os
from pathlib import Path
from typing import Tuple
from typing import Any, Dict, Optional, Tuple


def remove_prefix(string: str, prefix: str) -> str:
Expand Down Expand Up @@ -61,42 +61,53 @@ def get_relative_and_absolute_paths(path: str) -> Tuple[str, str]:
return str(relative_path), str(absolute_path)


def read_notebook(notebook):
def read_notebook(notebook: str) -> Tuple[Optional[Dict[str, Any]], Optional[bool]]:
"""
Read notebook.
If it's .md, try reading it with jupytext. If can't, ignore it.
Parameters
----------
notebook
Path of notebook.
Returns
-------
notebook_json
Parsed notebook
trailing_newline
Whether the notebook originally had a trailing newline
"""
trailing_newline = True
_, ext = os.path.splitext(notebook)
with open(notebook, encoding="utf-8") as handle:
content = handle.read()
if ext == ".ipynb":
trailing_newline = content.endswith("\n")
return json.loads(content), trailing_newline
elif ext == ".md":
try:
import jupytext
except ImportError:
return None, None
md_content = jupytext.jupytext.read(notebook)

# get lexer: see https://github.com/mwouts/jupytext/issues/993
from markdown_it import MarkdownIt # must be installed if you have jupytext

parser = (
MarkdownIt("commonmark")
# we only need to parse block level components (for efficiency)
.disable("inline", True)
)
parsed = parser.parse(content)
lexer = None
for token in parsed:
if token.type == "fence" and token.info.startswith("{code-cell}"):
lexer = remove_prefix(
parser.parse(content)[4].info, "{code-cell}"
).strip()
md_content["metadata"]["language_info"] = {"pygments_lexer": lexer}
break

for cell in md_content["cells"]:
cell["source"] = cell["source"].splitlines(keepends=True)
if "format_name" in md_content.get("metadata", {}).get("jupytext", {}).get(
"text_representation", {}
):
return md_content, True
assert ext == ".md"
try:
import jupytext # pylint: disable=import-outside-toplevel
from markdown_it import MarkdownIt # pylint: disable=import-outside-toplevel
except ImportError:
return None, None
md_content = jupytext.jupytext.read(notebook)

# get lexer: see https://github.com/mwouts/jupytext/issues/993
parser = MarkdownIt("commonmark").disable("inline", True)
parsed = parser.parse(content)
lexer = None
for token in parsed:
if token.type == "fence" and token.info.startswith("{code-cell}"):
lexer = remove_prefix(parser.parse(content)[4].info, "{code-cell}").strip()
md_content["metadata"]["language_info"] = {"pygments_lexer": lexer}
break

for cell in md_content["cells"]:
cell["source"] = cell["source"].splitlines(keepends=True)
if "format_name" in md_content.get("metadata", {}).get("jupytext", {}).get(
"text_representation", {}
):
return md_content, True
return None, None
24 changes: 21 additions & 3 deletions nbqa/replace_source.py
Expand Up @@ -10,7 +10,7 @@
import sys
from difflib import unified_diff
from shutil import move
from typing import Any, Iterator, List, Mapping, MutableMapping, Sequence, Set
from typing import Any, Dict, Iterator, List, Mapping, MutableMapping, Sequence, Set

import tokenize_rt

Expand Down Expand Up @@ -172,7 +172,21 @@ def _notebook_cells(
yield cell


def _write_notebook(temp_notebook, trailing_newline, notebook_json):
def _write_notebook(
temp_notebook: str, trailing_newline: bool, notebook_json: Dict[str, Any]
) -> None:
"""
Write notebook to disc.
Parameters
----------
temp_notebook
Location of temporary notebook
trailing_newline
Whether notebook originally had trailing newline
notebook_json
New source for notebook.
"""
_, ext = os.path.splitext(temp_notebook)
if ext == ".ipynb":
with open(temp_notebook, "w", encoding="utf-8") as handle:
Expand All @@ -185,7 +199,7 @@ def _write_notebook(temp_notebook, trailing_newline, notebook_json):
f"{json.dumps(notebook_json, indent=1, ensure_ascii=False)}"
)
elif ext == ".md":
import jupytext
import jupytext # pylint: disable=import-outside-toplevel

for cell in notebook_json["cells"]:
cell["source"] = "".join(cell["source"])
Expand Down Expand Up @@ -213,6 +227,9 @@ def mutate(
Whether mutation actually happened.
"""
notebook_json, trailing_newline = read_notebook(notebook)
assert notebook_json is not None # if we got here, it was a valid notebook
assert trailing_newline is not None

original_notebook_json = copy.deepcopy(notebook_json)

cells_to_remove = []
Expand Down Expand Up @@ -302,6 +319,7 @@ def diff(
Whether non-null diff was produced.
"""
notebook_json, _ = read_notebook(notebook)
assert notebook_json is not None # if we got here, it was a valid notebook

cells = _get_cells(python_file, len(notebook_info.temporary_lines), md=md)

Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Expand Up @@ -5,12 +5,14 @@ blacken-docs
coverage[toml]
flake8
isort>=5.4.2
jupytext
mdformat
mypy
pre-commit-hooks
pydocstyle
pylint
pytest
pytest-cov
pytest-randomly
pyupgrade
yapf
81 changes: 0 additions & 81 deletions tests/data/notebook_for_testing.md

This file was deleted.

3 changes: 2 additions & 1 deletion tests/test_non_python_notebook.py
Expand Up @@ -24,7 +24,8 @@ def test_non_python_notebook(capsys: "CaptureFixture") -> None:
"No valid notebooks found in given path(s)\n"
"\n"
"\x1b[1m\n"
"If you believe the notebook(s) to be valid, please report a bug at https://github.com/nbQA-dev/nbQA/issues \x1b[0m\n"
"If you believe the notebook(s) to be valid, please report a bug "
"at https://github.com/nbQA-dev/nbQA/issues \x1b[0m\n"
"\n"
)
assert err == expected_err
11 changes: 2 additions & 9 deletions tests/test_runtime_errors.py
Expand Up @@ -235,14 +235,7 @@ def test_directory_without_notebooks(capsys: "CaptureFixture") -> None:
capsys
Pytest fixture to capture stdout and stderr.
"""
main(["black", "docs"])
main(["black", "LICENSES"])
_, err = capsys.readouterr()
expected_err = (
'No valid notebooks found in given path(s)\n'
'\n'
'\x1b[1m\n'
'If you believe the notebook(s) to be valid, please report a bug at '
'https://github.com/nbQA-dev/nbQA/issues \x1b[0m\n'
'\n'
)
expected_err = "No notebooks found in given path(s)\n"
assert err == expected_err

0 comments on commit 7d013fb

Please sign in to comment.