Skip to content

Commit

Permalink
Reproduce and fix the issue with notebooks paired with scripts in a f…
Browse files Browse the repository at this point in the history
…older
  • Loading branch information
mwouts committed Jul 8, 2021
1 parent b32c4b4 commit 1732c2f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
5 changes: 4 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
Jupytext ChangeLog
==================

1.11.4 (2021-07-??)
1.11.4 (2021-07-09)
-------------------

**Fixed**
- Fixed an `InconsistentPath` issue with notebooks paired with scripts in a folder ([#806](https://github.com/mwouts/jupytext/issues/806))

**Changed**
- (Documentation) the `cell_markers` option (and the other ones) can be set directly in the `jupytext.toml` config file ([#809](https://github.com/mwouts/jupytext/issues/809)).

Expand Down
24 changes: 22 additions & 2 deletions jupytext/paired_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,31 @@ def paired_paths(main_path, fmt, formats):
if not formats:
return [(main_path, {"extension": os.path.splitext(main_path)[1]})]

if fmt is None:
fmt = {"extension": os.path.splitext(main_path)[1]}

fmt = long_form_one_format(fmt)
formats = long_form_multiple_formats(formats)

paths = []
# Is there a format that matches the main path?
base = base_path(main_path, fmt)
paths = [full_path(base, f) for f in formats]
for f in formats:
if f["extension"] != fmt["extension"]:
continue
if (
"format_name" in fmt
and "format_name" in f
and f["format_name"] != fmt["format_name"]
):
continue
# extend 'fmt' with the format information (prefix, suffix) from f
extended_fmt = {key: fmt.get(key, value) for key, value in f.items()}

base = base_path(main_path, extended_fmt)
paths = [full_path(base, f) for f in formats]

if main_path in paths:
break

if main_path not in paths:
raise InconsistentPath(
Expand Down
39 changes: 39 additions & 0 deletions tests/test_paired_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import pytest

import jupytext
from jupytext.cli import jupytext as jupytext_cli
from jupytext.compare import compare
from jupytext.contentsmanager import TextFileContentsManager
from jupytext.formats import (
Expand Down Expand Up @@ -247,3 +249,40 @@ def test_cm_paired_paths():
zero = ""
cm.update_paired_notebooks("nb.ipynb", zero)
assert cm.paired_notebooks == {}


def test_paired_path_with_prefix(
nb_file="scripts/test.py",
fmt={"extension": ".py", "format_name": "percent"},
formats=[
{"extension": ".ipynb"},
{"prefix": "scripts/", "format_name": "percent", "extension": ".py"},
],
):
assert paired_paths(nb_file, fmt, formats) == [
("test.ipynb", {"extension": ".ipynb"}),
(
"scripts/test.py",
{"prefix": "scripts/", "format_name": "percent", "extension": ".py"},
),
]


def test_paired_notebook_ipynb_root_scripts_in_folder_806(
tmpdir, cwd_tmpdir, python_notebook
):
test_ipynb = tmpdir / "test.ipynb"
jupytext.write(python_notebook, str(test_ipynb))
jupytext_cli(["--set-formats", "ipynb,scripts//py:percent", "test.ipynb"])
assert (tmpdir / "scripts" / "test.py").exists()
test_ipynb.remove()
jupytext_cli(
[
"--to",
"notebook",
"--output",
"test.ipynb",
"scripts/test.py",
]
)
assert test_ipynb.exists()

0 comments on commit 1732c2f

Please sign in to comment.