Skip to content

Commit

Permalink
Merge pull request #78 from ayasyrev:dev
Browse files Browse the repository at this point in the history
0.1.2
  • Loading branch information
ayasyrev authored Sep 16, 2024
2 parents 42fb0b6 + 35bb9ad commit 47d2753
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
files: \.ipynb$
language: python
language_version: python3

# Same as nbmetaclean, for compatibility.
- id: nbclean
name: nbclean
description: Clean Jupyter Notebooks metadata and optionally output.
entry: nbclean
files: \.ipynb$
language: python
language_version: python3

- id: nbcheck
name: nbcheck
description: Check Jupyter Notebooks for correct sequence of execution_count and (or) errors in outputs.
Expand Down
15 changes: 14 additions & 1 deletion src/nbmetaclean/app_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from nbmetaclean.check import check_nb_ec, check_nb_errors, check_nb_warnings
from nbmetaclean.helpers import get_nb_names_from_list, read_nb
from nbmetaclean.version import __version__


parser = argparse.ArgumentParser(
prog="nbcheck",
Expand Down Expand Up @@ -48,6 +50,12 @@
action="store_true",
help="Verbose mode. Print extra information.",
)
parser.add_argument(
"-v",
"--version",
action="store_true",
help="Print version information.",
)


def check_ec(nb_files: list[Path], strict: bool, no_exec: bool) -> list[Path]:
Expand Down Expand Up @@ -100,14 +108,19 @@ def print_results(
def app_check() -> None:
"""Check notebooks for correct sequence of execution_count and errors in outputs."""
cfg = parser.parse_args()

if cfg.version:
print(f"nbcheck from nbmetaclean, version: {__version__}")
sys.exit(0)

if not cfg.ec and not cfg.err and not cfg.warn:
print(
"No checks are selected. Please select at least one check: "
"--ec (for execution_count) or "
"--err (for errors in outputs) or "
"--warn (for warnings in outputs)."
)
return
sys.exit(1)

nb_files = get_nb_names_from_list(cfg.path)
if cfg.verbose:
Expand Down
14 changes: 14 additions & 0 deletions src/nbmetaclean/app_clean.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from __future__ import annotations

import argparse
import sys
from pathlib import Path
from typing import Union

from nbmetaclean.clean import CleanConfig, TupleStr, clean_nb_file
from nbmetaclean.helpers import get_nb_names_from_list
from nbmetaclean.version import __version__


parser = argparse.ArgumentParser(
prog="nbmetaclean",
Expand Down Expand Up @@ -80,6 +83,12 @@
action="store_true",
help="Verbose mode. Print extra information.",
)
parser.add_argument(
"-v",
"--version",
action="store_true",
help="Print version information.",
)


def process_mask(mask: Union[list[str], None]) -> Union[tuple[TupleStr, ...], None]:
Expand Down Expand Up @@ -116,6 +125,11 @@ def print_result(
def app_clean() -> None:
"""Clean metadata and execution_count from Jupyter notebook."""
cfg = parser.parse_args()

if cfg.version:
print(f"nbmetaclean version: {__version__}")
sys.exit(0)

clean_config = CleanConfig(
clear_nb_metadata=not cfg.dont_clear_nb_metadata,
clear_cell_metadata=cfg.clear_cell_metadata,
Expand Down
2 changes: 1 addition & 1 deletion src/nbmetaclean/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "0.1.1" # pragma: no cover
__version__ = "0.1.2" # pragma: no cover

__all__ = ["__version__"] # pragma: no cover
12 changes: 12 additions & 0 deletions tests/test_app_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import subprocess

from nbmetaclean.helpers import read_nb, write_nb
from nbmetaclean.version import __version__


def run_app(
Expand Down Expand Up @@ -187,3 +188,14 @@ def test_check_nb_warnings(tmp_path):
assert res_out.startswith("1 notebooks with warnings in outputs:\n")
assert res_out.endswith("test_nb_3_ec.ipynb\n")
assert not res_err


def test_check_app_version():
"""test check `--version` option."""
res_out, res_err = run_app("--version")
assert res_out == f"nbcheck from nbmetaclean, version: {__version__}\n"
assert not res_err

res_out, res_err = run_app("-v")
assert res_out == f"nbcheck from nbmetaclean, version: {__version__}\n"
assert not res_err
12 changes: 12 additions & 0 deletions tests/test_app_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess

from nbmetaclean.helpers import read_nb, write_nb
from nbmetaclean.version import __version__


def run_app(
Expand Down Expand Up @@ -173,3 +174,14 @@ def test_clean_nb_wrong_file(tmp_path: Path):
assert res_out.startswith("with errors: 1")
assert str(nb_name) in res_out
assert not res_err


def test_app_clean_version():
"""test check `--version` option."""
res_out, res_err = run_app("--version")
assert res_out == f"nbmetaclean version: {__version__}\n"
assert not res_err

res_out, res_err = run_app("-v")
assert res_out == f"nbmetaclean version: {__version__}\n"
assert not res_err

0 comments on commit 47d2753

Please sign in to comment.