def assert_conversion_same_as_mirror(nb_file, ext, mirror_name, format_name=None, compare_notebook=False): dirname, basename = os.path.split(nb_file) file_name, org_ext = os.path.splitext(basename) mirror_file = os.path.join(dirname, '..', 'mirror', mirror_name, file_name + ext) notebook = jupytext.readf(nb_file, format_name=format_name) create_mirror_file_if_missing(mirror_file, notebook, format_name=format_name) # 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) actual = jupytext.writes(notebook, ext=org_ext, format_name=format_name) with open(nb_file, encoding='utf-8') as fp: expected = fp.read() else: actual = jupytext.writes(notebook, ext=ext, format_name=format_name) with open(mirror_file, encoding='utf-8') as fp: expected = fp.read() if format_name and len(actual.splitlines()) > len(expected.splitlines()): actual = '\n'.join(actual.splitlines()[-len(expected.splitlines()):] + ['']) compare(expected, actual) # Compare the two notebooks if ext != '.ipynb': notebook = jupytext.readf(nb_file) nb_mirror = jupytext.readf(mirror_file, format_name=format_name) if 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 = {} if ext == '.md': for cell in notebook.cells: cell.metadata = {} compare_notebooks(notebook, nb_mirror, ext=ext) combine_inputs_with_outputs(nb_mirror, notebook) compare_notebooks(notebook, nb_mirror, ext=ext, compare_outputs=True)
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)
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)
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)