diff --git a/HISTORY.rst b/HISTORY.rst index e41d19fa0..7883e6777 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -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) diff --git a/jupytext/contentsmanager.py b/jupytext/contentsmanager.py index f836cf3cd..a488cca4e 100644 --- a/jupytext/contentsmanager.py +++ b/jupytext/contentsmanager.py @@ -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 @@ -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, @@ -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) @@ -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) diff --git a/jupytext/jupytext.py b/jupytext/jupytext.py index cb8625f18..b4ce23181 100644 --- a/jupytext/jupytext.py +++ b/jupytext/jupytext.py @@ -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""" @@ -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 @@ -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) @@ -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) diff --git a/tests/test_header.py b/tests/test_header.py index 88a48705c..5e24d1305 100644 --- a/tests/test_header.py +++ b/tests/test_header.py @@ -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):