Show More
@@ -1,144 +1,149 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 | |
|
44 | """Stores which version (i.e. `gui`) was requested the first time.""" | |
|
45 | ||
|
46 | ||
|
47 | 43 | def set_qt_api(gui): |
|
48 | 44 | """Sets the `QT_API` environment variable if it isn't already set.""" |
|
49 | 45 | |
|
50 | global last_qt_version | |
|
46 | qt_api = os.environ.get("QT_API", None) | |
|
51 | 47 | |
|
52 | if gui != "qt" and last_qt_version is not None: | |
|
53 | if last_qt_version != gui: | |
|
54 | raise ValueError( | |
|
55 | "Cannot switch Qt versions for this session; " | |
|
56 | f"must use {last_qt_version}." | |
|
48 | from IPython.external.qt_loaders import ( | |
|
49 | QT_API_PYQT, | |
|
50 | QT_API_PYQT5, | |
|
51 | QT_API_PYQT6, | |
|
52 | QT_API_PYSIDE, | |
|
53 | QT_API_PYSIDE2, | |
|
54 | QT_API_PYSIDE6, | |
|
55 | QT_API_PYQTv1, | |
|
56 | loaded_api, | |
|
57 | 57 | ) |
|
58 | 58 | |
|
59 | qt_api = os.environ.get("QT_API", None) | |
|
60 | if qt_api is not None and gui != "qt": | |
|
61 |
|
|
|
62 | "pyside": "qt4", | |
|
63 | "pyqt": "qt4", | |
|
64 | "pyside2": "qt5", | |
|
65 | "pyqt5": "qt5", | |
|
66 | "pyside6": "qt6", | |
|
67 | "pyqt6": "qt6", | |
|
59 | loaded = loaded_api() | |
|
60 | ||
|
61 | qt_env2gui = { | |
|
62 | QT_API_PYSIDE: 'qt4', | |
|
63 | QT_API_PYQTv1: 'qt4', | |
|
64 | QT_API_PYQT: 'qt4', | |
|
65 | QT_API_PYSIDE2: 'qt5', | |
|
66 | QT_API_PYQT5: 'qt5', | |
|
67 | QT_API_PYSIDE6: 'qt6', | |
|
68 | QT_API_PYQT6: 'qt6', | |
|
68 | 69 |
|
|
69 | if env2gui[qt_api] != gui: | |
|
70 | if loaded is not None and gui != 'qt': | |
|
71 | if qt_env2gui[loaded] != gui: | |
|
72 | raise ImportError( | |
|
73 | f'Cannot switch Qt versions for this session; must use {qt_env2gui[loaded]}.' | |
|
74 | ) | |
|
75 | ||
|
76 | if qt_api is not None and gui != 'qt': | |
|
77 | if qt_env2gui[qt_api] != gui: | |
|
70 | 78 | print( |
|
71 | 79 | f'Request for "{gui}" will be ignored because `QT_API` ' |
|
72 | 80 | f'environment variable is set to "{qt_api}"' |
|
73 | 81 | ) |
|
74 | 82 | else: |
|
75 | 83 | # NOTE: 'qt4' is not selectable because it's set as an alias for 'qt'; see `aliases` above. |
|
76 | 84 | if gui == "qt4": |
|
77 | 85 | try: |
|
78 | 86 | import PyQt # noqa |
|
79 | 87 | |
|
80 | 88 | os.environ["QT_API"] = "pyqt" |
|
81 | 89 | except ImportError: |
|
82 | 90 | try: |
|
83 | 91 | import PySide # noqa |
|
84 | 92 | |
|
85 | 93 | os.environ["QT_API"] = "pyside" |
|
86 | 94 | except ImportError: |
|
87 | 95 | # Neither implementation installed; set it to something so IPython gives an error |
|
88 | 96 | os.environ["QT_API"] = "pyqt" |
|
89 | 97 | elif gui == "qt5": |
|
90 | 98 | try: |
|
91 | 99 | import PyQt5 # noqa |
|
92 | 100 | |
|
93 | 101 | os.environ["QT_API"] = "pyqt5" |
|
94 | 102 | except ImportError: |
|
95 | 103 | try: |
|
96 | 104 | import PySide2 # noqa |
|
97 | 105 | |
|
98 | 106 | os.environ["QT_API"] = "pyside2" |
|
99 | 107 | except ImportError: |
|
100 | 108 | os.environ["QT_API"] = "pyqt5" |
|
101 | 109 | elif gui == "qt6": |
|
102 | 110 | try: |
|
103 | 111 | import PyQt6 # noqa |
|
104 | 112 | |
|
105 | 113 | os.environ["QT_API"] = "pyqt6" |
|
106 | 114 | except ImportError: |
|
107 | 115 | try: |
|
108 | 116 | import PySide6 # noqa |
|
109 | 117 | |
|
110 | 118 | os.environ["QT_API"] = "pyside6" |
|
111 | 119 | except ImportError: |
|
112 | 120 | os.environ["QT_API"] = "pyqt6" |
|
113 | 121 | elif gui == "qt": |
|
114 | 122 | # Don't set QT_API; let IPython logic choose the version. |
|
115 | 123 | if "QT_API" in os.environ.keys(): |
|
116 | 124 | del os.environ["QT_API"] |
|
117 | 125 | else: |
|
118 | 126 | raise ValueError( |
|
119 | 127 | f'Unrecognized Qt version: {gui}. Should be "qt4", "qt5", "qt6", or "qt".' |
|
120 | 128 | ) |
|
121 | 129 | |
|
122 | # Due to the import mechanism, we can't change Qt versions once we've chosen one. So we tag the | |
|
123 | # version so we can check for this and give an error. | |
|
124 | last_qt_version = gui | |
|
125 | 130 | |
|
126 | 131 | def get_inputhook_name_and_func(gui): |
|
127 | 132 | print(f"`get_inputhook_name_and_func` called with {gui=}") |
|
128 | 133 | if gui in registered: |
|
129 | 134 | return gui, registered[gui] |
|
130 | 135 | |
|
131 | 136 | if gui not in backends: |
|
132 | 137 | raise UnknownBackend(gui) |
|
133 | 138 | |
|
134 | 139 | if gui in aliases: |
|
135 | 140 | print("gui has an alias") |
|
136 | 141 | return get_inputhook_name_and_func(aliases[gui]) |
|
137 | 142 | |
|
138 | 143 | gui_mod = gui |
|
139 | 144 | if gui.startswith("qt"): |
|
140 | 145 | set_qt_api(gui) |
|
141 | 146 | gui_mod = "qt" |
|
142 | 147 | |
|
143 | 148 | mod = importlib.import_module("IPython.terminal.pt_inputhooks." + gui_mod) |
|
144 | 149 | return gui, mod.inputhook |
General Comments 0
You need to be logged in to leave comments.
Login now