From afbf113f4817257674def199d95265a1539ab511 Mon Sep 17 00:00:00 2001 From: Zero Date: Thu, 18 Aug 2022 14:19:27 +0800 Subject: [PATCH 1/4] Create standard RPC class --- flowlauncher/RPC.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 flowlauncher/RPC.py diff --git a/flowlauncher/RPC.py b/flowlauncher/RPC.py new file mode 100644 index 0000000..3237c15 --- /dev/null +++ b/flowlauncher/RPC.py @@ -0,0 +1,30 @@ +from __future__ import annotations + +from dataclasses import dataclass, field +from json import dumps, loads + + +@dataclass +class RPC: + method: str = "query" + parameters: list = field(default_factory=list) + result: list = field(default_factory=list) + + debugMessage: str = "" + + # proxy is not working now + # proxy: dict = field(default_factory=dict) + + def to_dict(self) -> dict: + return self.__dict__ + + def to_string(self) -> str: + return dumps(self.to_dict()) + + @classmethod + def from_dict(cls, **kwargs) -> RPC: + return cls(**kwargs) + + @classmethod + def from_string(cls, string: str) -> RPC: + return cls(**loads(string)) if string else cls() From 754effe8ff7a8c65000b77b64e641917e57c7cdb Mon Sep 17 00:00:00 2001 From: Zero Date: Thu, 18 Aug 2022 14:21:15 +0800 Subject: [PATCH 2/4] Use RPC to simplify codes --- flowlauncher/FlowLauncher.py | 38 ++++++++-------------- flowlauncher/FlowLauncherAPI.py | 57 ++++++++++++++------------------- 2 files changed, 38 insertions(+), 57 deletions(-) diff --git a/flowlauncher/FlowLauncher.py b/flowlauncher/FlowLauncher.py index 53312e5..c2e5f6e 100644 --- a/flowlauncher/FlowLauncher.py +++ b/flowlauncher/FlowLauncher.py @@ -1,9 +1,10 @@ # -*- coding: utf-8 -*- -import inspect import sys -from json import loads, dumps +from inspect import getmembers, ismethod + +from RPC import RPC class FlowLauncher: @@ -12,33 +13,22 @@ class FlowLauncher: """ def __init__(self): - - # defalut jsonrpc - self.rpc_request = {'method': 'query', 'parameters': ['']} - self.debugMessage = "" - if len(sys.argv) > 1: - # Gets JSON-RPC from Flow Launcher process. - self.rpc_request = loads(sys.argv[1]) - - # proxy is not working now - # self.proxy = self.rpc_request.get("proxy", {}) - - request_method_name = self.rpc_request.get("method", "query") - request_parameters = self.rpc_request.get("parameters", []) + self.rpc_request = RPC.from_string(sys.argv[1]) + else: + # defalut JSON-RPC + self.rpc_request = RPC() - methods = inspect.getmembers(self, predicate=inspect.ismethod) - request_method = dict(methods)[request_method_name] - results = request_method(*request_parameters) + # Run 'method' + method_name = getmembers(self, predicate=ismethod) + request_method = dict(method_name)[self.rpc_request.method] + self.rpc_request.result = request_method(*self.rpc_request.parameters) - if request_method_name in ("query", "context_menu"): - print(dumps({ - "result": results, - "debugMessage": self.debugMessage - })) + if self.rpc_request.method in {"query", "context_menu"}: + self.rpc_request.to_string() - def query(self, param: str = '') -> list: + def query(self, param: str = "") -> list: """ sub class need to override this method """ diff --git a/flowlauncher/FlowLauncherAPI.py b/flowlauncher/FlowLauncherAPI.py index 526b52d..adaf35c 100644 --- a/flowlauncher/FlowLauncherAPI.py +++ b/flowlauncher/FlowLauncherAPI.py @@ -1,95 +1,86 @@ -# -*- coding: utf-8 -*- +from RPC import RPC -from json import dumps class FlowLauncherAPI: - @classmethod def change_query(cls, query, requery: bool = False): """ change flow launcher query """ - print(dumps({ - "method": "Flow.Launcher.ChangeQuery", - "parameters": [query, requery]})) + RPC( + method="Flow.Launcher.ChangeQuery", + parameters=[query, requery], + ).to_string() @classmethod def shell_run(cls, cmd): """ run shell commands """ - print(dumps({ - "method": "Flow.Launcher.ShellRun", - "parameters": [cmd]})) + RPC( + method="Flow.Launcher.ShellRun", + parameters=[cmd], + ).to_string() @classmethod def close_app(cls): """ close flow launcher """ - print(dumps({ - "method": "Flow.Launcher.CloseApp", - "parameters": []})) + RPC(method="Flow.Launcher.CloseApp").to_string() @classmethod def hide_app(cls): """ hide flow launcher """ - print(dumps({ - "method": "Flow.Launcher.HideApp", - "parameters": []})) + RPC(method="Flow.Launcher.HideApp").to_string() @classmethod def show_app(cls): """ show flow launcher """ - print(dumps({ - "method": "Flow.Launcher.ShowApp", - "parameters": []})) + RPC(method="Flow.Launcher.ShowApp").to_string() @classmethod def show_msg(cls, title: str, sub_title: str, ico_path: str = ""): """ show messagebox """ - print(dumps({ - "method": "Flow.Launcher.ShowMsg", - "parameters": [title, sub_title, ico_path]})) + RPC( + method="Flow.Launcher.ShowMsg", + parameters=[ + title, + sub_title, + ico_path, + ], + ).to_string() @classmethod def open_setting_dialog(cls): """ open setting dialog """ - print(dumps({ - "method": "Flow.Launcher.OpenSettingDialog", - "parameters": []})) + RPC(method="Flow.Launcher.OpenSettingDialog").to_string() @classmethod def start_loadingbar(cls): """ start loading animation in flow launcher """ - print(dumps({ - "method": "Flow.Launcher.StartLoadingBar", - "parameters": []})) + RPC(method="Flow.Launcher.StartLoadingBar").to_string() @classmethod def stop_loadingbar(cls): """ stop loading animation in flow launcher """ - print(dumps({ - "method": "Flow.Launcher.StopLoadingBar", - "parameters": []})) + RPC(method="Flow.Launcher.StartLoadingBar").to_string() @classmethod def reload_plugins(cls): """ reload all flow launcher plugins """ - print(dumps({ - "method": "Flow.Launcher.ReloadPlugins", - "parameters": []})) + RPC(method="Flow.Launcher.ReloadPlugins").to_string() From 6cbf7b99085c220b204deda97f62fab89fa9af69 Mon Sep 17 00:00:00 2001 From: Zero Date: Thu, 18 Aug 2022 14:21:39 +0800 Subject: [PATCH 3/4] expose RPC --- flowlauncher/__init__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/flowlauncher/__init__.py b/flowlauncher/__init__.py index e7ae4ea..2b16c55 100644 --- a/flowlauncher/__init__.py +++ b/flowlauncher/__init__.py @@ -1,8 +1,7 @@ -# -*- coding: utf-8 -*- - from ._version import get_versions -from .FlowLauncher import FlowLauncher # noqa -from .FlowLauncherAPI import FlowLauncherAPI # noqa +from .FlowLauncher import FlowLauncher # noqa: F401 +from .FlowLauncherAPI import FlowLauncherAPI # noqa: F401 +from .RPC import RPC __version__ = get_versions()["version"] del get_versions From babe42cd4b5fa1b5cbc8d5048ab080ba40a5e95b Mon Sep 17 00:00:00 2001 From: Zero Date: Thu, 18 Aug 2022 14:33:26 +0800 Subject: [PATCH 4/4] Finished basic docstring --- flowlauncher/RPC.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/flowlauncher/RPC.py b/flowlauncher/RPC.py index 3237c15..c1c1317 100644 --- a/flowlauncher/RPC.py +++ b/flowlauncher/RPC.py @@ -6,6 +6,24 @@ @dataclass class RPC: + """ + FlowLauncher standard JSON-RPC container. + + Parameters + ---------- + method : {"query", "context_menu", "Flow.Launcher.ChangeQuery", \ +"Flow.Launcher.ShellRun", "Flow.Launcher.CloseApp", "Flow.Launcher.HideApp", \ +"Flow.Launcher.ShowApp", "Flow.Launcher.ShowMsg", "Flow.Launcher.OpenSettingDialog",\ +"Flow.Launcher.StartLoadingBar", "Flow.Launcher.StartLoadingBar", \ +"Flow.Launcher.ReloadPlugins"}, default "query" + + parameters : list, default [] + + result : list, default [] + + debugMessage : str, default "" + """ + method: str = "query" parameters: list = field(default_factory=list) result: list = field(default_factory=list) @@ -16,15 +34,33 @@ class RPC: # proxy: dict = field(default_factory=dict) def to_dict(self) -> dict: + """ + Return JSON-RPC as dict. + """ + return self.__dict__ def to_string(self) -> str: + """ + Convert JSON-RPC to string. + """ + return dumps(self.to_dict()) @classmethod def from_dict(cls, **kwargs) -> RPC: + """ + Generate JSON-RPC from dict. + + Equivalent to ``RPC(**kwargs)``. + """ + return cls(**kwargs) @classmethod def from_string(cls, string: str) -> RPC: + """ + Generate JOSN-RPC from string. + """ + return cls(**loads(string)) if string else cls()