Skip to content

Commit

Permalink
Merge pull request #120 from SylvainCorlay/update_traitlets
Browse files Browse the repository at this point in the history
Use traitlets>=4.1 APIs
  • Loading branch information
minrk committed Apr 9, 2016
2 parents cbdebb1 + 0a615d3 commit bc4fe89
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 73 deletions.
23 changes: 17 additions & 6 deletions ipykernel/comm/comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ipykernel.kernelbase import Kernel

from ipykernel.jsonutil import json_clean
from traitlets import Instance, Unicode, Bytes, Bool, Dict, Any
from traitlets import Instance, Unicode, Bytes, Bool, Dict, Any, default


class Comm(LoggingConfigurable):
Expand All @@ -21,15 +21,22 @@ class Comm(LoggingConfigurable):
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
allow_none=True)
kernel = Instance('ipykernel.kernelbase.Kernel')
def _kernel_default(self):

@default('kernel')
def _default_kernel(self):
if Kernel.initialized():
return Kernel.instance()

iopub_socket = Any()
def _iopub_socket_default(self):

@default('iopub_socket')
def _default_iopub_socket(self):
return self.kernel.iopub_socket

session = Instance('jupyter_client.session.Session')
def _session_default(self):

@default('session')
def _default_session(self):
if self.kernel is not None:
return self.kernel.session

