forked from carker/dbfsplit
/
Dbfsplit.py
611 lines (554 loc) · 27.6 KB
/
Dbfsplit.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QColor, QStandardItemModel
from PyQt5.QtWidgets import QStyle, QItemDelegate,QStyleOptionProgressBar,QHeaderView
from PyQt5.QtWidgets import QItemDelegate, QWidget, QStyleOptionViewItem, QCheckBox
from PyQt5.QtCore import QModelIndex,QVariant
from Ui_Dbfsplit import Ui_MainWindow
# import myxml
import myxml2 as myxml
from myemail import Myemail
import base64
import time
import sys
import os
import shutil
import log
from work_thread import Work_Thread
import datetime
from work import Task
from process_msg_thread import Msg_Thread
from functools import partial
from multiprocessing.pool import Pool
from multiprocessing import Process, Manager
def do_works(*args,**kwds):
# mylog = kwds.get('log',None)
config = kwds.get('config',{})
task_list = kwds.get('task_list')
process_id = kwds.get('id')
# loglevel = config.get('loglevel', 'DEBUG')
# today = time.localtime(time.time())
# today = time.strftime("%Y%m%d", today)
# mylog = log.Log(filename='log/process_all.%s.log'%today,cmdlevel=loglevel)
# err_log = log.Log('error',filename='error/process_err.%s.log'%today,filelevel='error')
# mylog = mylog.addFileLog(err_log)
# if not mylog: mylog = log.Log()
msg_queue = args[0]
mylog = ProcessLog(msg_queue)
mylog.debug('进程开始:%s'%process_id)
success = 0
for item in task_list:
try:
mylog.debug('task%s:%s开始'%(item['id'],item['source']['FileName']))
task = Task(mylog, config, item, msg_queue)
tmp_dict = {}
if task.iftxt:
if split_txt(tmp_dict,msg_queue,task):
success += 1
else:
# if split_dbf_by_append(tmp_dict,msg_queue,task):
if task.work_new():
success +=1
except:
mylog.error('task%s:%s未知错误%s'%(item['id'],item['source']['FileName'],sys.exc_info()[:2]))
msg_queue.put(('process_end',(process_id, success)))
mylog.info('--------------------------')
mylog.info('进程[%s]执行完成,成功:%s'%(process_id,success))
def split_dbf_by_append(tmp_dict,msg_queue,task):
msg_queue.put(('process_progress',(task.id, 5)))
if task.check_data():
task.log.debug("task%s:%s check_data ok" %(task.id,task.fileid))
msg_queue.put(('process_progress',(task.id, 10)))
if tmp_dict.get('total_records'):
total_records = tmp_dict.get('total_records')
total_records_num = tmp_dict.get('total_records_num')
else:
total_records = task.read_dbf()
if total_records == False: return False
total_records_num = len(total_records)
tmp_dict['total_records'] = total_records
tmp_dict['total_records_num'] = total_records_num
task.log.info('task%s:%s 总记录 %s'%(task.id,task.fileid,total_records_num))
msg_queue.put(('process_progress',(task.id, 30)))
msg_queue.put(('process_total_records',(task.id, total_records_num)))
select_records = task.get_dbf_data(total_records)
select_records_num = len(select_records)
if not task.write_local_dbf_by_append(select_records):
task.log.debug("task%s:%s write_local_dbf fail" %(task.id,task.fileid))
return False
msg_queue.put(('process_progress',(task.id, 60)))
msg_queue.put(('process_filter_records',(task.id, select_records_num)))
task.log.info('task%s:%s 匹配 %s'%(task.id,task.fileid,select_records_num))
task.log.debug("task%s:%s write_local_dbf ok" %(task.id,task.fileid))
if task.copy_to_destination():
task.log.info('task%s:%s 执行完成'%(task.id,task.fileid))
msg_queue.put(('process_progress',(task.id, 100)))
return True
if task.send_ok_file():
task.log.info('task%s:%s 执行完成'%(task.id,task.fileid))
msg_queue.put(('process_progress',(task.id, 100)))
return True
return False
def split_txt(tmp_dict,msg_queue,task):
msg_queue.put(('process_progress',(task.id, 5)))
if task.check_data():
task.log.debug("[%s] %s check_data ok" %(task.id,task.fileid))
msg_queue.put(('process_progress',(task.id, 10)))
if tmp_dict.get('total_records'):
total_records = tmp_dict.get('total_records')
total_records_num = tmp_dict.get('total_records_num')
else:
total_records = task.read_txt()
if total_records == False: return False
total_records_num = len(total_records)
tmp_dict['total_records'] = total_records
tmp_dict['total_records_num'] = total_records_num
task.log.info('[%s] %s 总记录 %s'%(task.id,task.fileid,total_records_num))
msg_queue.put(('process_progress',(task.id, 30)))
msg_queue.put(('process_total_records',(task.id, total_records_num)))
select_records = task.get_txt_data(total_records)
select_records_num = len(select_records)
if not task.write_local_txt(select_records):
task.log.debug("[%s] %s write_local_dbf fail" %(task.id,task.fileid))
return False
msg_queue.put(('process_progress',(task.id, 60)))
msg_queue.put(('process_filter_records',(task.id, select_records_num)))
task.log.info('[%s] %s 匹配 %s'%(task.id,task.fileid,select_records_num))
task.log.debug("[%s] %s write_local_dbf ok" %(task.id,task.fileid))
if task.copy_to_destination():
task.log.info('[%s] %s 执行完成'%(task.id,task.fileid))
msg_queue.put(('process_progress',(task.id, 100)))
return True
if task.send_ok_file():
task.log.info('[%s] %s 执行完成'%(task.id,task.fileid))
msg_queue.put(('process_progress',(task.id, 100)))
return True
return False
class ProcessLog():
def __init__(self,queue):
self.queue = queue
def log(self,loglevel,msg):
# print('发送%s %s'%(loglevel,msg))
self.queue.put(('log',(loglevel, msg)))
def debug(self, msg):
self.log('debug', msg)
def info(self, msg):
self.log('info', msg)
def warning(self, msg):
self.log('warning', msg)
def error(self, msg):
self.log('error', msg)
def critical(self, msg):
self.log('critical', msg)
def exception(self, msg):
self.log('exception', msg)
# def debug(self,msg):
# msg_queue.put({'log':('debug', msg)})
class CheckBoxDelegate(QtWidgets.QItemDelegate):
def __init__(self, parent=None):
QItemDelegate.__init__(self, parent)
self.chkboxSize = 19 #?!
def createEditor(self, parent, option, index):
chkbox = QtWidgets.QCheckBox(parent)
chkbox.setText('')
chkbox.setTristate(False) #只用两个状态
left = option.rect.x() + (option.rect.width() - self.chkboxSize) / 2
top = option.rect.y() + (option.rect.height() - self.chkboxSize) / 2
chkbox.setGeometry(left, top, self.chkboxSize, self.chkboxSize)
return chkbox
def paint(self, painter, option, index):
value = bool(index.data())
# value = False
opt = QtWidgets.QStyleOptionButton()
opt.state |= QStyle.State_Enabled | (QStyle.State_On if value else QStyle.State_Off)
opt.text = ''
left = option.rect.x() + (option.rect.width() - self.chkboxSize) / 2
top = option.rect.y() + (option.rect.height() - self.chkboxSize) / 2
opt.rect = QtCore.QRect(left, top, self.chkboxSize, self.chkboxSize)
QtWidgets.QApplication.style().drawControl(QStyle.CE_CheckBox, opt, painter)
def updateEditorGeometry(self, editor, option, index):
pass
class ProgressDelegate(QItemDelegate):
def __init__(self,parent=None):
QItemDelegate.__init__(self, parent)
def paint(self, *args, **kwargs):
column = args[2].column()
value = args[2].data()
if column == 2:
progressBarOption = QStyleOptionProgressBar()
progressBarOption.rect = args[1].rect
progressBarOption.minimum = 0
progressBarOption.maximum = 100
if isinstance(value, int) :
progressBarOption.progress = int(value)
progressBarOption.text = "{0}%".format(int(value))
progressBarOption.textVisible = True
QtWidgets.QApplication.style().drawControl(QStyle.CE_ProgressBar, progressBarOption, args[0], None)
return
return QItemDelegate.paint(self, *args, **kwargs)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent = None,log=None,config={},data=[]):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.config = myxml.config
self.init()
self.data = data
self.log = log
self.init_sysdate2today()
self.model = QStandardItemModel(len(self.data), 10, self.task_QTableView)
self.init_task_frame(data)
self.signl_connect()
self.msg_thread = None # 多进程获取消息线程
self.p = None
# self.threadnum = int(self.config.get('threadnum', '10'))
self.threadnum = self.threadnum_spinBox.value()
self.thread_list = [] # task按源文件分成多个线程
if self.newmodel_checkBox.isChecked():
self.config.update({'newmodel':'yes'})
else:
self.config.update({'newmodel':'no'})
self.show()
if 'Y' in self.config.get('autorun', 'Y').upper():
self.on_run_pushButton_clicked()
def createdirs(self,task_list):
if not os.path.exists('tmp'):os.mkdir('tmp')
if not os.path.exists('tmp_read'):os.mkdir('tmp_read')
if not os.path.exists('dbfmodel'):os.mkdir('dbfmodel')
if 'Y' in self.config.get("copyresult",'yes').upper():
for task in task_list:
for destination in task['destination']:
dir_path,_ = os.path.split(destination.get('SaveName'))
try:
if not os.path.exists(dir_path): os.makedirs(dir_path)
self.log.debug("创建目的路径%s成功"%dir_path)
except:
self.log.error("创建目的路径%s失败"%dir_path)
self.log.trace()
def init(self):
self.threadnum = self.threadnum_spinBox.value()
if self.newmodel_checkBox.isChecked():
self.config.update({'newmodel':'yes'})
else:
self.config.update({'newmodel':'no'})
self.msg_label.setText('')
self.thread_result = {} # taskid:success
self.thread = [] # 每启动一个线程就放进来,用于停止所有线程操作
self.task_select = [] # 存储选中的task
def signl_connect(self):
self.sysdate_dateEdit.dateChanged.connect(self.load_xml)
def load_xml(self):
sysdate = self.sysdate_dateEdit.date().toString("yyyyMMdd")
self.config = myxml.get_sysconfig_from_xml('config.xml')
self.data = myxml.get_task_from_xml('config.xml', sysdate=sysdate)
self.init_task_frame(self.data)
def init_sysdate2today(self):
today = datetime.datetime.now()
self.sysdate_dateEdit.setDateTime(today)
def init_task_frame3(self,task_list=[]):
table = self.task_QTableView
# table = QtWidgets.QTableView()
model = QStandardItemModel(3, 3, table)
model.setHorizontalHeaderLabels(['Name', 'Description', 'Animal?'])
model.setData(model.index(0, 0, QModelIndex()), QVariant('Squirrel'))
model.setData(model.index(0, 1, QModelIndex()), QVariant(u'可爱的松树精灵'))
model.setData(model.index(0, 2, QModelIndex()), QVariant(True))
model.setData(model.index(1, 0, QModelIndex()), QVariant('Soybean'))
model.setData(model.index(1, 1, QModelIndex()), QVariant(u'他站在田野里吹风'))
model.setData(model.index(1, 2, QModelIndex()), QVariant(False))
table.setModel(model)
self.verticalLayout_2.addWidget(table)
# table.setItemDelegateForColumn(2, CheckBoxDelegate(table))
# table.resizeColumnToContents(1)
# table.horizontalHeader().setStretchLastSection(True)
def init_task_frame(self,task_list=[]):
if len(task_list)==0:task_list = self.data
self.model.setHorizontalHeaderLabels(['任务ID','是否启用','处理进度','OK文件','匹配数',
'总记录数','文件编码','文件描述','文件名称','保存名称'])
for i in range(len(task_list)):
task = task_list[i]
taskid = task['id']
fileid = task['attrib']['FileID']
filename = task['attrib']['Description']
source = task['source']['FileName']
destination = ''
for des in task['destination']:
destination += '%s\n'%des['SaveName']
self.model.setData(self.model.index(i,0,QModelIndex()),QVariant(taskid))
self.model.item(i,0).setTextAlignment(QtCore.Qt.AlignCenter)
self.model.setData(self.model.index(i,1,QModelIndex()),QVariant(True))
self.model.setData(self.model.index(i,2,QModelIndex()),QVariant(0))
self.model.setData(self.model.index(i,3,QModelIndex()),QVariant(''))
self.model.setData(self.model.index(i,4,QModelIndex()),QVariant(''))
self.model.setData(self.model.index(i,5,QModelIndex()),QVariant(''))
self.model.setData(self.model.index(i,6,QModelIndex()),QVariant(fileid))
self.model.setData(self.model.index(i,7,QModelIndex()),QVariant(filename))
self.model.setData(self.model.index(i,8,QModelIndex()),QVariant(source))
self.model.setData(self.model.index(i,9,QModelIndex()),QVariant(destination[:-1]))
self.task_QTableView.setModel(self.model)
self.task_QTableView.setItemDelegateForColumn(1, CheckBoxDelegate(self.task_QTableView))
self.task_QTableView.setItemDelegateForColumn(2, ProgressDelegate(self.task_QTableView))
self.task_QTableView.horizontalHeader().setStretchLastSection(True)
self.task_QTableView.resizeColumnsToContents()
self.task_QTableView.resizeRowsToContents()
def get_select_task(self):
self.task_select = []
for task in self.data:
if self.model.data(self.model.index(task['id'],1)):
self.task_select.append(task)
return self.task_select
def sort_data(self,task_list):
dict_list = {}
for task in task_list:
if task['source']['FileName'] not in dict_list:
# task_dict = {task['source']['FileName']:[task]}
dict_list[task['source']['FileName']] = [task]
else:
new_list = dict_list[task['source']['FileName']]
new_list.append(task)
dict_list[task['source']['FileName']] = new_list
return dict_list
def sendmail(self):
server = {'host':self.config.get('email_sender_host', ''),
'port':int(self.config.get('email_sender_port', '25'))}
receivers = self.config.get('email_receivers', '')
sender = {'address':self.config.get('email_sender_adress', ''),
'user':self.config.get('email_sender_user', ''),
'password':base64.b64decode(self.config.get('email_sender_password', '')[::-1]).decode(),
'header_from':'系统提醒'}
subject = 'DBF分拆提醒'
if 'y' in self.config.get('email', '').lower():
try:
total_success = sum([y for (x,y) in self.thread_result.items() if y >0])
myemail = Myemail(server, sender, receivers)
today = time.localtime(time.time())
today = time.strftime("%Y%m%d", today)
filename='error/err.%s.log'%today
msg = open(filename).read()
msg = '任务完成!%s个任务,成功: %s个\n\n----------------\n%s'%(len(self.task_select), total_success, msg)
result = myemail.sendmail(subject, msg)
self.log.debug('send email:%s'%result)
except:
self.log.trace()
def create_msg_thread(self, log, queue, threadid="日志线程"):
msg_thread = Msg_Thread(log, queue)
msg_thread.setIdentity(threadid)
msg_thread.setDaemon(True)
msg_thread.msg_update_progress.connect(self.update_progress)
msg_thread.msg_total_records.connect(self.update_total_records)
msg_thread.msg_filter_records.connect(self.update_filter_records)
msg_thread.msg_thread_end.connect(self.process_end) # 跟线程结束处理有冲突,重写处理
# self.thread_result[threadid] = -2
return msg_thread
@QtCore.pyqtSlot()
def on_run_pushButton_clicked(self):
# QtWidgets.QMessageBox.critical(self, "Critical", u"错误:正则表达式错误")
self.init()
select_task = self.get_select_task()
# self.model.setData(self.model.index(5,1),False)
# return
self.createdirs(select_task)
self.thread_list = [(source,task_thread) for source,task_thread in self.sort_data(select_task).items()]
self.log.debug('len thread_list:%s'%len(self.thread_list))
p = Pool()
self.p=p
manager = Manager()
q = manager.Queue()
task_num = len(self.thread_list)
for i in range(task_num):
if len(self.thread_list)>0:
source,task_list = self.thread_list.pop(0)
self.thread_result[source] = -2
p.apply_async(do_works, args = (q,), kwds = {"id":source,"queue":q,"config":self.config,"task_list":task_list})
self.log.debug('添加进程成功 %s'%source)
self.msg_thread = self.create_msg_thread(self.log, q)
self.msg_thread.start()
# for i in range(5):
# p = Process(target = do_works, kwds = {"config":self.config,"log":self.log,"task_list":self.thread_list.pop(0)[1]})
# p.start()
# if self.check_thread_end():
# for i in range(self.threadnum):
# if self.thread_list:
# source,task_thread = self.thread_list.pop()
# work = self.work(task_thread,source)
# work.start()
self.log.debug('end')
def sendokfile(self,task):
tmp_ok_file = os.path.abspath('tmp/ok.ok')
if not os.path.exists(tmp_ok_file):
f=open(tmp_ok_file,'w')
f.write('')
f.close()
for fileto in task['destination']:
ok_file = fileto.get('SaveName','')
if '.ok' not in ok_file.lower(): ok_file += '.ok'
self.log.debug('ok file:%s'%ok_file)
try:
shutil.copy(tmp_ok_file, ok_file)
except:
self.log.trace()
# os.system(r'copy %s %s'%(tmp_ok_file, ok_file))
# result = tools.command_run(r'copy %s %s'%(tmp_ok_file, ok_file), 3)
if not os.path.exists(ok_file):
self.log.error('task%s:%s copy okfile to destination:%s fail'%(task['id'],task['attrib'].get('FileID',''),ok_file))
return False
self.log.debug('task%s:%s copy okfile to destination:%s success'%(task['id'],task['attrib'].get('FileID',''),ok_file))
return True
@QtCore.pyqtSlot()
def on_sendok_PushButton_clicked(self):
self.log.debug('sendok button click')
select_task = self.get_select_task()
self.createdirs(select_task)
for task in select_task:
taskid = task['id']
self.statusBar().showMessage('任务[%s]正在发送OK文件'%(self.model.data(self.model.index(taskid,0))))
result = self.sendokfile(task)
self.log.debug(result)
self.update_okflag(task, result)
flag = '成功' if result else '失败'
self.statusBar().showMessage('任务[%s]发送OK文件%s'%(self.model.data(self.model.index(taskid,0)), flag))
def update_okflag(self, task, result):
try:
row = int(task['id'])
flag = '√' if result else 'x'
self.model.setData(self.model.index(row,3),QVariant(flag))
except:
self.log.trace()
def reset(self):
rowmax = self.model.rowCount()
for rowcount in range(rowmax):
self.model.setData(self.model.index(rowcount,2),QVariant(0))
self.model.setData(self.model.index(rowcount,3),QVariant(''))
self.model.setData(self.model.index(rowcount,4),QVariant(''))
self.model.setData(self.model.index(rowcount,5),QVariant(''))
self.statusBar().showMessage('')
@QtCore.pyqtSlot()
def on_reset_pushButton_clicked(self):
self.reset()
@QtCore.pyqtSlot()
def on_stop_pushButton_clicked(self):
self.msg_label.setText('正在停止线程...')
if self.p:self.p.terminate()
for thread in self.thread:
thread.stop()
@QtCore.pyqtSlot()
def on_exit_pushButton_clicked(self):
# self.init_sysdate()
self.close()
@QtCore.pyqtSlot()
def on_errorlog_PushButton_clicked(self):
try:
today = time.localtime(time.time())
today = time.strftime("%Y%m%d", today)
error_log_path = os.path.abspath('error/err.%s.log'%today)
if os.path.exists(error_log_path):
os.popen('notepad %s'%error_log_path)
except:
self.log.trace()
@QtCore.pyqtSlot()
def on_select_pushButton_clicked(self):
rowmax = self.model.rowCount()
for rowcount in range(rowmax):
self.model.setData(self.model.index(rowcount,1),QVariant(True))
@QtCore.pyqtSlot()
def on_unselect_pushButton_clicked(self):
rowmax = self.model.rowCount()
for rowcount in range(rowmax):
self.model.setData(self.model.index(rowcount,1),QVariant(False))
@QtCore.pyqtSlot(tuple)
def update_progress(self, data_tuple):
status = {5:'任务开始',10:'参数校验成功',12:'无过滤条件任务拷贝失败',
15:'CPP加载参数成功',20:'CPP解析参数成功',25:'CPP读取源文件dbf成功',
30:'读取源文件dbf成功',40:'CPP获取过滤数据成功',60:'写本地dbf成功',
90:'CPP写目的dbf成功',100:'拷贝到目的地成功,任务结束'}
taskid, num = data_tuple
self.model.setData(self.model.index(taskid,2),QVariant(num))
self.statusBar().showMessage('线程[%s]正在处理: 任务%s,当前进程状态%s'%(self.model.data(self.model.index(int(taskid),8)), taskid, status.get(num,str(num))))
if num in (12,15,20,25,40,90):
self.model.item(int(taskid), 6).setBackground(QColor('red'))
else:
self.model.item(int(taskid), 6).setBackground(QColor(255,255,255,0))
@QtCore.pyqtSlot(tuple)
def update_total_records(self, data_tuple):
taskid, num = data_tuple
self.log.debug('[thread msg] total records:%s,%s'%data_tuple)
self.model.setData(self.model.index(int(taskid),5),QVariant(num))
@QtCore.pyqtSlot(tuple)
def update_filter_records(self, data_tuple):
taskid, num = data_tuple
self.log.debug('[thread msg] filter records:%s,%s'%data_tuple)
self.model.setData(self.model.index(int(taskid),4),QVariant(num))
@QtCore.pyqtSlot(tuple)
def process_end(self, data_tuple):
processid, success = data_tuple
self.thread_result[processid] = success
# mylog.error('进程[%s]执行完成,任务成功:%s'%(threadid,success))
total_success = sum([y for (x,y) in self.thread_result.items() if y>0])
self.msg_label.setText('已完成: %s个任务'%total_success)
if self.check_thread_end():
self.log.debug('[thread end msg] %s success: %s'%data_tuple)
self.log.info('任务完成!%s个任务,成功: %s个'%(len(self.task_select), total_success))
if 'Y' in self.config.get('autorun', 'Y').upper():
self.sendmail()
self.close()
else:
self.msg_label.setText('任务完成!%s个任务,成功: %s个'%(len(self.task_select), total_success))
self.statusBar().showMessage('结束')
self.msg_thread.stop()
@QtCore.pyqtSlot(tuple)
def work_thread_end(self, data_tuple):
threadid, success = data_tuple
self.thread_result[threadid] = success
# mylog.error('进程[%s]执行完成,任务成功:%s'%(threadid,success))
if self.msg_label.text() != '正在停止线程...':
total_success = sum([y for (x,y) in self.thread_result.items() if y>0])
self.msg_label.setText('已完成: %s个任务'%total_success)
if len(self.thread_list)>0:
source,task_thread = self.thread_list.pop()
work = self.work(task_thread,source)
work.start()
else:
self.statusBar().showMessage('线程[%s]结束'%threadid)
if self.check_thread_end():
total_success = sum([y for (x,y) in self.thread_result.items() if y >0])
self.log.debug('[thread end msg] %s success: %s'%data_tuple)
self.log.debug('任务完成!%s个任务,成功: %s个'%(len(self.task_select), total_success))
if 'Y' in self.config.get('autorun', 'Y').upper():
self.sendmail()
self.close()
else:
self.msg_label.setText('任务完成!%s个任务,成功: %s个'%(len(self.task_select), total_success))
self.statusBar().showMessage('结束')
def work(self, data, threadid="Work_Thread"):
work = Work_Thread(self.log, self.config, data)
work.setIdentity(threadid)
work.setDaemon(True)
work.msg_update_progress.connect(self.update_progress)
work.msg_total_records.connect(self.update_total_records)
work.msg_filter_records.connect(self.update_filter_records)
work.msg_thread_end.connect(self.work_thread_end)
self.thread_result[threadid] = -2
self.thread.append(work)
return work
def check_thread_end(self):
for thread, success in self.thread_result.items():
if success < 0 :
self.log.debug('check_thread_end:%s not end'%thread)
return False
return True
if __name__ == '__main__':
# manager = Manager()
# q = manager.Queue()
# mylog = ProcessLog(q)
# mylog.debug('进程开始')
# print(a)
config = myxml.config
data = myxml.get_task_from_xml('config.xml')
loglevel = config.get('loglevel', 'DEBUG')
today = time.localtime(time.time())
today = time.strftime("%Y%m%d", today)
mylog = log.Log(filename='log/all.%s.log'%today,cmdlevel=loglevel)
err_log = log.Log('error',filename='error/err.%s.log'%today,filelevel='error')
mylog = mylog.addFileLog(err_log)
app = QtWidgets.QApplication(sys.argv)
window = MainWindow(data=data, config=config, log=mylog)
# window.show()
sys.exit(app.exec_())