Exemple #1
0
def assert_conversion_same_as_mirror(nb_file,
                                     fmt,
                                     mirror_name,
                                     compare_notebook=False):
    dirname, basename = os.path.split(nb_file)
    file_name, org_ext = os.path.splitext(basename)
    fmt = long_form_one_format(fmt)
    notebook = jupytext.read(nb_file, fmt=fmt)
    check_auto_ext(fmt, notebook.metadata, '')
    ext = fmt['extension']
    mirror_file = os.path.join(dirname, '..', 'mirror', mirror_name,
                               full_path(file_name, fmt))

    # it's better not to have Jupytext metadata in test notebooks:
    if fmt == 'ipynb' and 'jupytext' in notebook.metadata:  # pragma: no cover
        notebook.metadata.pop('jupytext')
        jupytext.write(nb_file, fmt=fmt)

    create_mirror_file_if_missing(mirror_file, notebook, fmt)

    # Compare the text representation of the two notebooks
    if compare_notebook:
        # Read and convert the mirror file to the latest nbformat version if necessary
        nb_mirror = jupytext.read(mirror_file, as_version=notebook.nbformat)
        nb_mirror.nbformat_minor = notebook.nbformat_minor
        compare(nb_mirror, notebook)
        return
    elif ext == '.ipynb':
        notebook = jupytext.read(mirror_file)
        fmt.update({'extension': org_ext})
        actual = jupytext.writes(notebook, fmt)
        with open(nb_file, encoding='utf-8') as fp:
            expected = fp.read()
    else:
        actual = jupytext.writes(notebook, fmt)
        with open(mirror_file, encoding='utf-8') as fp:
            expected = fp.read()

    if not actual.endswith('\n'):
        actual = actual + '\n'
    compare(actual, expected)

    # Compare the two notebooks
    if ext != '.ipynb':
        notebook = jupytext.read(nb_file)
        nb_mirror = jupytext.read(mirror_file, fmt=fmt)

        if fmt.get('format_name') == 'sphinx':
            nb_mirror.cells = nb_mirror.cells[1:]
            for cell in notebook.cells:
                cell.metadata = {}
            for cell in nb_mirror.cells:
                cell.metadata = {}

        compare_notebooks(nb_mirror, notebook, fmt)

        combine_inputs_with_outputs(nb_mirror, notebook)
        compare_notebooks(nb_mirror, notebook, fmt, compare_outputs=True)
Exemple #2
0
def assert_conversion_same_as_mirror(nb_file,
                                     fmt,
                                     mirror_name,
                                     compare_notebook=False):
    dirname, basename = os.path.split(nb_file)
    file_name, org_ext = os.path.splitext(basename)
    fmt = long_form_one_format(fmt)
    notebook = jupytext.read(nb_file, fmt=fmt)
    fmt = check_auto_ext(fmt, notebook.metadata, "")
    ext = fmt["extension"]
    mirror_file = os.path.join(dirname, "..", "mirror", mirror_name,
                               full_path(file_name, fmt))

    # it's better not to have Jupytext metadata in test notebooks:
    if fmt == "ipynb" and "jupytext" in notebook.metadata:  # pragma: no cover
        notebook.metadata.pop("jupytext")
        jupytext.write(nb_file, fmt=fmt)

    create_mirror_file_if_missing(mirror_file, notebook, fmt)

    # Compare the text representation of the two notebooks
    if compare_notebook:
        # Read and convert the mirror file to the latest nbformat version if necessary
        nb_mirror = jupytext.read(mirror_file, as_version=notebook.nbformat)
        nb_mirror.nbformat_minor = notebook.nbformat_minor
        compare(nb_mirror, notebook)
        return
    elif ext == ".ipynb":
        notebook = jupytext.read(mirror_file)
        fmt.update({"extension": org_ext})
        actual = jupytext.writes(notebook, fmt)
        with open(nb_file, encoding="utf-8") as fp:
            expected = fp.read()
    else:
        actual = jupytext.writes(notebook, fmt)
        with open(mirror_file, encoding="utf-8") as fp:
            expected = fp.read()

    if not actual.endswith("\n"):
        actual = actual + "\n"
    compare(actual, expected)

    # Compare the two notebooks
    if ext != ".ipynb":
        notebook = jupytext.read(nb_file)
        nb_mirror = jupytext.read(mirror_file, fmt=fmt)

        if fmt.get("format_name") == "sphinx":
            nb_mirror.cells = nb_mirror.cells[1:]
            for cell in notebook.cells:
                cell.metadata = {}
            for cell in nb_mirror.cells:
                cell.metadata = {}

        compare_notebooks(nb_mirror, notebook, fmt)

        nb_mirror = combine_inputs_with_outputs(nb_mirror, notebook)
        compare_notebooks(nb_mirror, notebook, fmt, compare_outputs=True)
Exemple #3
0
def assert_conversion_same_as_mirror(nb_file,
                                     fmt,
                                     mirror_name,
                                     compare_notebook=False):
    dirname, basename = os.path.split(nb_file)
    file_name, org_ext = os.path.splitext(basename)
    fmt = long_form_one_format(fmt)
    ext = fmt['extension']
    mirror_file = os.path.join(dirname, '..', 'mirror', mirror_name,
                               full_path(file_name, fmt))

    with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER', False):
        notebook = jupytext.readf(nb_file, fmt)
    create_mirror_file_if_missing(mirror_file, notebook, fmt)

    # Compare the text representation of the two notebooks
    if compare_notebook:
        nb_mirror = jupytext.readf(mirror_file)
        compare(nb_mirror, notebook)
        return
    elif ext == '.ipynb':
        notebook = jupytext.readf(mirror_file)
        fmt.update({'extension': org_ext})
        with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER',
                        False):
            actual = jupytext.writes(notebook, fmt)
        with open(nb_file, encoding='utf-8') as fp:
            expected = fp.read()
    else:
        with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER',
                        False):
            actual = jupytext.writes(notebook, fmt)
        with open(mirror_file, encoding='utf-8') as fp:
            expected = fp.read()

    if not actual.endswith('\n'):
        actual = actual + '\n'
    compare(expected, actual)

    # Compare the two notebooks
    if ext != '.ipynb':
        with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER',
                        False):
            notebook = jupytext.readf(nb_file)
            nb_mirror = jupytext.readf(mirror_file, fmt)

        if fmt.get('format_name') == 'sphinx':
            nb_mirror.cells = nb_mirror.cells[1:]
            for cell in notebook.cells:
                cell.metadata = {}
            for cell in nb_mirror.cells:
                cell.metadata = {}

        compare_notebooks(notebook, nb_mirror, fmt)

        combine_inputs_with_outputs(nb_mirror, notebook)
        compare_notebooks(notebook, nb_mirror, fmt, compare_outputs=True)
Exemple #4
0
def test_full_path_in_tree_from_non_root():
    fmt = long_form_one_format("notebooks///ipynb")
    assert (
        full_path("/parent_folder///subfolder/test", fmt=fmt)
        == "/parent_folder/notebooks/subfolder/test.ipynb"
    )
Exemple #5
0
def test_full_path_in_tree_from_root_no_subfolder():
    fmt = long_form_one_format("notebooks///ipynb")
    assert full_path("//test", fmt=fmt) == "notebooks/test.ipynb"
    assert full_path("///test", fmt=fmt) == "/notebooks/test.ipynb"
Exemple #6
0
def test_full_path_dotdot():
    fmt = long_form_one_format("../scripts//py")
    assert full_path("scripts/test", fmt=fmt) == "scripts/test.py"