diff --git a/IPython/html/notebookapp.py b/IPython/html/notebookapp.py index 40300f3..51902a2 100644 --- a/IPython/html/notebookapp.py +++ b/IPython/html/notebookapp.py @@ -83,13 +83,12 @@ 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, Instance, + DottedObjectName ) from IPython.utils import py3compat from IPython.utils.path import filefind, get_ipython_dir @@ -218,10 +217,6 @@ class NotebookWebApplication(web.Application): return new_handlers -#----------------------------------------------------------------------------- -# Subcommands -#----------------------------------------------------------------------------- - class NbserverListApp(BaseIPythonApplication): description="List currently running notebook servers in this profile." @@ -244,37 +239,6 @@ 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') - self.notary.sign(nb) - 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 #----------------------------------------------------------------------------- @@ -349,7 +313,6 @@ class NotebookApp(BaseIPythonApplication): subcommands = dict( list=(NbserverListApp, NbserverListApp.description.splitlines()[0]), - trust=(NotebookTrustApp, NotebookTrustApp.description), ) kernel_argv = List(Unicode) diff --git a/IPython/nbformat/sign.py b/IPython/nbformat/sign.py index 002242f..4e4feda 100644 --- a/IPython/nbformat/sign.py +++ b/IPython/nbformat/sign.py @@ -18,8 +18,11 @@ import io import os from IPython.utils.py3compat import string_types, unicode_type, cast_bytes -from IPython.config import LoggingConfigurable from IPython.utils.traitlets import Instance, Bytes, Enum, Any, Unicode +from IPython.config import LoggingConfigurable +from IPython.core.application import BaseIPythonApplication + +from .current import read, write #----------------------------------------------------------------------------- # Code @@ -198,6 +201,41 @@ class NotebookNotary(LoggingConfigurable): if not cell.get('trusted', False): return False return True + + +class TrustNotebookApp(BaseIPythonApplication): + + description="""Sign one or more IPython notebooks with your key, + to trust their dynamic (HTML, Javascript) output. + Otherwise, you will have to re-execute the notebook to see output. + """ + + examples="""ipython trust mynotebook.ipynb and_this_one.ipynb""" + + notary = Instance(NotebookNotary) + def _notary_default(self): + return 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 = read(f, 'json') + if self.notary.check_signature(nb): + print("Notebook already signed: %s" % notebook_path) + else: + print("Signing notebook: %s" % notebook_path) + self.notary.sign(nb) + with io.open(notebook_path, 'w', encoding='utf8') as f: + 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: + self.sign_notebook(notebook_path) - \ No newline at end of file diff --git a/IPython/terminal/ipapp.py b/IPython/terminal/ipapp.py index 2605e16..4f15d20 100755 --- a/IPython/terminal/ipapp.py +++ b/IPython/terminal/ipapp.py @@ -249,6 +249,9 @@ class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp): nbconvert=('IPython.nbconvert.nbconvertapp.NbConvertApp', "Convert notebooks to/from other formats." ), + trust=('IPython.nbformat.sign.TrustNotebookApp', + "Sign notebooks to trust their potentially unsafe contents at load." + ), )) # *do* autocreate requested profile, but don't create the config file.