-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
130 lines (102 loc) · 4.06 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
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from wox import Wox, WoxAPI
import os
import sys
import shutil
import hashlib
import logging
logging.basicConfig(level=logging.WARNING,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='wox_qrcpde.log',
filemode='w')
try:
import qrcode
from PIL import Image
HAS_QRCODE = True
except Exception as e:
HAS_QRCODE = False
def md5(value):
m = hashlib.md5()
m.update(str(value).encode("utf-8"))
return m.hexdigest()
class WoxQRcode(Wox):
request_query = ''
plugin_ico = "Images/pic.png"
def __init__(self):
logging.debug("sys.argv: %s" % sys.argv[1])
self.plugin_path = os.path.dirname(os.path.realpath(__file__))
self.img_path = os.path.join(self.plugin_path, '.cahe')
if not os.path.exists(self.img_path):
os.mkdir(self.img_path)
logging.debug("Im runing,plugin_path is '%s' , img_path is '%s'" % (self.plugin_path ,self.img_path))
super(WoxQRcode, self).__init__()
def query(self, request_query):
logging.debug("method:query, parameters:%s " % request_query)
self.request_query = request_query
results = []
if request_query in 'clean':
# clean cache img
results.append(self.get_result('CLEAN_IMAGE_CACHE', request_query))
if HAS_QRCODE is not True:
# if not has qrcode module , plugin can not work
results.append(self.get_result('NOT_HAS_QRCODE_MODULE', request_query))
else:
results.append(self.get_result('MAKE_QRCODE', request_query))
logging.debug("results = %s" % results)
return results
def make_qr_image(self, context):
logging.debug("starting make QRImage...")
QRImageName = "qrcode_{}.jpg".format(md5(context))
QRImagePath = os.path.join(self.img_path, QRImageName)
logging.debug("QRImagePath is %s" % QRImagePath)
if not os.path.exists(QRImagePath):
logging.debug("QRImage file not exists,so need make")
img = qrcode.make(context, border=1)
img.save(QRImagePath)
logging.debug("make QRImage ok, this file size is %s" % os.path.getsize(QRImagePath))
else:
logging.debug("has cache, use cache image")
# Wox输入框可能会遮挡二维码
WoxAPI.hide_app()
return self.show_image(QRImagePath)
def show_image(self, QRImagePath):
logging.debug("starting show QRImage...")
os.startfile(QRImagePath)
logging.debug("show QRImage end")
def clean_cache(self, contest):
shutil.rmtree(self.img_path)
WoxAPI.show_msg("Success", " clean up cache file done", self.plugin_ico)
# messgae
def get_result(self, key, request_query):
messages = {
'CLEAN_IMAGE_CACHE': {
"Title": "Clean Cache",
"SubTitle": "input 'clean', can clean this plugin cache",
"IcoPath": self.plugin_ico,
"JsonRPCAction": {
"method": "clean_cache",
"parameters": [request_query],
"dontHideAfterAction": True
}
},
'NOT_HAS_QRCODE_MODULE': {
"Title": "ERROR: ",
"SubTitle": "Can Not Import Module: qrcode ",
"IcoPath": self.plugin_ico,
},
'MAKE_QRCODE': {
"Title": "Mak QR Code",
"SubTitle": "Context: {}".format(request_query),
"IcoPath": self.plugin_ico,
"JsonRPCAction": {
"method": "make_qr_image",
"parameters": [request_query],
"dontHideAfterAction": False
}
}
}
return messages.get(key, {"Title": "Mak QR Code", "SubTitle": "Context: {}".format(request_query)})
if __name__ == "__main__":
WoxQRcode()