Skip to content

Commit

Permalink
Notebook pairings are bounded by the config directory
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jun 1, 2020
1 parent d8f7b32 commit 7bffac6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
9 changes: 5 additions & 4 deletions jupytext/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
long_form_multiple_formats,
rearrange_jupytext_metadata,
)
from .paired_paths import (
base_path,
InconsistentPath,
)

JUPYTEXT_CONFIG_FILES = [
"jupytext",
Expand Down Expand Up @@ -138,6 +134,11 @@ def set_default_format_options(self, format_options, read=False):

def default_formats(self, path):
"""Return the default formats, if they apply to the current path #157"""
from .paired_paths import (
base_path,
InconsistentPath,
)

formats = long_form_multiple_formats(self.default_jupytext_formats)
for fmt in formats:
try:
Expand Down
12 changes: 12 additions & 0 deletions jupytext/paired_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .formats import long_form_one_format, long_form_multiple_formats
from .formats import short_form_one_format, short_form_multiple_formats
from .formats import NOTEBOOK_EXTENSIONS
from .config import find_jupytext_configuration_file


class InconsistentPath(ValueError):
Expand Down Expand Up @@ -67,6 +68,14 @@ def base_path(main_path, fmt):
notebook_dir, notebook_file_name = split(base)
sep = base[len(notebook_dir) : -len(notebook_file_name)]

base_dir = None
config_file = find_jupytext_configuration_file(notebook_dir)
if config_file:
config_file_dir = os.path.dirname(config_file)
if notebook_dir.startswith(config_file_dir):
base_dir = config_file_dir
notebook_dir = notebook_dir[len(config_file_dir) :]

if prefix_file_name:
if not notebook_file_name.startswith(prefix_file_name):
raise InconsistentPath(
Expand Down Expand Up @@ -116,6 +125,9 @@ def base_path(main_path, fmt):
notebook_dir = "///".join(long_notebook_dir.rsplit(long_prefix_root, 1))
notebook_dir = notebook_dir[len(sep) : -len(sep)]

if base_dir:
notebook_dir = base_dir + notebook_dir

if not notebook_dir:
return notebook_file_name

Expand Down
41 changes: 41 additions & 0 deletions tests/test_paired_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,47 @@ def test_full_path_in_tree_from_non_root():
)


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
with pytest.raises(
InconsistentPath,
match="Notebook directory '/other/subfolder' does not match prefix root 'notebooks'",
):
paired_paths(str(notebook_in_other_dir), fmt, formats)


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"]
Expand Down

0 comments on commit 7bffac6

Please sign in to comment.