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) 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)
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 parse_jupytext_format(fmt, name): """ Parse a jupytext format string (such as notebooks//ipynb) and return the path to the file and the extension """ fmt_parsed = long_form_one_format(fmt) path = Path(fmt_parsed['prefix'], f'{name}{fmt_parsed["extension"]}') del fmt_parsed['prefix'] return path, short_form_one_format(fmt_parsed)
def test_write_notebook_does_not_change_it(nb_file, fmt, tmpdir): nb_org = read(nb_file) nb_org_copied = deepcopy(nb_org) ext = long_form_one_format(fmt, nb_org.metadata)["extension"] writes(nb_org, fmt) compare(nb_org, nb_org_copied) tmp_dest = str(tmpdir.join("notebook" + ext)) write(nb_org, tmp_dest, fmt=fmt) compare(nb_org, nb_org_copied)
def test_convert_and_update_preserves_notebook(nb_file, fmt, tmpdir): # cannot encode magic parameters in markdown yet if "magic" in nb_file and fmt == "md": return tmp_ipynb = str(tmpdir.join("notebook.ipynb")) copyfile(nb_file, tmp_ipynb) ext = long_form_one_format(fmt)["extension"] tmp_text = str(tmpdir.join("notebook" + ext)) jupytext(["--to", fmt, tmp_ipynb]) jupytext(["--to", "ipynb", "--update", tmp_text]) nb_org = read(nb_file) nb_now = read(tmp_ipynb) compare(nb_now, nb_org)
def test_convert_and_update_preserves_notebook(nb_file, fmt, tmpdir): # cannot encode magic parameters in markdown yet if 'magic' in nb_file and fmt == 'md': return tmp_ipynb = str(tmpdir.join('notebook.ipynb')) copyfile(nb_file, tmp_ipynb) ext = long_form_one_format(fmt)['extension'] tmp_text = str(tmpdir.join('notebook' + ext)) jupytext(['--to', fmt, tmp_ipynb]) jupytext(['--to', 'ipynb', '--update', tmp_text]) nb_org = readf(nb_file) nb_now = readf(tmp_ipynb) compare(nb_org, nb_now)
def test_save_in_cm_matches_cli(tmpdir, cwd_tmpdir, fmt, input_fn): # attributes of this format long_fmt = long_form_one_format(fmt) ext = long_fmt["extension"] # format_name = long_fmt.get('format_name', 'markdown') # the input file is md:myst requires_myst() # read sample input which is a md:myst notebook from pathlib import Path with (Path(__file__).parent / input_fn).open() as feed: myst_input = feed.read() nb = jupytext.reads(myst_input, "md:myst") nb.metadata["jupytext"]["formats"] = fmt # store notebook as ipynb in tmpdir ipynb_path = str(tmpdir.join("notebook.ipynb")) nbformat.write(nb, ipynb_path) # invoke jupytext to produce version1 name1 = f"notebook1{ext}" jupytext_cli(["--to", fmt, "-o", name1, "notebook.ipynb"]) # produce version2 with contents manager cm = jupytext.TextFileContentsManager() cm.root_dir = str(tmpdir) name2 = f"notebook2{ext}" cm.save(model=notebook_model(nb), path=name2) text1 = tmpdir.join(name1).read() text2 = tmpdir.join(name2).read() compare(text1, text2) # yet another tour name3 = f"notebook3{ext}" jupytext_cli(["--to", fmt, "-o", name3, name1]) text3 = tmpdir.join(name3).read() compare(text1, text3)
def format(self, fmt): """Change source format Returns ------- str The path if the extension changed, None otherwise """ nb_clean = _cleanup_rendered_nb(self._nb_obj_unrendered) ext_file = self._path.suffix ext_format = long_form_one_format(fmt)['extension'] extension_changed = ext_file != ext_format if extension_changed: path = self._path.with_suffix(ext_format) Path(self._path).unlink() else: path = self._path jupytext.write(nb_clean, path, fmt=fmt) return path if extension_changed else None
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" )
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"
def test_base_path_in_tree_from_non_root(): fmt = long_form_one_format("scripts///py") assert ( base_path("/parent_folder/scripts/subfolder/test.py", fmt=fmt) == "/parent_folder///subfolder/test" )
def test_full_path_dotdot(): fmt = long_form_one_format("../scripts//py") assert full_path("scripts/test", fmt=fmt) == "scripts/test.py"
def test_base_path(): fmt = long_form_one_format("dir/prefix_/ipynb") assert base_path("dir/prefix_NAME.ipynb", fmt) == "NAME" with pytest.raises(InconsistentPath): base_path("dir/incorrect_prefix_NAME.ipynb", fmt)
def test_base_path(): fmt = long_form_one_format('dir/prefix_/ipynb') assert base_path('dir/prefix_NAME.ipynb', fmt) == 'NAME' with pytest.raises(InconsistentPath): base_path('dir/incorrect_prefix_NAME.ipynb', fmt)