-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathwin_app_ops.py
executable file
·204 lines (176 loc) · 6.91 KB
/
win_app_ops.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
# _*_ coding:UTF-8 _*_
import os
import sys
import traceback
import json
import time
import re
from optparse import OptionParser
from lib.helper.xmlparse import APPOpsXml
from lib.winutil.utils import ProcUtil,WinUtil,MouseUtil,CursorUtil,MsgUtil
reload(sys)
sys.setdefaultencoding('utf8')
def get_realpath():
return os.path.split(os.path.realpath(__file__))[0]
def get_binname():
return os.path.split(os.path.realpath(__file__))[1]
def get_right_content(content):
try:
content = content.decode("utf8")
except Exception:
try:
content = content.decode("gbk")
except Exception:
try:
content = content.decode("GB2312")
except Exception:
pass
return content
def actions(action_steps):
try:
hwds = []
id_whds = {}
for idx,action_conf in enumerate(action_steps):
action = action_conf.get("action", "")
data = action_conf.get("data", {})
func = action_conf.get("func", "")
func = get_right_content(func)
print u"[STEP-%s]" % (idx+1)
print u"Action: %s" % (action)
print u"Func: %s" % (func)
print u"Config: %s" % (json.dumps(data, encoding="UTF-8", ensure_ascii=False))
## GUI程序
if action == "run":
binpath = data.get("binpath", "")
params = data.get("params", "")
wid = data.get("id", None)
if not wid:
print "[Warn] Do Not Assign ID."
params = params if params else ""
bps = re.split(";", binpath)
avail_bp = None
for bp in bps:
if bp and os.path.exists(bp):
avail_bp = bp
break
if avail_bp:
(proc_hd, thread_hd, proc_id, thread_id) = ProcUtil.CreateProc(avail_bp, paramstr=params)
#必须sleep 1
time.sleep(1)
whd = WinUtil.GetHWndByProcId(proc_id)
hwds.append(whd)
id_whds[wid] = whd
## 执行命令行命令
if action == "command":
cmdline = data.get("cmdline", None)
if cmdline:
os.system(cmdline)
time.sleep(0.5)
if action == "getwin":
title = data.get("title", None)
clsname = data.get("clsname", None)
wid = data.get("id", None)
ref_id = data.get("ref_id", None)
if ref_id: ## 高优先级
hwds.append(id_whds[ref_id])
id_whds[wid] = id_whds[ref_id]
continue
if not wid:
print "[ERROR] GetWin Must Contain ID."
sys.exit(1)
if not title and not clsname:
print "[ERROR] GetWin Must Assign One of (clsname, win_title) Or Both"
sys.exit(1)
whd = WinUtil.GetWinByTitle(clsname=clsname, win_title=title)
if not whd:
print "[WARN] GetWin Can't Find."
#sys.exit(1)
if whd: WinUtil.SetWinCenter(whd)
hwds.append(whd)
id_whds[wid] = whd
if action == "moveto":
point = data.get("point", {})
ref_win_id = data.get("ref_win_id", None)
whd = None
if not ref_win_id:
whd = hwds[-1]
else:
whd = id_whds.get(ref_win_id, None)
if not whd:
print "[WARN] MoveTo Can't Find Ref Win."
continue
#sys.exit(1)
x = int(point.get("x", None))
y = int(point.get("y", None))
rect = WinUtil.GetCompRect(whd)
point = (rect.left+x, rect.top+y)
MouseUtil.MouseMove(point[0], point[1])
if action == "lclick":
MouseUtil.LClick()
if action == "rclick":
MouseUtil.RClick()
if action == "settext":
text = data.get("text", None)
if text == None:continue
focus = WinUtil.GetFocus()
MsgUtil.SetText(focus, text)
if action == "btnclick":
title = data.get("title", None)
ref_win_id = data.get("ref_win_id", None)
if title == None:continue
whd = None
if not ref_win_id:
whd = hwds[-1]
else:
whd = id_whds.get(ref_win_id, None)
if not whd:
print "[WARN] BtnClick Can't Find Ref Win."
continue
#sys.exit(1)
WinUtil.SetForegroundWindow(whd)
btn = WinUtil.GetComponent(whd, win_title=title)
(x, y) = WinUtil.GetCompCenterPos(btn)
CursorUtil.SetCursorPos(x, y)
MouseUtil.LClick()
print
except Exception:
print traceback.format_exc()
def main():
rp = get_realpath()
try:
parser = OptionParser()
parser.add_option("-c", "--conffile",
action="store", dest="conf", default=None,
help="configure file", metavar="CONFFILE")
parser.add_option("-a", "--action",
action="store", dest="action", default=None,
help="action: such as start/stop which action define in conffile", metavar="ACTION")
parser.add_option("-d", "--debug", dest="debug", default=False,
action="store_true", help="if debug, default is false")
(options, args) = parser.parse_args()
conffile = options.conf
actionstr = options.action
debug = options.debug
if not conffile:
parser.print_help()
sys.exit(1)
#conffile = os.path.abspath(conffile)
conffile = rp + "\\etc\\" + conffile
if os.path.exists(conffile) == False:
parser.print_help()
sys.exit(0)
px = APPOpsXml(conffile)
actions_def = px.get_actions()
actionstr = str(actionstr).strip().lower()
if actionstr not in actions_def:
parser.print_help()
sys.exit(1)
action_steps = px.get_action_steps(actionstr)
if not action_steps:
print "Can Not Find Action Defined, Exit..."
sys.exit(1)
actions(action_steps)
except Exception as expt:
print traceback.format_exc()
if __name__ == "__main__":
main()