##// END OF EJS Templates
Backport PR #9976: Let IPython.lib.guisupport detect terminal-integrated event loops...
Backport PR #9976: Let IPython.lib.guisupport detect terminal-integrated event loops Closes gh-9974 This is a bit more invasive than most backported changes, but it fixes a regression in IPython 5. My thinking: - The `guisupport` APIs that worked before should continue working until/unless we deprecate them. - There should be a common way to check if an event loop is already running in both the terminal and an IPython kernel. - It should be possible to check for any event loop, not just Qt and Wx (which `guisupport` has checks for). My plan is to make a public attribute `shell.active_eventloop`, which is either None or a string naming the event loop which IPython will run when waiting for input. E.g. `qt` or `gtk3`. (Todo: should we also expose the event loop object in cases where there is one? Not sure if anything useful can be done with it). This PR adds that attribute for terminal IPython; if we agree on it I'll make a separate PR for ipykernel. The functions in guisupport then become a convenient shortcut for checking this, and we can decide whether to deprecate them in favour or something more uniform, or add similar convenience functions for other common event loops. Signed-off-by: Thomas Kluyver <thomas@kluyver.me.uk>

File last commit:

r23138:fb70b991
r23138:fb70b991
Show More
__init__.py
49 lines | 1.1 KiB | text/x-python | PythonLexer
import importlib
import os
aliases = {
'qt4': 'qt',
'gtk2': 'gtk',
}
backends = [
'qt', 'qt4', 'qt5',
'gtk', 'gtk2', 'gtk3',
'tk',
'wx',
'pyglet', 'glut',
'osx',
]
registered = {}
def register(name, inputhook):
"""Register the function *inputhook* as an event loop integration."""
registered[name] = inputhook
class UnknownBackend(KeyError):
def __init__(self, name):
self.name = name
def __str__(self):
return ("No event loop integration for {!r}. "
"Supported event loops are: {}").format(self.name,
', '.join(backends + sorted(registered)))
def get_inputhook_name_and_func(gui):
if gui in registered:
return gui, registered[gui]
if gui not in backends:
raise UnknownBackend(gui)
if gui in aliases:
return get_inputhook_name_and_func(aliases[gui])
gui_mod = gui
if gui == 'qt5':
os.environ['QT_API'] = 'pyqt5'
gui_mod = 'qt'
mod = importlib.import_module('IPython.terminal.pt_inputhooks.'+gui_mod)
return gui, mod.inputhook