Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor inputhook to allow easy extension #5666

Merged
merged 6 commits into from
Sep 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions IPython/kernel/zmq/eventloops.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ def process_stream_events():
notifier = QtCore.QSocketNotifier(fd, QtCore.QSocketNotifier.Read, kernel.app)
notifier.activated.connect(process_stream_events)

# mapping of keys to loop functions
loop_map = {
'inline': None,
None : None,
}

def register_integration(*toolkitnames):
"""Decorator to register an event loop to integrate with the IPython kernel

The decorator takes names to register the event loop as for the %gui magic.
You can provide alternative names for the same toolkit.

The decorated function should take a single argument, the IPython kernel
instance, arrange for the event loop to call ``kernel.do_one_iteration()``
at least every ``kernel._poll_interval`` seconds, and start the event loop.

:mod:`IPython.kernel.zmq.eventloops` provides and registers such functions
for a few common event loops.
"""
def decorator(func):
for name in toolkitnames:
loop_map[name] = func
return func

return decorator


@register_integration('qt', 'qt4')
def loop_qt4(kernel):
"""Start a kernel with PyQt4 event loop integration."""

Expand All @@ -65,6 +93,7 @@ def loop_qt4(kernel):
start_event_loop_qt4(kernel.app)


@register_integration('wx')
def loop_wx(kernel):
"""Start a kernel with wx event loop support."""

Expand Down Expand Up @@ -117,6 +146,7 @@ def OnInit(self):
start_event_loop_wx(kernel.app)


@register_integration('tk')
def loop_tk(kernel):
"""Start a kernel with the Tk event loop."""

Expand Down Expand Up @@ -146,6 +176,7 @@ def start(self):
kernel.timer.start()


@register_integration('gtk')
def loop_gtk(kernel):
"""Start the kernel, coordinating with the GTK event loop"""
from .gui.gtkembed import GTKEmbed
Expand All @@ -154,6 +185,7 @@ def loop_gtk(kernel):
gtk_kernel.start()


@register_integration('gtk3')
def loop_gtk3(kernel):
"""Start the kernel, coordinating with the GTK event loop"""
from .gui.gtk3embed import GTKEmbed
Expand All @@ -162,6 +194,7 @@ def loop_gtk3(kernel):
gtk_kernel.start()


@register_integration('osx')
def loop_cocoa(kernel):
"""Start the kernel, coordinating with the Cocoa CFRunLoop event loop
via the matplotlib MacOSX backend.
Expand Down Expand Up @@ -232,18 +265,6 @@ def doi():
# ensure excepthook is restored
sys.excepthook = real_excepthook

# mapping of keys to loop functions
loop_map = {
'qt' : loop_qt4,
'qt4': loop_qt4,
'inline': None,
'osx': loop_cocoa,
'wx' : loop_wx,
'tk' : loop_tk,
'gtk': loop_gtk,
'gtk3': loop_gtk3,
None : None,
}


def enable_gui(gui, kernel=None):
Expand Down
Loading