Skip to content

Commit

Permalink
WIP new argument require_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jan 9, 2024
1 parent 0606f03 commit 2d4854b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,29 +184,37 @@ def get(
content=True,
type=None,
format=None,
require_hash=False,
load_alternative_format=True,
):
"""Takes a path for an entity and returns its model"""
path = path.strip("/")
ext = os.path.splitext(path)[1]

super_kwargs = {"content": content, "type": type, "format": format}
if require_hash:
super_kwargs["require_hash"] = require_hash

# Not a notebook?
if (
not self.file_exists(path)
or self.dir_exists(path)
or (type is not None and type != "notebook")
):
return self.super.get(path, content, type, format)
return self.super.get(path, **super_kwargs)

config = self.get_config(path, use_cache=content is False)
if ext not in self.all_nb_extensions(config):
return self.super.get(path, content, type, format)
return self.super.get(path, **super_kwargs)

fmt = preferred_format(ext, config.preferred_jupytext_formats_read)
if ext == ".ipynb":
model = self.super.get(path, content, type="notebook", format=format)
super_kwargs["type"] = "notebook"
model = self.super.get(path, **super_kwargs)
else:
model = self.super.get(path, content, type="file", format="text")
super_kwargs["type"] = "file"
super_kwargs["format"] = "text"
model = self.super.get(path, **super_kwargs)
model["type"] = "notebook"
if content:
# We may need to update these keys, inherited from text files formats
Expand Down Expand Up @@ -304,6 +312,18 @@ def read_one_file(alt_path, alt_fmt):
# Modification time of a paired notebook is the timestamp of inputs #118 #978
model["last_modified"] = inputs.timestamp

if require_hash:
if inputs.path is None or outputs.path is None:
return model
model_other = self.super.get(
inputs.path if path == outputs.path else outputs.path,
require_hash=True,
)
# TODO: combine the two hashes into one with the initial length
# we might use e.g. https://docs.python.org/3/library/hashlib.html#tree-mode
model["hash"] = model["hash"] + model_other["hash"]
return model

if not content:
return model

Expand Down
30 changes: 30 additions & 0 deletions tests/integration/contents_manager/test_contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1851,3 +1851,33 @@ def test_move_paired_notebook_to_subdir_1059(tmp_path, python_notebook):
model = cm.get("scripts/subdir/my_notebook.py")
nb = model["content"]
compare_notebooks(nb, python_notebook, fmt="py:percent")


def test_hash_changes_if_paired_file_is_edited(tmp_path, python_notebook):
# 1. write py ipynb
cm = jupytext.TextFileContentsManager()
cm.formats = "ipynb,py"
cm.root_dir = str(tmp_path)

# save ipynb
nb = python_notebook
nb_name = "notebook.ipynb"
cm.save(model=notebook_model(nb), path=nb_name)
org_model = cm.get(nb_name, require_hash=True)

py_file = tmp_path / "notebook.py"

text = py_file.read_text()
assert "# %% [markdown]" in text.splitlines(), text

# modify the timestamp of the paired file
time.sleep(0.5)
py_file.write_text(text)
model = cm.get(nb_name, require_hash=True)
assert model["hash"] == org_model["hash"]

# modify the paired file
py_file.write_text(text + "\n# %%\n1 + 1\n")

new_model = cm.get(nb_name, require_hash=True)
assert new_model["hash"] != org_model["hash"]

0 comments on commit 2d4854b

Please sign in to comment.