__init__.py
139 lines
| 3.5 KiB
| text/x-python
|
PythonLexer
Thomas Kluyver
|
r21934 | import importlib | ||
import os | ||||
Matthias Bussonnier
|
r28399 | from typing import Tuple, Callable | ||
Thomas Kluyver
|
r21934 | |||
aliases = { | ||||
Thomas Kluyver
|
r21941 | 'qt4': 'qt', | ||
Thomas Kluyver
|
r22118 | 'gtk2': 'gtk', | ||
Thomas Kluyver
|
r21934 | } | ||
Thomas Kluyver
|
r22118 | backends = [ | ||
Thomas A Caswell
|
r26684 | "qt", | ||
"qt5", | ||||
"qt6", | ||||
"gtk", | ||||
"gtk2", | ||||
"gtk3", | ||||
Elliott Sales de Andrade
|
r26726 | "gtk4", | ||
Thomas A Caswell
|
r26684 | "tk", | ||
"wx", | ||||
"pyglet", | ||||
"glut", | ||||
"osx", | ||||
"asyncio", | ||||
Thomas Kluyver
|
r22118 | ] | ||
Thomas Kluyver
|
r22613 | registered = {} | ||
def register(name, inputhook): | ||||
"""Register the function *inputhook* as an event loop integration.""" | ||||
registered[name] = inputhook | ||||
Thomas A Caswell
|
r26683 | |||
Thomas Kluyver
|
r22118 | 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, | ||||
Thomas Kluyver
|
r22613 | ', '.join(backends + sorted(registered))) | ||
Thomas Kluyver
|
r22118 | |||
Thomas A Caswell
|
r26683 | |||
Emilio Graff
|
r27990 | def set_qt_api(gui): | ||
"""Sets the `QT_API` environment variable if it isn't already set.""" | ||||
Emilio Graff
|
r27998 | qt_api = os.environ.get("QT_API", None) | ||
Emilio Graff
|
r27992 | |||
Emilio Graff
|
r27998 | from IPython.external.qt_loaders import ( | ||
QT_API_PYQT, | ||||
QT_API_PYQT5, | ||||
QT_API_PYQT6, | ||||
QT_API_PYSIDE, | ||||
QT_API_PYSIDE2, | ||||
QT_API_PYSIDE6, | ||||
QT_API_PYQTv1, | ||||
loaded_api, | ||||
) | ||||
loaded = loaded_api() | ||||
qt_env2gui = { | ||||
Emilio Graff
|
r28001 | QT_API_PYSIDE: "qt4", | ||
QT_API_PYQTv1: "qt4", | ||||
QT_API_PYQT: "qt4", | ||||
QT_API_PYSIDE2: "qt5", | ||||
QT_API_PYQT5: "qt5", | ||||
QT_API_PYSIDE6: "qt6", | ||||
QT_API_PYQT6: "qt6", | ||||
Emilio Graff
|
r27998 | } | ||
Emilio Graff
|
r28001 | if loaded is not None and gui != "qt": | ||
Emilio Graff
|
r27998 | if qt_env2gui[loaded] != gui: | ||
Emilio Graff
|
r28046 | print( | ||
Emilio Graff
|
r28142 | f"Cannot switch Qt versions for this session; will use {qt_env2gui[loaded]}." | ||
Emilio Graff
|
r27992 | ) | ||
Emilio Graff
|
r28145 | return qt_env2gui[loaded] | ||
Emilio Graff
|
r27990 | |||
Emilio Graff
|
r28001 | if qt_api is not None and gui != "qt": | ||
Emilio Graff
|
r27998 | if qt_env2gui[qt_api] != gui: | ||
Emilio Graff
|
r27990 | print( | ||
f'Request for "{gui}" will be ignored because `QT_API` ' | ||||
f'environment variable is set to "{qt_api}"' | ||||
) | ||||
Emilio Graff
|
r28145 | return qt_env2gui[qt_api] | ||
Emilio Graff
|
r27990 | else: | ||
Emilio Graff
|
r28021 | if gui == "qt5": | ||
Emilio Graff
|
r27990 | try: | ||
import PyQt5 # noqa | ||||
os.environ["QT_API"] = "pyqt5" | ||||
except ImportError: | ||||
try: | ||||
import PySide2 # noqa | ||||
os.environ["QT_API"] = "pyside2" | ||||
except ImportError: | ||||
os.environ["QT_API"] = "pyqt5" | ||||
elif gui == "qt6": | ||||
try: | ||||
import PyQt6 # noqa | ||||
os.environ["QT_API"] = "pyqt6" | ||||
except ImportError: | ||||
try: | ||||
import PySide6 # noqa | ||||
os.environ["QT_API"] = "pyside6" | ||||
except ImportError: | ||||
os.environ["QT_API"] = "pyqt6" | ||||
elif gui == "qt": | ||||
# Don't set QT_API; let IPython logic choose the version. | ||||
if "QT_API" in os.environ.keys(): | ||||
del os.environ["QT_API"] | ||||
else: | ||||
Emilio Graff
|
r28049 | print(f'Unrecognized Qt version: {gui}. Should be "qt5", "qt6", or "qt".') | ||
Emilio Graff
|
r28149 | return | ||
Emilio Graff
|
r28145 | # Import it now so we can figure out which version it is. | ||
from IPython.external.qt_for_kernel import QT_API | ||||
Emilio Graff
|
r28148 | |||
Emilio Graff
|
r28145 | return qt_env2gui[QT_API] | ||
Emilio Graff
|
r27990 | |||
Matthias Bussonnier
|
r28399 | def get_inputhook_name_and_func(gui: str) -> Tuple[str, Callable]: | ||
Thomas Kluyver
|
r22613 | if gui in registered: | ||
Thomas Kluyver
|
r22913 | return gui, registered[gui] | ||
Thomas Kluyver
|
r22613 | |||
Thomas Kluyver
|
r22118 | if gui not in backends: | ||
raise UnknownBackend(gui) | ||||
Thomas Kluyver
|
r21934 | if gui in aliases: | ||
Thomas Kluyver
|
r22913 | return get_inputhook_name_and_func(aliases[gui]) | ||
Thomas Kluyver
|
r21934 | |||
Thomas Kluyver
|
r22913 | gui_mod = gui | ||
Emilio Graff
|
r27990 | if gui.startswith("qt"): | ||
Emilio Graff
|
r28145 | gui = set_qt_api(gui) | ||
Thomas A Caswell
|
r26684 | gui_mod = "qt" | ||
Thomas Kluyver
|
r21934 | |||
Emilio Graff
|
r27990 | mod = importlib.import_module("IPython.terminal.pt_inputhooks." + gui_mod) | ||
Thomas Kluyver
|
r22913 | return gui, mod.inputhook | ||