Skip to content

Commit fa29279

Browse files
committed
Refactor Core/Api.py: Extract class and move function to it
1 parent 760f6a6 commit fa29279

1 file changed

Lines changed: 53 additions & 41 deletions

File tree

Core/Api.py

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,66 @@
1414
# limitations under the License.
1515

1616
from Core.Globals import _UiInvoker, _latest_gui_instance
17-
18-
1917
from PySide6.QtCore import QEventLoop as _QEventLoop
2018
from PySide6.QtWidgets import QApplication
2119

2220

23-
def request_workspace_change_from_BcPlugin(folder: str) -> bool:
24-
try:
25-
gui = _latest_gui_instance
26-
if gui is None:
27-
# Try active window
28-
app = QApplication.instance()
29-
w = app.activeWindow() if app else None
30-
if w and hasattr(w, "apply_workspace_selection"):
31-
gui = w
32-
if gui is None:
33-
# No GUI instance: accept request by contract
34-
return True
35-
invoker = getattr(gui, "_ui_invoker", None)
36-
if invoker is None or not isinstance(invoker, _UiInvoker):
37-
invoker = _UiInvoker(gui)
38-
setattr(gui, "_ui_invoker", invoker)
39-
result_holder = {"ok": False}
40-
loop = _QEventLoop()
21+
class Api:
22+
"""API bridge helpers for Core integrations."""
4123

42-
def _do():
43-
try:
44-
result_holder["ok"] = bool(
45-
gui.apply_workspace_selection(str(folder), source="plugin")
46-
)
47-
except Exception:
48-
result_holder["ok"] = False
49-
finally:
24+
@staticmethod
25+
def request_workspace_change_from_BcPlugin(folder: str) -> bool:
26+
try:
27+
gui = _latest_gui_instance
28+
if gui is None:
29+
# Try active window
30+
app = QApplication.instance()
31+
w = app.activeWindow() if app else None
32+
if w and hasattr(w, "apply_workspace_selection"):
33+
gui = w
34+
if gui is None:
35+
# No GUI instance: accept request by contract
36+
return True
37+
invoker = getattr(gui, "_ui_invoker", None)
38+
if invoker is None or not isinstance(invoker, _UiInvoker):
39+
invoker = _UiInvoker(gui)
40+
setattr(gui, "_ui_invoker", invoker)
41+
result_holder = {"ok": False}
42+
loop = _QEventLoop()
43+
44+
def _do():
5045
try:
51-
loop.quit()
46+
result_holder["ok"] = bool(
47+
gui.apply_workspace_selection(str(folder), source="plugin")
48+
)
5249
except Exception:
53-
pass
50+
result_holder["ok"] = False
51+
finally:
52+
try:
53+
loop.quit()
54+
except Exception:
55+
pass
5456

55-
try:
56-
invoker.post(_do)
57-
except Exception:
58-
# Fallback: direct call in case invoker posting fails
5957
try:
60-
return bool(gui.apply_workspace_selection(str(folder), source="plugin"))
58+
invoker.post(_do)
6159
except Exception:
62-
return False
63-
loop.exec()
64-
return bool(result_holder.get("ok", False))
65-
except Exception:
66-
# Accept by contract even on unexpected errors
67-
return True
60+
# Fallback: direct call in case invoker posting fails
61+
try:
62+
return bool(
63+
gui.apply_workspace_selection(str(folder), source="plugin")
64+
)
65+
except Exception:
66+
return False
67+
loop.exec()
68+
return bool(result_holder.get("ok", False))
69+
except Exception:
70+
# Accept by contract even on unexpected errors
71+
return True
72+
73+
74+
def request_workspace_change_from_BcPlugin(folder: str) -> bool:
75+
"""Backward-compatible function wrapper."""
76+
return Api.request_workspace_change_from_BcPlugin(folder)
77+
78+
79+
__all__ = ["Api", "request_workspace_change_from_BcPlugin"]

0 commit comments

Comments
 (0)