-
Notifications
You must be signed in to change notification settings - Fork 1
/
raw_window.py
362 lines (317 loc) · 12.3 KB
/
raw_window.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
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from threading import Thread
import sys
import os
import zipfile
from functools import wraps
from extractor import PerformanceExtractor
from adapter import GuoJinAdapter
from connection import MongoConnection
import logging
import bson
_logger_formatter_str = "\t".join([
"%(levelname)s",
"%(asctime)s",
"%(filename)s",
"%(lineno)d",
"%(message)s"
])
_table_columns = ["级别", "日期", "时间", "文件名", "行号", "信息"]
_max_rows = 1000
RESULT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "record")
class FileUploader:
def __init__(self):
self._files = []
self._count = 0
self._error = 0
self._running = False
self._thread = None
self._connection = MongoConnection()
def set_files(self, files):
self._files = files
def _handle_error(self, error):
self._error += 1
logging.exception(error)
def _run(self):
self._running = True
self._count = 0
self._error = 0
logging.info("上传文件开始")
for path in self._files:
if not self._running:
break
if path.endswith(".xls") or path.endswith(".xlsx"):
try:
e = PerformanceExtractor()
e.open_with_name(path)
self._upload(e.strategy)
except Exception as e:
self._handle_error(e)
continue
elif path.endswith(".zip"):
try:
with zipfile.ZipFile(path) as z:
for name in z.filelist:
try:
if not self._running:
break
file = z.read(name)
e = PerformanceExtractor()
e.open_with_content(file)
self._upload(e.strategy)
except Exception as e:
self._handle_error(e)
continue
except Exception as e:
self._handle_error(e)
continue
logging.info("上传文件结束,成功上传文件总数:%s,错误数:%s" % (self._count, self._error))
def _upload(self, strategy):
try:
self._connection.collection.update_one({"info.策略名称": strategy["info"]["策略名称"]},
{"$set": strategy},
upsert=True)
except Exception as e:
logging.warning("策略%s上传失败" % strategy["info"]["策略名称"])
self._handle_error(e)
return None
self._count += 1
size = sys.getsizeof(bson.BSON.encode(strategy))
logging.info("策略[%s]上传成功,大小:%s bytes,此次已成功上传文件数:%s,错误数:%s" %
(strategy["info"]["策略名称"],
size, self._count, self._error)
)
def start(self):
if self._running:
return
self._thread = Thread(target=self._run)
self._thread.start()
def stop(self):
if not self._running:
return
self._running = False
if self._thread is not None:
self._thread.join()
def name_setter(func):
@wraps(func)
def wrapper(*args, file=None, **kwargs):
symbol, *name = os.path.basename(file).replace(" ", " ").split(" ")[:-1] # get symbol + strategy name
name = " ".join(name)
return func(*args, name=name, symbol=symbol, file=file, **kwargs)
return wrapper
def ema_filter(func): # filter ema future symbol.
@wraps(func)
def wrapper(*args, name=None, symbol=None, **kwargs):
if "000000" in symbol:
return
else:
return func(*args, name=name, symbol=symbol, **kwargs)
return wrapper
def dk_filter(func): # filter strategy base on daily k bar.
@wraps(func)
def wrapper(*args, name=None, symbol=None, **kwargs):
if "id" in name:
return
else:
return func(*args, name=name, symbol=symbol, **kwargs)
return wrapper
class FileTransformer(object):
def __init__(self):
self._files = []
self._running = False
self._thread = None
self._error = 0
self._count = 0
def set_files(self, files):
self._files = files
def _handle_error(self, error):
self._error += 1
logging.exception(error)
def _run(self):
self._running = True
self._count = 0
self._error = 0
logging.info("转换文件开始")
for path in self._files:
if not self._running:
break
if path.endswith(".xls") or path.endswith(".xlsx"):
try:
self.to_csv(file=path)
except Exception as e:
self._handle_error(e)
continue
elif path.endswith(".zip"):
try:
with zipfile.ZipFile(path) as z:
for name in z.filelist:
try:
if not self._running:
break
content = z.read(name)
self.to_csv(file=name.filename, content=content)
except Exception as e:
self._handle_error(e)
continue
except Exception as e:
self._handle_error(e)
continue
logging.info("转换文件结束,成功转换文件总数:%s,错误数:%s" % (self._count, self._error))
@name_setter
# @ema_filter
# @dk_filter
def to_csv(self, name=None, symbol=None, file=None, content=None):
e = PerformanceExtractor()
if content:
e.open_with_content(content)
elif file:
e.open_with_name(file)
else:
return
adapter = GuoJinAdapter(e)
try:
df1 = adapter.orders
df2 = adapter.positions
df1.to_csv(os.path.join(RESULT_DIR, "order", name + "_orders.csv"), index=False)
df2.to_csv(os.path.join(RESULT_DIR, "position", name + "_positions.csv"), index=False)
except Exception as e:
logging.warning("策略[%s]转换失败" % name if name else "未知")
self._handle_error(e)
return None
self._count += 1
logging.info("策略[%s]转换成功,此次已成功转换文件数:%s,错误数:%s" %
(name if name else "未知", self._count, self._error))
def start(self):
if self._running:
return
self._thread = Thread(target=self._run)
self._thread.start()
def stop(self):
if not self._running:
return
self._running = False
if self._thread is not None:
self._thread.join()
class LoggingHandleMixin:
def get_handle(self):
raise NotImplementedError
class MainWidget(QMainWindow, LoggingHandleMixin):
def __init__(self):
super().__init__()
self.setFixedSize(QSize(1280, 720))
self.setWindowTitle("上传绩效")
self._files = []
self._uploader = FileUploader()
self._transformer = FileTransformer()
central_widget = QWidget()
self.setCentralWidget(central_widget)
self.file_list = QListWidget()
left_layout = QVBoxLayout()
left_layout.addWidget(QLabel("上传文件列表:"))
left_layout.addWidget(self.file_list)
right_layout = QVBoxLayout()
file_select = QPushButton()
file_select.setText("选择文件")
file_select.setDefault(True)
file_select.clicked.connect(self._on_file_select)
dir_select = QPushButton()
dir_select.setText("选择文件夹")
dir_select.clicked.connect(self._on_dir_select)
file_upload = QPushButton()
file_upload.setText("上传文件")
file_upload.clicked.connect(self._on_file_upload)
upload_stop = QPushButton()
upload_stop.setText("停止上传")
upload_stop.clicked.connect(self._uploader.stop)
transform_start = QPushButton()
transform_start.setText("开始转换")
transform_start.clicked.connect(self._on_transform_start)
transform_stop = QPushButton()
transform_stop.setText("停止转换")
transform_stop.clicked.connect(self._transformer.stop)
right_layout.addWidget(file_select)
right_layout.addWidget(dir_select)
right_layout.addWidget(file_upload)
right_layout.addWidget(upload_stop)
right_layout.addWidget(transform_start)
right_layout.addWidget(transform_stop)
top_layout = QHBoxLayout()
top_layout.addLayout(left_layout)
top_layout.addLayout(right_layout)
self.log_widget = QTableWidget()
self.log_widget.setColumnCount(6)
self.log_widget.setHorizontalHeaderLabels(_table_columns)
self.log_widget.horizontalHeader().setClickable(False)
self.log_widget.verticalHeader().setVisible(False)
self.log_widget.horizontalHeader().setStretchLastSection(True)
self.log_widget.setEditTriggers(QAbstractItemView.NoEditTriggers)
bottom_layout = QVBoxLayout()
bottom_layout.addWidget(QLabel("日志信息:"))
bottom_layout.addWidget(self.log_widget)
layout = QVBoxLayout()
layout.addLayout(top_layout)
layout.addLayout(bottom_layout)
central_widget.setLayout(layout)
def _on_file_select(self):
file_dialog = QFileDialog(self)
file_dialog.setWindowTitle("选择策略文件(.zip|.xls|.xlsx)")
file_dialog.setDirectory(".")
file_dialog.setFilter("files (*.xls *.xlsx *.zip)")
file_dialog.setFileMode(QFileDialog.ExistingFiles)
if file_dialog.exec() == QDialog.Accepted:
self._files = file_dialog.selectedFiles()
self._refresh_file_list()
def _on_dir_select(self):
dir_dialog = QFileDialog(self)
dir_dialog.setWindowTitle("选择文件夹(该文件夹下所有[.zip|.xls|.xlsx]文件都会被上传)")
dir_dialog.setDirectory(".")
dir_dialog.setFileMode(QFileDialog.DirectoryOnly)
if dir_dialog.exec() == QDialog.Accepted:
pass
def _refresh_file_list(self):
self.file_list.clear()
self.file_list.addItems(self._files)
def _on_file_upload(self):
self._uploader.stop()
self._uploader.set_files(self._files)
self._uploader.start()
def _on_transform_start(self):
self._transformer.stop()
self._transformer.set_files(self._files)
self._transformer.start()
@property
def get_handle(self):
"""
Returns:
logging.Handler
"""
class TableHandler(logging.Handler):
def __init__(self, table: QTableWidget):
super().__init__()
self.setFormatter(logging.Formatter(_logger_formatter_str))
self._table = table
def emit(self, record: logging.LogRecord):
fields = self.format(record).split('\t')
fields[1:2] = fields[1].split(' ')
if self._table.rowCount() >= _max_rows:
self._table.removeRow(0)
row_count = self._table.rowCount()
self._table.insertRow(row_count)
for i in range(self._table.columnCount()):
self._table.setItem(row_count, i, QTableWidgetItem(fields[i]))
self._table.scrollToBottom()
return TableHandler(self.log_widget)
if __name__ == '__main__':
def _get_handler():
handler = logging.StreamHandler()
formatter = logging.Formatter(_logger_formatter_str)
handler.setFormatter(formatter)
return handler
logging.root.addHandler(_get_handler())
logging.root.setLevel(logging.INFO)
app = QApplication(sys.argv)
window = MainWidget()
logging.root.addHandler(window.get_handle)
window.show()
sys.exit(app.exec_())