Skip to content

Commit

Permalink
Ignore jupytext specific cell metadata
Browse files Browse the repository at this point in the history
Reproduce and fix #139
  • Loading branch information
mwouts committed Jan 12, 2019
1 parent 5a280e1 commit 0219149
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 3 additions & 2 deletions jupytext/jupytext.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
update_jupytext_formats_metadata, format_name_for_ext, transition_to_jupytext_section_in_metadata
from .header import header_to_metadata_and_cell, metadata_and_cell_to_header, \
encoding_and_executable, insert_or_test_version_number
from .languages import default_language_from_metadata_and_ext, \
set_main_and_cell_language
from .languages import default_language_from_metadata_and_ext, set_main_and_cell_language
from .cell_metadata import _JUPYTEXT_CELL_METADATA


class TextNotebookReader(NotebookReader):
Expand Down Expand Up @@ -59,6 +59,7 @@ def reads(self, s, **_):
lines = lines[pos:]

if self.freeze_metadata and 'metadata_filter' not in metadata.get('jupytext', {}):
cell_metadata = [m for m in cell_metadata if m not in _JUPYTEXT_CELL_METADATA]
metadata.setdefault('jupytext', {})['metadata_filter'] = {
'notebook': {'additional': list(metadata.keys()), 'excluded': 'all'},
'cells': {'additional': list(cell_metadata), 'excluded': 'all'}}
Expand Down
27 changes: 27 additions & 0 deletions tests/test_contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,33 @@ def test_load_save_rename_nbpy(nb_file, tmpdir):
assert os.path.isfile(str(tmpdir.join('new.nb.py')))


@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('script', list_notebooks('python', skip='light'))
def test_load_save_py_freeze_metadata(script, tmpdir):
tmp_nbpy = 'notebook.py'

cm = jupytext.TextFileContentsManager()
cm.freeze_metadata = True
cm.root_dir = str(tmpdir)

# read original file
with open(script) as fp:
text_py = fp.read()

# write to tmp_nbpy
with open(str(tmpdir.join(tmp_nbpy)), 'w') as fp:
fp.write(text_py)

# open and save notebook
nb = cm.get(tmp_nbpy)['content']
cm.save(model=dict(type='notebook', content=nb), path=tmp_nbpy)

with open(str(tmpdir.join(tmp_nbpy))) as fp:
text_py2 = fp.read()

compare(text_py, text_py2)


@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('nb_file', list_notebooks('ipynb_py'))
def test_load_save_rename_notebook_with_dot(nb_file, tmpdir):
Expand Down

0 comments on commit 0219149

Please sign in to comment.