Skip to content

Commit

Permalink
Change cli tools to not use the jupyter apps framework.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Sandve Alnæs committed Dec 14, 2015
1 parent 0d998a4 commit 00b334d
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 105 deletions.
3 changes: 2 additions & 1 deletion nbdime/diffing/comparing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@


def is_atomic(x):
atomic_strings = False # TODO: Configuration framework?
# TODO: Configuration framework?
atomic_strings = False

if atomic_strings:
return not isinstance(x, (list, dict))
Expand Down
70 changes: 35 additions & 35 deletions nbdime/nbdiffapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

from __future__ import print_function

import os
import sys
import nbformat
import json
from jupyter_core.application import JupyterApp, base_flags
from ._version import __version__
from .diffing.notebooks import diff_notebooks
from .dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE

nbdiff_flags = {
}
nbdiff_flags.update(base_flags)


# TODO: Improve and make a more reusable utility.
import pprint
Expand Down Expand Up @@ -49,43 +47,45 @@ def pretty_print_diff(d, indent=0):
return u"\n".join(pp)


class NBDiffApp(JupyterApp):
version = __version__
_usage = """\
Compute the difference between two Jupyter notebooks.
This is nbdiff from nbdime version {}.
Example usage:
description="""Compute the diff of two Jupyter notebooks.
"""
jupyter nbdiff before.ipynb after.ipynb patch.json
""".format(__version__)

examples = """
jupyter nbdiff before.ipynb after.ipynb patch.json
"""

flags = nbdiff_flags
def main_diff(afn, bfn, dfn):
for fn in (afn, bfn):
if not os.path.exists(fn):
print("Missing file {}".format(fn))
return 1

def start(self):
if len(self.extra_args) != 3:
self.log.critical("Specify filenames of exactly two notebooks to diff and the output patch json filename.")
self.exit(1)
afn, bfn, dfn = self.extra_args
if not os.path.exists(afn):
self.log.critical("Missing file {}".format(afn))
self.exit(1)
if not os.path.exists(bfn):
self.log.critical("Missing file {}".format(bfn))
self.exit(1)
a = nbformat.read(afn, as_version=4)
b = nbformat.read(bfn, as_version=4)

a = nbformat.read(afn, as_version=4)
b = nbformat.read(bfn, as_version=4)
d = diff_notebooks(a, b)

d = diff_notebooks(a, b)
verbose = True
if verbose:
print(pretty_print_diff(d))

verbose = True
if verbose:
print(pretty_print_diff(d))
with open(dfn, "w") as df:
json.dump(d, df)
# Verbose version:
#json.dump(d, df, indent=4, separators=(",", ": "))
return 0

with open(dfn, "w") as df:
json.dump(d, df)
# Verbose version:
#json.dump(d, df, indent=4, separators=(",", ": "))

def main():
NBDiffApp.launch_instance()
args = sys.argv[1:]
if len(args) != 3:
r = 1
else:
r = main_diff(*args)
if r:
print(_usage)
sys.exit(r)
65 changes: 33 additions & 32 deletions nbdime/nbmergeapp.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
"""Utilities for mergeing notebooks"""
"""Utilities for merging notebooks"""

# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

from __future__ import print_function

import os
import sys
import nbformat
import json
from jupyter_core.application import JupyterApp, base_flags
from ._version import __version__
from .merging import merge_notebooks

nbmerge_flags = {
}
nbmerge_flags.update(base_flags)

class NBMergeApp(JupyterApp):
version = __version__
_usage = """\
Merge two Jupyter notebooks "local" and "remote" with a common ancestor "base".
description="""Merge Jupyter notebooks.
"""
This is nbmerge from nbdime version {}.
examples = """
jupyter nbmerge base.ipynb local.ipynb remote.ipynb merged.ipynb
"""
Example usage:
flags = nbmerge_flags
jupyter nbmerge base.ipynb local.ipynb remote.ipynb merged.ipynb
""".format(__version__)

def start(self):
if len(self.extra_args) != 4:
self.log.critical("Specify filenames for four notebooks: base local remote merged.")
self.exit(1)

