Skip to content

Commit

Permalink
New --paired-paths arguments in jupytext CLI #146
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jan 15, 2019
1 parent 42d4ab5 commit 6f17d2e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
23 changes: 21 additions & 2 deletions jupytext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .jupytext import readf, reads, writef, writes
from .formats import NOTEBOOK_EXTENSIONS, JUPYTEXT_FORMATS, check_file_version
from .formats import long_form_one_format
from .paired_paths import paired_paths
from .combine import combine_inputs_with_outputs
from .compare import test_round_trip_conversion, NotebookDifference
from .languages import _SCRIPT_EXTENSIONS
Expand Down Expand Up @@ -242,6 +243,9 @@ def cli_jupytext(args=None):
help='One or more notebook(s). Input is read from stdin when no notebook '
'is provided, but then the --from field is mandatory',
nargs='*')
parser.add_argument('--paired-paths', '-p',
help='Return the locations of the alternative representations for this notebook.',
action='store_true')
parser.add_argument('--pre-commit', action='store_true',
help="""Run Jupytext on the ipynb files in the git index.
Create a pre-commit hook with:
Expand Down Expand Up @@ -282,17 +286,22 @@ def cli_jupytext(args=None):
if args.version:
return args

args.to = canonize_format(args.to, args.output)

if args.input_format:
args.input_format = canonize_format(args.input_format)
if not args.notebooks and not args.output:
args.output = '-'

if args.paired_paths:
if len(args.notebooks) != 1:
raise ValueError('--paired-paths applies to a single notebook')
return args

if not args.input_format:
if not args.notebooks and not args.pre_commit:
raise ValueError('Please specificy either --from, --pre-commit or notebooks')

args.to = canonize_format(args.to, args.output)

if args.update and not (args.test or args.test_strict) and args.to != 'ipynb':
raise ValueError('--update works exclusively with --to notebook ')

Expand All @@ -314,6 +323,16 @@ def jupytext(args=None):
sys.stdout.write(__version__ + '\n')
return

if args.paired_paths:
main_path = args.notebooks[0]
nb = readf(main_path, args.input_format)
formats = nb.metadata.get('jupytext', {}).get('formats')
if formats:
for path, _ in paired_paths(main_path, formats):
if path != main_path:
sys.stdout.write(path + '\n')
return

convert_notebook_files(nb_files=args.notebooks,
fmt=args.to,
input_format=args.input_format,
Expand Down
17 changes: 17 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from jupytext import readf, writef, writes
from jupytext.cli import convert_notebook_files, cli_jupytext, jupytext, system
from jupytext.compare import compare_notebooks
from jupytext.paired_paths import paired_paths
from .utils import list_notebooks


Expand Down Expand Up @@ -384,3 +385,19 @@ def test_update_metadata(py_file, tmpdir, capsys):

out, err = capsys.readouterr()
assert 'JSONDecodeError' in err


@pytest.mark.parametrize('nb_file', list_notebooks('ipynb_py'))
def test_paired_paths(nb_file, tmpdir, capsys):
tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
nb = readf(nb_file)
nb.metadata.setdefault('jupytext', {})['formats'] = 'ipynb,_light.py,_percent.py:percent'
writef(nb, tmp_ipynb)

jupytext(['--paired-paths', tmp_ipynb])

out, err = capsys.readouterr()
assert not err

formats = nb.metadata.get('jupytext', {}).get('formats')
assert set(out.splitlines()).union([tmp_ipynb]) == set([path for path, _ in paired_paths(tmp_ipynb, formats)])

0 comments on commit 6f17d2e

Please sign in to comment.