示例#1
0
def test_rmd_notebooks_are_trusted(nb_file):
    cm = TextFileContentsManager()
    root, file = os.path.split(nb_file)
    cm.root_dir = root
    nb = cm.get(file)
    for cell in nb['content'].cells:
        assert cell.metadata.get('trusted', True)
def test_raise_on_wrong_format(tmpdir):
    tmp_ipynb = str(tmpdir.join('notebook.ipynb'))

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)

    with pytest.raises(HTTPError):
        cm.save(path=tmp_ipynb, model=dict(
            type='notebook',
            content=new_notebook(nbformat=4, metadata=dict(jupytext_formats=['.doc']))))
示例#3
0
def test_text_notebooks_can_be_trusted(nb_file, tmpdir,
                                       no_jupytext_version_number):
    cm = TextFileContentsManager()
    root, file = os.path.split(nb_file)
    py_file = str(tmpdir.join(file))
    shutil.copy(nb_file, py_file)

    cm.root_dir = str(tmpdir)
    model = cm.get(file)
    model["type"] == "notebook"
    cm.save(model, file)

    # Unsign notebook
    nb = model["content"]
    for cell in nb.cells:
        cell.metadata.pop("trusted", True)

    cm.notary.unsign(nb)

    # Trust and reload
    cm.trust_notebook(file)

    model = cm.get(file)
    for cell in model["content"].cells:
        assert cell.metadata.get("trusted", True)
def test_rmd_is_ok(nb_file, tmpdir):
    nb = jupytext.readf(nb_file)
    tmp_ipynb = 'notebook.ipynb'
    tmp_rmd = 'notebook.Rmd'

    nb.metadata.setdefault('jupytext', {})['formats'] = 'ipynb,Rmd'

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)

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

    nb2 = jupytext.readf(str(tmpdir.join(tmp_rmd)))

    compare_notebooks(nb, nb2, 'Rmd')
def test_rmd_is_ok(nb_file, tmpdir):
    nb = jupytext.read(nb_file)
    tmp_ipynb = "notebook.ipynb"
    tmp_rmd = "notebook.Rmd"

    nb.metadata.setdefault("jupytext", {})["formats"] = "ipynb,Rmd"

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)

    cm.save(model=notebook_model(nb), path=tmp_ipynb)

    nb2 = jupytext.read(str(tmpdir.join(tmp_rmd)))

    compare_notebooks(nb2, nb, "Rmd")
def test_all_files_created(nb_file, tmpdir):
    nb = jupytext.readf(nb_file)
    tmp_ipynb = 'notebook.ipynb'
    tmp_rmd = 'notebook.Rmd'
    tmp_py = 'notebook.py'
    nb.metadata['jupytext'] = {'formats': 'ipynb,Rmd,py'}

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)

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

    nb2 = jupytext.readf(str(tmpdir.join(tmp_py)))
    compare_notebooks(nb, nb2)

    nb3 = jupytext.readf(str(tmpdir.join(tmp_rmd)))
    compare_notebooks(nb, nb3, 'Rmd')
def test_all_files_created(nb_file, tmpdir):
    nb = jupytext.read(nb_file)
    tmp_ipynb = "notebook.ipynb"
    tmp_rmd = "notebook.Rmd"
    tmp_py = "notebook.py"
    nb.metadata["jupytext"] = {"formats": "ipynb,Rmd,py"}

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)

    cm.save(model=notebook_model(nb), path=tmp_ipynb)

    nb2 = jupytext.read(str(tmpdir.join(tmp_py)))
    compare_notebooks(nb2, nb)

    nb3 = jupytext.read(str(tmpdir.join(tmp_rmd)))
    compare_notebooks(nb3, nb, "Rmd")
示例#8
0
def test_cm_paired_paths():
    cm = TextFileContentsManager()
    cm.paired_notebooks = dict()

    three = "ipynb,py,md"
    cm.update_paired_notebooks("nb.ipynb", three)
    assert cm.paired_notebooks == {
        "nb." + fmt: (fmt, three) for fmt in three.split(",")
    }

    two = "ipynb,Rmd"
    cm.update_paired_notebooks("nb.ipynb", two)
    assert cm.paired_notebooks == {"nb." + fmt: (fmt, two) for fmt in two.split(",")}

    one = "ipynb"
    cm.update_paired_notebooks("nb.ipynb", one)
    assert cm.paired_notebooks == {}
