forked from ev01ing/Wox.Plugin.Tips
/
main.py
119 lines (103 loc) · 3.47 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import time
import json
import traceback
import logging
import clipboard
import sys
from wox import Wox, WoxAPI
LOG_FILE = "./logs/log_tips.log"
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=LOG_FILE,
filemode='a')
class Main(Wox):
TIPS_FILE = "./DB/tips.json"
def query(self, key):
if key == "":
return self.list_tips()
if key in "delete":
return self.list_delete_tips()
results = []
results.append({
"Title": 'stroe tip : "%s"' % key,
"SubTitle": 'tips : "%s"' % key,
"IcoPath": "Images/pic.png",
"JsonRPCAction": {
"method": "store_tip",
"parameters": [key],
"dontHideAfterAction": True
}
})
return results
def store_tip(self, info):
try:
times = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
tip = {"tip": info, "updated_time": times}
with open(self.TIPS_FILE, "a") as f:
f.write(json.dumps(tip) + "\n")
WoxAPI.change_query("tip")
except:
logging.error(traceback.format_exc())
def get_tips(self):
try:
results = []
with open(self.TIPS_FILE, "r") as f:
for line in f:
line = line.strip()
if line:
tip = json.loads(line.strip())
results.append({"tip": tip['tip'], "updated_time": tip['updated_time']})
return reversed(results)
except:
logging.error(traceback.format_exc())
def list_delete_tips(self):
tips = self.get_tips()
results = []
for tip in tips:
results.append({
"Title": "delete %s" % tip['tip'],
"SubTitle": tip['updated_time'],
"IcoPath": "Images/pic.png",
"JsonRPCAction": {
"method": "delete_tip",
"parameters": [tip['updated_time']],
"dontHideAfterAction": True
}
})
return results
def delete_tip(self, times):
try:
tips = []
with open(self.TIPS_FILE, "r") as f:
for line in f:
tips.append(json.loads(line.strip()))
for tip in tips:
if times == tip["updated_time"]:
tips.remove(tip)
break
with open(self.TIPS_FILE, "w") as f:
for tip in tips:
f.write(json.dumps(tip) + "\n")
WoxAPI.change_query("tip")
except:
logging.error(traceback.format_exc())
def list_tips(self):
tips = self.get_tips()
results = []
for tip in tips:
results.append({
"Title": tip['tip'],
"SubTitle": tip['updated_time'],
"IcoPath": "Images/pic.png",
"JsonRPCAction": {
"method": "copy_to_clip",
"parameters": [tip['tip']],
"dontHideAfterAction": False
}
})
return results
def copy_to_clip(self, text):
clipboard.copy(text)
if __name__ == "__main__":
Main()