Skip to content

Commit

Permalink
Don't modify fmt, instead, return an updated copy
Browse files Browse the repository at this point in the history
Fix #428
  • Loading branch information
mwouts committed Feb 11, 2020
1 parent 780bee2 commit bcf1a7b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions jupytext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def jupytext_single_file(nb_file, args, log):
# Compute actual extension when using script/auto, and update nb_dest if necessary
dest_fmt = args.to
if dest_fmt and dest_fmt['extension'] == '.auto':
check_auto_ext(dest_fmt, notebook.metadata, '--to')
dest_fmt = check_auto_ext(dest_fmt, notebook.metadata, '--to')
if not args.output and nb_file != '-':
nb_dest = full_path(base_path(nb_file, args.input_format), dest_fmt)

Expand Down Expand Up @@ -625,7 +625,7 @@ def pipe_notebook(notebook, command, fmt='py:percent', update=True, prefix=None)
command = command + ' {}'

fmt = long_form_one_format(fmt, notebook.metadata, auto_ext_requires_language_info=False)
check_auto_ext(fmt, notebook.metadata, '--pipe-fmt')
fmt = check_auto_ext(fmt, notebook.metadata, '--pipe-fmt')
text = writes(notebook, fmt)

command = command.split(' ')
Expand Down
7 changes: 4 additions & 3 deletions jupytext/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,14 +587,15 @@ def auto_ext_from_metadata(metadata):


def check_auto_ext(fmt, metadata, option):
"""Raise a ValueError when the .auto extension cannot be determined"""
"""Replace the auto extension with the actual file extension, and raise a ValueError if it cannot be determined"""
if fmt['extension'] != '.auto':
return
return fmt

auto_ext = auto_ext_from_metadata(metadata)
if auto_ext:
fmt = fmt.copy()
fmt['extension'] = auto_ext
return
return fmt

raise ValueError("The notebook does not have a 'language_info' metadata. "
"Please replace 'auto' with the actual language extension in the {} option (currently {})."
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def assert_conversion_same_as_mirror(nb_file, fmt, mirror_name, compare_notebook
file_name, org_ext = os.path.splitext(basename)
fmt = long_form_one_format(fmt)
notebook = jupytext.read(nb_file, fmt=fmt)
check_auto_ext(fmt, notebook.metadata, '')
fmt = check_auto_ext(fmt, notebook.metadata, '')
ext = fmt['extension']
mirror_file = os.path.join(dirname, '..', 'mirror', mirror_name, full_path(file_name, fmt))

Expand Down

0 comments on commit bcf1a7b

Please sign in to comment.