示例#9
0
def test_cm_paired_paths():
    cm = TextFileContentsManager()
    cm.paired_notebooks = dict()

    three = 'ipynb,py,md'
    cm.update_paired_notebooks('nb.ipynb', 'ipynb', three)
    assert cm.paired_notebooks == {
        'nb.' + fmt: (fmt, three)
        for fmt in three.split(',')
    }

    two = 'ipynb,Rmd'
    cm.update_paired_notebooks('nb.ipynb', 'ipynb', two)
    assert cm.paired_notebooks == {
        'nb.' + fmt: (fmt, two)
        for fmt in two.split(',')
    }

    one = 'ipynb'
    cm.update_paired_notebooks('nb.ipynb', 'ipynb', one)
    assert cm.paired_notebooks == {}
示例#10
0
def test_no_rmd_on_not_notebook(tmpdir):
    tmp_ipynb = 'notebook.ipynb'
    tmp_rmd = 'notebook.Rmd'

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    cm.default_jupytext_formats = 'ipynb,Rmd'

    with pytest.raises(HTTPError):
        cm.save(model=dict(type='not notebook', content=new_notebook()), path=tmp_ipynb)
    assert not os.path.isfile(str(tmpdir.join(tmp_rmd)))
示例#11
0
def test_no_rmd_on_not_notebook(tmpdir):
    tmp_ipynb = "notebook.ipynb"
    tmp_rmd = "notebook.Rmd"

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    cm.formats = "ipynb,Rmd"

    with pytest.raises(HTTPError):
        cm.save(model=dict(type="not notebook", content=new_notebook()), path=tmp_ipynb)
    assert not os.path.isfile(str(tmpdir.join(tmp_rmd)))
示例#12
0
def test_no_rmd_on_not_v4(tmpdir):
    tmp_ipynb = "notebook.ipynb"
    tmp_rmd = "notebook.Rmd"

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    cm.default_jupytext_formats = "ipynb,Rmd"

    with pytest.raises(NotebookValidationError):
        cm.save(model=notebook_model(new_notebook(nbformat=3)), path=tmp_rmd)

    assert not os.path.isfile(str(tmpdir.join(tmp_ipynb)))
示例#13
0
def test_ipynb_is_ok(nb_file, tmpdir):
    nb = jupytext.readf(nb_file)
    tmp_ipynb = 'notebook.ipynb'
    tmp_rmd = 'notebook.Rmd'

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    cm.default_jupytext_formats = 'ipynb,Rmd'

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

    nb2 = jupytext.readf(str(tmpdir.join(tmp_ipynb)))
    compare_notebooks(nb, nb2)
示例#14
0
def test_ipynb_is_ok(nb_file, tmpdir):
    nb = jupytext.read(nb_file)
    tmp_ipynb = "notebook.ipynb"
    tmp_rmd = "notebook.Rmd"

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    cm.default_jupytext_formats = "ipynb,Rmd"

    cm.save(model=notebook_model(nb), path=tmp_rmd)

    nb2 = jupytext.read(str(tmpdir.join(tmp_ipynb)))
    compare_notebooks(nb2, nb)
示例#15
0
def test_no_files_created_on_no_format(tmpdir):
    tmp_ipynb = 'notebook.ipynb'
    tmp_rmd = 'notebook.Rmd'
    tmp_py = 'notebook.py'

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    cm.default_jupytext_formats = ''

    cm.save(model=dict(type='notebook', content=new_notebook(nbformat=4, metadata=dict())), path=tmp_ipynb)

    assert not os.path.isfile(str(tmpdir.join(tmp_py)))
    assert not os.path.isfile(str(tmpdir.join(tmp_rmd)))
示例#16
0
def test_no_files_created_on_no_format(tmpdir):
    tmp_ipynb = "notebook.ipynb"
    tmp_rmd = "notebook.Rmd"
    tmp_py = "notebook.py"

    cm = TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    cm.default_jupytext_formats = ""

    cm.save(
        model=notebook_model(new_notebook(nbformat=4, metadata=dict())),
        path=tmp_ipynb,
    )

    assert not os.path.isfile(str(tmpdir.join(tmp_py)))
    assert not os.path.isfile(str(tmpdir.join(tmp_rmd)))
示例#17
0
def test_ipynb_notebooks_can_be_trusted(nb_file, tmpdir):
    cm = TextFileContentsManager()
    root, file = os.path.split(nb_file)
    tmp_ipynb = str(tmpdir.join(file))
    py_file = file.replace('.ipynb', '.py')
    tmp_py = str(tmpdir.join(py_file))
    shutil.copy(nb_file, tmp_ipynb)

    cm.default_jupytext_formats = 'ipynb,py'
    cm.root_dir = str(tmpdir)
    nb = cm.get(file)

    # Sign notebook explicitely (save it, and reload without
    # validating to remove 'trusted' metadata in cells)
    cm.save(nb, py_file)
    cm.trust_notebook(py_file)

    nb = cm.get(file)
    for cell in nb['content'].cells:
        assert cell.metadata.get('trusted', True)

    # Remove py file, content should be the same
    os.remove(tmp_py)
    nb2 = cm.get(file)
    for cell in nb2['content'].cells:
        assert cell.metadata.get('trusted', True)

    assert nb['content'] == nb2['content']
