diff --git a/IPython/html/notebookapp.py b/IPython/html/notebookapp.py index 51902a2..bc8405d 100644 --- a/IPython/html/notebookapp.py +++ b/IPython/html/notebookapp.py @@ -83,12 +83,13 @@ from IPython.kernel.zmq.kernelapp import ( kernel_flags, kernel_aliases, ) +from IPython.nbformat import current, sign from IPython.utils.importstring import import_item from IPython.utils.localinterfaces import localhost from IPython.utils import submodule from IPython.utils.traitlets import ( Dict, Unicode, Integer, List, Bool, Bytes, - DottedObjectName + DottedObjectName, Instance, ) from IPython.utils import py3compat from IPython.utils.path import filefind, get_ipython_dir @@ -217,6 +218,10 @@ class NotebookWebApplication(web.Application): return new_handlers +#----------------------------------------------------------------------------- +# Subcommands +#----------------------------------------------------------------------------- + class NbserverListApp(BaseIPythonApplication): description="List currently running notebook servers in this profile." @@ -239,6 +244,37 @@ class NbserverListApp(BaseIPythonApplication): else: print(serverinfo['url'], "::", serverinfo['notebook_dir']) + +class NotebookTrustApp(BaseIPythonApplication): + + description="""Sign one or more IPython notebooks with your key, + to trust their dynamic (HTML, Javascript) output.""" + + examples="""ipython notebook trust mynotebook.ipynb""" + + notary = Instance(sign.NotebookNotary) + def _notary_default(self): + return sign.NotebookNotary(parent=self, profile_dir=self.profile_dir) + + def sign_notebook(self, notebook_path): + if not os.path.exists(notebook_path): + self.log.error("Notebook missing: %s" % notebook_path) + self.exit(1) + with io.open(notebook_path, encoding='utf8') as f: + nb = current.read(f, 'json') + sign.trust_notebook(nb, self.notary.secret, self.notary.signature_scheme) + with io.open(notebook_path, 'w', encoding='utf8') as f: + current.write(nb, f, 'json') + + def start(self): + if not self.extra_args: + self.log.critical("Specify at least one notebook to sign.") + self.exit(1) + + for notebook_path in self.extra_args: + print("Signing notebook: %s" % notebook_path) + self.sign_notebook(notebook_path) + #----------------------------------------------------------------------------- # Aliases and Flags #----------------------------------------------------------------------------- @@ -313,6 +349,7 @@ class NotebookApp(BaseIPythonApplication): subcommands = dict( list=(NbserverListApp, NbserverListApp.description.splitlines()[0]), + trust=(NotebookTrustApp, NotebookTrustApp.description), ) kernel_argv = List(Unicode)