Skip to content

Commit

Permalink
Test format_name from global config #93 #95
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 10, 2018
1 parent 9845952 commit b97f3e7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
4 changes: 3 additions & 1 deletion jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ def _save_notebook(self, os_path, nb):
alt_ext = '.' + alt_fmt.split('.')[-1]

if alt_ext in self.nb_extensions:
format_name = format_name_for_ext(nb.metadata, alt_fmt, explicit_default=False) or \
format_name = format_name_for_ext(nb.metadata, alt_fmt,
self.default_jupytext_formats,
explicit_default=False) or \
self.preferred_format(alt_fmt, self.preferred_jupytext_formats_save)
with mock.patch('nbformat.writes',
_jupytext_writes(alt_fmt, format_name)):
Expand Down
4 changes: 2 additions & 2 deletions jupytext/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def auto_ext_from_metadata(metadata):
return auto_ext


def format_name_for_ext(metadata, ext, explicit_default=True):
def format_name_for_ext(metadata, ext, cm_default_formats=None, explicit_default=True):
"""Return the format name for that extension"""

# Current format: Don't change it unless an explicit instruction is given in the 'formats' field.
Expand All @@ -326,7 +326,7 @@ def format_name_for_ext(metadata, ext, explicit_default=True):

auto_ext = auto_ext_from_metadata(metadata)

formats = metadata.get('jupytext', {}).get('formats', '')
formats = metadata.get('jupytext', {}).get('formats', '') or cm_default_formats
formats = parse_formats(formats)
for fmt_ext, ext_format_name in formats:
if fmt_ext.endswith(ext) or (fmt_ext.endswith('.auto') and auto_ext and ext.endswith(auto_ext)):
Expand Down
34 changes: 34 additions & 0 deletions tests/test_contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,40 @@ def test_save_in_auto_extension_global(nb_file, tmpdir):
compare_notebooks(nb, model['content'])


@pytest.mark.parametrize('nb_file', list_notebooks('ipynb'))
def test_save_in_auto_extension_global_with_format(nb_file, tmpdir):
# load notebook
nb = jupytext.readf(nb_file)
if 'language_info' not in nb.metadata:
return

auto_ext = auto_ext_from_metadata(nb.metadata)
tmp_ipynb = 'notebook.ipynb'
tmp_script = 'notebook' + auto_ext

# create contents manager with default load format as percent
cm = jupytext.TextFileContentsManager()
cm.default_jupytext_formats = 'ipynb,auto:percent'
cm.root_dir = str(tmpdir)

# save notebook
with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER', True):
cm.save(model=dict(type='notebook', content=nb), path=tmp_ipynb)

# check that text representation exists, and is in percent format
with open(str(tmpdir.join(tmp_script))) as stream:
assert read_format_from_metadata(stream.read(), auto_ext) == 'percent'

# reload and compare with original notebook
with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER', True):
model = cm.get(path=tmp_script)

# saving should not create a format entry #95
assert 'formats' not in model['content'].metadata.get('jupytext', {})

compare_notebooks(nb, model['content'])


@pytest.mark.parametrize('nb_file', list_notebooks('ipynb'))
def test_save_in_auto_extension_local(nb_file, tmpdir):
# load notebook
Expand Down

0 comments on commit b97f3e7

Please sign in to comment.