示例#18
0
def test_ipynb_notebooks_can_be_trusted_even_with_metadata_filter(nb_file, tmpdir):
    with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER', False):
        cm = TextFileContentsManager()
        root, file = os.path.split(nb_file)
        tmp_ipynb = str(tmpdir.join(file))
        py_file = file.replace('.ipynb', '.py')
        tmp_py = str(tmpdir.join(py_file))
        shutil.copy(nb_file, tmp_ipynb)

        cm.default_jupytext_formats = 'ipynb,py'
        cm.default_notebook_metadata_filter = 'all'
        cm.default_cell_metadata_filter = '-all'
        cm.root_dir = str(tmpdir)
        model = cm.get(file)
        cm.save(model, py_file)

        # Unsign notebook
        nb = model['content']
        for cell in nb.cells:
            if 'trusted' in cell.metadata:
                cell.metadata.pop('trusted')

        cm.notary.unsign(nb)

        # Trust and reload
        cm.trust_notebook(py_file)

        model = cm.get(file)
        for cell in model['content'].cells:
            assert cell.metadata.get('trusted', True)

        # Remove py file, content should be the same
        os.remove(tmp_py)
        nb2 = cm.get(file)
        for cell in nb2['content'].cells:
            assert cell.metadata.get('trusted', True)

        assert model['content'] == nb2['content']
示例#19
0
def test_ipynb_notebooks_can_be_trusted(nb_file, tmpdir):
    cm = TextFileContentsManager()
    root, file = os.path.split(nb_file)
    tmp_ipynb = str(tmpdir.join(file))
    py_file = file.replace('.ipynb', '.py')
    tmp_py = str(tmpdir.join(py_file))
    shutil.copy(nb_file, tmp_ipynb)

    cm.default_jupytext_formats = 'ipynb,py'
    cm.root_dir = str(tmpdir)
    model = cm.get(file)
    nb = model['content']
    if nb.metadata.get('jupytext', {}).get('formats'):
        del nb.metadata['jupytext']['formats']

    cm.save(model, py_file)

    # Unsign and test notebook
    nb = model['content']
    for cell in nb.cells:
        if 'trusted' in cell.metadata:
            cell.metadata.pop('trusted')

    cm.notary.unsign(nb)

    model = cm.get(file)
    for cell in model['content'].cells:
        assert 'trusted' not in cell.metadata or not cell.metadata['trusted']

    # Trust and reload
    cm.trust_notebook(py_file)

    model = cm.get(file)
    for cell in model['content'].cells:
        assert cell.metadata.get('trusted', True)

    # Remove py file, content should be the same
    os.remove(tmp_py)
    nb2 = cm.get(file)
    for cell in nb2['content'].cells:
        assert cell.metadata.get('trusted', True)

    assert model['content'] == nb2['content']
示例#20
0
def test_ipynb_notebooks_can_be_trusted_even_with_metadata_filter(
        nb_file, tmpdir, no_jupytext_version_number):
    cm = TextFileContentsManager()
    root, file = os.path.split(nb_file)
    tmp_ipynb = str(tmpdir.join(file))
    py_file = file.replace(".ipynb", ".py")
    tmp_py = str(tmpdir.join(py_file))
    shutil.copy(nb_file, tmp_ipynb)

    cm.default_jupytext_formats = "ipynb,py"
    cm.default_notebook_metadata_filter = "all"
    cm.default_cell_metadata_filter = "-all"
    cm.root_dir = str(tmpdir)
    model = cm.get(file)
    cm.save(model, py_file)

    # Unsign notebook
    nb = model["content"]
    for cell in nb.cells:
        cell.metadata.pop("trusted", True)

    cm.notary.unsign(nb)

    # Trust and reload
    cm.trust_notebook(py_file)

    model = cm.get(file)
    for cell in model["content"].cells:
        assert cell.metadata.get("trusted", True)

    # Remove py file, content should be the same
    os.remove(tmp_py)
    nb2 = cm.get(file)
    for cell in nb2["content"].cells:
        assert cell.metadata.get("trusted", True)

    compare_notebooks(nb2["content"], model["content"])