##// END OF EJS Templates
Bring in Qt import logic from `ipykernel`...
Emilio Graff -
Show More
@@ -1,68 +1,137 b''
1 import importlib
1 import importlib
2 import os
2 import os
3
3
4 aliases = {
4 aliases = {
5 'qt4': 'qt',
5 'qt4': 'qt',
6 'gtk2': 'gtk',
6 'gtk2': 'gtk',
7 }
7 }
8
8
9 backends = [
9 backends = [
10 "qt",
10 "qt",
11 "qt4",
11 "qt4",
12 "qt5",
12 "qt5",
13 "qt6",
13 "qt6",
14 "gtk",
14 "gtk",
15 "gtk2",
15 "gtk2",
16 "gtk3",
16 "gtk3",
17 "gtk4",
17 "gtk4",
18 "tk",
18 "tk",
19 "wx",
19 "wx",
20 "pyglet",
20 "pyglet",
21 "glut",
21 "glut",
22 "osx",
22 "osx",
23 "asyncio",
23 "asyncio",
24 ]
24 ]
25
25
26 registered = {}
26 registered = {}
27
27
28 def register(name, inputhook):
28 def register(name, inputhook):
29 """Register the function *inputhook* as an event loop integration."""
29 """Register the function *inputhook* as an event loop integration."""
30 registered[name] = inputhook
30 registered[name] = inputhook
31
31
32
32
33 class UnknownBackend(KeyError):
33 class UnknownBackend(KeyError):
34 def __init__(self, name):
34 def __init__(self, name):
35 self.name = name
35 self.name = name
36
36
37 def __str__(self):
37 def __str__(self):
38 return ("No event loop integration for {!r}. "
38 return ("No event loop integration for {!r}. "
39 "Supported event loops are: {}").format(self.name,
39 "Supported event loops are: {}").format(self.name,
40 ', '.join(backends + sorted(registered)))
40 ', '.join(backends + sorted(registered)))
41
41
42
42
43 def set_qt_api(gui):
44 """Sets the `QT_API` environment variable if it isn't already set."""
45 # TODO: how do we do this here?
46 # if hasattr(kernel, "app"):
47 # raise RuntimeError("Kernel already running a Qt event loop.")
48
49 # if gui != "qt" and hasattr(kernel, "last_qt_version"):
50 # if kernel.last_qt_version != gui:
51 # raise ValueError(
52 # "Cannot switch Qt versions for this session; "
53 # f"must use {kernel.last_qt_version}."
54 # )
55
56 qt_api = os.environ.get("QT_API", None)
57 if qt_api is not None and gui != "qt":
58 env2gui = {
59 "pyside": "qt4",
60 "pyqt": "qt4",
61 "pyside2": "qt5",
62 "pyqt5": "qt5",
63 "pyside6": "qt6",
64 "pyqt6": "qt6",
65 }
66 if env2gui[qt_api] != gui:
67 print(
68 f'Request for "{gui}" will be ignored because `QT_API` '
69 f'environment variable is set to "{qt_api}"'
70 )
71 else:
72 if gui == "qt4":
73 try:
74 import PyQt # noqa
75
76 os.environ["QT_API"] = "pyqt"
77 except ImportError:
78 try:
79 import PySide # noqa
80
81 os.environ["QT_API"] = "pyside"
82 except ImportError:
83 # Neither implementation installed; set it to something so IPython gives an error
84 os.environ["QT_API"] = "pyqt"
85 elif gui == "qt5":
86 try:
87 import PyQt5 # noqa
88
89 os.environ["QT_API"] = "pyqt5"
90 except ImportError:
91 try:
92 import PySide2 # noqa
93
94 os.environ["QT_API"] = "pyside2"
95 except ImportError:
96 os.environ["QT_API"] = "pyqt5"
97 elif gui == "qt6":
98 try:
99 import PyQt6 # noqa
100
101 os.environ["QT_API"] = "pyqt6"
102 except ImportError:
103 try:
104 import PySide6 # noqa
105
106 os.environ["QT_API"] = "pyside6"
107 except ImportError:
108 os.environ["QT_API"] = "pyqt6"
109 elif gui == "qt":
110 # Don't set QT_API; let IPython logic choose the version.
111 if "QT_API" in os.environ.keys():
112 del os.environ["QT_API"]
113 else:
114 raise ValueError(
115 f'Unrecognized Qt version: {gui}. Should be "qt4", "qt5", "qt6", or "qt".'
116 )
117
118
43 def get_inputhook_name_and_func(gui):
119 def get_inputhook_name_and_func(gui):
44 print(f'`get_inputhook_name_and_func` called with {gui=}')
120 print(f'`get_inputhook_name_and_func` called with {gui=}')
45 if gui in registered:
121 if gui in registered:
46 return gui, registered[gui]
122 return gui, registered[gui]
47
123
48 if gui not in backends:
124 if gui not in backends:
49 raise UnknownBackend(gui)
125 raise UnknownBackend(gui)
50
126
51 if gui in aliases:
127 if gui in aliases:
128 print('gui has an alias')
52 return get_inputhook_name_and_func(aliases[gui])
129 return get_inputhook_name_and_func(aliases[gui])
53
130
54 gui_mod = gui
131 gui_mod = gui
55 if gui == "qt5":
132 if gui.startswith("qt"):
56 os.environ["QT_API"] = "pyqt5"
133 set_qt_api(gui)
57 gui_mod = "qt"
58 elif gui == "qt6":
59 # XXX: this locks us into pyqt6 even if pyside6 is installed.
60 os.environ["QT_API"] = "pyqt6"
61 gui_mod = "qt"
134 gui_mod = "qt"
62
135
63 print(f'{gui_mod=}')
136 mod = importlib.import_module("IPython.terminal.pt_inputhooks." + gui_mod)
64 # Note: `IPython.terminal.pt_inputhooks.qt` imports `IPython.external.qt_for_kernel` and that's
65 # where the environment variable locks us into `pyqt6`, despite the fact that it seems `PySide6`
66 # is supported.
67 mod = importlib.import_module('IPython.terminal.pt_inputhooks.'+gui_mod)
68 return gui, mod.inputhook
137 return gui, mod.inputhook
General Comments 0
You need to be logged in to leave comments. Login now