Skip to content

Commit

Permalink
Merge pull request #544 from vidartf/fix-merge-empty-base
Browse files Browse the repository at this point in the history
Fix error with empty base in merge
  • Loading branch information
vidartf committed Sep 29, 2020
2 parents 0f0a90a + 4d2e319 commit 141b2dc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
3 changes: 2 additions & 1 deletion nbdime/nbmergeapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def main_merge(args):
# Agreed on deletion = no conflics = return 0
return 0

b = read_notebook(bfn, on_null='minimal')
# Git seems to give empty base file for double insertions
b = read_notebook(bfn, on_null='minimal', on_empty='minimal')
l = read_notebook(lfn, on_null='minimal')
r = read_notebook(rfn, on_null='minimal')

Expand Down
26 changes: 24 additions & 2 deletions nbdime/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import codecs
from collections import defaultdict
import errno
import io
import locale
import os
import re
Expand All @@ -22,7 +23,7 @@
EXPLICIT_MISSING_FILE = '/dev/null'


def read_notebook(f, on_null):
def read_notebook(f, on_null, on_empty=None):
"""Read and return notebook json from filename
Parameters:
Expand All @@ -32,6 +33,10 @@ def read_notebook(f, on_null):
on_null: What to return when filename null
"empty": return empty dict
"minimal": return miminal valid notebook
on_empty: What to return when the file is completely empty (0 size)
None: Raise an error
"empty": return empty dict
"minimal: return minimal valid notebook
"""
if f == EXPLICIT_MISSING_FILE:
if on_null == 'empty':
Expand All @@ -43,7 +48,24 @@ def read_notebook(f, on_null):
'Not valid value for `on_null`: %r. Valid values '
'are "empty" or "minimal"' % (on_null,))
else:
return nbformat.read(f, as_version=4)
try:
return nbformat.read(f, as_version=4)
except nbformat.reader.NotJSONError:
if on_empty is None:
raise
# Reraise if file is not empty
if isinstance(f, string_types):
with io.open(f, encoding='utf-8') as fo:
if len(fo.read(10)) != 0:
raise
if on_empty == 'empty':
return {}
elif on_empty == 'minimal':
return nbformat.v4.new_notebook()
else:
raise ValueError(
'Not valid value for `on_empty`: %r. Valid values '
'are None, "empty" or "minimal"' % (on_empty,))


def as_text(text):
Expand Down
16 changes: 13 additions & 3 deletions nbdime/webapp/nbdimeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def write_error(self, status_code, **kwargs):
return self.finish(str(value))
return super(NbdimeHandler, self).write_error(status_code, **kwargs)

def read_notebook(self, arg):
def read_notebook(self, arg, fail_on_empty=True):
# Currently assuming arg is a filename relative to
# where the server was started from, later we may
# want to accept urls or full notebooks as well.
Expand All @@ -96,7 +96,17 @@ def read_notebook(self, arg):
if path == EXPLICIT_MISSING_FILE:
nb = nbformat.v4.new_notebook()
elif os.path.exists(path):
nb = nbformat.read(path, as_version=4)
try:
nb = nbformat.read(path, as_version=4)
except nbformat.reader.NotJSONError:
if fail_on_empty:
raise
# Handle empty notebook file
if isinstance(path, string_types):
with io.open(path, encoding='utf-8') as fo:
if len(fo.read(10)) != 0:
raise
nb = nbformat.v4.new_notebook()
else:
nb = nbformat.reads(r.text, as_version=4)
except requests.exceptions.HTTPError as e:
Expand Down Expand Up @@ -261,7 +271,7 @@ def post(self):
def get_notebook_argument(self, argname):
if 'mergetool_args' in self.params:
arg = self.params['mergetool_args'][argname]
return self.read_notebook(arg)
return self.read_notebook(arg, fail_on_empty=False)
return super(ApiMergeHandler, self).get_notebook_argument(argname)


Expand Down

0 comments on commit 141b2dc

Please sign in to comment.