Skip to content

Commit

Permalink
jupytext --sync ignores unpaired notebooks
Browse files Browse the repository at this point in the history
Fixes #281
  • Loading branch information
mwouts committed Jul 8, 2019
1 parent 6fd582e commit c3e51b6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
13 changes: 11 additions & 2 deletions jupytext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,11 @@ def writef_git_add(notebook_, nb_file_, fmt_):
# Read paired notebooks
if args.sync:
set_prefix_and_suffix(fmt, notebook, nb_file)
notebook, inputs_nb_file, outputs_nb_file = load_paired_notebook(notebook, fmt, nb_file, log)
try:
notebook, inputs_nb_file, outputs_nb_file = load_paired_notebook(notebook, fmt, nb_file, log)
except NotAPairedNotebook as err:
sys.stderr.write('[jupytext] Warning: ' + str(err) + '\n')
continue

# II. ### Apply commands onto the notebook ###
# Pipe the notebook into the desired commands
Expand Down Expand Up @@ -473,12 +477,17 @@ def set_prefix_and_suffix(fmt, notebook, nb_file):
continue


class NotAPairedNotebook(ValueError):
"""An error raised when a notebook is not a paired notebook"""
pass


def load_paired_notebook(notebook, fmt, nb_file, log):
"""Update the notebook with the inputs and outputs of the most recent paired files"""
formats = notebook.metadata.get('jupytext', {}).get('formats')

if not formats:
raise ValueError("'{}' is not a paired notebook".format(nb_file))
raise NotAPairedNotebook("'{}' is not a paired notebook".format(nb_file))

max_mtime_inputs = None
max_mtime_outputs = None
Expand Down
18 changes: 10 additions & 8 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,16 +590,17 @@ def test_paired_paths(nb_file, tmpdir, capsys):


@pytest.mark.parametrize('nb_file', list_notebooks('ipynb_py'))
def test_sync(nb_file, tmpdir):
def test_sync(nb_file, tmpdir, capsys):
tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
tmp_py = str(tmpdir.join('notebook.py'))
tmp_rmd = str(tmpdir.join('notebook.Rmd'))
nb = read(nb_file)
write(nb, tmp_ipynb)

# Test that sync fails when notebook is not paired
with pytest.raises(ValueError, match='is not a paired notebook'):
jupytext(['--sync', tmp_ipynb])
# Test that sync issues a warning when the notebook is not paired
jupytext(['--sync', tmp_ipynb])
_, err = capsys.readouterr()
assert 'is not a paired notebook' in err

# Now with a pairing information
nb.metadata.setdefault('jupytext', {})['formats'] = 'py,Rmd,ipynb'
Expand Down Expand Up @@ -641,15 +642,16 @@ def test_sync(nb_file, tmpdir):

@requires_pandoc
@pytest.mark.parametrize('nb_file', list_notebooks('ipynb_py', skip='(Notebook with|flavors)'))
def test_sync_pandoc(nb_file, tmpdir):
def test_sync_pandoc(nb_file, tmpdir, capsys):
tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
tmp_md = str(tmpdir.join('notebook.md'))
nb = read(nb_file)
write(nb, tmp_ipynb)

# Test that sync fails when notebook is not paired
with pytest.raises(ValueError, match='is not a paired notebook'):
jupytext(['--sync', tmp_ipynb])
# Test that sync issues a warning when the notebook is not paired
jupytext(['--sync', tmp_ipynb])
_, err = capsys.readouterr()
assert 'is not a paired notebook' in err

# Now with a pairing information
nb.metadata.setdefault('jupytext', {})['formats'] = 'ipynb,md:pandoc'
Expand Down

0 comments on commit c3e51b6

Please sign in to comment.