Skip to content

Commit

Permalink
Add --dry-run flag, fixes kynan#122
Browse files Browse the repository at this point in the history
Only lists which files would have been stripped, but makes no changes.
  • Loading branch information
ooiM committed May 10, 2020
1 parent f0d6a74 commit cca7520
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Change Log
------------------

* Windows compatibility: "" quote Python interpreter path (#115, @fcollonval)
* Add ``--dry-run`` flag (#122)

0.3.7 - 2020-01-05
------------------
Expand Down
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ configuration summary of filter and attributes if installed

nbstripout --status

Do a dry run and only list which files would have been stripped: ::

nbstripout --dry-run FILE.ipynb [FILE2.ipynb ...]

Print the version: ::

nbstripout --version
Expand Down
18 changes: 16 additions & 2 deletions nbstripout/_nbstripout.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
nbstripout --status
Do a dry run and only list which files would have been stripped: ::
nbstripout --dry-run FILE.ipynb [FILE2.ipynb ...]
Print the version: ::
nbstripout --version
Expand Down Expand Up @@ -234,6 +238,8 @@ def main():
from subprocess import check_output, CalledProcessError
parser = ArgumentParser(epilog=__doc__, formatter_class=RawDescriptionHelpFormatter)
task = parser.add_mutually_exclusive_group()
task.add_argument('--dry-run', action='store_true',
help='Print which notebooks would have been stripped')
task.add_argument('--install', action='store_true',
help='Install nbstripout in the current repository (set '
'up the git filter and attributes)')
Expand Down Expand Up @@ -306,6 +312,10 @@ def main():
with io.open(filename, 'r', encoding='utf8') as f:
nb = read(f, as_version=NO_CONVERT)
nb = strip_output(nb, args.keep_output, args.keep_count, extra_keys)
if args.dry_run:
output_stream.write('Dry run: would have stripped {}\n'.format(
filename))
continue
if args.textconv:
write(nb, output_stream)
output_stream.flush()
Expand All @@ -324,8 +334,12 @@ def main():
try:
nb = strip_output(read(input_stream, as_version=NO_CONVERT),
args.keep_output, args.keep_count, extra_keys)
write(nb, output_stream)
output_stream.flush()
if args.dry_run:
output_stream.write('Dry run: would have stripped input from '
'stdin\n')
else:
write(nb, output_stream)
output_stream.flush()
except NotJSONError:
print('No valid notebook detected', file=sys.stderr)
sys.exit(1)
4 changes: 4 additions & 0 deletions tests/test-dry-run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$ cat ${TESTDIR}/test_metadata.ipynb | ${NBSTRIPOUT_EXE:-nbstripout} --dry-run
Dry run: would have stripped input from stdin
$ ${NBSTRIPOUT_EXE:-nbstripout} --dry-run ${TESTDIR}/test_metadata.ipynb
Dry run: would have stripped .*/test_metadata.ipynb (re)

0 comments on commit cca7520

Please sign in to comment.