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