-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy patheventHandle.py
421 lines (383 loc) · 19.3 KB
/
eventHandle.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#coding:utf-8
import wx
import time
#import codecs
import os,win32api,sys
#from wx.lib.wordwrap import wordwrap
import logging
from ItemOperate import AddProgramDialog,EditProgramDialog
from InExport import exportDialog
from dataList import DataHandle
import idDefine as gen
import subprocess
import LogOutput as LoadLog
from SettingUI import settingDlg
from SettingData import HandleSetting
from HelpDoc import HtmlDocDlg
class EventHandle(object):
def __init__(self,filename=None,plist=None):
self.filename = filename
##传入列表控件对象
self.ProgrameList = plist
self.datahandle = DataHandle(self.filename)
def OnAdd_Edit_Dialog(self,event):
#print "add_edit_dialog"
if event.GetId() == wx.ID_ADD:
self.dlg = AddProgramDialog(u"增加项目",u"请输入添加项目的相关内容:")
LoadLog.LogMsg(gen.logger.info,u"打开增加对话框")
self.kid = None
elif event.GetId() == wx.ID_EDIT:
self.kid = self.CheckEdit()
if self.kid is None:
return
self.dlg = EditProgramDialog(u"编辑项目",u"请输入编辑项目的相关内容:")
LoadLog.LogMsg(gen.logger.info,u"打开编辑对话框")
self.dlg.SetDialogData(self.filename,self.kid)
########## 双击事件 #############
elif event.GetId() == wx.ID_VIEW_LIST:
rowObj = self.ProgrameList.GetSelectedObject()
self.kid = rowObj.kid
##清空选择
self.OnSelectAll()
##在双击的行上选择
allobj = self.ProgrameList.GetObjects()
self.ProgrameList.SetCheckState(rowObj, True)
##需要刷新对象
self.ProgrameList.RefreshObjects(allobj)
self.dlg = EditProgramDialog(u"编辑项目",u"请输入编辑程序的相关内容:")
LoadLog.LogMsg(gen.logger.info,u"打开编辑对话框")
#kid = "5"
self.dlg.SetDialogData(self.filename,self.kid)
else:
return
#addpdlg.ShowModal()
##增加程序数据
if self.dlg.ShowModal() == wx.ID_OK:
##对话框获取返回的数据
name,programe,logfile,monitor,operator,processNum,runAs,note = self.dlg.GetDialogData()
kid = self.kid
##对话框返回的数据,调用增加数据的方法来处理
#print "#####processNum:",processNum
#print "#####runAs:",runAs
LoadLog.LogMsg(gen.logger.debug,u"取得对话框的数据")
self.datahandle.Add_Edit_Data(kid,name,programe,logfile,monitor,operator,processNum,runAs,note)
##调用handleList()方法,得到给列表控件显示的数据
gen.IsRunStatusChanged = True
list_data = self.datahandle.handleList()
#print u"handle_list处理后显示给列表控件的值",list_data
##设置列表控件数据显示
LoadLog.LogMsg(gen.logger.debug,u"刷新控件列表")
self.ProgrameList.SetObjects(list_data)
gen.IsItemChanged = True
self.dlg.Destroy()
def OnDelDialog(self,evnt):
allobj = self.ProgrameList.GetObjects()
tbool = []
for obj in allobj:
tbool.append(self.ProgrameList.IsChecked(obj))
if tbool.count(True) == 0:
LoadLog.LogMsg(gen.logger.info,u"未选择删除项")
wx.MessageBox(u"至少选择一行进行删除!", u'信息', wx.OK | wx.ICON_INFORMATION,parent=self.ProgrameList)
return
else:
pdata = self.datahandle.ReadData()
#print u"==删除字典项前,从文件读取的字典数据:",pdata
delList = []
for obj in allobj:
if self.ProgrameList.IsChecked(obj) == True:
kid = obj.kid
#print u"==删除选中的kid ====> ",kid
#print pdata[kid]
delList.append(kid)
##提示是否要删除
delQuestionDlg = wx.MessageDialog(self.ProgrameList,u"是否确认要删除所选内容?", u'确认提示',
wx.YES_NO|wx.ICON_QUESTION)
if delQuestionDlg.ShowModal() == wx.ID_YES:
LoadLog.LogMsg(gen.logger.warning,u"删除数据项")
for keyid in delList:
##从列表中删除选中字典项
del pdata[keyid]
self.datahandle.WriteData(pdata)
list_data = self.datahandle.handleList()
self.ProgrameList.SetObjects(list_data)
gen.IsItemChanged = True
else:
LoadLog.LogMsg(gen.logger.debug,u"取消删除项")
self.OnSelectAll()
del delList
#self.ProgrameList.RefreshObjects(allobj)
return
#datahandle = DataHandle(self.filename)
#datahandle.WriteData()
def OnExportDlg(self,event):
#print "=== onExport dialog ==="
self.dlg = exportDialog(u"导出数据",self.filename)
if self.dlg.ShowModal() == wx.ID_OK:
self.dlg.handleExportDlg()
self.dlg.Destroy()
def OnImportDlog(self,event):
LoadLog.LogMsg(gen.logger.info,u"打开导入对话框")
wildcard = "Json File (*.json)|*.json|" \
"All files (*.*)|*.*"
importDlg = wx.FileDialog(None, u"选择导入的文件",os.getcwd(), "", wildcard, wx.OPEN)
if importDlg.ShowModal() == wx.ID_OK:
ipathfile = importDlg.GetPath()
importObj = DataHandle(ipathfile)
pdata = importObj.ReadData()
#print u"===从导入文件读取到的字典: ",pdata
if pdata != None:
for eachKey in pdata.keys():
if eachKey.isdigit():
break
subkeys = ["status","name","runAs","note","programe","processNum","kid","logfile","monitor"]
for kid in pdata.keys():
if len(pdata[kid].keys()) != len(subkeys):
break
for eachsubKey in subkeys:
if eachsubKey not in pdata[kid]:
break
#print u"===从导入文件读取到,并通过的字典:",pdata
writeDlg = wx.MessageDialog(self.ProgrameList,u"导入的文件数据将覆盖现在程序的数据,是否继续?", u'确认提示',
wx.YES_NO|wx.ICON_QUESTION)
if writeDlg.ShowModal() == wx.ID_YES:
saveObj = DataHandle(self.filename)
saveObj.WriteData(pdata)
LoadLog.LogMsg(gen.logger.info,u"成功导入")
list_data = self.datahandle.handleList()
LoadLog.LogMsg(gen.logger.info,u"刷新控件列表")
self.ProgrameList.SetObjects(list_data)
else:
LoadLog.LogMsg(gen.logger.info,u"取消导入")
return
else:
wx.MessageBox(u"无法导入,类型或错误的文件结构", u'错误', wx.OK | wx.ICON_ERROR)
LoadLog.LogMsg(gen.logger.error,u"无法导入,类型或错误的文件结构")
importDlg.Destroy()
def CheckEdit(self):
allobj = self.ProgrameList.GetObjects()
tbool = []
for obj in allobj:
tbool.append(self.ProgrameList.IsChecked(obj))
if tbool.count(True) == 0:
LoadLog.LogMsg(gen.logger.info,u"未选取项目进行编辑")
wx.MessageBox(u"请选择一行进行编辑", u'信息', wx.OK | wx.ICON_INFORMATION,parent=self.ProgrameList)
self.kid = None
elif tbool.count(True) > 1:
LoadLog.LogMsg(gen.logger.info,u"不能选取多项同时进行编辑")
wx.MessageBox(u"一次只能选择一行进行编辑", u'信息', wx.OK | wx.ICON_INFORMATION,parent=self.ProgrameList)
##清空选择
self.OnSelectAll()
self.kid = None
else:
rowObj = self.ProgrameList.GetObjects()
for obj in rowObj:
if self.ProgrameList.IsChecked(obj) == True:
self.kid = obj.kid
return self.kid
def OnSelectAll(self,event=None):
objects = self.ProgrameList.GetObjects()
for obj in objects:
if event == None:
self.ProgrameList.SetCheckState(obj, False)
elif event.GetId() == wx.ID_SELECTALL:
self.ProgrameList.SetCheckState(obj, True)
else:
self.ProgrameList.SetCheckState(obj,False)
self.ProgrameList.RefreshObjects(objects)
def OnOperationPro(self,event):
processKidList = []
objects = self.ProgrameList.GetObjects()
for obj in objects:
if self.ProgrameList.IsChecked(obj):
kid = obj.kid
processKidList.append(kid)
if len(processKidList) == 0:
wx.MessageBox(u"请选择一项进行操作", u'信息', wx.OK | wx.ICON_INFORMATION,parent=self.ProgrameList)
LoadLog.LogMsg(gen.logger.info,u"未选取项目")
return
pdata = self.datahandle.ReadData()
for kid in processKidList:
pLastName = pdata[kid]["programe"]
Pro_Argument = pdata[kid]["logfile"]
#print u"获取到可执行的程序名称",pLastName
ProName = str(os.path.basename(pLastName))
p = os.popen('tasklist /FI "IMAGENAME eq %s"' % ProName)
GetProNum = p.read().count(ProName)
if GetProNum == "":
GetProNum = 0
LoadLog.LogMsg(gen.logger.debug,u"获取%s进程数为 %d" % (ProName,GetProNum))
if event.GetId() == gen.ID_START:
if GetProNum == pdata[kid]["processNum"] :
wx.MessageBox(u"该项目的进程已经在运行中", u'提示信息', wx.OK | wx.ICON_INFORMATION,parent=self.ProgrameList)
LoadLog.LogMsg(gen.logger.info,u"该项目的进程%s已经在运行中" % pLastName)
else:
LoadLog.LogMsg(gen.logger.info,u"启动进程%s" % pLastName)
try:
a = win32api.ShellExecute(0, 'open', pLastName, Pro_Argument,'',1)
#self.UpdateRunStatus(kid,True)
gen.IsRunStatusChanged = True
pdata[kid]["status"] = True
list_data = self.datahandle.handleList(pdata)
self.ProgrameList.SetObjects(list_data)
except:
LoadLog.LogMsg(gen.logger.warning,u"启动进程: %s失败" % ProName)
elif event.GetId() == gen.ID_STOP:
#print "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::::::",pdata[kid]["monitor"]
if GetProNum != 0 :
StopDlg = wx.MessageDialog(self.ProgrameList,u"您确定要结束该进程吗?", u'确认提示',
wx.YES_NO|wx.YES_NO)
if StopDlg.ShowModal() == wx.ID_YES:
try:
LoadLog.LogMsg(gen.logger.warning,u"结束进程: %s" % ProName)
subprocess.Popen("taskkill /F /im %s" % os.path.basename(pLastName) , shell=True)
#self.UpdateRunStatus(kid,False)
gen.IsRunStatusChanged = True
pdata[kid]["status"] = False
list_data = self.datahandle.handleList(pdata)
self.ProgrameList.SetObjects(list_data)
except:
LoadLog.LogMsg(gen.logger.warning,u"结束进程: %s失败" % ProName)
StopDlg.Destroy()
else:
LoadLog.LogMsg(gen.logger.info,u"当前项目未运行,没有进程可结束")
else:
RestartDlg = wx.MessageDialog(self.ProgrameList,u"您确定要重启该进程吗?", u'确认提示',
wx.YES_NO|wx.ICON_QUESTION)
if RestartDlg.ShowModal() == wx.ID_YES:
if GetProNum != 0 :
LoadLog.LogMsg(gen.logger.info,u"当前项目未运行,没有进程可结束")
try:
LoadLog.LogMsg(gen.logger.warning,u"重启进程: %s" % ProName)
subprocess.Popen("taskkill /F /im %s" % os.path.basename(pLastName) , shell=True)
gen.IsRunStatusChanged = True
pdata[kid]["status"] = False
time.sleep(2)
self.a = win32api.ShellExecute(0, 'open', pLastName, Pro_Argument,'',1)
pdata[kid]["status"] = True
list_data = self.datahandle.handleList(pdata)
self.ProgrameList.SetObjects(list_data)
except:
LoadLog.LogMsg(gen.logger.warning,u"重启进程: %s失败" % ProName)
RestartDlg.Destroy()
processKidList = []
# def UpdateRunStatus(self,kid,isRun):
# LoadLog.LogMsg(gen.logger.debug,u"更新运行状态")
# datahandle = DataHandle(self.filename)
# pdata = datahandle.ReadData()
# if pdata[kid]["monitor"] == True:
# pdata[kid]["status"] = isRun
# list_data = datahandle.handleList(pdata)
# self.ProgrameList.SetObjects(list_data)
# #gen.IsItemRunChanged = False
def OnOpenLogFile(self,event):
LoadLog.LogMsg(gen.logger.info,u"打开日志文件%s" % gen.LOGFILE)
try:
subprocess.Popen("C:\\windows\\system32\\notepad.exe %s" % gen.LOGFILE, shell=True)
except:
LoadLog.LogMsg(gen.logger.warning,u"打开日志文件%s 失败" % gen.LOGFILE)
def OnHelp(self,event):
html = HtmlDocDlg(None)
html.ShowModal()
html.Destroy()
class EventSettingMenuAndLog(object):
def __init__(self,viewbtn,stopbtn,clearbtn,cTextCrtl,IntervalBox):
self.viewbtn = viewbtn
self.stopbtn = stopbtn
self.clearbtn = clearbtn
self.IntervalBox = IntervalBox
self.cTextCrtl = cTextCrtl
self.f = HandleSetting(gen.CONF_FILE)
def OnViewLog(self,event):
gen.console_switch = True
gen.logswitch = True
self.f.WriteSingleConf("log","enable_console_show","True")
self.f.WriteSingleConf("log","enable_log","True")
self.viewbtn.Disable()
self.stopbtn.Enable()
def OnStopLog(self,event):
gen.console_switch = False
self.f.WriteSingleConf("log","enable_console_show","False")
self.viewbtn.Enable()
self.stopbtn.Disable()
def OnClearLog(self,event):
self.cTextCrtl.SetValue("")
def OnClearMax(self,event):
if self.IntervalBox.GetValue() in gen.SAMPLELIST:
gen.MSG_MAX = self.IntervalBox.GetValue()
f = HandleSetting(gen.CONF_FILE)
f.WriteSingleConf("log","msg_max",gen.MSG_MAX)
LoadLog.LogMsg(gen.logger.info,u"更新自动清空控制台的字节大小为%s" % gen.MSG_MAX)
def OnSettingUI(self,event):
LoadLog.LogMsg(gen.logger.info,u"打开设置面板")
if os.path.exists(gen.CONF_FILE) is True:
try:
f = HandleSetting(gen.CONF_FILE)
configdict = f.ReadConfFile()
except:
configdict = gen.DEFAULT_CONFIG_DICT
else:
configdict = gen.DEFAULT_CONFIG_DICT
dlg = settingDlg(u"设置",configdict)
dlg.Center()
if dlg.ShowModal() == wx.ID_OK:
newConfDict = dlg.SetUI.GenerateConfDict()
#print newConfDict
f = HandleSetting(gen.CONF_FILE,newConfDict)
f.WriteConfFile()
gen.CONF_DICT = newConfDict
#print newConfDict
self.UpdateConfData()
LoadLog.LogMsg(gen.logger.info,u"更新设置面板的数据")
if gen.CONF_DICT["log"]["enable_console_show"] is True:
self.viewbtn.Disable()
self.stopbtn.Enable()
else:
self.viewbtn.Enable()
self.stopbtn.Disable()
#print gen.LOOP_TIME
dlg.Destroy()
def UpdateConfData(self):
gen.LOOP_TIME = gen.CONF_DICT["monitor"]["round_robin_scheduling"] * 1000
gen._filedata = gen.CONF_DICT["monitor"]["data_file"]
gen.CPU_LOOP_TIME = gen.CONF_DICT["monitor"]["cpu_scheduling"] * 1000
gen.MEMORY_LOOP_TIME = gen.CONF_DICT["monitor"]["memory_scheduling"] * 1000
gen.CPU_MON = gen.CONF_DICT["monitor"]["enable_monitor_cpu"]
gen.MEMORY_MON = gen.CONF_DICT["monitor"]["enable_monitor_memory"]
gen.logswitch = gen.CONF_DICT["log"]["enable_log"]
gen.console_switch = gen.CONF_DICT["log"]["enable_console_show"]
gen.LOGFILE = gen.CONF_DICT["log"]["log_file"]
gen.LOG_MAX_BYTES = gen.CONF_DICT["log"]["log_size"] * 1024 * 1024
gen.BACKUP_COUNT = gen.CONF_DICT["log"]["log_leave"]
gen.MSG_MAX = gen.CONF_DICT["log"]["msg_max"]
print type(gen.MSG_MAX)
gen.FILE_LOG_LEVEL = gen.CONF_DICT["log"]["log_level"]
gen.level = gen.LEVELS.get(gen.FILE_LOG_LEVEL,logging.NOTSET)
gen.CONSOLE_LOG_LEVEL = gen.CONF_DICT["log"]["console_level"]
###这里搜索没什么用,以后扩展可能用到吧
def FindString(self,findString):
#self.readFindFlags()
##大小写敏感匹配检测
FileContents = self.cTextCrtl.GetValue()
#findString,FileContents = self.StrmatchCase(findString)
##获取文本的选择范围,返回一个字节point位置的元组
selectPoint = self.cTextCrtl.GetSelection()
##如果没有选取文本范围,默认 selectPoint 返回元组的两个数是相等的,也就是等下光标的插入点
##如果选取了范围,selectPoint返回元组的两人个数是不相等的
if selectPoint[0] != selectPoint[1]:
startFindAt = max(selectPoint) ##设置查找点 startFindAt 为元组最大的数
else:
startFindAt = self.cTextCrtl.GetInsertionPoint() ##设置开始查找点为光标插入点
if startFindAt == self.cTextCrtl.GetLastPosition(): ##如果查找点为尽头,重置为0
startFindAt = 0
##开始查找指定的字符
foundStr = FileContents.find(findString,startFindAt)
if foundStr != -1:
##如果找到,求出字符的最后位置
EndStr = len(findString) + foundStr
##设置文本选取范围
self.cTextCrtl.SetSelection(EndStr,foundStr)
##设置文本选取范围的focus
self.cTextCrtl.SetFocus()
return True
else:
return False