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