Ejemplo n.º 1
0
def test_paired_paths_windows_no_subfolder():
    nb_file = "C:\\Users\\notebooks\\notebooks\\nb.ipynb"
    formats = "notebooks///ipynb,scripts///py"
    with mock.patch("os.path.sep", "\\"):
        assert base_path(nb_file,
                         "notebooks///ipynb") == "C:\\Users\\notebooks\\//nb"
        paired_paths(nb_file, "notebooks///ipynb", formats)
Ejemplo n.º 2
0
def test_simple_pair():
    formats = long_form_multiple_formats('ipynb,py')
    expected_paths = ['notebook.ipynb', 'notebook.py']
    compare(list(zip(expected_paths, formats)),
            paired_paths('notebook.ipynb', 'ipynb', formats))
    compare(list(zip(expected_paths, formats)),
            paired_paths('notebook.py', 'py', formats))
Ejemplo n.º 3
0
def test_many_and_suffix():
    formats = long_form_multiple_formats("ipynb,.pct.py,_lgt.py")
    expected_paths = ["notebook.ipynb", "notebook.pct.py", "notebook_lgt.py"]
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))

    with pytest.raises(InconsistentPath):
        paired_paths("wrong_suffix.py", "py", formats)
Ejemplo n.º 4
0
def test_many_and_suffix():
    formats = long_form_multiple_formats('ipynb,.pct.py,_lgt.py')
    expected_paths = ['notebook.ipynb', 'notebook.pct.py', 'notebook_lgt.py']
    for fmt, path in zip(formats, expected_paths):
        compare(zip(expected_paths, formats), paired_paths(path, fmt, formats))

    with pytest.raises(InconsistentPath):
        paired_paths('wrong_suffix.py', 'py', formats)
Ejemplo n.º 5
0
def test_simple_pair():
    formats = long_form_multiple_formats("ipynb,py")
    expected_paths = ["notebook.ipynb", "notebook.py"]
    compare(
        paired_paths("notebook.ipynb", "ipynb", formats),
        list(zip(expected_paths, formats)),
    )
    compare(paired_paths("notebook.py", "py", formats),
            list(zip(expected_paths, formats)))
Ejemplo n.º 6
0
def test_path_in_tree_limited_to_config_dir(tmpdir):
    root_nb_dir = tmpdir.mkdir("notebooks")
    nb_dir = root_nb_dir.mkdir("notebooks")
    src_dir = root_nb_dir.mkdir("scripts")
    other_dir = root_nb_dir.mkdir("other")

    formats = "notebooks///ipynb,scripts///py"
    fmt = long_form_multiple_formats(formats)[0]

    # Notebook in nested 'notebook' dir is paired
    notebook_in_nb_dir = nb_dir.join("subfolder").join("nb.ipynb")

    assert set(
        path
        for (path,
             _) in paired_paths(str(notebook_in_nb_dir), fmt, formats)) == {
                 str(notebook_in_nb_dir),
                 str(src_dir.join("subfolder").join("nb.py"))
             }

    # Notebook in base 'notebook' dir is paired if no config file is found
    notebook_in_other_dir = other_dir.mkdir("subfolder").join("nb.ipynb")
    assert set(
        path
        for (path,
             _) in paired_paths(str(notebook_in_other_dir), fmt, formats)) == {
                 str(notebook_in_other_dir),
                 str(
                     tmpdir.join("scripts").join("other").join(
                         "subfolder").join("nb.py")),
             }

    # When a config file exists
    root_nb_dir.join("jupytext.toml").write("\n")

    # Notebook in nested 'notebook' dir is still paired
    assert set(
        path
        for (path,
             _) in paired_paths(str(notebook_in_nb_dir), fmt, formats)) == {
                 str(notebook_in_nb_dir),
                 str(src_dir.join("subfolder").join("nb.py"))
             }

    # But the notebook in base 'notebook' dir is not paired any more
    alert = (
        "Notebook directory '/other/subfolder' does not match prefix root 'notebooks'"
        if os.path.sep == "/"
        # we escape twice the backslash because pytest.raises matches it as a regular expression
        else
        "Notebook directory '\\\\other\\\\subfolder' does not match prefix root 'notebooks'"
    )
    with pytest.raises(InconsistentPath, match=alert):
        paired_paths(str(notebook_in_other_dir), fmt, formats)
