##// END OF EJS Templates
Address @ccordoba12's comments
Emilio Graff -
Show More
@@ -1,132 +1,134 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 "qt5",
11 "qt5",
12 "qt6",
12 "qt6",
13 "gtk",
13 "gtk",
14 "gtk2",
14 "gtk2",
15 "gtk3",
15 "gtk3",
16 "gtk4",
16 "gtk4",
17 "tk",
17 "tk",
18 "wx",
18 "wx",
19 "pyglet",
19 "pyglet",
20 "glut",
20 "glut",
21 "osx",
21 "osx",
22 "asyncio",
22 "asyncio",
23 ]
23 ]
24
24
25 registered = {}
25 registered = {}
26
26
27 def register(name, inputhook):
27 def register(name, inputhook):
28 """Register the function *inputhook* as an event loop integration."""
28 """Register the function *inputhook* as an event loop integration."""
29 registered[name] = inputhook
29 registered[name] = inputhook
30
30
31
31
32 class UnknownBackend(KeyError):
32 class UnknownBackend(KeyError):
33 def __init__(self, name):
33 def __init__(self, name):
34 self.name = name
34 self.name = name
35
35
36 def __str__(self):
36 def __str__(self):
37 return ("No event loop integration for {!r}. "
37 return ("No event loop integration for {!r}. "
38 "Supported event loops are: {}").format(self.name,
38 "Supported event loops are: {}").format(self.name,
39 ', '.join(backends + sorted(registered)))
39 ', '.join(backends + sorted(registered)))
40
40
41
41
42 def set_qt_api(gui):
42 def set_qt_api(gui):
43 """Sets the `QT_API` environment variable if it isn't already set."""
43 """Sets the `QT_API` environment variable if it isn't already set."""
44
44
45 qt_api = os.environ.get("QT_API", None)
45 qt_api = os.environ.get("QT_API", None)
46
46
47 from IPython.external.qt_loaders import (
47 from IPython.external.qt_loaders import (
48 QT_API_PYQT,
48 QT_API_PYQT,
49 QT_API_PYQT5,
49 QT_API_PYQT5,
50 QT_API_PYQT6,
50 QT_API_PYQT6,
51 QT_API_PYSIDE,
51 QT_API_PYSIDE,
52 QT_API_PYSIDE2,
52 QT_API_PYSIDE2,
53 QT_API_PYSIDE6,
53 QT_API_PYSIDE6,
54 QT_API_PYQTv1,
54 QT_API_PYQTv1,
55 loaded_api,
55 loaded_api,
56 )
56 )
57
57
58 loaded = loaded_api()
58 loaded = loaded_api()
59
59
60 qt_env2gui = {
60 qt_env2gui = {
61 QT_API_PYSIDE: "qt4",
61 QT_API_PYSIDE: "qt4",
62 QT_API_PYQTv1: "qt4",
62 QT_API_PYQTv1: "qt4",
63 QT_API_PYQT: "qt4",
63 QT_API_PYQT: "qt4",
64 QT_API_PYSIDE2: "qt5",
64 QT_API_PYSIDE2: "qt5",
65 QT_API_PYQT5: "qt5",
65 QT_API_PYQT5: "qt5",
66 QT_API_PYSIDE6: "qt6",
66 QT_API_PYSIDE6: "qt6",
67 QT_API_PYQT6: "qt6",
67 QT_API_PYQT6: "qt6",
68 }
68 }
69 if loaded is not None and gui != "qt":
69 if loaded is not None and gui != "qt":
70 if qt_env2gui[loaded] != gui:
70 if qt_env2gui[loaded] != gui:
71 raise ImportError(
71 print(
72 f"Cannot switch Qt versions for this session; must use {qt_env2gui[loaded]}."
72 f"Cannot switch Qt versions for this session; must use {qt_env2gui[loaded]}."
73 )
73 )
74 return
74
75
75 if qt_api is not None and gui != "qt":
76 if qt_api is not None and gui != "qt":
76 if qt_env2gui[qt_api] != gui:
77 if qt_env2gui[qt_api] != gui:
77 print(
78 print(
78 f'Request for "{gui}" will be ignored because `QT_API` '
79 f'Request for "{gui}" will be ignored because `QT_API` '
79 f'environment variable is set to "{qt_api}"'
80 f'environment variable is set to "{qt_api}"'
80 )
81 )
81 else:
82 else:
82 if gui == "qt5":
83 if gui == "qt5":
83 try:
84 try:
84 import PyQt5 # noqa
85 import PyQt5 # noqa
85
86
86 os.environ["QT_API"] = "pyqt5"
87 os.environ["QT_API"] = "pyqt5"
87 except ImportError:
88 except ImportError:
88 try:
89 try:
89 import PySide2 # noqa
90 import PySide2 # noqa
90
91
91 os.environ["QT_API"] = "pyside2"
92 os.environ["QT_API"] = "pyside2"
92 except ImportError:
93 except ImportError:
93 os.environ["QT_API"] = "pyqt5"
94 os.environ["QT_API"] = "pyqt5"
94 elif gui == "qt6":
95 elif gui == "qt6":
95 try:
96 try:
96 import PyQt6 # noqa
97 import PyQt6 # noqa
97
98
98 os.environ["QT_API"] = "pyqt6"
99 os.environ["QT_API"] = "pyqt6"
99 except ImportError:
100 except ImportError:
100 try:
101 try:
101 import PySide6 # noqa
102 import PySide6 # noqa
102
103
103 os.environ["QT_API"] = "pyside6"
104 os.environ["QT_API"] = "pyside6"
104 except ImportError:
105 except ImportError:
105 os.environ["QT_API"] = "pyqt6"
106 os.environ["QT_API"] = "pyqt6"
106 elif gui == "qt":
107 elif gui == "qt":
107 # Don't set QT_API; let IPython logic choose the version.
108 # Don't set QT_API; let IPython logic choose the version.
108 if "QT_API" in os.environ.keys():
109 if "QT_API" in os.environ.keys():
109 del os.environ["QT_API"]
110 del os.environ["QT_API"]
110 else:
111 else:
111 raise ValueError(
112 print(
112 f'Unrecognized Qt version: {gui}. Should be "qt5", "qt6", or "qt".'
113 f'Unrecognized Qt version: {gui}. Should be "qt5", "qt6", or "qt".'
113 )
114 )
115 return
114
116
115
117
116 def get_inputhook_name_and_func(gui):
118 def get_inputhook_name_and_func(gui):
117 if gui in registered:
119 if gui in registered:
118 return gui, registered[gui]
120 return gui, registered[gui]
119
121
120 if gui not in backends:
122 if gui not in backends:
121 raise UnknownBackend(gui)
123 raise UnknownBackend(gui)
122
124
123 if gui in aliases:
125 if gui in aliases:
124 return get_inputhook_name_and_func(aliases[gui])
126 return get_inputhook_name_and_func(aliases[gui])
125
127
126 gui_mod = gui
128 gui_mod = gui
127 if gui.startswith("qt"):
129 if gui.startswith("qt"):
128 set_qt_api(gui)
130 set_qt_api(gui)
129 gui_mod = "qt"
131 gui_mod = "qt"
130
132
131 mod = importlib.import_module("IPython.terminal.pt_inputhooks." + gui_mod)
133 mod = importlib.import_module("IPython.terminal.pt_inputhooks." + gui_mod)
132 return gui, mod.inputhook
134 return gui, mod.inputhook
General Comments 0
You need to be logged in to leave comments. Login now