Skip to content

Commit

Permalink
Merge pull request #41 from ayasyrev:fix_clean
Browse files Browse the repository at this point in the history
Fix_clean
  • Loading branch information
ayasyrev authored Aug 8, 2024
2 parents 793472a + 6fc083b commit c893960
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 31 deletions.
11 changes: 2 additions & 9 deletions src/nbmetaclean/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@
action="store_true",
help="perform a trial run, don't write results",
)
parser.add_argument(
"-V",
"--verbose",
action="store_true",
help="Print more info - cleaned notebooks.",
)


def process_mask(mask: Union[list[str], None]) -> Union[tuple[TupleStr, ...], None]:
Expand Down Expand Up @@ -113,9 +107,8 @@ def app() -> None:
)
if not cfg.silent:
print(f"cleaned nbs: {len(cleaned)}")
if cfg.verbose:
for nb in cleaned:
print("- ", nb)
for nb in cleaned:
print("- ", nb)
if errors:
print(f"with errors: {len(errors)}")
for nb, exc in errors:
Expand Down
11 changes: 3 additions & 8 deletions src/nbmetaclean/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class CleanConfig:
mask_merge (bool): Merge masks. Add new mask to default.
If False - use new mask. Defaults to True.
dry_run (bool): perform a trial run, don't write results. Defaults to False.
verbose (bool): Print more info - cleaned notebooks. Defaults to False.
"""

clear_nb_metadata: bool = True
Expand All @@ -49,7 +48,6 @@ class CleanConfig:
cell_metadata_preserve_mask: Optional[tuple[TupleStr, ...]] = None
mask_merge: bool = True
dry_run: bool = False
verbose: bool = False


def filter_meta_mask(
Expand Down Expand Up @@ -184,14 +182,12 @@ def clean_nb_file(
Returns:
tuple[List[Path], List[TuplePath]]: List of cleaned notebooks, list of notebooks with errors.
"""
if cfg is None:
cfg = CleanConfig()
cfg = cfg or CleanConfig()
if not isinstance(path, list):
path = [path]
cleaned: list[Path] = []
errors: list[tuple[Path, Exception]] = []
to_clean = len(path)
for num, filename in enumerate(path):
for filename in path:
try:
nb = read_nb(filename)
except Exception as ex:
Expand All @@ -210,6 +206,5 @@ def clean_nb_file(
write_nb(nb, filename)
if cfg.preserve_timestamp:
os.utime(filename, (stat.st_atime, stat.st_mtime))
if not cfg.silent:
print(f"done {num + 1} of {to_clean}: {filename}")

return cleaned, errors
14 changes: 0 additions & 14 deletions tests/test_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,28 +289,14 @@ def test_clean_nb_file(tmp_path: Path, capsys: CaptureFixture[str]):
# clean meta, execution_count
# path as list
cleaned, errors = clean_nb_file([test_nb_path], CleanConfig())
captured = capsys.readouterr()
out = captured.out
assert out.startswith("done")
assert "test_clean_nb_file0/.test_nb_2_meta.ipynb" in out
assert len(cleaned) == 1
nb = read_nb(cleaned[0])
assert nb == nb_clean

# try clean cleaned
cleaned, errors = clean_nb_file(test_nb_path, CleanConfig())
assert len(cleaned) == 0
captured = capsys.readouterr()
out = captured.out
assert not out.strip()

# silent
test_nb_path = write_nb(read_nb(path / nb_name), tmp_path / nb_name)
cleaned, errors = clean_nb_file(test_nb_path, CleanConfig(silent=True))
assert len(cleaned) == 1
assert len(errors) == 0
captured = capsys.readouterr()
assert not captured.out.strip()


def test_clean_nb_file_errors(capsys: CaptureFixture[str], tmp_path: Path):
Expand Down

0 comments on commit c893960

Please sign in to comment.