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