##// END OF EJS Templates
move utils.localinterfaces to jupyter_client
move utils.localinterfaces to jupyter_client

File last commit:

r20949:73db0182
r21097:fedd2bd6
Show More
kernelspecapp.py
142 lines | 5.0 KiB | text/x-python | PythonLexer
Thomas Kluyver
Add kernelspec command line entry point
r16560
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import errno
import os.path
from IPython.config.application import Application
Thomas Kluyver
Trim which options are exposed on kernelspec CLI
r16597 from IPython.core.application import (
BaseIPythonApplication, base_flags, base_aliases
)
Thomas Kluyver
Add kernelspec command line entry point
r16560 from IPython.utils.traitlets import Instance, Dict, Unicode, Bool
Thomas Kluyver
Deterministic order for kernels in notebook UI
r17381 from .kernelspec import KernelSpecManager, _pythonfirst
Thomas Kluyver
Add kernelspec command line entry point
r16560
class ListKernelSpecs(BaseIPythonApplication):
description = """List installed kernel specifications."""
kernel_spec_manager = Instance(KernelSpecManager)
Matthias BUSSONNIER
Kernelspec handle case where folder already exists...
r17958
Thomas Kluyver
Trim which options are exposed on kernelspec CLI
r16597 # Not all of the base aliases are meaningful (e.g. profile)
aliases = {k: base_aliases[k] for k in ['ipython-dir', 'log-level']}
flags = {'debug': base_flags['debug'],}
Thomas Kluyver
Add kernelspec command line entry point
r16560
def _kernel_spec_manager_default(self):
Min RK
Add KernelSpecManager.whitelist...
r20316 return KernelSpecManager(parent=self, ipython_dir=self.ipython_dir)
Thomas Kluyver
Add kernelspec command line entry point
r16560
def start(self):
print("Available kernels:")
for kernelname in sorted(self.kernel_spec_manager.find_kernel_specs(),
key=_pythonfirst):
print(" %s" % kernelname)
class InstallKernelSpec(BaseIPythonApplication):
description = """Install a kernel specification directory."""
kernel_spec_manager = Instance(KernelSpecManager)
def _kernel_spec_manager_default(self):
return KernelSpecManager(ipython_dir=self.ipython_dir)
sourcedir = Unicode()
kernel_name = Unicode("", config=True,
help="Install the kernel spec with this name"
)
def _kernel_name_default(self):
return os.path.basename(self.sourcedir)
Thomas Kluyver
Update kernelspec command line interface
r19523 user = Bool(False, config=True,
Thomas Kluyver
Add kernelspec command line entry point
r16560 help="""
Thomas Kluyver
Update kernelspec command line interface
r19523 Try to install the kernel spec to the per-user directory instead of
the system or environment directory.
Thomas Kluyver
Add kernelspec command line entry point
r16560 """
)
replace = Bool(False, config=True,
help="Replace any existing kernel spec with this name."
)
aliases = {'name': 'InstallKernelSpec.kernel_name'}
Thomas Kluyver
Trim which options are exposed on kernelspec CLI
r16597 for k in ['ipython-dir', 'log-level']:
aliases[k] = base_aliases[k]
Thomas Kluyver
Add kernelspec command line entry point
r16560
Thomas Kluyver
Update kernelspec command line interface
r19523 flags = {'user': ({'InstallKernelSpec': {'user': True}},
"Install to the per-user kernel registry"),
Thomas Kluyver
Add kernelspec command line entry point
r16560 'replace': ({'InstallKernelSpec': {'replace': True}},
"Replace any existing kernel spec with this name."),
Thomas Kluyver
Trim which options are exposed on kernelspec CLI
r16597 'debug': base_flags['debug'],
Thomas Kluyver
Add kernelspec command line entry point
r16560 }
def parse_command_line(self, argv):
super(InstallKernelSpec, self).parse_command_line(argv)
# accept positional arg as profile name
if self.extra_args:
self.sourcedir = self.extra_args[0]
else:
print("No source directory specified.")
self.exit(1)
def start(self):
try:
self.kernel_spec_manager.install_kernel_spec(self.sourcedir,
kernel_name=self.kernel_name,
Thomas Kluyver
Update kernelspec command line interface
r19523 user=self.user,
Thomas Kluyver
Add kernelspec command line entry point
r16560 replace=self.replace,
)
except OSError as e:
if e.errno == errno.EACCES:
print("Permission denied")
self.exit(1)
elif e.errno == errno.EEXIST:
Thomas Kluyver
Deterministic sort for kernelspec list, better error for install
r16722 print("A kernel spec is already present at %s" % e.filename)
Thomas Kluyver
Add kernelspec command line entry point
r16560 self.exit(1)
raise
Thomas Kluyver
Make installation of native kernelspec explicit...
r17747 class InstallNativeKernelSpec(BaseIPythonApplication):
description = """Install the native kernel spec directory for this Python."""
kernel_spec_manager = Instance(KernelSpecManager)
def _kernel_spec_manager_default(self):
return KernelSpecManager(ipython_dir=self.ipython_dir)
Thomas Kluyver
Update kernelspec command line interface
r19523 user = Bool(False, config=True,
Thomas Kluyver
Make installation of native kernelspec explicit...
r17747 help="""
Thomas Kluyver
Update kernelspec command line interface
r19523 Try to install the kernel spec to the per-user directory instead of
the system or environment directory.
Thomas Kluyver
Make installation of native kernelspec explicit...
r17747 """
)
# Not all of the base aliases are meaningful (e.g. profile)
aliases = {k: base_aliases[k] for k in ['ipython-dir', 'log-level']}
Thomas Kluyver
Update kernelspec command line interface
r19523 flags = {'user': ({'InstallNativeKernelSpec': {'user': True}},
"Install to the per-user kernel registry"),
Thomas Kluyver
Make installation of native kernelspec explicit...
r17747 'debug': base_flags['debug'],
}
def start(self):
try:
Thomas Kluyver
Update kernelspec command line interface
r19523 self.kernel_spec_manager.install_native_kernel_spec(user=self.user)
Thomas Kluyver
Make installation of native kernelspec explicit...
r17747 except OSError as e:
MinRK
fix typo in KernelSpec flag...
r17963 self.exit(e)
Thomas Kluyver
Make installation of native kernelspec explicit...
r17747
Thomas Kluyver
Add kernelspec command line entry point
r16560 class KernelSpecApp(Application):
Thomas Kluyver
Fix applications displaying subcommands
r16561 name = "ipython kernelspec"
Thomas Kluyver
Add kernelspec command line entry point
r16560 description = """Manage IPython kernel specifications."""
Thomas Kluyver
Make installation of native kernelspec explicit...
r17747 subcommands = Dict({
'list': (ListKernelSpecs, ListKernelSpecs.description.splitlines()[0]),
'install': (InstallKernelSpec, InstallKernelSpec.description.splitlines()[0]),
'install-self': (InstallNativeKernelSpec, InstallNativeKernelSpec.description.splitlines()[0]),
})
Thomas Kluyver
Trim which options are exposed on kernelspec CLI
r16597 aliases = {}
flags = {}
Thomas Kluyver
Add kernelspec command line entry point
r16560
def start(self):
if self.subapp is None:
print("No subcommand specified. Must specify one of: %s"% list(self.subcommands))
print()
self.print_description()
self.print_subcommands()
self.exit(1)
else:
return self.subapp.start()