Skip to content

Commit 754effe

Browse files
committed
Use RPC to simplify codes
1 parent afbf113 commit 754effe

File tree

2 files changed

+38
-57
lines changed

2 files changed

+38
-57
lines changed

flowlauncher/FlowLauncher.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# -*- coding: utf-8 -*-
22

33

4-
import inspect
54
import sys
6-
from json import loads, dumps
5+
from inspect import getmembers, ismethod
6+
7+
from RPC import RPC
78

89

910
class FlowLauncher:
@@ -12,33 +13,22 @@ class FlowLauncher:
1213
"""
1314

1415
def __init__(self):
15-
16-
# defalut jsonrpc
17-
self.rpc_request = {'method': 'query', 'parameters': ['']}
18-
self.debugMessage = ""
19-
2016
if len(sys.argv) > 1:
21-
2217
# Gets JSON-RPC from Flow Launcher process.
23-
self.rpc_request = loads(sys.argv[1])
24-
25-
# proxy is not working now
26-
# self.proxy = self.rpc_request.get("proxy", {})
27-
28-
request_method_name = self.rpc_request.get("method", "query")
29-
request_parameters = self.rpc_request.get("parameters", [])
18+
self.rpc_request = RPC.from_string(sys.argv[1])
19+
else:
20+
# defalut JSON-RPC
21+
self.rpc_request = RPC()
3022

31-
methods = inspect.getmembers(self, predicate=inspect.ismethod)
32-
request_method = dict(methods)[request_method_name]
33-
results = request_method(*request_parameters)
23+
# Run 'method'
24+
method_name = getmembers(self, predicate=ismethod)
25+
request_method = dict(method_name)[self.rpc_request.method]
26+
self.rpc_request.result = request_method(*self.rpc_request.parameters)
3427

35-
if request_method_name in ("query", "context_menu"):
36-
print(dumps({
37-
"result": results,
38-
"debugMessage": self.debugMessage
39-
}))
28+
if self.rpc_request.method in {"query", "context_menu"}:
29+
self.rpc_request.to_string()
4030

41-
def query(self, param: str = '') -> list:
31+
def query(self, param: str = "") -> list:
4232
"""
4333
sub class need to override this method
4434
"""

flowlauncher/FlowLauncherAPI.py

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,86 @@
1-
# -*- coding: utf-8 -*-
1+
from RPC import RPC
22

3-
from json import dumps
43

54
class FlowLauncherAPI:
6-
75
@classmethod
86
def change_query(cls, query, requery: bool = False):
97
"""
108
change flow launcher query
119
"""
12-
print(dumps({
13-
"method": "Flow.Launcher.ChangeQuery",
14-
"parameters": [query, requery]}))
10+
RPC(
11+
method="Flow.Launcher.ChangeQuery",
12+
parameters=[query, requery],
13+
).to_string()
1514

1615
@classmethod
1716
def shell_run(cls, cmd):
1817
"""
1918
run shell commands
2019
"""
21-
print(dumps({
22-
"method": "Flow.Launcher.ShellRun",
23-
"parameters": [cmd]}))
20+
RPC(
21+
method="Flow.Launcher.ShellRun",
22+
parameters=[cmd],
23+
).to_string()
2424

2525
@classmethod
2626
def close_app(cls):
2727
"""
2828
close flow launcher
2929
"""
30-
print(dumps({
31-
"method": "Flow.Launcher.CloseApp",
32-
"parameters": []}))
30+
RPC(method="Flow.Launcher.CloseApp").to_string()
3331

3432
@classmethod
3533
def hide_app(cls):
3634
"""
3735
hide flow launcher
3836
"""
39-
print(dumps({
40-
"method": "Flow.Launcher.HideApp",
41-
"parameters": []}))
37+
RPC(method="Flow.Launcher.HideApp").to_string()
4238

4339
@classmethod
4440
def show_app(cls):
4541
"""
4642
show flow launcher
4743
"""
48-
print(dumps({
49-
"method": "Flow.Launcher.ShowApp",
50-
"parameters": []}))
44+
RPC(method="Flow.Launcher.ShowApp").to_string()
5145

5246
@classmethod
5347
def show_msg(cls, title: str, sub_title: str, ico_path: str = ""):
5448
"""
5549
show messagebox
5650
"""
57-
print(dumps({
58-
"method": "Flow.Launcher.ShowMsg",
59-
"parameters": [title, sub_title, ico_path]}))
51+
RPC(
52+
method="Flow.Launcher.ShowMsg",
53+
parameters=[
54+
title,
55+
sub_title,
56+
ico_path,
57+
],
58+
).to_string()
6059

6160
@classmethod
6261
def open_setting_dialog(cls):
6362
"""
6463
open setting dialog
6564
"""
66-
print(dumps({
67-
"method": "Flow.Launcher.OpenSettingDialog",
68-
"parameters": []}))
65+
RPC(method="Flow.Launcher.OpenSettingDialog").to_string()
6966

7067
@classmethod
7168
def start_loadingbar(cls):
7269
"""
7370
start loading animation in flow launcher
7471
"""
75-
print(dumps({
76-
"method": "Flow.Launcher.StartLoadingBar",
77-
"parameters": []}))
72+
RPC(method="Flow.Launcher.StartLoadingBar").to_string()
7873

7974
@classmethod
8075
def stop_loadingbar(cls):
8176
"""
8277
stop loading animation in flow launcher
8378
"""
84-
print(dumps({
85-
"method": "Flow.Launcher.StopLoadingBar",
86-
"parameters": []}))
79+
RPC(method="Flow.Launcher.StartLoadingBar").to_string()
8780

8881
@classmethod
8982
def reload_plugins(cls):
9083
"""
9184
reload all flow launcher plugins
9285
"""
93-
print(dumps({
94-
"method": "Flow.Launcher.ReloadPlugins",
95-
"parameters": []}))
86+
RPC(method="Flow.Launcher.ReloadPlugins").to_string()

0 commit comments

Comments
 (0)