Skip to content

Commit

Permalink
New config option additional_metadata_on_text_files #110
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 24, 2018
1 parent a8468bf commit 167e9bc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Release History
**Improvements**

- Notebook metadata is filtered - only the most common metadata are stored in the text representation (#105)
- Notebooks created from light and sphinx scripts do not have YAML header or cell metadata if there was none initially (#110)
- New config option `additional_metadata_on_text_files` on the content manager. Defaults to `True`. Change its value to `False` to avoid creating a YAML header or cell metadata if there was none initially (#110)
- First markdown cell exported as a docstring when using the Sphinx format (#107)

0.8.3 (2018-10-19)
Expand Down
20 changes: 15 additions & 5 deletions jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ def _writes(nbk, version=nbformat.NO_CONVERT, **kwargs):
return _writes


def _jupytext_reads(ext, format_name, rst2md):
def _jupytext_reads(ext, format_name, rst2md, additional_metadata_on_text_files):
def _reads(text, as_version, **kwargs):
return jupytext.reads(text, ext=ext, format_name=format_name, rst2md=rst2md, as_version=as_version, **kwargs)
return jupytext.reads(text, ext=ext, format_name=format_name, rst2md=rst2md,
additional_metadata_on_text_files=additional_metadata_on_text_files,
as_version=as_version, **kwargs)

return _reads

Expand Down Expand Up @@ -148,6 +150,13 @@ def all_nb_extensions(self):
"Examples: 'all', 'hide_input,hide_output'",
config=True)

additional_metadata_on_text_files = Bool(
True,
help='Allow (or not) additional notebook and cell metadata to be saved to a text file '
'that has no "jupyter" section or no YAML header.',
config=True
)

comment_magics = Enum(
values=[True, False],
allow_none=True,
Expand Down Expand Up @@ -228,7 +237,9 @@ def _read_notebook(self, os_path, as_version=4):
_, fmt, ext = file_fmt_ext(os_path)
if ext in self.nb_extensions:
format_name = self.preferred_format(fmt, self.preferred_jupytext_formats_read)
with mock.patch('nbformat.reads', _jupytext_reads(fmt, format_name, self.sphinx_convert_rst2md)):
with mock.patch('nbformat.reads', _jupytext_reads(fmt, format_name,
self.sphinx_convert_rst2md,
self.additional_metadata_on_text_files)):
return super(TextFileContentsManager, self)._read_notebook(os_path, as_version)
else:
return super(TextFileContentsManager, self)._read_notebook(os_path, as_version)
Expand All @@ -251,8 +262,7 @@ def _save_notebook(self, os_path, nb):
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)):
with mock.patch('nbformat.writes', _jupytext_writes(alt_fmt, format_name)):
super(TextFileContentsManager, self)._save_notebook(os_path_fmt, nb)
else:
super(TextFileContentsManager, self)._save_notebook(os_path_fmt, nb)
Expand Down
9 changes: 5 additions & 4 deletions jupytext/jupytext.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
class TextNotebookReader(NotebookReader):
"""Text notebook reader"""

def __init__(self, ext, format_name=None):
def __init__(self, ext, format_name=None, additional_metadata_on_text_files=True):
self.ext = ext
self.format = get_format(ext, format_name)
self.additional_metadata_on_text_files = additional_metadata_on_text_files

def reads(self, s, **_):
"""Read a notebook from text"""
Expand Down Expand Up @@ -57,7 +58,7 @@ def reads(self, s, **_):
raise Exception('Blocked at lines ' + '\n'.join(lines[:6]))
lines = lines[pos:]

if not metadata and self.format.format_name in ['markdown', 'light', 'sphinx', 'sphinx-rst2md']:
if not self.additional_metadata_on_text_files and not metadata:
metadata['jupytext'] = {'metadata_filter': {'notebook': False}}
if not cell_metadata:
metadata['jupytext']['metadata_filter']['cells'] = False
Expand Down Expand Up @@ -138,7 +139,7 @@ def writes(self, nb, **kwargs):


def reads(text, ext, format_name=None,
rst2md=False, as_version=4, **kwargs):
rst2md=False, additional_metadata_on_text_files=True, as_version=4, **kwargs):
"""Read a notebook from a string"""
if ext.endswith('.ipynb'):
return nbformat.reads(text, as_version, **kwargs)
Expand All @@ -150,7 +151,7 @@ def reads(text, ext, format_name=None,
if format_name == 'sphinx' and rst2md:
format_name = 'sphinx-rst2md'

reader = TextNotebookReader(ext, format_name)
reader = TextNotebookReader(ext, format_name, additional_metadata_on_text_files)
notebook = reader.reads(text, **kwargs)
transition_to_jupytext_section_in_metadata(notebook.metadata, False)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_metadata_and_cell_to_header2():
def test_notebook_from_plain_script_has_metadata_filter(script="""print('Hello world")
"""):
with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER', True):
nb = jupytext.reads(script, '.py')
nb = jupytext.reads(script, '.py', additional_metadata_on_text_files=False)
assert nb.metadata.get('jupytext', {}).get('metadata_filter', {}).get('notebook') is False
assert nb.metadata.get('jupytext', {}).get('metadata_filter', {}).get('cells') is False
with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER', True):
Expand Down

0 comments on commit 167e9bc

Please sign in to comment.