Skip to content

Commit aa145ff

Browse files
committed
🎨 🐛 ⬆️
- 🎨 add typing, add fstring to replace format - 🐛 ⬆️ use `demjson` replace `json` to solve `json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)` problem. `json` can't recognize `'`
1 parent 2bae283 commit aa145ff

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

flowlauncher/FlowLauncher.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22

33

44
import inspect
5-
import json
65
import sys
76

7+
import demjson
8+
89

910
class FlowLauncher:
1011
"""
1112
Flow.Launcher python plugin base
1213
"""
1314

1415
def __init__(self):
15-
# TODO: try other way to get argv not using index
16-
# with this, to test a plugin, it has to likes `python test.py {}`. Add `{}`.
17-
# It is better with like this `python test.py`, when the input is empty.
18-
rpc_request = json.loads(sys.argv[1])
16+
17+
rpc_request = {'method': 'query', 'parameters': ['']}
18+
if len(sys.argv) > 1:
19+
rpc_request = demjson.decode(sys.argv[1])
1920

2021
# proxy is not working now
21-
self.proxy = rpc_request.get("proxy", {})
22+
# self.proxy = rpc_request.get("proxy", {})
23+
2224
request_method_name = rpc_request.get("method", "query")
2325
request_parameters = rpc_request.get("parameters", [])
2426
methods = inspect.getmembers(self, predicate=inspect.ismethod)
@@ -27,23 +29,23 @@ def __init__(self):
2729
results = request_method(*request_parameters)
2830

2931
if request_method_name in ["query", "context_menu"]:
30-
print(json.dumps({"result": results}))
32+
print(demjson.encode({"result": results}))
3133

32-
def query(self, param: str = ''):
34+
def query(self, param: str = '') -> list:
3335
"""
3436
sub class need to override this method
3537
"""
3638
return []
3739

38-
def context_menu(self, data):
40+
def context_menu(self, data) -> list:
3941
"""
4042
optional context menu entries for a result
4143
"""
4244
return []
4345

44-
def debug(self, msg):
46+
def debug(self, msg: str):
4547
"""
4648
alert msg
4749
"""
48-
print("DEBUG:{}".format(msg))
50+
print(f"DEBUG:{msg}")
4951
sys.exit()

flowlauncher/FlowLauncherAPI.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# -*- coding: utf-8 -*-
22

3-
import json
3+
import demjson
44

55

6-
class FlowLauncherAPI(object):
6+
class FlowLauncherAPI:
77

88
@classmethod
9-
def change_query(cls, query, requery=False):
9+
def change_query(cls, query, requery: bool = False):
1010
"""
1111
change flow launcher query
1212
"""
13-
print(json.dumps({
13+
print(demjson.encode({
1414
"method": "Wox.ChangeQuery",
1515
"parameters": [query, requery]}))
1616

@@ -19,7 +19,7 @@ def shell_run(cls, cmd):
1919
"""
2020
run shell commands
2121
"""
22-
print(json.dumps({
22+
print(demjson.encode({
2323
"method": "Flow.Launcher.ShellRun",
2424
"parameters": [cmd]}))
2525

@@ -28,7 +28,7 @@ def close_app(cls):
2828
"""
2929
close flow launcher
3030
"""
31-
print(json.dumps({
31+
print(demjson.encode({
3232
"method": "Flow.Launcher.CloseApp",
3333
"parameters": []}))
3434

@@ -37,7 +37,7 @@ def hide_app(cls):
3737
"""
3838
hide flow launcher
3939
"""
40-
print(json.dumps({
40+
print(demjson.encode({
4141
"method": "Flow.Launcher.HideApp",
4242
"parameters": []}))
4343

@@ -46,7 +46,7 @@ def show_app(cls):
4646
"""
4747
show flow launcher
4848
"""
49-
print(json.dumps({
49+
print(demjson.encode({
5050
"method": "Flow.Launcher.ShowApp",
5151
"parameters": []}))
5252

@@ -55,7 +55,7 @@ def show_msg(cls, title: str, sub_title: str, ico_path: str = ""):
5555
"""
5656
show messagebox
5757
"""
58-
print(json.dumps({
58+
print(demjson.encode({
5959
"method": "Flow.Launcher.ShowMsg",
6060
"parameters": [title, sub_title, ico_path]}))
6161

@@ -64,7 +64,7 @@ def open_setting_dialog(cls):
6464
"""
6565
open setting dialog
6666
"""
67-
print(json.dumps({
67+
print(demjson.encode({
6868
"method": "Flow.Launcher.OpenSettingDialog",
6969
"parameters": []}))
7070

@@ -73,7 +73,7 @@ def start_loadingbar(cls):
7373
"""
7474
start loading animation in flow launcher
7575
"""
76-
print(json.dumps({
76+
print(demjson.encode({
7777
"method": "Flow.Launcher.StartLoadingBar",
7878
"parameters": []}))
7979

@@ -82,7 +82,7 @@ def stop_loadingbar(cls):
8282
"""
8383
stop loading animation in flow launcher
8484
"""
85-
print(json.dumps({
85+
print(demjson.encode({
8686
"method": "Flow.Launcher.StopLoadingBar",
8787
"parameters": []}))
8888

@@ -91,6 +91,6 @@ def reload_plugins(cls):
9191
"""
9292
reload all flow launcher plugins
9393
"""
94-
print(json.dumps({
94+
print(demjson.encode({
9595
"method": "Flow.Launcher.ReloadPlugins",
9696
"parameters": []}))

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
demjson

setup.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,9 @@
4646
"Topic :: Software Development :: Libraries :: Application Frameworks",
4747
]
4848

49-
try:
50-
f = open("requirements.txt", "r")
51-
REQUIRES = [i.strip() for i in f.readlines()]
52-
except:
53-
print("'requirements.txt' not found!")
54-
REQUIRES = []
49+
with open("requirements.txt", "r") as f:
50+
REQUIRES = [package.strip() for package in f.readlines()]
51+
5552

5653
setup(
5754
name=NAME,

0 commit comments

Comments
 (0)