Skip to content

Commit

Permalink
Refactor plugin loading mechanism in Python host and streamline launc…
Browse files Browse the repository at this point in the history
…her view handling in Flutter UI
  • Loading branch information
qianlifeng committed Dec 30, 2024
1 parent b51c9bb commit 2da8c04
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
61 changes: 39 additions & 22 deletions wox.plugin.host.python/src/wox_plugin_host/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,46 @@ async def load_plugin(ctx: Context, request: Dict[str, Any]) -> None:
if path.exists(deps_dir) and deps_dir not in sys.path:
sys.path.append(deps_dir)

# Combine plugin directory and entry file to get full path
full_entry_path: str = path.join(plugin_directory, entry)

# Import the plugin module
spec = importlib.util.spec_from_file_location("plugin", full_entry_path)
if spec is None or spec.loader is None:
raise ImportError(f"Could not load plugin from {full_entry_path}")

module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

if not hasattr(module, "plugin"):
raise AttributeError("Plugin module does not have a 'plugin' attribute")

plugin_instances[plugin_id] = PluginInstance(
plugin=module.plugin,
api=None,
module_path=full_entry_path,
actions={},
refreshes={},
)
try:
# Add the parent directory to Python path
parent_dir = path.dirname(plugin_directory)
if parent_dir not in sys.path:
sys.path.append(parent_dir)

# Convert entry path to module path
# e.g., "killprocess/main.py" -> "dist.killprocess.main"
plugin_dir_name = path.basename(plugin_directory)
entry_without_ext = entry.replace(".py", "").replace("/", ".")
module_path = f"{plugin_dir_name}.{entry_without_ext}"

await logger.info(
ctx.get_trace_id(),
f"module_path: {module_path}, plugin_dir_name: {plugin_dir_name}, entry_without_ext: {entry_without_ext}",
)

# Import the module
module = importlib.import_module(module_path)

if not hasattr(module, "plugin"):
raise AttributeError("Plugin module does not have a 'plugin' attribute")

plugin_instances[plugin_id] = PluginInstance(
plugin=module.plugin,
api=None,
module_path=plugin_directory,
actions={},
refreshes={},
)

await logger.info(ctx.get_trace_id(), f"<{plugin_name}> load plugin successfully")
except Exception as e:
error_stack = traceback.format_exc()
await logger.error(
ctx.get_trace_id(),
f"<{plugin_name}> load plugin failed: {str(e)}\nStack trace:\n{error_stack}",
)
raise e

await logger.info(ctx.get_trace_id(), f"<{plugin_name}> load plugin successfully")
except Exception as e:
error_stack = traceback.format_exc()
await logger.error(
Expand Down
6 changes: 0 additions & 6 deletions wox.ui.flutter/wox/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,6 @@ class _WoxAppState extends State<WoxApp> with WindowListener, ProtocolListener {
return;
}

// switch to the launcher view if in setting view
var launcherController = Get.find<WoxLauncherController>();
if (launcherController.isInSettingView.value) {
launcherController.isInSettingView.value = false;
}

WoxApi.instance.onFocusLost();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ class WoxLauncherController extends GetxController {
await clearQueryResults();
}

// switch to the launcher view if in setting view
if (isInSettingView.value) {
isInSettingView.value = false;
}

await WoxApi.instance.onHide(currentQuery.value);
}

Expand Down

0 comments on commit 2da8c04

Please sign in to comment.