Example #1
0
def test_save_in_pct_and_lgt_auto_extensions(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_pct_script = 'notebook.pct' + auto_ext
    tmp_lgt_script = 'notebook.lgt' + auto_ext

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

    # save notebook
    cm.save(model=dict(type='notebook', content=nb), path=tmp_ipynb)

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

    # check that text representation exists in light format
    with open(str(tmpdir.join(tmp_lgt_script))) as stream:
        assert read_format_from_metadata(stream.read(), auto_ext) == 'light'
Example #2
0
def test_save_in_auto_extension_global_with_format(nb_file, tmpdir):
    # load notebook
    nb = jupytext.readf(nb_file)
    if nb.metadata.get('jupytext', {}).get('formats'):
        del nb.metadata['jupytext']['formats']
    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'])
Example #3
0
def test_save_in_auto_extension_local(nb_file, tmpdir):
    # load notebook
    nb = jupytext.readf(nb_file)
    nb.metadata.setdefault('jupytext', {})['formats'] = 'ipynb,auto:percent'
    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.root_dir = str(tmpdir)

    # save notebook
    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
    model = cm.get(path=tmp_script)

    compare_notebooks(nb, model['content'])
Example #4
0
def test_save_in_auto_extension_global_with_format(nb_file, tmpdir):
    # load notebook
    nb = jupytext.read(nb_file)

    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
    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
    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'])
Example #5
0
def test_auto_from_kernelspecs_works(nb_file):
    nb = read(nb_file)
    language_info = nb.metadata.pop('language_info')
    expected_ext = language_info.get('file_extension')
    if not expected_ext:
        pytest.skip('No file_extension in language_info')
    if expected_ext == '.r':
        expected_ext = '.R'
    auto_ext = auto_ext_from_metadata(nb.metadata)
    assert auto_ext == expected_ext
Example #6
0
def test_ipynb_to_percent_to_light(nb_file):
    nb = jupytext.read(nb_file)
    pct = jupytext.writes(nb, 'auto:percent')
    auto_ext = auto_ext_from_metadata(nb.metadata)
    comment = _SCRIPT_EXTENSIONS[auto_ext]['comment']
    lgt = (pct.replace(comment + ' %%\n', comment + ' +\n').replace(
        comment + ' %% ',
        comment + ' + ').replace(comment + '       format_name: percent',
                                 comment + '       format_name: light'))
    nb2 = jupytext.reads(lgt, auto_ext)
    compare_notebooks(nb2, nb)
Example #7
0
def test_auto_from_kernelspecs_works(nb_file):
    nb = read(nb_file)
    language_info = nb.metadata.pop("language_info")
    expected_ext = language_info.get("file_extension")
    if not expected_ext:
        pytest.skip("No file_extension in language_info")
    if expected_ext == ".r":
        expected_ext = ".R"
    elif expected_ext == ".fs":
        expected_ext = ".fsx"
    auto_ext = auto_ext_from_metadata(nb.metadata)
    assert auto_ext == expected_ext
Example #8
0
def test_ipynb_to_percent_to_light(nb_file):
    nb = jupytext.read(nb_file)
    pct = jupytext.writes(nb, "auto:percent")
    auto_ext = auto_ext_from_metadata(nb.metadata)
    comment = _SCRIPT_EXTENSIONS[auto_ext]["comment"]
    lgt = (pct.replace(comment + " %%\n", comment + " +\n").replace(
        comment + " %% ", comment + " + ").replace(
            comment + "       format_name: percent",
            comment + "       format_name: light",
        ))
    nb2 = jupytext.reads(lgt, auto_ext)
    compare_notebooks(nb2, nb)
Example #9
0
def test_auto_in_fmt(nb_file):
    nb = read(nb_file)
    auto_ext = auto_ext_from_metadata(nb.metadata)
    fmt = auto_ext[1:] + ":percent"
    text = writes(nb, "auto:percent")

    assert "auto" not in text

    nb2 = reads(text, fmt)
    assert nb2.metadata["jupytext"]["text_representation"]["extension"]
    assert nb2.metadata["jupytext"]["text_representation"]["format_name"] == "percent"

    del nb.metadata["language_info"]
    del nb.metadata["kernelspec"]
    with pytest.raises(JupytextFormatError):
        writes(nb, "auto:percent")
Example #10
0
def test_auto_from_kernelspecs_works(nb_file):
    nb = read(nb_file)
    language_info = nb.metadata.pop("language_info")
    expected_ext = language_info.get("file_extension")
    if not expected_ext:
        pytest.skip("No file_extension in language_info")
    if expected_ext == ".r":
        expected_ext = ".R"
    elif expected_ext == ".fs":
        expected_ext = ".fsx"
    auto_ext = auto_ext_from_metadata(nb.metadata)
    if auto_ext == ".sage":
        pytest.xfail(
            "Sage notebooks have Python in their language_info metadata, see #727"
        )
    assert auto_ext == expected_ext
Example #11
0
def test_auto_in_fmt(nb_file):
    nb = readf(nb_file)
    auto_ext = auto_ext_from_metadata(nb.metadata)
    fmt = auto_ext[1:] + ':percent'
    text = writes(nb, 'auto:percent')

    assert 'auto' not in text

    nb2 = reads(text, fmt)
    assert nb2.metadata['jupytext']['text_representation']['extension']
    assert nb2.metadata['jupytext']['text_representation'][
        'format_name'] == 'percent'

    del nb.metadata['language_info']
    with pytest.raises(JupytextFormatError):
        writes(nb, 'auto:percent')
Example #12
0
def test_auto_in_formats(nb_file):
    nb = readf(nb_file)
    nb.metadata['jupytext'] = {'formats': 'ipynb,auto:percent'}
    fmt = auto_ext_from_metadata(nb.metadata)[1:] + ':percent'
    expected_formats = 'ipynb,' + fmt

    text = writes(nb, 'ipynb')
    assert 'auto' not in text
    nb2 = reads(text, 'ipynb')
    assert nb2.metadata['jupytext']['formats'] == expected_formats

    text = writes(nb, 'auto:percent')
    assert 'auto' not in text
    nb2 = reads(text, fmt)
    assert nb2.metadata['jupytext']['formats'] == expected_formats

    del nb.metadata['language_info']
    with pytest.raises(JupytextFormatError):
        writes(nb, 'ipynb')
    with pytest.raises(JupytextFormatError):
        writes(nb, 'auto:percent')
Example #13
0
def test_auto_in_formats(nb_file):
    nb = read(nb_file)
    nb.metadata["jupytext"] = {"formats": "ipynb,auto:percent"}
    fmt = auto_ext_from_metadata(nb.metadata)[1:] + ":percent"
    expected_formats = "ipynb," + fmt

    text = writes(nb, "ipynb")
    assert "auto" not in text
    nb2 = reads(text, "ipynb")
    assert nb2.metadata["jupytext"]["formats"] == expected_formats

    text = writes(nb, "auto:percent")
    assert "auto" not in text
    nb2 = reads(text, fmt)
    assert nb2.metadata["jupytext"]["formats"] == expected_formats

    del nb.metadata["language_info"]
    del nb.metadata["kernelspec"]
    with pytest.raises(JupytextFormatError):
        writes(nb, "ipynb")
    with pytest.raises(JupytextFormatError):
        writes(nb, "auto:percent")