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