Expand All @@ -38,7 +45,9 @@ def _session_default(self):
which to load comm target.""")

topic = Bytes()
def _topic_default(self):

@default('topic')
def _default_topic(self):
return ('comm-%s' % self.comm_id).encode('ascii')

_open_data = Dict(help="data dict, if any, to be included in comm_open")
Expand All @@ -49,7 +58,9 @@ def _topic_default(self):

_closed = Bool(True)
comm_id = Unicode()
def _comm_id_default(self):

@default('comm_id')
def _default_comm_id(self):
return uuid.uuid4().hex

primary = Bool(True, help="Am I the primary or secondary Comm?")
Expand Down
11 changes: 8 additions & 3 deletions ipykernel/comm/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from ipython_genutils.importstring import import_item
from ipython_genutils.py3compat import string_types
from traitlets import Instance, Unicode, Dict, Any
from traitlets import Instance, Unicode, Dict, Any, default

from .comm import Comm

Expand All @@ -34,10 +34,15 @@ class CommManager(LoggingConfigurable):
kernel = Instance('ipykernel.kernelbase.Kernel')

iopub_socket = Any()
def _iopub_socket_default(self):

@default('iopub_socket')
def _default_iopub_socket(self):
return self.kernel.iopub_socket

session = Instance('jupyter_client.session.Session')
def _session_default(self):

@default('session')
def _default_session(self):
return self.kernel.session

comms = Dict()
Expand Down
10 changes: 5 additions & 5 deletions ipykernel/inprocess/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@

# IPython imports
from ipykernel.inprocess.socket import DummySocket
from traitlets import Type, Instance
from traitlets import Type, Instance, default
from jupyter_client.clientabc import KernelClientABC
from jupyter_client.client import KernelClient

# Local imports
from .channels import (
InProcessChannel,
InProcessHBChannel,

)

#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -50,11 +49,12 @@ class InProcessKernelClient(KernelClient):
#--------------------------------------------------------------------------
# Channel management methods
#--------------------------------------------------------------------------

def _blocking_class_default(self):

@default('blocking_class')
def _default_blocking_class(self):
from .blocking import BlockingInProcessKernelClient
return BlockingInProcessKernelClient

def get_connection_info(self):
d = super(InProcessKernelClient, self).get_connection_info()
d['kernel'] = self.kernel
Expand Down
33 changes: 21 additions & 12 deletions ipykernel/inprocess/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from IPython.core.interactiveshell import InteractiveShellABC
from ipykernel.jsonutil import json_clean
from traitlets import Any, Enum, Instance, List, Type
from traitlets import Any, Enum, Instance, List, Type, default
from ipykernel.ipkernel import IPythonKernel
from ipykernel.zmqshell import ZMQInteractiveShell

Expand Down Expand Up @@ -52,21 +52,25 @@ class InProcessKernel(IPythonKernel):
control_stream = Any()
_underlying_iopub_socket = Instance(DummySocket, ())
iopub_thread = Instance(IOPubThread)
def _iopub_thread_default(self):

@default('iopub_thread')
def _default_iopub_thread(self):
thread = IOPubThread(self._underlying_iopub_socket)
thread.start()
return thread

iopub_socket = Instance(BackgroundSocket)
def _iopub_socket_default(self):

@default('iopub_socket')
def _default_iopub_socket(self):
return self.iopub_thread.background_socket

stdin_socket = Instance(DummySocket, ())

def __init__(self, **traits):
super(InProcessKernel, self).__init__(**traits)

self._underlying_iopub_socket.on_trait_change(self._io_dispatch, 'message_sent')
self._underlying_iopub_socket.observe(self._io_dispatch, names=['message_sent'])
self.shell.kernel = self

def execute_request(self, stream, ident, parent):
Expand Down Expand Up @@ -119,7 +123,7 @@ def _redirected_io(self):

#------ Trait change handlers --------------------------------------------

def _io_dispatch(self):
def _io_dispatch(self, change):
""" Called when a message is sent to the IO socket.
"""
ident, msg = self.session.recv(self.iopub_socket, copy=False)
Expand All @@ -128,20 +132,25 @@ def _io_dispatch(self):

#------ Trait initializers -----------------------------------------------

def _log_default(self):
@default('log')
def _default_log(self):
return logging.getLogger(__name__)

def _session_default(self):
@default('session')
def _default_session(self):
from jupyter_client.session import Session
return Session(parent=self, key=b'')

def _shell_class_default(self):
@default('shell_class')
def _default_shell_class(self):
return InProcessInteractiveShell

def _stdout_default(self):
@default('stdout')
def _default_stdout(self):
return OutStream(self.session, self.iopub_thread, u'stdout')

def _stderr_default(self):
@default('stderr')
def _default_stderr(self):
return OutStream(self.session, self.iopub_thread, u'stderr')

#-----------------------------------------------------------------------------
Expand Down
9 changes: 6 additions & 3 deletions ipykernel/inprocess/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

from traitlets import Instance, DottedObjectName
from traitlets import Instance, DottedObjectName, default
from jupyter_client.managerabc import KernelManagerABC
from jupyter_client.manager import KernelManager
from jupyter_client.session import Session
Expand All @@ -24,11 +24,14 @@ class InProcessKernelManager(KernelManager):
allow_none=True)
# the client class for KM.client() shortcut
client_class = DottedObjectName('ipykernel.inprocess.BlockingInProcessKernelClient')
def _blocking_class_default(self):

@default('blocking_class')
def _default_blocking_class(self):
from .blocking import BlockingInProcessKernelClient
return BlockingInProcessKernelClient

def _session_default(self):
@default('session')
def _default_session(self):
# don't sign in-process messages
return Session(key=b'', parent=self)

Expand Down
23 changes: 13 additions & 10 deletions ipykernel/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,24 @@ def __init__(self, **kwargs):
'text': "pandas",
'url': "http://pandas.pydata.org/pandas-docs/stable/",
},
], config=True)
]).tag(config=True)

# Kernel info fields
implementation = 'ipython'
implementation_version = release.version
language_info = {
'name': 'python',
'version': sys.version.split()[0],
'mimetype': 'text/x-python',
'codemirror_mode': {'name': 'ipython',
'version': sys.version_info[0]},
'pygments_lexer': 'ipython%d' % (3 if PY3 else 2),
'nbconvert_exporter': 'python',
'file_extension': '.py'
}
'name': 'python',
'version': sys.version.split()[0],
'mimetype': 'text/x-python',
'codemirror_mode': {
'name': 'ipython',
'version': sys.version_info[0]
},
'pygments_lexer': 'ipython%d' % (3 if PY3 else 2),
'nbconvert_exporter': 'python',
'file_extension': '.py'
}

@property
def banner(self):
return self.shell.banner
Expand Down
21 changes: 10 additions & 11 deletions ipykernel/kernelapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from IPython.utils import io
from ipython_genutils.path import filefind, ensure_dir_exists
from traitlets import (
Any, Instance, Dict, Unicode, Integer, Bool, DottedObjectName, Type,
Any, Instance, Dict, Unicode, Integer, Bool, DottedObjectName, Type, default
)
from ipython_genutils.importstring import import_item
from jupyter_core.paths import jupyter_runtime_dir
Expand Down Expand Up @@ -112,7 +112,7 @@ class IPKernelApp(BaseIPythonApplication, InteractiveShellApp,
poller = Any() # don't restrict this even though current pollers are all Threads
heartbeat = Instance(Heartbeat, allow_none=True)
ports = Dict()

subcommands = {
'install': (
'ipykernel.kernelspec.InstallIPythonKernelSpecApp',
Expand All @@ -122,7 +122,9 @@ class IPKernelApp(BaseIPythonApplication, InteractiveShellApp,

# connection info:
connection_dir = Unicode()
def _connection_dir_default(self):

@default('connection_dir')
def _default_connection_dir(self):
return jupyter_runtime_dir()

@property
Expand All @@ -132,7 +134,6 @@ def abs_connection_file(self):
else:
return self.connection_file


# streams, etc.
no_stdout = Bool(False, help="redirect stdout to the null device").tag(config=True)
no_stderr = Bool(False, help="redirect stderr to the null device").tag(config=True)
Expand All @@ -153,7 +154,7 @@ def abs_connection_file(self):

def init_crash_handler(self):
sys.excepthook = self.excepthook

def excepthook(self, etype, evalue, tb):
# write uncaught traceback to 'real' stderr, not zmq-forwarder
traceback.print_exception(etype, evalue, tb, file=sys.__stderr__)
Expand Down Expand Up @@ -241,7 +242,7 @@ def init_sockets(self):
self.control_socket.linger = 1000
self.control_port = self._bind_socket(self.control_socket, self.control_port)
self.log.debug("control ROUTER Channel on port: %i" % self.control_port)

self.init_iopub(context)

def init_iopub(self, context):
Expand All @@ -253,7 +254,6 @@ def init_iopub(self, context):
self.iopub_thread.start()
# backward-compat: wrap iopub socket API in background thread
self.iopub_socket = self.iopub_thread.background_socket


def init_heartbeat(self):
"""start the heart beating"""
Expand Down Expand Up @@ -312,9 +312,9 @@ def init_io(self):
if self.displayhook_class:
displayhook_factory = import_item(str(self.displayhook_class))
sys.displayhook = displayhook_factory(self.session, self.iopub_socket)

self.patch_io()

def patch_io(self):
"""Patch important libraries that can't handle sys.stdout forwarding"""
try:
Expand Down Expand Up @@ -388,7 +388,7 @@ def init_shell(self):
self.shell = getattr(self.kernel, 'shell', None)
if self.shell:
self.shell.configurables.append(self)

def init_extensions(self):
super(IPKernelApp, self).init_extensions()
# BEGIN HARDCODED WIDGETS HACK
Expand Down Expand Up @@ -434,7 +434,6 @@ def initialize(self, argv=None):
def start(self):
if self.subapp is not None:
return self.subapp.start()

if self.poller is not None:
self.poller.start()
self.kernel.start()
Expand Down
Loading

0 comments on commit bc4fe89

Please sign in to comment.