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