Skip to content

Commit

Permalink
Add some comments and raise error in to_json_patch to make sure its n…
Browse files Browse the repository at this point in the history
…ot used.
  • Loading branch information
Martin Sandve Alnæs committed Jan 29, 2016
1 parent daab7d9 commit 7ae62b8
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 22 deletions.
1 change: 1 addition & 0 deletions nbdime/diff_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ def to_json_patch(d, path=""):
This is untested and will need some details worked out.
"""
raise RuntimeError("to_json_patch is currently not correct, see github issue.")
jp = []
for e in d:
op = e.op
Expand Down
61 changes: 43 additions & 18 deletions nbdime/diffing/notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,38 @@
__all__ = ["diff_notebooks"]


def __unused__notebook_diff_data():
# TODO: It might be possible to encode the below
# functions more compactly with collections
# of predicate functions and generalize.
# Basically, sequence diffs should be applied with multilevel
# algorithm for paths with more than one predicate,
# and using operator.__eq__ if no match in there.
predicates = {
"/cells": [
compare_cell_source_approximate,
compare_cell_source_exact,
compare_cell_source_and_outputs,
],
"/cells/#/outputs": [
compare_output_data_keys,
compare_output_data,
]
}
diff_algorithms = {
"/": diff_notebooks,
"/cells": diff_cells,
"/cells/#": diff_single_cells,
"/cells/#/source": diff_source,
"/cells/#/outputs": diff_outputs,
"/cells/#/outputs/#": diff_single_outputs,
}


def compare_cell_source_approximate(x, y):
"Compare source of cells x,y with approximate heuristics."
# Cell types must match
if x["cell_type"] != y["cell_type"]:
if x.cell_type != y["cell_type"]:
return False

# Convert from list to single string
Expand Down Expand Up @@ -120,8 +148,6 @@ def compare_output_data(x, y):
return False
if x["text"] != y["text"]:
return False
#d = diff(x["text"], y["text"])
#return bool(d) # FIXME
else: # if ot == "display_data" or ot == "execute_result":
if set(x["data"].keys()) != set(y["data"].keys()):
return False
Expand All @@ -135,16 +161,14 @@ def compare_output_data(x, y):
return True


def diff_source(a, b, compare="ignored"):
"Diff a pair of sources."
# FIXME: Make sure we use linebased diff of sources
# TODO: Use google-diff-patch-match library to diff the sources?
return diff(a, b)


def diff_single_outputs(a, b, compare="ignored"):
"Diff a pair of output cells."
# TODO: Handle output diffing with plugins? I.e. image diff, svg diff, json diff, etc.
# FIXME: Use linebased diff of some types of outputs:
# if a.output_type in ("execute_result", "display_data"):
# a.data.key if key.startswith('text/') or key in _non_text_split_mimes = {
# 'application/javascript','image/svg+xml'}
# a.text
return diff(a, b)


Expand All @@ -155,6 +179,13 @@ def diff_outputs(a, b, compare="ignored"):
return diff_sequence_multilevel(a, b, predicates, diff_single_outputs)


def diff_source(a, b, compare="ignored"):
"Diff a pair of sources."
# FIXME: Make sure we use linebased diff of sources
# TODO: Use google-diff-patch-match library to diff the sources?
return diff(a, b)


def diff_single_cells(a, b):
return diff_dicts(a, b, subdiffs={"source": diff_source, "outputs": diff_outputs})

Expand All @@ -172,12 +203,6 @@ def diff_cells(a, b, compare="ignored"):
return diff_sequence_multilevel(a, b, predicates, diff_single_cells)


def diff_notebooks(nba, nbb):
def diff_notebooks(a, b):
"""Compute the diff of two notebooks."""
try:
r = diff_dicts(nba, nbb, subdiffs={"cells": diff_cells})
except Exception as e:
import IPython
IPython.embed()
raise
return r
return diff_dicts(a, b, subdiffs={"cells": diff_cells})
5 changes: 2 additions & 3 deletions nbdime/merging/notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

from .generic import merge


def merge_notebooks(base, local, remote):
"""Merge changes introduced by notebooks local and remote from a shared ancestor base.
Return new (partially) merged notebook and unapplied diffs from the local and remote side.
"""
# FIXME: Implement notebook aware merge
merged, local_conflict_diffs, remote_conflict_diffs = merge(base, local, remote)
return nbformat.from_dict(merged), local_conflict_diffs, remote_conflict_diffs
merged = nbformat.from_dict(merged)
return merged, local_conflict_diffs, remote_conflict_diffs
4 changes: 4 additions & 0 deletions nbdime/nbdiffapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def main_diff(afn, bfn, dfn=None):
a = nbformat.read(afn, as_version=4)
b = nbformat.read(bfn, as_version=4)

# TODO: Split lines here?
#a = split_lines(a)
#b = split_lines(b)

d = diff_notebooks(a, b)

verbose = True
Expand Down
5 changes: 5 additions & 0 deletions nbdime/nbmergeapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def main_merge(bfn, lfn, rfn, mfn):
l = nbformat.read(lfn, as_version=4)
r = nbformat.read(rfn, as_version=4)

# TODO: Split lines here?
#b = split_lines(b)
#l = split_lines(l)
#r = split_lines(r)

m, lc, rc = merge_notebooks(b, l, r)

if mfn:
Expand Down
3 changes: 3 additions & 0 deletions nbdime/nbpatchapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def main_patch(bfn, dfn, afn):
d = json.load(df)
d = to_diffentry_dicts(d)

# TODO: Split lines here? Must be consistent with the diff for patch_notebook to work correctly!?
#before = split_lines(before)

after = patch_notebook(before, d)

if afn:
Expand Down
1 change: 0 additions & 1 deletion nbdime/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ def revert_strings_to_lists(obj):
return [revert_strings_to_lists(v) for v in obj]
else:
return obj

0 comments on commit 7ae62b8

Please sign in to comment.