Skip to content

Commit

Permalink
In dry run mode, only print output if there would have been a change
Browse files Browse the repository at this point in the history
Closes #152
  • Loading branch information
kynan committed Nov 16, 2024
1 parent 26191da commit 6d181dc
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 33 deletions.
43 changes: 21 additions & 22 deletions nbstripout/_nbstripout.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,45 +332,44 @@ def status(git_config, install_location=INSTALL_LOCATION_LOCAL, verbose=False):
return 1

def process_jupyter_notebook(input_stream, output_stream, args, extra_keys, filename='input from stdin'):
any_change = False

with warnings.catch_warnings():
warnings.simplefilter("ignore", category=UserWarning)
nb = nbformat.read(input_stream, as_version=nbformat.NO_CONVERT)

nb_orig = copy.deepcopy(nb)
nb = strip_output(nb, args.keep_output, args.keep_count, args.keep_id,
extra_keys, args.drop_empty_cells,
args.drop_tagged_cells.split(), args.strip_init_cells,
_parse_size(args.max_size))
nb_stripped = strip_output(nb, args.keep_output, args.keep_count,
args.keep_id, extra_keys, args.drop_empty_cells,
args.drop_tagged_cells.split(),
args.strip_init_cells, _parse_size(args.max_size))

if nb_orig != nb:
any_change = True
any_change = nb_orig != nb_stripped

if args.dry_run:
output_stream.write(f'Dry run: would have stripped {filename}\n')
else:
if output_stream.seekable():
output_stream.seek(0)
output_stream.truncate()
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=UserWarning)
nbformat.write(nb, output_stream)
output_stream.flush()
if any_change:
output_stream.write(f'Dry run: would have stripped {filename}\n')
return any_change

if output_stream.seekable():
output_stream.seek(0)
output_stream.truncate()
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=UserWarning)
nbformat.write(nb_stripped, output_stream)
output_stream.flush()
return any_change

def process_zeppelin_notebook(input_stream, output_stream, args, extra_keys, filename='input from stdin'):
any_change = False

nb = json.load(input_stream, object_pairs_hook=collections.OrderedDict)
nb_orig = copy.deepcopy(nb)
nb_stripped = strip_zeppelin_output(nb)
if nb_orig != nb_stripped:
any_change = True

any_change = nb_orig != nb_stripped

if args.dry_run:
output_stream.write(f'Dry run: would have stripped {filename}\n')
if any_change:
output_stream.write(f'Dry run: would have stripped {filename}\n')
return any_change

if output_stream.seekable():
output_stream.seek(0)
output_stream.truncate()
Expand Down
61 changes: 61 additions & 0 deletions tests/e2e_notebooks/test_nochange.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook should not be changed by nbstripout."
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"1+1 # This cell has `\"init_cell:\" true`"
]
},
{
"cell_type": "code",
"metadata": {
"keep_output": true
},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2+2 # This cell has `\"keep_output:\" true`"
]
}
],
"metadata": {
"celltoolbar": "Edit Metadata",
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
22 changes: 11 additions & 11 deletions tests/test_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
]

DRY_RUN_CASES = [
("test_metadata.ipynb", []),
("test_zeppelin.zpln", ["--mode", "zeppelin"]),
("test_metadata.ipynb", [], True),
("test_zeppelin.zpln", ["--mode", "zeppelin"], True),
("test_nochange.ipynb", [], False),
]

ERR_OUTPUT_CASES = [
Expand Down Expand Up @@ -110,9 +111,9 @@ def test_end_to_end_file(input_file: str, expected_file: str, args: List[str], t
assert not pc.stdout and p.read_text() == expected


@pytest.mark.parametrize("input_file, extra_args", DRY_RUN_CASES)
@pytest.mark.parametrize("input_file, extra_args, any_change", DRY_RUN_CASES)
@pytest.mark.parametrize("verify", (True, False))
def test_dry_run_stdin(input_file: str, extra_args: List[str], verify: bool):
def test_dry_run_stdin(input_file: str, extra_args: List[str], any_change:bool, verify: bool):
expected = "Dry run: would have stripped input from stdin\n"

with open(NOTEBOOKS_FOLDER / input_file, mode="r") as f:
Expand All @@ -122,23 +123,22 @@ def test_dry_run_stdin(input_file: str, extra_args: List[str], verify: bool):
pc = run(args, stdin=f, stdout=PIPE, universal_newlines=True)
output = pc.stdout

assert output == expected
assert pc.returncode == (1 if verify else 0)
assert output == (expected if any_change else '')
assert pc.returncode == (1 if verify and any_change else 0)


@pytest.mark.parametrize("input_file, extra_args", DRY_RUN_CASES)
@pytest.mark.parametrize("input_file, extra_args, any_change", DRY_RUN_CASES)
@pytest.mark.parametrize("verify", (True, False))
def test_dry_run_args(input_file: str, extra_args: List[str], verify: bool):
def test_dry_run_args(input_file: str, extra_args: List[str], any_change: bool, verify: bool):
expected_regex = re.compile(f"Dry run: would have stripped .*[/\\\\]{input_file}\n")
args = [nbstripout_exe(), str(NOTEBOOKS_FOLDER / input_file), "--dry-run", ] + extra_args
if verify:
args.append("--verify")
pc = run(args, stdout=PIPE, universal_newlines=True)
output = pc.stdout

assert expected_regex.match(output)
if verify:
assert pc.returncode == 1
assert expected_regex.match(output) if any_change else output == ''
assert pc.returncode == (1 if verify and any_change else 0)


@pytest.mark.parametrize("input_file, expected_errs, extra_args", ERR_OUTPUT_CASES)
Expand Down

0 comments on commit 6d181dc

Please sign in to comment.