Ejemplo n.º 7
0
def test_prefix_on_root_174():
    short_formats = "ipynb,python//py:percent"

    formats = long_form_multiple_formats(short_formats)
    assert short_form_multiple_formats(formats) == short_formats

    expected_paths = ["Untitled.ipynb", "python/Untitled.py"]
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))
Ejemplo n.º 8
0
def test_prefix_on_root_174():
    short_formats = 'ipynb,python//py:percent'

    formats = long_form_multiple_formats(short_formats)
    assert short_form_multiple_formats(formats) == short_formats

    expected_paths = ['/Untitled.ipynb', '/python/Untitled.py']
    for fmt, path in zip(formats, expected_paths):
        compare(zip(expected_paths, formats), paired_paths(path, fmt, formats))
Ejemplo n.º 9
0
def test_paired_paths(nb_file, tmpdir, capsys):
    tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
    nb = readf(nb_file)
    nb.metadata.setdefault('jupytext', {})['formats'] = 'ipynb,_light.py,_percent.py:percent'
    writef(nb, tmp_ipynb)

    jupytext(['--paired-paths', tmp_ipynb])

    out, err = capsys.readouterr()
    assert not err

    formats = nb.metadata.get('jupytext', {}).get('formats')
    assert set(out.splitlines()).union([tmp_ipynb]) == set(
        [path for path, _ in paired_paths(tmp_ipynb, 'ipynb', formats)])
Ejemplo n.º 10
0
def test_paired_paths(nb_file, tmpdir, capsys):
    tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
    nb = read(nb_file)
    nb.metadata.setdefault(
        "jupytext", {})["formats"] = "ipynb,_light.py,_percent.py:percent"
    write(nb, tmp_ipynb)

    jupytext(["--paired-paths", tmp_ipynb])

    out, err = capsys.readouterr()
    assert not err

    formats = nb.metadata.get("jupytext", {}).get("formats")
    assert set(out.splitlines()).union([tmp_ipynb]) == set(
        [path for path, _ in paired_paths(tmp_ipynb, "ipynb", formats)])
Ejemplo n.º 11
0
def test_prefix_and_suffix():
    short_formats = (
        "notebook_folder/notebook_prefix_/_notebook_suffix.ipynb,"
        "script_folder//_in_percent_format.py:percent,"
        "script_folder//_in_light_format.py"
    )

    formats = long_form_multiple_formats(short_formats)
    assert short_form_multiple_formats(formats) == short_formats

    expected_paths = [
        "parent/notebook_folder/notebook_prefix_NOTEBOOK_NAME_notebook_suffix.ipynb",
        "parent/script_folder/NOTEBOOK_NAME_in_percent_format.py",
        "parent/script_folder/NOTEBOOK_NAME_in_light_format.py",
    ]
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))

    # without the parent folder
    expected_paths = [path[7:] for path in expected_paths]
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))

    # Not the expected parent folder
    with pytest.raises(InconsistentPath):
        paired_paths(
            "script_folder_incorrect/NOTEBOOK_NAME_in_percent_format.py",
            formats[1],
            formats,
        )

    # Not the expected suffix
    with pytest.raises(InconsistentPath):
        paired_paths(
            "parent/script_folder/NOTEBOOK_NAME_in_LIGHT_format.py", formats[2], formats
        )

    # Not the expected extension
    with pytest.raises(InconsistentPath):
        paired_paths(
            "notebook_folder/notebook_prefix_NOTEBOOK_NAME_notebook_suffix.py",
            formats[0],
            formats,
        )
Ejemplo n.º 12
0
def test_base_path_in_tree_from_non_root_no_subfolder():
    nb_file = "/parent/notebooks/wrap_markdown.ipynb"
    formats = "notebooks///ipynb,scripts///py:percent"
    fmt = "notebooks///ipynb"
    assert base_path(nb_file, fmt) == "/parent///wrap_markdown"
    paired_paths(nb_file, fmt, formats)
Ejemplo n.º 13
0
def test_duplicated_paths():
    formats = long_form_multiple_formats("ipynb,py:percent,py:light")
    with pytest.raises(InconsistentPath):
        paired_paths("notebook.ipynb", "ipynb", formats)
Ejemplo n.º 14
0
def test_paired_path_dotdot_564():
    main_path = "examples/tutorials/colabs/rigid_object_tutorial.ipynb"
    formats = "../nb_python//py:percent,../colabs//ipynb"
    paired_paths(main_path, "ipynb", formats)