bfn, lfn, rfn, mfn = self.extra_args
def main_merge(bfn, lfn, rfn, mfn):
for fn in (bfn, lfn, rfn):
if not os.path.exists(fn):
print("Cannot find file '{}'".format(fn))
print(_usage)
return 1

for fn in (bfn, lfn, rfn):
if not os.path.exists(fn):
self.log.critical("Missing file {}".format(fn))
self.exit(1)
b = nbformat.read(bfn, as_version=4)
l = nbformat.read(lfn, as_version=4)
r = nbformat.read(rfn, as_version=4)

b = nbformat.read(bfn, as_version=4)
l = nbformat.read(lfn, as_version=4)
r = nbformat.read(rfn, as_version=4)
m = merge_notebooks(b, l, r)

m = merge_notebooks(b, l, r)
verbose = True
if verbose:
print(m)

verbose = True
if verbose:
print(m)
nbformat.write(mfn, m)
return 0

nbformat.write(mfn, m)

def main():
NBMergeApp.launch_instance()
args = sys.argv[1:]
if len(args) != 4:
r = 1
else:
r = main_merge(*args)
if r:
print(_usage)
sys.exit(r)
63 changes: 32 additions & 31 deletions nbdime/nbpatchapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,54 @@
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

from __future__ import print_function

import os
import sys
import nbformat
import json
from jupyter_core.application import JupyterApp, base_flags
from ._version import __version__
from .patching import patch_notebook

nbpatch_flags = {
}
nbpatch_flags.update(base_flags)

class NBPatchApp(JupyterApp):
version = __version__
_usage = """\
Apply patch from nbpatch to a Jupyter notebook.
description="""Apply patch to a Jupyter notebook.
"""
This is nbpatch from nbdime version {}.
examples = """
jupyter nbpatch before.ipynb patch.json after.ipynb
"""
Example usage:
flags = nbpatch_flags
jupyter nbpatch before.ipynb patch.json after.ipynb
""".format(__version__)

def start(self):
if len(self.extra_args) != 3:
self.log.critical("Specify one notebook and one patch to apply.")
self.exit(1)

bfn, dfn, afn = self.extra_args
def main_patch(bfn, dfn, afn):
for fn in (bfn, dfn):
if not os.path.exists(fn):
print("Missing file {}".format(fn))
return 1

for fn in (bfn, dfn):
if not os.path.exists(fn):
self.log.critical("Missing file {}".format(fn))
self.exit(1)
before = nbformat.read(bfn, as_version=4)
with open(dfn) as df:
d = json.load(df)

before = nbformat.read(bfn, as_version=4)
with open(dfn) as df:
d = json.load(df)
after = patch_notebook(before, d)

after = patch_notebook(before, d)
verbose = True
if verbose:
print(after)

verbose = True
if verbose:
print(after)
print("Writing", afn)
nbformat.write(after, afn)
return 0

print "Writing", afn
nbformat.write(after, afn)

def main():
NBPatchApp.launch_instance()
args = sys.argv[1:]
if len(args) != 3:
r = 1
else:
r = main_patch(*args)
if r:
print(_usage)
sys.exit(r)
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 7 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
],
)

Expand All @@ -85,12 +87,11 @@

setuptools_args = {}
install_requires = setuptools_args['install_requires'] = [
'jupyter_core',
'nbformat',
'nbformat', 'six', 'numpy',
]

extras_require = setuptools_args['extras_require'] = {
# 'test': ['pytest', 'ipykernel'],
'test': ['pytest'],
# 'execute': ['jupyter_client'],
}

Expand All @@ -100,9 +101,9 @@
# force entrypoints with setuptools (needed for Windows, unconditional because of wheels)
setup_args['entry_points'] = {
'console_scripts': [
'jupyter-nbdiff = nbdime.nbdiffapp:main',
'jupyter-nbpatch= nbdime.nbpatchapp:main',
'jupyter-nbmerge = nbdime.nbmergeapp:main',
'nbdiff = nbdime.nbdiffapp:main',
'nbpatch= nbdime.nbpatchapp:main',
'nbmerge = nbdime.nbmergeapp:main',
]
}
setup_args.pop('scripts', None)
Expand Down

0 comments on commit 00b334d

Please sign in to comment.