Skip to content

Commit

Permalink
Test the default_comment_magics in the contents manager #89
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 5, 2018
1 parent 13efb64 commit f899e20
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def all_nb_extensions(self):
'scripts as Sphinx gallery scripts.',
config=True)

comment_magics = Enum(
default_comment_magics = Enum(
values=[True, False],
allow_none=True,
help='Should Jupyter magic commands be commented out in the text representation?',
Expand Down Expand Up @@ -219,8 +219,8 @@ def _read_notebook(self, os_path, as_version=4):

def set_comment_magics_if_none(self, nb):
"""Set the 'comment_magics' metadata if default is not None"""
if self.comment_magics is not None and 'commment_magics' not in nb.metadata.get('jupytext', {}):
nb.metadata.setdefault('jupytext', {})['commment_magics'] = self.comment_magics
if self.default_comment_magics is not None and 'comment_magics' not in nb.metadata.get('jupytext', {}):
nb.metadata.setdefault('jupytext', {})['comment_magics'] = self.default_comment_magics

def _save_notebook(self, os_path, nb):
"""Save a notebook to an os_path."""
Expand Down
20 changes: 20 additions & 0 deletions tests/test_escape_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,23 @@ def test_magics_are_not_commented(ext_and_format_name):
if format_name == 'sphinx':
nb2.cells = nb2.cells[1:]
compare_notebooks(nb, nb2)


def test_force_comment_using_contents_manager(tmpdir):
tmp_py = 'notebook.py'

cm = jupytext.TextFileContentsManager()
cm.preferred_jupytext_formats_save = 'py:percent'
cm.root_dir = str(tmpdir)

nb = new_notebook(cells=[new_code_cell('%pylab inline')])

cm.save(model=dict(type='notebook', content=nb), path=tmp_py)
with open(str(tmpdir.join(tmp_py))) as stream:
assert '%pylab inline' in stream.read().splitlines()

cm.default_comment_magics = True
cm.save(model=dict(type='notebook', content=nb), path=tmp_py)
with open(str(tmpdir.join(tmp_py))) as stream:
assert '# %pylab inline' in stream.read().splitlines()

0 comments on commit f899e20

Please sign in to comment.