-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.py
275 lines (231 loc) · 8.03 KB
/
actions.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from base import *
from exceptions import *
import utils
import time
import os
from PIL import ImageGrab
import aircv as ac
import pyautogui
_actions_available = {
"move_mouse_to",
"sleep",
"mouse_click",
"get_image_pos",
"screen_shot",
"remove_file",
"text_input",
"text_input_file",
"screen_shot_mem",
}
_actions_map = dict()
class Action(Base):
def __init__(self, config_: dict, root_):
self._root = root_
self._parse_config(config_)
def _parse_config(self, config_: dict) -> None:
pass
def call(self) -> bool:
return True
def load_action(action_: dict, root) -> Action:
key_words = ["type", "action"]
check = utils.check_config_arguments(key_words, action_)
if not check:
raise ParseErrorException("Loading action error, argument missing.")
if action_["type"] != "action":
raise ParseErrorException(
"Loading action error, type error: {}".format(action_["type"])
)
if action_["action"] not in _actions_available:
raise ParseErrorException("Action not support: {}".format(action_["action"]))
actobj = _actions_map[action_["action"]]
return actobj(action_, root)
class Action_move_mouse_to(Action):
def _parse_config(self, config_: dict) -> None:
key_words = ["type", "action", "var"]
check = utils.check_config_arguments(key_words, config_)
if not check:
raise ParseErrorException(
"Loading Action_move_mouse_to error, missing argument: var"
)
self._var = config_["var"]
def call(self) -> bool:
target = self._root.get_var(self._var)
pyautogui.moveTo(target)
return True
class Action_sleep(Action):
def _parse_config(self, config_: dict) -> None:
key_words = ["type", "action", "sleep_time"]
check = utils.check_config_arguments(key_words, config_)
if not check:
raise ParseErrorException(
"Loading Action_sleep error, argument missing: sleep_time"
)
self._sleep_time = config_["sleep_time"]
def call(self) -> bool:
time.sleep(self._sleep_time)
return True
class Action_mouse_click(Action):
def _parse_config(self, config_: dict) -> None:
key_words = ["type", "action", "push_time"]
check = utils.check_config_arguments(key_words, config_)
if not check:
raise ParseErrorException(
"Loading Action_mouse_click error, missing argument: push_time"
)
self._push_time = config_["push_time"]
def call(self) -> bool:
pyautogui.mouseDown()
time.sleep(self._push_time)
pyautogui.mouseUp()
return True
class Action_get_image_pos(Action):
def _parse_config(self, config_: dict) -> None:
key_words = ["type", "action", "var", "base", "image", "precision"]
check = utils.check_config_arguments(key_words, config_)
if not check:
raise ParseErrorException(
"Loading Action_get_image_pos error, missing arguments."
)
self._var = config_["var"]
self._base = config_["base"]
self._image = config_["image"]
self._precision = config_["precision"]
def call(self) -> bool:
# im_src
if self._base[0] == "$":
data = self._root.get_var(self._base)
im_src = utils.aircv_read_from_array(data)
else:
im_src = ac.imread(self._base)
# im_tgt
if self._image[0] == "$":
data = self._root.get_var(self._image)
im_tgt = utils.aircv_read_from_array(data)
else:
im_tgt = ac.imread(self._image)
get = ac.find_template(im_src, im_tgt)
if get is None:
return False
if get["confidence"] < self._precision:
return False
self._root.set_var(self._var, get["result"])
return True
class Action_screen_shot(Action):
def _parse_config(self, config_: dict) -> None:
key_words = ["type", "action", "save"]
check = utils.check_config_arguments(key_words, config_)
if not check:
raise ParseErrorException(
"Loading Action_screen_shot error, missing argument: save"
)
self._save = config_["save"]
def call(self) -> bool:
img = ImageGrab.grab()
img.save(self._save)
return True
class Action_remove_file(Action):
def _parse_config(self, config_: dict) -> None:
key_words = ["type", "action", "ignore_error", "path"]
check = utils.check_config_arguments(key_words, config_)
if not check:
raise ParseErrorException(
"Loading Action_remove_file error, missing arguments."
)
self._ignore_error = config_["ignore_error"]
self._path = config_["path"]
def call(self) -> bool:
exists = os.path.exists(self._path)
if not exists:
if self._ignore_error:
return True
return False
try:
os.remove(self._path)
except:
if not self._ignore_error:
return False
return True
class Action_text_input(Action):
def __init__(self, config_: dict, root_):
super().__init__(config_, root_)
self._key_to_change = {" ": "space"}
def _parse_config(self, config_: dict) -> None:
key_words = ["type", "action", "text"]
check = utils.check_config_arguments(key_words, config_)
if not check:
raise ParseErrorException(
"Loading Action_text_input error, missing argument: text"
)
self._text = config_["text"]
def _press_key(self, key: str):
if len(key) == 0:
return
if len(key) > 1:
key = key[0]
upper = False
if key.isupper():
upper = True
if upper:
pyautogui.keyDown("shiftleft")
k = key
if k in self._key_to_change:
k = self._key_to_change[k]
pyautogui.keyDown(k)
pyautogui.keyUp(k)
if upper:
pyautogui.keyUp("shiftleft")
def call(self) -> bool:
for i in self._text:
self._press_key(i)
return True
class Action_text_input_file(Action):
def __init__(self, config_: dict, root_):
super().__init__(config_, root_)
self._key_to_change = {" ": "space"}
def _parse_config(self, config_: dict) -> None:
key_words = ["type", "action", "file"]
check = utils.check_config_arguments(key_words, config_)
if not check:
raise ParseErrorException(
"Loading Action_text_input error, missing argument: text"
)
path = config_["file"]
with open(path, "r", encoding="utf_8") as f:
self._text = f.read()
def _press_key(self, key: str):
if len(key) == 0:
return
if len(key) > 1:
key = key[0]
upper = False
if key.isupper():
upper = True
if upper:
pyautogui.keyDown("shiftleft")
k = key
if k in self._key_to_change:
k = self._key_to_change[k]
pyautogui.keyDown(k)
pyautogui.keyUp(k)
if upper:
pyautogui.keyUp("shiftleft")
def call(self) -> bool:
for i in self._text:
self._press_key(i)
return True
class Action_screen_shot_mem(Action):
def _parse_config(self, config_: dict) -> None:
key_words = ["type", "action", "var"]
check = utils.check_config_arguments(key_words, config_)
if not check:
raise ParseErrorException(
"Loading Action_screen_shot_to_mem error, missing argument."
)
self._var = config_["var"]
def call(self) -> bool:
image = ImageGrab.grab()
arr = utils.pillow_save_as_array(image)
self._root.set_var(self._var, arr)
return True
for i in _actions_available:
_actions_map[i] = eval("Action_" + i)